Skip to content
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

Merged
merged 13 commits into from
Jun 14, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions changelog.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
### Features

- [react-jss] New experimental styled API (undocumented intentionally)([#1094](https://github.com/cssinjs/jss/pull/1094))
- [css-jss] New experimental css() API (undocumented intentionally)([#1129](https://github.com/cssinjs/jss/pull/1129))

### Bug fixes

Expand Down
1 change: 1 addition & 0 deletions flow-typed/mocha.js
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
31 changes: 31 additions & 0 deletions packages/css-jss/.size-snapshot.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
{
"dist/css-jss.js": {
"bundled": 56911,
"minified": 20186,
"gzipped": 6756
},
"dist/css-jss.min.js": {
"bundled": 56157,
"minified": 19727,
"gzipped": 6538
},
"dist/css-jss.cjs.js": {
"bundled": 2505,
"minified": 1002,
"gzipped": 569
},
"dist/css-jss.esm.js": {
"bundled": 2287,
"minified": 834,
"gzipped": 491,
"treeshaked": {
"rollup": {
"code": 98,
"import_statements": 63
},
"webpack": {
"code": 1151
}
}
}
}
8 changes: 8 additions & 0 deletions packages/css-jss/LICENSE
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.
44 changes: 44 additions & 0 deletions packages/css-jss/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
{
"name": "css-jss",
kof marked this conversation as resolved.
Show resolved Hide resolved
"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"
}
}
26 changes: 26 additions & 0 deletions packages/css-jss/readme.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
# css-jss

[![Version](https://img.shields.io/npm/v/css-jss.svg?style=flat)](https://npmjs.org/package/css-jss)
[![License](https://img.shields.io/npm/l/css-jss.svg?style=flat)](https://github.com/cssinjs/jss/blob/master/LICENSE)
[![Downlodas](https://img.shields.io/npm/dm/css-jss.svg?style=flat)](https://npmjs.org/package/css-jss)
[![Size](https://img.shields.io/bundlephobia/minzip/css-jss.svg?style=flat)](https://npmjs.org/package/css-jss)
[![Dependencies](https://img.shields.io/david/cssinjs/jss.svg?path=packages%2Fcss-jss&style=flat)](https://npmjs.org/package/css-jss)
[![Gitter](https://badges.gitter.im/JoinChat.svg)](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
```
68 changes: 68 additions & 0 deletions packages/css-jss/src/createCss.js
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} from './types'

const createCss = (sheet: StyleSheet): Css => {
const cache = new Map()
let ruleIndex = 0

return function css(/* :: ..._: StyleArg[] */): string {
// 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.indexOf(style.label) === -1) 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
195 changes: 195 additions & 0 deletions packages/css-jss/src/createCss.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,195 @@
// @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'},
{float: 'left', label: 'yyy'}
)
expect(className).to.be('xxx-yyy-0-id')
expect(sheet.toString()).to.be(stripIndent`
.xxx-yyy-0-id {
color: red;
background: green;
float: left;
}
`)
})

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', () => {})
})
Loading