-
-
Notifications
You must be signed in to change notification settings - Fork 389
New css interface #1129
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
New css interface #1129
Changes from 8 commits
edb9609
99fd079
f17c9f0
213ea44
3cc326c
098a7b1
e038461
54d0a26
07a9152
03f2e40
d1689bd
a7ea8bc
772824e
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,2 +1,3 @@ | ||
| declare function describe(string, Function): void | ||
| declare function it(string, Function): void | ||
| declare function beforeEach(Function): void |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,31 @@ | ||
| { | ||
| "dist/css-jss.js": { | ||
| "bundled": 56983, | ||
| "minified": 20207, | ||
| "gzipped": 6762 | ||
| }, | ||
| "dist/css-jss.min.js": { | ||
| "bundled": 56229, | ||
| "minified": 19748, | ||
| "gzipped": 6543 | ||
| }, | ||
| "dist/css-jss.cjs.js": { | ||
| "bundled": 2569, | ||
| "minified": 1038, | ||
| "gzipped": 575 | ||
| }, | ||
| "dist/css-jss.esm.js": { | ||
| "bundled": 2360, | ||
| "minified": 876, | ||
| "gzipped": 504, | ||
| "treeshaked": { | ||
| "rollup": { | ||
| "code": 157, | ||
| "import_statements": 63 | ||
| }, | ||
| "webpack": { | ||
| "code": 1180 | ||
| } | ||
| } | ||
| } | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,8 @@ | ||
| The MIT License (MIT) | ||
| Copyright (c) 2014-present Oleg Isonen (Slobodskoi) & contributors | ||
|
|
||
| Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: | ||
|
|
||
| The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. | ||
|
|
||
| THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,44 @@ | ||
| { | ||
| "name": "css-jss", | ||
| "description": "Implements css() interface on top of JSS", | ||
| "version": "10.0.0-alpha.17", | ||
| "license": "MIT", | ||
| "homepage": "https://cssinjs.org/", | ||
| "main": "dist/css-jss.cjs.js", | ||
| "module": "dist/css-jss.esm.js", | ||
| "unpkg": "dist/css-jss.bundle.js", | ||
| "typings": "./src/index.d.ts", | ||
| "author": "JSS Team", | ||
| "repository": { | ||
| "type": "git", | ||
| "url": "https://github.com/cssinjs/jss" | ||
| }, | ||
| "bugs": { | ||
| "url": "https://github.com/cssinjs/jss/issues/new" | ||
| }, | ||
| "files": [ | ||
| "dist", | ||
| "src", | ||
| "LICENSE" | ||
| ], | ||
| "keywords": [ | ||
| "jss", | ||
| "style", | ||
| "sheet", | ||
| "stylesheet", | ||
| "css", | ||
| "components", | ||
| "composable", | ||
| "css in js", | ||
| "css-in-js" | ||
| ], | ||
| "scripts": { | ||
| "build": "node ../../scripts/build.js", | ||
| "check-snapshot": "node ../../scripts/match-snapshot.js" | ||
| }, | ||
| "dependencies": { | ||
| "@babel/runtime": "^7.3.1", | ||
| "jss": "10.0.0-alpha.17", | ||
| "jss-preset-default": "10.0.0-alpha.17" | ||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,26 @@ | ||
| # css-jss | ||
|
|
||
| [](https://npmjs.org/package/css-jss) | ||
| [](https://github.com/cssinjs/jss/blob/master/LICENSE) | ||
| [](https://npmjs.org/package/css-jss) | ||
| [](https://npmjs.org/package/css-jss) | ||
| [](https://npmjs.org/package/css-jss) | ||
| [](https://gitter.im/cssinjs/lobby) | ||
|
|
||
| > Implements css() interface on top of JSS | ||
| See our website [css-jss](https://cssinjs.org/css-jss?v=v10.0.0-alpha.17) for more information. | ||
|
|
||
| ## Install | ||
|
|
||
| Using npm: | ||
|
|
||
| ```sh | ||
| npm install css-jss | ||
| ``` | ||
|
|
||
| or using yarn: | ||
|
|
||
| ```sh | ||
| yarn add css-jss | ||
| ``` |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,68 @@ | ||
| // @flow | ||
| import type {StyleSheet} from 'jss' | ||
| // eslint-disable-next-line no-unused-vars | ||
| import type {Css, StyleArg, ClassName} from './types' | ||
|
|
||
| const createCss = (sheet: StyleSheet): Css => { | ||
| const cache = new Map() | ||
| let ruleIndex = 0 | ||
|
|
||
| return function css(/* :: ..._: StyleArg[] */): ClassName { | ||
| // eslint-disable-next-line prefer-rest-params | ||
| const args = arguments | ||
|
|
||
| // We can avoid the need for stringification with a babel plugin, | ||
| // which could generate a hash at build time and add it to the object. | ||
| const argsStr = JSON.stringify(args) | ||
| const cached = cache.get(argsStr) | ||
| if (cached) return cached.className | ||
|
|
||
| const flatArgs = [] | ||
|
|
||
| // Flatten arguments which can be | ||
| // - style objects | ||
| // - array of style objects | ||
| // - arrays of style objects | ||
| for (const argIndex in args) { | ||
| const arg = args[argIndex] | ||
| if (!Array.isArray(arg)) { | ||
| flatArgs.push(arg) | ||
| continue | ||
| } | ||
| for (let innerArgIndex = 0; innerArgIndex < arg.length; innerArgIndex++) { | ||
| flatArgs.push(arg[innerArgIndex]) | ||
| } | ||
| } | ||
|
|
||
| const mergedStyle = {} | ||
| const labels = [] | ||
|
|
||
| for (let i = 0; i < flatArgs.length; i++) { | ||
| let style = flatArgs[i] | ||
| if (!style) continue | ||
| // It can be a class name that css() has previously generated. | ||
| if (typeof style === 'string') { | ||
| // eslint-disable-next-line no-shadow | ||
| const cached = cache.get(style) | ||
| if (cached) { | ||
| // eslint-disable-next-line prefer-spread | ||
| if (cached.labels.length) labels.push.apply(labels, cached.labels) | ||
| style = cached.style | ||
| } | ||
| } | ||
| if (style.label) labels.push(style.label) | ||
| Object.assign(mergedStyle, style) | ||
| } | ||
| delete mergedStyle.label | ||
| const label = labels.length === 0 ? 'css' : labels.join('-') | ||
| const key = `${label}-${ruleIndex++}` | ||
| sheet.addRule(key, mergedStyle) | ||
| const className = sheet.classes[key] | ||
| const cacheValue = {style: mergedStyle, labels, className} | ||
| cache.set(argsStr, cacheValue) | ||
| cache.set(className, cacheValue) | ||
| return className | ||
| } | ||
| } | ||
|
|
||
| export default createCss |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,190 @@ | ||
| // @flow | ||
| import expect from 'expect.js' | ||
| import {stripIndent} from 'common-tags' | ||
| import {create as createJss} from 'jss' | ||
| import {createGenerateId} from '../../../tests/utils' | ||
| import {create as createCss} from './index' | ||
|
|
||
| describe('css-jss', () => { | ||
| let sheet | ||
| let css | ||
|
|
||
| beforeEach(() => { | ||
| sheet = createJss({createGenerateId}).createStyleSheet() | ||
| css = createCss(sheet) | ||
| }) | ||
|
|
||
| it('should accept a single style object argument', () => { | ||
| const className = css({color: 'red'}) | ||
| expect(className).to.be('css-0-id') | ||
| expect(sheet.toString()).to.be(stripIndent` | ||
| .css-0-id { | ||
| color: red; | ||
| } | ||
| `) | ||
| }) | ||
|
|
||
| it('should accept multiple style object arguments', () => { | ||
| const className = css({color: 'red'}, {background: 'green'}) | ||
| expect(className).to.be('css-0-id') | ||
| expect(sheet.toString()).to.be(stripIndent` | ||
| .css-0-id { | ||
| color: red; | ||
| background: green; | ||
| } | ||
| `) | ||
| }) | ||
|
|
||
| it('should accept multiple style object array', () => { | ||
| const className = css([{color: 'red'}, {background: 'green'}]) | ||
| expect(className).to.be('css-0-id') | ||
| expect(sheet.toString()).to.be(stripIndent` | ||
| .css-0-id { | ||
| color: red; | ||
| background: green; | ||
| } | ||
| `) | ||
| }) | ||
|
|
||
| it('should accept multiple style object array and style objects', () => { | ||
| const className = css([{color: 'red'}, {background: 'green'}], {float: 'left'}) | ||
| expect(className).to.be('css-0-id') | ||
| expect(sheet.toString()).to.be(stripIndent` | ||
| .css-0-id { | ||
| color: red; | ||
| background: green; | ||
| float: left; | ||
| } | ||
| `) | ||
| }) | ||
|
|
||
| it('should accept multiple style object arrays', () => { | ||
| const className = css([{color: 'red'}, {background: 'green'}], [{float: 'left'}]) | ||
| expect(className).to.be('css-0-id') | ||
| expect(sheet.toString()).to.be(stripIndent` | ||
| .css-0-id { | ||
| color: red; | ||
| background: green; | ||
| float: left; | ||
| } | ||
| `) | ||
| }) | ||
|
|
||
| it('should compose css() calls', () => { | ||
| const className = css(css({color: 'red'}), css({background: 'green'})) | ||
| expect(className).to.be('css-2-id') | ||
| expect(sheet.toString()).to.be(stripIndent` | ||
| .css-0-id { | ||
| color: red; | ||
| } | ||
| .css-1-id { | ||
| background: green; | ||
| } | ||
| .css-2-id { | ||
| color: red; | ||
| background: green; | ||
| } | ||
| `) | ||
| }) | ||
|
|
||
| it('should compose css() calls inside of array arg', () => { | ||
| const className = css([css({color: 'red'}), css({background: 'green'})]) | ||
| expect(className).to.be('css-2-id') | ||
| expect(sheet.toString()).to.be(stripIndent` | ||
| .css-0-id { | ||
| color: red; | ||
| } | ||
| .css-1-id { | ||
| background: green; | ||
| } | ||
| .css-2-id { | ||
| color: red; | ||
| background: green; | ||
| } | ||
| `) | ||
| }) | ||
|
|
||
| it('should compose css() calls from mixed array and strings', () => { | ||
| const className = css([css({color: 'red'}), css({background: 'green'})], css({float: 'left'})) | ||
| expect(className).to.be('css-3-id') | ||
| expect(sheet.toString()).to.be(stripIndent` | ||
| .css-0-id { | ||
| color: red; | ||
| } | ||
| .css-1-id { | ||
| background: green; | ||
| } | ||
| .css-2-id { | ||
| float: left; | ||
| } | ||
| .css-3-id { | ||
| color: red; | ||
| background: green; | ||
| float: left; | ||
| } | ||
| `) | ||
| }) | ||
|
|
||
| it('should ignore empty values', () => { | ||
| const className = css(null, {color: 'red'}, '', {background: 'green'}, undefined) | ||
| expect(className).to.be('css-0-id') | ||
| expect(sheet.toString()).to.be(stripIndent` | ||
| .css-0-id { | ||
| color: red; | ||
| background: green; | ||
| } | ||
| `) | ||
| }) | ||
|
|
||
| it('should accept label', () => { | ||
| const className = css({color: 'red', label: 'xxx'}, {background: 'green'}) | ||
| expect(className).to.be('xxx-0-id') | ||
| expect(sheet.toString()).to.be(stripIndent` | ||
| .xxx-0-id { | ||
| color: red; | ||
| background: green; | ||
| } | ||
| `) | ||
| }) | ||
|
|
||
| it('should merge label', () => { | ||
| const className = css({color: 'red', label: 'xxx'}, {background: 'green', label: 'yyy'}) | ||
| expect(className).to.be('xxx-yyy-0-id') | ||
| expect(sheet.toString()).to.be(stripIndent` | ||
| .xxx-yyy-0-id { | ||
| color: red; | ||
| background: green; | ||
| } | ||
| `) | ||
| }) | ||
|
|
||
| it('should cache a single style', () => { | ||
| const style = {color: 'red'} | ||
| const className1 = css(style) | ||
| const className2 = css(style) | ||
| expect(className1).to.be('css-0-id') | ||
| expect(className2).to.be('css-0-id') | ||
| expect(sheet.toString()).to.be(stripIndent` | ||
| .css-0-id { | ||
| color: red; | ||
| } | ||
| `) | ||
| }) | ||
|
|
||
| it('should cache multiple styles', () => { | ||
| const style1 = {color: 'red'} | ||
| const style2 = {background: 'green'} | ||
| const className1 = css(style1, style2) | ||
| const className2 = css(style1, style2) | ||
| expect(className1).to.be('css-0-id') | ||
| expect(className2).to.be('css-0-id') | ||
| expect(sheet.toString()).to.be(stripIndent` | ||
| .css-0-id { | ||
| color: red; | ||
| background: green; | ||
| } | ||
| `) | ||
| }) | ||
|
|
||
| it('should try get the cached rule by using a ref first before trying to stringify', () => {}) | ||
| }) |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,18 @@ | ||
| // @flow | ||
| import {create as createJss} from 'jss' | ||
| import type {StyleSheet} from 'jss' | ||
| import preset from 'jss-preset-default' | ||
| import createCss from './createCss' | ||
| import type {Css, Style} from './types' | ||
|
|
||
| const jss = createJss(preset()) | ||
| const defaultSheet = jss.createStyleSheet().attach() | ||
|
|
||
| export type {Css, Style} | ||
|
|
||
| export const create = (sheet?: StyleSheet): Css => { | ||
| if (sheet) defaultSheet.detach() | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Why would we detach in such a case? The user still could have used the default sheet
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yeah, not sure what to do here, the problem I see is that if user creates custom jss/sheet, this sheet will already be in the DOM with no rules. So maybe I need to additionally check if there are rules
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Would this be a big benefit? I don’t think it will make a big difference in performance
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Just the fact that there would be an empty style tag in the head, though evtl it doesn't matter for client side and won't happen on the server so fuck it, I am removing the line. |
||
| return createCss(sheet || defaultSheet) | ||
| } | ||
|
|
||
| export default create() | ||
|
kof marked this conversation as resolved.
Outdated
|
||
Uh oh!
There was an error while loading. Please reload this page.