├── .travis.yml
├── .npmignore
├── docs
├── logo.png
├── system.js
├── Link.js
├── theme.js
├── Pre.js
├── Heading.js
├── Box.js
├── Text.js
├── Button.js
├── Logo.js
├── App.js
└── index.html
├── test.js.snap
├── .gitignore
├── .babelrc
├── LICENSE.md
├── package.json
├── src
└── index.js
├── test.js
├── test.js.md
└── README.md
/.travis.yml:
--------------------------------------------------------------------------------
1 | language: node_js
2 | node_js:
3 | - 9.0
4 |
--------------------------------------------------------------------------------
/.npmignore:
--------------------------------------------------------------------------------
1 | docs
2 | .nyc_output
3 | coverage
4 | test.js*
5 |
--------------------------------------------------------------------------------
/docs/logo.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/jxnblk/axs/HEAD/docs/logo.png
--------------------------------------------------------------------------------
/test.js.snap:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/jxnblk/axs/HEAD/test.js.snap
--------------------------------------------------------------------------------
/.gitignore:
--------------------------------------------------------------------------------
1 | .nyc_output
2 | coverage
3 | dist
4 | package-lock.json
5 | yarn-lock.json
6 |
--------------------------------------------------------------------------------
/.babelrc:
--------------------------------------------------------------------------------
1 | {
2 | "presets": [
3 | "env",
4 | "stage-0",
5 | "react"
6 | ]
7 | }
8 |
--------------------------------------------------------------------------------
/docs/system.js:
--------------------------------------------------------------------------------
1 | import objss from 'objss'
2 |
3 | const system = (...funcs) => props => funcs
4 | .map(func => func(props))
5 | .filter(obj => obj !== null)
6 | .map(objss)
7 | .join('')
8 |
9 | export default system
10 |
--------------------------------------------------------------------------------
/docs/Link.js:
--------------------------------------------------------------------------------
1 | import React from 'react'
2 | import Base from '../src'
3 |
4 | const Link = props =>
5 |
12 |
13 | export default Link
14 |
--------------------------------------------------------------------------------
/docs/theme.js:
--------------------------------------------------------------------------------
1 | const theme = {
2 | fontSizes: [
3 | 12, 16, 20, 24, 32, 48, 64, 96, 128
4 | ],
5 | space: [
6 | 0, 4, 8, 16, 32, 64, 128, 256
7 | ],
8 | colors: {
9 | black: '#000',
10 | blue: '#07c'
11 | }
12 | }
13 |
14 | export default theme
15 |
--------------------------------------------------------------------------------
/docs/Pre.js:
--------------------------------------------------------------------------------
1 | import React from 'react'
2 | import Text from './Text'
3 |
4 | const Pre = props =>
5 |
15 |
16 | export default Pre
17 |
--------------------------------------------------------------------------------
/docs/Heading.js:
--------------------------------------------------------------------------------
1 | import React from 'react'
2 | import Text from './Text'
3 |
4 | const Heading = props =>
5 |
16 |
17 | export default Heading
18 |
--------------------------------------------------------------------------------
/docs/Box.js:
--------------------------------------------------------------------------------
1 | import React from 'react'
2 | import Base from '../src'
3 | import { width, space, color } from 'styled-system'
4 | import { withTheme } from 'theming'
5 | import system from './system'
6 |
7 | const Box = withTheme(props =>
8 |
15 | )
16 |
17 | export default Box
18 |
--------------------------------------------------------------------------------
/docs/Text.js:
--------------------------------------------------------------------------------
1 | import React from 'react'
2 | import Base from '../src'
3 | import { fontSize, fontWeight, space, color } from 'styled-system'
4 | import { withTheme } from 'theming'
5 | import system from './system'
6 |
7 | const Text = withTheme(props =>
8 |
16 | )
17 |
18 | export default Text
19 |
--------------------------------------------------------------------------------
/docs/Button.js:
--------------------------------------------------------------------------------
1 | import React from 'react'
2 | import Base from '../src'
3 | import { withTheme } from 'theming'
4 |
5 | const Button = withTheme(({ theme, ...props }) =>
6 |
27 | )
28 |
29 | export default Button
30 |
--------------------------------------------------------------------------------
/LICENSE.md:
--------------------------------------------------------------------------------
1 |
2 | # The MIT License (MIT)
3 | Copyright (c) 2016 – 2017 Brent Jackson
4 |
5 | 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:
6 |
7 | The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
8 |
9 | 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.
10 |
11 |
--------------------------------------------------------------------------------
/docs/Logo.js:
--------------------------------------------------------------------------------
1 | import React from 'react'
2 | import Base from '../src'
3 |
4 | const M = 'M'
5 | const L = 'L'
6 |
7 | const a = 2 // edge offset
8 | const b = 8 - a
9 | const c = 16 - a // outer edge offset
10 | const base = 12.5
11 | const tri = [
12 | M, a, base, L, c, base,
13 | L, 8, base - b * Math.sqrt(3),
14 | 'z'
15 | ].join(' ')
16 |
17 | const rad = deg => Math.PI * deg / 180
18 | const H = (c - a) * 2/4
19 | const A = Math.cos(rad(30)) * H
20 | const B = Math.sin(rad(30)) * H
21 |
22 | const shiftx = n => n + A
23 | const shifty = n => n + B
24 | const handle = [
25 | M, shiftx(a), shifty(base),
26 | L, shiftx(8), shifty(base) - b * Math.sqrt(3)
27 | ].join(' ')
28 |
29 | const Logo = ({
30 | size = 768,
31 | color = 'currentcolor',
32 | ...props
33 | }) =>
34 |
47 |
48 |
49 |
50 |
51 | export default Logo
52 |
--------------------------------------------------------------------------------
/package.json:
--------------------------------------------------------------------------------
1 | {
2 | "name": "axs",
3 | "version": "4.0.0-1",
4 | "description": "Stupid simple style components for React",
5 | "main": "dist/index.js",
6 | "module": "dist/index.es.js",
7 | "scripts": {
8 | "prepublish": "babel src -d dist && babel src/index.js -o dist/index.es.js --no-babelrc --presets=stage-0,react",
9 | "start": "x0 dev docs/App.js -o",
10 | "build": "x0 build docs/App.js --static -d docs",
11 | "logo": "repng docs/Logo.js -w 768 -h 768 -o docs -f logo",
12 | "size": "npm run prepublish && bundlesize",
13 | "cover": "nyc report --reporter=html --reporter=lcov",
14 | "test": "nyc ava"
15 | },
16 | "keywords": [
17 | "react",
18 | "react-component",
19 | "style",
20 | "stylis",
21 | "ui",
22 | "css",
23 | "css-in-js"
24 | ],
25 | "author": "Brent Jackson",
26 | "license": "MIT",
27 | "devDependencies": {
28 | "@compositor/x0": "^3.0.2",
29 | "ava": "^0.24.0",
30 | "babel-cli": "^6.26.0",
31 | "babel-preset-env": "^1.6.1",
32 | "babel-preset-react": "^6.24.1",
33 | "babel-preset-stage-0": "^6.24.1",
34 | "babel-register": "^6.26.0",
35 | "bundlesize": "^0.15.3",
36 | "nyc": "^11.4.1",
37 | "objss": "^1.0.3",
38 | "react-test-renderer": "^16.2.0",
39 | "repng": "^1.0.1",
40 | "styled-system": "^1.1.1",
41 | "theming": "^1.3.0"
42 | },
43 | "dependencies": {
44 | "html-tags": "^2.0.0",
45 | "prop-types": "^15.6.0",
46 | "react": "^16.2.0",
47 | "stylis": "^3.4.5"
48 | },
49 | "ava": {
50 | "require": [
51 | "babel-register"
52 | ],
53 | "babel": "inherit"
54 | },
55 | "bundlesize": [
56 | {
57 | "path": "src/index.js",
58 | "maxSize": "0.8 kb"
59 | },
60 | {
61 | "path": "dist/index.js",
62 | "maxSize": "1.8 kb"
63 | }
64 | ]
65 | }
66 |
--------------------------------------------------------------------------------
/docs/App.js:
--------------------------------------------------------------------------------
1 | import React from 'react'
2 | import { ThemeProvider } from 'theming'
3 | import Base, { Style } from '../src'
4 | import theme from './theme'
5 |
6 | import Box from './Box'
7 | import Logo from './Logo'
8 | import Heading from './Heading'
9 | import Text from './Text'
10 | import Pre from './Pre'
11 | import Button from './Button'
12 | import Link from './Link'
13 |
14 | const App = props => (
15 |
16 |
17 | Axs
18 |
19 |
20 |
21 |
22 |
23 |
24 |
25 |
26 |
27 |
28 |
29 |
30 | Axs
31 |
32 |
33 | {description}
34 |
35 | npm i axs
36 |
40 |
41 |
42 |
43 |
44 |
45 |
46 | Made by Jxnblk
47 |
48 |
49 |
50 |
51 |
52 | )
53 |
54 | const basecss = `
55 | * { box-sizing: border-box }
56 | body {
57 | font-family: -apple-system, system-ui, sans-serif;
58 | line-height: 1.5;
59 | margin: 0;
60 | }
61 | `
62 | const description = 'Stupid simple style components for React'
63 | const example = `const Heading = props =>
64 | `
65 |
66 | export default App
67 |
--------------------------------------------------------------------------------
/src/index.js:
--------------------------------------------------------------------------------
1 | import React from 'react'
2 | import PropTypes from 'prop-types'
3 | import stylis from 'stylis'
4 | import tags from 'html-tags'
5 |
6 | let _id = 0
7 | let cache = {}
8 | const uuid = () => 'x' + (_id++).toString(36)
9 |
10 | const createCSS = css => {
11 | const className = cache[css] || uuid()
12 | const rules = stylis('.' + className, css)
13 | const cached = !!cache[css]
14 | cache[css] = className
15 |
16 | // This might need adjustments
17 | if (cached) return { rules: false, className }
18 |
19 | return { rules, className }
20 | }
21 |
22 | const reset = () => {
23 | _id = 0
24 | cache = {}
25 | }
26 |
27 | export const Style = props => (
28 |
33 | )
34 |
35 | export class Base extends React.Component {
36 | static reset = reset
37 |
38 | static propTypes = {
39 | css: PropTypes.string,
40 | innerRef: PropTypes.func,
41 | is: PropTypes.oneOfType([
42 | PropTypes.string,
43 | PropTypes.func
44 | ])
45 | }
46 |
47 | static defaultProps = {
48 | is: 'div',
49 | css: ''
50 | }
51 |
52 | constructor (props) {
53 | super()
54 |
55 | this.state = createCSS(props.css)
56 | }
57 |
58 | componentWillReceiveProps (next) {
59 | if (next.css === this.props.css) return
60 | delete cache[this.props.css]
61 | const nextState = createCSS(next.css)
62 | this.setState(nextState)
63 | }
64 |
65 | render () {
66 | const {
67 | is,
68 | css,
69 | innerRef,
70 | ...props
71 | } = this.props
72 | const { rules, className } = this.state
73 | const Comp = is
74 |
75 | if (typeof Comp === 'function') {
76 | return
77 | }
78 |
79 | return (
80 |
81 | {rules && }
82 |
87 |
88 | )
89 | }
90 | }
91 |
92 | tags.forEach(tag => {
93 | Base[tag] = props =>
94 | })
95 |
96 | export default Base
97 |
--------------------------------------------------------------------------------
/test.js:
--------------------------------------------------------------------------------
1 | import test from 'ava'
2 | import React from 'react'
3 | import { create as render } from 'react-test-renderer'
4 | import Base, { Style } from './src'
5 |
6 | test.afterEach(() => {
7 | Base.reset()
8 | })
9 |
10 | test('renders', t => {
11 | const a = render().toJSON()
12 | t.snapshot(a)
13 | })
14 |
15 | test('Style renders', t => {
16 | const a = render().toJSON()
17 | t.snapshot(a)
18 | })
19 |
20 | test('renders with css prop', t => {
21 | const a = render().toJSON()
22 | t.snapshot(a)
23 | })
24 |
25 | test('renders with decorated HTML element key', t => {
26 | const a = render().toJSON()
27 | t.snapshot(a)
28 | t.is(a[1].type, 'h2')
29 | })
30 |
31 | test('caches results', t => {
32 | const a = render(
33 |
34 |
35 |
36 |
37 | ).toJSON()
38 | t.snapshot(a)
39 | })
40 |
41 | test('Base.reset() clears cache', t => {
42 | const a = render().toJSON()
43 | Base.reset()
44 | const b = render().toJSON()
45 | t.snapshot(a)
46 | t.snapshot(b)
47 | t.deepEqual(a[0], b[0])
48 | })
49 |
50 | test('Updates styles on css change', t => {
51 | const base = render()
52 | const a = base.toJSON()
53 | base.update()
54 | const b = base.toJSON()
55 | t.is(a[1].props.className, 'x0')
56 | t.is(b[1].props.className, 'x1')
57 | t.snapshot(a)
58 | t.snapshot(b)
59 | })
60 |
61 | test('Skips update when css is the same', t => {
62 | const base = render()
63 | const a = base.toJSON()
64 | base.update()
65 | const b = base.toJSON()
66 | t.is(a[1].props.className, 'x0')
67 | t.is(b[1].props.className, 'x0')
68 | t.deepEqual(a, b)
69 | t.snapshot(a)
70 | t.snapshot(b)
71 | })
72 |
73 | test('Extends Axs components', t => {
74 | const A = props => (
75 |
81 | )
82 | const a = render().toJSON()
83 | t.regex(a[0].props.dangerouslySetInnerHTML.__html, /color:tomato;font-size:32px/)
84 | t.snapshot(a)
85 | })
86 |
87 |
--------------------------------------------------------------------------------
/docs/index.html:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
AxsAxs
Stupid simple style components for React
npm i axs
GitHubconst Heading = props =>
12 | <Base.h2 {...props} css='color:tomato;' />
13 |
14 |
15 |
--------------------------------------------------------------------------------
/test.js.md:
--------------------------------------------------------------------------------
1 | # Snapshot report for `test.js`
2 |
3 | The actual snapshot is saved in `test.js.snap`.
4 |
5 | Generated by [AVA](https://ava.li).
6 |
7 | ## Base.reset() clears cache
8 |
9 | > Snapshot 1
10 |
11 | [
12 | ,
19 | ,
22 | ]
23 |
24 | > Snapshot 2
25 |
26 | [
27 | ,
34 | ,
37 | ]
38 |
39 | ## Extends Axs components
40 |
41 | > Snapshot 1
42 |
43 | [
44 | ,
51 | ,
54 | ]
55 |
56 | ## Skips update when css is the same
57 |
58 | > Snapshot 1
59 |
60 | [
61 | ,
68 | ,
71 | ]
72 |
73 | > Snapshot 2
74 |
75 | [
76 | ,
83 | ,
86 | ]
87 |
88 | ## Style renders
89 |
90 | > Snapshot 1
91 |
92 |
99 |
100 | ## Updates styles on css change
101 |
102 | > Snapshot 1
103 |
104 | [
105 | ,
112 | ,
115 | ]
116 |
117 | > Snapshot 2
118 |
119 | [
120 | ,
127 | ,
130 | ]
131 |
132 | ## caches results
133 |
134 | > Snapshot 1
135 |
136 | [
137 | ,
144 | ,
147 | ,
150 | ]
151 |
152 | ## renders
153 |
154 | > Snapshot 1
155 |
156 | [
157 | '',
158 | ,
161 | ]
162 |
163 | ## renders with css prop
164 |
165 | > Snapshot 1
166 |
167 | [
168 | ,
175 | ,
178 | ]
179 |
180 | ## renders with decorated HTML element key
181 |
182 | > Snapshot 1
183 |
184 | [
185 | ,
192 | ,
195 | ]
196 |
--------------------------------------------------------------------------------
/README.md:
--------------------------------------------------------------------------------
1 |
2 | # Axs
3 |
4 | Stupid simple style components for React
5 |
6 | If you know React and you know CSS, you already know how to use this.
7 |
8 | [![Build Status][badge]][travis]
9 |
10 | [badge]: https://img.shields.io/travis/jxnblk/axs/master.svg?style=flat-square
11 | [travis]: https://travis-ci.org/jxnblk/axs
12 |
13 |
14 |
15 | ```sh
16 | npm i axs
17 | ```
18 |
19 | ```jsx
20 | const Heading = props =>
21 |
27 | ```
28 |
29 | > A hand axe is a prehistoric stone tool... the longest-used tool in human history.
30 |
31 | ## Features
32 |
33 | - Minimal API surface area
34 | - One component, three props
35 | - React component-based API
36 | - Server side rendering with no additional setup
37 | - No custom Babel plugins or additional configuration needed
38 | - Works in iframes
39 | - 0.7 kb in under 100 LOC
40 |
41 | ## Usage
42 |
43 | ### Basic
44 |
45 | Create components by wrapping the Base component and passing props and a `css` prop to add style.
46 |
47 | ```jsx
48 | const Heading = props =>
49 |
55 | ```
56 |
57 | ### CSS syntax
58 |
59 | The `css` prop uses [stylis][stylis] for a CSS-like syntax that allows pseudo-classes, media queries, animations, and nested selectors to be defined inline.
60 | See the [stylis docs][stylis] for more info.
61 |
62 | ```jsx
63 | const Heading = props =>
64 |
73 | ```
74 |
75 | ### HTML elements
76 |
77 | To change the underlying HTML element, pass a tag name to the `is` prop.
78 |
79 | ```jsx
80 | const Heading = props =>
81 |
88 | ```
89 |
90 | Alternatively, the Base component is decorated with keys for all valid HTML elements, which map to the `is` prop.
91 |
92 | ```jsx
93 | const Heading = props =>
94 |
100 | ```
101 |
102 | When using the component, the underlying element can be changed on a per-instance basis using the `is` prop.
103 | This is especially helpful for ensuring the use of correct HTML semantics, while keeping the component styles decoupled.
104 |
105 | ```jsx
106 |
107 | Hello
108 |
109 | ```
110 |
111 | ### Dynamic styles
112 |
113 | Styles can be set dynamically based on props.
114 |
115 | ```jsx
116 | const Heading = props =>
117 |
123 | ```
124 |
125 | Using the component above with a custom `color` passed as a prop would look like this:
126 |
127 | ```jsx
128 |
129 | Hello
130 |
131 | ```
132 |
133 | **Note:** unlike other libraries like [styled-components][styled-components], the `css` prop takes a string, not a tagged template literal, so passing functions into the `css` prop will do nothing.
134 |
135 | ### Removing style props
136 |
137 | To remove props used for styling from the underlying HTML element, use [destructuring][destructuring] to pick out the props used for styling.
138 |
139 | ```jsx
140 | const Heading = ({ color, ...props }) =>
141 |
147 | ```
148 |
149 | ### Styling other components
150 |
151 | To style other components, pass the component to the `is` prop.
152 |
153 | ```jsx
154 | const Link = props =>
155 |
162 | ```
163 |
164 | ### Extending components
165 |
166 | To make an Axs component extensible, pass the `css` prop argument to the Base component's `css` prop after the base styles.
167 |
168 | ```jsx
169 | const Heading = ({ css, ...props }) =>
170 |
177 | ```
178 |
179 | To make an extension from the base Heading component, pass it to the `is` prop.
180 |
181 | ```jsx
182 | const BigHeading = props =>
183 |
190 | ```
191 |
192 | ### Server side rendering
193 |
194 | Server side rendering works without the need for any additional setup.
195 | This is because CSS rulesets are created within the Base component,
196 | which then renders an inline `