-
-
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
Merged
New css interface #1129
Changes from 1 commit
Commits
Show all changes
13 commits
Select commit
Hold shift + click to select a range
edb9609
initial verion of css innterface
kof 99fd079
use string based cache, which can be optimized later with a babel plugin
kof f17c9f0
merge labels
kof 213ea44
support arrays
kof 3cc326c
check cache early
kof 098a7b1
implement composition
kof e038461
add flow types [ci skip]
kof 54d0a26
fix tests, export types
kof 07a9152
Update packages/css-jss/src/index.js
kof 03f2e40
cr changes
kof d1689bd
dedupe labels
kof a7ea8bc
changelog
kof 772824e
revert browsers.json changes
kof File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,31 @@ | ||
| { | ||
| "dist/css-jss.js": { | ||
| "bundled": 56349, | ||
| "minified": 20060, | ||
| "gzipped": 6697 | ||
| }, | ||
| "dist/css-jss.min.js": { | ||
| "bundled": 55595, | ||
| "minified": 19601, | ||
| "gzipped": 6480 | ||
| }, | ||
| "dist/css-jss.cjs.js": { | ||
| "bundled": 1951, | ||
| "minified": 934, | ||
| "gzipped": 499 | ||
| }, | ||
| "dist/css-jss.esm.js": { | ||
| "bundled": 1740, | ||
| "minified": 770, | ||
| "gzipped": 424, | ||
| "treeshaked": { | ||
| "rollup": { | ||
| "code": 98, | ||
| "import_statements": 63 | ||
| }, | ||
| "webpack": { | ||
| "code": 1382 | ||
| } | ||
| } | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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" | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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 | ||
| ``` |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,29 @@ | ||
| // @flow | ||
|
|
||
| // A reference based cache. | ||
| // Will only work if the same style object refereces have been passed. | ||
|
|
||
| const createCache = () => { | ||
| const cache = new WeakMap() | ||
|
|
||
| return { | ||
| get(args) { | ||
| const cached = cache.get(args[0]) | ||
| if (!cached) return undefined | ||
| // Check if all arguments are equal. | ||
| for (const i in args) { | ||
| if (args[i] !== cached.args[i]) { | ||
| return undefined | ||
| } | ||
| } | ||
| return cached.className | ||
| }, | ||
| set(args, className) { | ||
| if (!args[0]) return className | ||
| cache.set(args[0], {className, args}) | ||
| return className | ||
| } | ||
| } | ||
| } | ||
|
|
||
| export default createCache |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,31 @@ | ||
| // @flow | ||
| const createCss = (sheet, cache) => { | ||
| let ruleIndex = 0 | ||
|
|
||
| function css() { | ||
| // eslint-disable-next-line prefer-rest-params | ||
| const args = arguments | ||
| const className = cache.get(args) | ||
|
|
||
| if (className) return className | ||
|
|
||
| const style = {} | ||
| let label = 'css' | ||
|
|
||
| for (const i in args) { | ||
| const arg = args[i] | ||
| if (arg) Object.assign(style, arg) | ||
| } | ||
| if ('label' in style) { | ||
| label = style.label | ||
| delete style.label | ||
| } | ||
| const key = `${label}${ruleIndex++}` | ||
| sheet.addRule(key, style) | ||
|
kof marked this conversation as resolved.
Outdated
|
||
| return cache.set(args, sheet.classes[key]) | ||
| } | ||
|
|
||
| return css | ||
| } | ||
|
|
||
| export default createCss | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,106 @@ | ||
| // @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 style object argument', () => { | ||
| const result = css({color: 'red'}) | ||
| expect(result).to.be('css0-id') | ||
| expect(sheet.toString()).to.be(stripIndent` | ||
| .css0-id { | ||
| color: red; | ||
| } | ||
| `) | ||
| }) | ||
|
|
||
| it('should accept multiple style object arguments', () => { | ||
| const result = css({color: 'red'}, {background: 'green'}) | ||
| expect(result).to.be('css0-id') | ||
| expect(sheet.toString()).to.be(stripIndent` | ||
| .css0-id { | ||
| color: red; | ||
| background: green; | ||
| } | ||
| `) | ||
| }) | ||
|
|
||
| it('should ignore empty values', () => { | ||
| const result = css(null, {color: 'red'}, '', {background: 'green'}, undefined) | ||
| expect(result).to.be('css0-id') | ||
| expect(sheet.toString()).to.be(stripIndent` | ||
| .css0-id { | ||
| color: red; | ||
| background: green; | ||
| } | ||
| `) | ||
| }) | ||
|
|
||
| it('should accept label', () => { | ||
| const result = css({color: 'red', label: 'xxx'}, {background: 'green'}) | ||
| expect(result).to.be('xxx0-id') | ||
| expect(sheet.toString()).to.be(stripIndent` | ||
| .xxx0-id { | ||
| color: red; | ||
| background: green; | ||
| } | ||
| `) | ||
| }) | ||
|
|
||
| describe('cache', () => { | ||
| it('should cache a single style by ref', () => { | ||
| const style = {color: 'red'} | ||
| const result1 = css(style) | ||
| const result2 = css(style) | ||
| expect(result1).to.be('css0-id') | ||
| expect(result2).to.be('css0-id') | ||
| expect(sheet.toString()).to.be(stripIndent` | ||
| .css0-id { | ||
| color: red; | ||
| } | ||
| `) | ||
| }) | ||
| it('should cache multiple styles by ref', () => { | ||
| const style1 = {color: 'red'} | ||
| const style2 = {background: 'green'} | ||
| const result1 = css(style1, style2) | ||
| const result2 = css(style1, style2) | ||
| expect(result1).to.be('css0-id') | ||
| expect(result2).to.be('css0-id') | ||
| expect(sheet.toString()).to.be(stripIndent` | ||
| .css0-id { | ||
| color: red; | ||
| background: green; | ||
| } | ||
| `) | ||
| }) | ||
|
|
||
| it('should not cache by ref if any of the styles refs changes', () => { | ||
| const style1 = {color: 'red'} | ||
| const result1 = css(style1, {background: 'green'}) | ||
| const result2 = css(style1, {background: 'purpule'}) | ||
| expect(result1).to.be('css0-id') | ||
| expect(result2).to.be('css1-id') | ||
| expect(sheet.toString()).to.be(stripIndent` | ||
| .css0-id { | ||
| color: red; | ||
| background: green; | ||
| } | ||
| .css1-id { | ||
| color: red; | ||
| background: purpule; | ||
| } | ||
| `) | ||
| }) | ||
| }) | ||
| }) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,16 @@ | ||
| // @flow | ||
| import {create as createJss} from 'jss' | ||
| import preset from 'jss-preset-default' | ||
| import createCss from './createCss' | ||
| import createCache from './createCache' | ||
|
|
||
| const jss = createJss(preset()) | ||
| const defaultSheet = jss.createStyleSheet().attach() | ||
|
|
||
| export const create = sheet => { | ||
| // Since user decided to create own style sheet, we can detach the default one. | ||
| defaultSheet.detach() | ||
| return createCss(sheet, createCache()) | ||
| } | ||
|
|
||
| export default createCss(defaultSheet) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,10 @@ | ||
| // @flow | ||
| import expect from 'expect.js' | ||
| import css, {create} from './index' | ||
|
|
||
| describe('css-jss: exports', () => { | ||
| it('should have correct exports', () => { | ||
| expect(css).to.be.a(Function) | ||
| expect(create).to.be.a(Function) | ||
| }) | ||
| }) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.