[d565449] | 1 | # CSS-in-JS Utilities
|
---|
| 2 | A library that provides useful utilities functions for CSS-in-JS solutions.<br>
|
---|
| 3 | They are intended to be used by CSS-in-JS library authors rather used directly.
|
---|
| 4 | <br>
|
---|
| 5 |
|
---|
| 6 | <img alt="TravisCI" src="https://travis-ci.org/rofrischmann/css-in-js-utils.svg?branch=master"> <img alt="Test Coverage" src="https://api.codeclimate.com/v1/badges/2964ee833f8d2104cac6/test_coverage" /> <img alt="npm downloads" src="https://img.shields.io/npm/dm/css-in-js-utils.svg"> <img alt="npm version" src="https://badge.fury.io/js/css-in-js-utils.svg"> <img alt="gzipped size" src="https://img.shields.io/bundlephobia/minzip/css-in-js-utils.svg?colorB=4c1&label=gzipped%20size">
|
---|
| 7 |
|
---|
| 8 | ## Installation
|
---|
| 9 | ```sh
|
---|
| 10 | yarn add css-in-js-utils
|
---|
| 11 | ```
|
---|
| 12 |
|
---|
| 13 | ## Why?
|
---|
| 14 | By now I have authored and collaborated on many different libraries and found I would rewrite the very same utility functions every time. That's why this repository is hosting small utilities especially built for CSS-in-JS solutions and tools. Even if there are tons of different libraries already, they all basically use the same mechanisms and utilities.
|
---|
| 15 |
|
---|
| 16 | ## Utilities
|
---|
| 17 | * [`assignStyle(base, ...extend)`](#assignstylebase-extend)
|
---|
| 18 | * [`camelCaseProperty(property)`](#camelcasepropertyproperty)
|
---|
| 19 | * [`cssifyDeclaration(property, value)`](#cssifydeclarationproperty-value)
|
---|
| 20 | * [`cssifyObject(object)`](#cssifyobjectobject)
|
---|
| 21 | * [`hyphenateProperty(property)`](#hyphenatepropertyproperty)
|
---|
| 22 | * [`isPrefixedProperty(property)`](#isprefixedpropertyproperty)
|
---|
| 23 | * [`isPrefixedValue(value)`](#isprefixedvaluevalue)
|
---|
| 24 | * [`isUnitlessProperty(property)`](#isunitlesspropertyproperty)
|
---|
| 25 | * [`normalizeProperty(property)`](#normalizepropertyproperty)
|
---|
| 26 | * [`resolveArrayValue(property, value)`](#resolvearrayvalueproperty-value)
|
---|
| 27 | * [`unprefixProperty(property)`](#unprefixpropertyproperty)
|
---|
| 28 | * [`unprefixValue(value)`](#unprefixvaluevalue)
|
---|
| 29 |
|
---|
| 30 | ------
|
---|
| 31 |
|
---|
| 32 | ### `assignStyle(base, ...extend)`
|
---|
| 33 | Merges deep style objects similar to `Object.assign`.<br>
|
---|
| 34 | It also merges array values into a single array whithout creating duplicates. The last occurence of every item wins.
|
---|
| 35 |
|
---|
| 36 | ```javascript
|
---|
| 37 | import { assignStyle } from 'css-in-js-utils'
|
---|
| 38 |
|
---|
| 39 | assignStyle(
|
---|
| 40 | { color: 'red', backgroundColor: 'black' },
|
---|
| 41 | { color: 'blue' }
|
---|
| 42 | )
|
---|
| 43 | // => { color: 'blue', backgroundColor: 'black' }
|
---|
| 44 |
|
---|
| 45 | assignStyle(
|
---|
| 46 | {
|
---|
| 47 | color: 'red',
|
---|
| 48 | ':hover': {
|
---|
| 49 | backgroundColor: 'black'
|
---|
| 50 | }
|
---|
| 51 | },
|
---|
| 52 | {
|
---|
| 53 | ':hover': {
|
---|
| 54 | backgroundColor: 'blue'
|
---|
| 55 | }
|
---|
| 56 | }
|
---|
| 57 | )
|
---|
| 58 | // => { color: 'red', ':hover': { backgroundColor: 'blue' }}
|
---|
| 59 | ```
|
---|
| 60 |
|
---|
| 61 | ------
|
---|
| 62 |
|
---|
| 63 | ### `camelCaseProperty(property)`
|
---|
| 64 | Converts the `property` to camelCase.
|
---|
| 65 |
|
---|
| 66 | ```javascript
|
---|
| 67 | import { camelCaseProperty } from 'css-in-js-utils'
|
---|
| 68 |
|
---|
| 69 | camelCaseProperty('padding-top')
|
---|
| 70 | // => 'paddingTop'
|
---|
| 71 |
|
---|
| 72 | camelCaseProperty('-webkit-transition')
|
---|
| 73 | // => 'WebkitTransition'
|
---|
| 74 | ```
|
---|
| 75 |
|
---|
| 76 | ------
|
---|
| 77 |
|
---|
| 78 | ### `cssifyDeclaration(property, value)`
|
---|
| 79 | Generates a CSS declaration (`property`:`value`) string.
|
---|
| 80 |
|
---|
| 81 | ```javascript
|
---|
| 82 | import { cssifyDeclaration } from 'css-in-js-utils'
|
---|
| 83 |
|
---|
| 84 | cssifyDeclaration('paddingTop', '400px')
|
---|
| 85 | // => 'padding-top:400px'
|
---|
| 86 |
|
---|
| 87 | cssifyDeclaration('WebkitFlex', 3)
|
---|
| 88 | // => '-webkit-flex:3'
|
---|
| 89 | ```
|
---|
| 90 |
|
---|
| 91 | ------
|
---|
| 92 |
|
---|
| 93 | ### `cssifyObject(object)`
|
---|
| 94 | Generates a CSS string using all key-property pairs in `object`.
|
---|
| 95 | It automatically removes declarations with value types other than `number` and `string`.
|
---|
| 96 |
|
---|
| 97 | ```javascript
|
---|
| 98 | import { cssifyObject } from 'css-in-js-utils'
|
---|
| 99 |
|
---|
| 100 | cssifyObject({
|
---|
| 101 | paddingTop: '400px',
|
---|
| 102 | paddingBottom: undefined,
|
---|
| 103 | WebkitFlex: 3,
|
---|
| 104 | _anyKey: [1, 2, 4]
|
---|
| 105 | })
|
---|
| 106 | // => 'padding-top:400px;-webkit-flex:3'
|
---|
| 107 | ```
|
---|
| 108 |
|
---|
| 109 | ------
|
---|
| 110 |
|
---|
| 111 | ### `hyphenateProperty(property)`
|
---|
| 112 | Converts the `property` to hyphen-case.
|
---|
| 113 | > Directly mirrors [hyphenate-style-name](https://github.com/rexxars/hyphenate-style-name).
|
---|
| 114 |
|
---|
| 115 | ```javascript
|
---|
| 116 | import { hyphenateProperty } from 'css-in-js-utils'
|
---|
| 117 |
|
---|
| 118 | hyphenateProperty('paddingTop')
|
---|
| 119 | // => 'padding-top'
|
---|
| 120 |
|
---|
| 121 | hyphenateProperty('WebkitTransition')
|
---|
| 122 | // => '-webkit-transition'
|
---|
| 123 | ```
|
---|
| 124 |
|
---|
| 125 | ------
|
---|
| 126 |
|
---|
| 127 | ### `isPrefixedProperty(property)`
|
---|
| 128 | Checks if a `property` includes a vendor prefix.
|
---|
| 129 |
|
---|
| 130 | ```javascript
|
---|
| 131 | import { isPrefixedProperty } from 'css-in-js-utils'
|
---|
| 132 |
|
---|
| 133 | isPrefixedProperty('paddingTop')
|
---|
| 134 | // => false
|
---|
| 135 |
|
---|
| 136 | isPrefixedProperty('WebkitTransition')
|
---|
| 137 | // => true
|
---|
| 138 | ```
|
---|
| 139 |
|
---|
| 140 | ------
|
---|
| 141 | ### `isPrefixedValue(value)`
|
---|
| 142 | Checks if a `value` includes vendor prefixes.
|
---|
| 143 |
|
---|
| 144 | ```javascript
|
---|
| 145 | import { isPrefixedValue } from 'css-in-js-utils'
|
---|
| 146 |
|
---|
| 147 | isPrefixedValue('200px')
|
---|
| 148 | isPrefixedValue(200)
|
---|
| 149 | // => false
|
---|
| 150 |
|
---|
| 151 | isPrefixedValue('-webkit-calc(100% - 50px)')
|
---|
| 152 | // => true
|
---|
| 153 | ```
|
---|
| 154 |
|
---|
| 155 | ------
|
---|
| 156 |
|
---|
| 157 | ### `isUnitlessProperty(property)`
|
---|
| 158 | Checks if a `property` accepts unitless values.
|
---|
| 159 |
|
---|
| 160 | ```javascript
|
---|
| 161 | import { isUnitlessProperty } from 'css-in-js-utils'
|
---|
| 162 |
|
---|
| 163 | isUnitlessProperty('width')
|
---|
| 164 | // => false
|
---|
| 165 |
|
---|
| 166 | isUnitlessProperty('flexGrow')
|
---|
| 167 | isUnitlessProperty('lineHeight')
|
---|
| 168 | isUnitlessProperty('line-height')
|
---|
| 169 | // => true
|
---|
| 170 | ```
|
---|
| 171 |
|
---|
| 172 | ------
|
---|
| 173 |
|
---|
| 174 | ### `normalizeProperty(property)`
|
---|
| 175 | Normalizes the `property` by unprefixing **and** camelCasing it.
|
---|
| 176 | > Uses the [`camelCaseProperty`](#camelcasepropertyproperty) and [`unprefixProperty`](#unprefixpropertyproperty)-methods.
|
---|
| 177 |
|
---|
| 178 | ```javascript
|
---|
| 179 | import { normalizeProperty } from 'css-in-js-utils'
|
---|
| 180 |
|
---|
| 181 | normalizeProperty('-webkit-transition-delay')
|
---|
| 182 | // => 'transitionDelay'
|
---|
| 183 | ```
|
---|
| 184 |
|
---|
| 185 | ------
|
---|
| 186 |
|
---|
| 187 | ### `resolveArrayValue(property, value)`
|
---|
| 188 | Concatenates array values to single CSS value.
|
---|
| 189 | > Uses the [`hyphenateProperty`](#hyphenatepropertyproperty)-method.
|
---|
| 190 |
|
---|
| 191 |
|
---|
| 192 | ```javascript
|
---|
| 193 | import { resolveArrayValue } from 'css-in-js-utils'
|
---|
| 194 |
|
---|
| 195 | resolveArrayValue('display', [ '-webkit-flex', 'flex' ])
|
---|
| 196 | // => '-webkit-flex;display:flex'
|
---|
| 197 |
|
---|
| 198 | resolveArrayValue('paddingTop', [ 'calc(100% - 50px)', '100px' ])
|
---|
| 199 | // => 'calc(100% - 50px);padding-top:100px'
|
---|
| 200 | ```
|
---|
| 201 |
|
---|
| 202 | ------
|
---|
| 203 |
|
---|
| 204 | ### `unprefixProperty(property)`
|
---|
| 205 | Removes the vendor prefix (if set) from the `property`.
|
---|
| 206 |
|
---|
| 207 | ```javascript
|
---|
| 208 | import { unprefixProperty } from 'css-in-js-utils'
|
---|
| 209 |
|
---|
| 210 | unprefixProperty('WebkitTransition')
|
---|
| 211 | // => 'transition'
|
---|
| 212 |
|
---|
| 213 | unprefixProperty('transitionDelay')
|
---|
| 214 | // => 'transitionDelay'
|
---|
| 215 | ```
|
---|
| 216 |
|
---|
| 217 | ------
|
---|
| 218 |
|
---|
| 219 | ### `unprefixValue(value)`
|
---|
| 220 | Removes all vendor prefixes (if any) from the `value`.
|
---|
| 221 |
|
---|
| 222 | ```javascript
|
---|
| 223 | import { unprefixValue } from 'css-in-js-utils'
|
---|
| 224 |
|
---|
| 225 | unprefixValue('-webkit-calc(-moz-calc(100% - 50px)/2)')
|
---|
| 226 | // => 'calc(calc(100% - 50px)/2)'
|
---|
| 227 |
|
---|
| 228 | unprefixValue('100px')
|
---|
| 229 | // => '100px'
|
---|
| 230 | ```
|
---|
| 231 |
|
---|
| 232 | ## Direct Import
|
---|
| 233 | Every utility function may be imported directly to save bundle size.
|
---|
| 234 |
|
---|
| 235 | ```javascript
|
---|
| 236 | import camelCaseProperty from 'css-in-js-utils/lib/camelCaseProperty'
|
---|
| 237 | ```
|
---|
| 238 |
|
---|
| 239 | ## License
|
---|
| 240 | css-in-js-utils is licensed under the [MIT License](http://opensource.org/licenses/MIT).<br>
|
---|
| 241 | Documentation is licensed under [Creative Common License](http://creativecommons.org/licenses/by/4.0/).<br>
|
---|
| 242 | Created with ♥ by [@rofrischmann](http://rofrischmann.de).
|
---|