Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.

3 лет назад
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241
  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. <img alt="TravisCI" src="https://travis-ci.org/rofrischmann/css-in-js-utils.svg?branch=master"> <a href="https://codeclimate.com/github/rofrischmann/css-in-js-utils/coverage"><img alt="Test Coverage" src="https://codeclimate.com/github/rofrischmann/css-in-js-utils/badges/coverage.svg"></a> <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">
  6. ## Installation
  7. ```sh
  8. yarn add css-in-js-utils
  9. ```
  10. ## Why?
  11. 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.
  12. ## Utilities
  13. * [`assignStyle(base, ...extend)`](#assignstylebase-extend)
  14. * [`camelCaseProperty(property)`](#camelcasepropertyproperty)
  15. * [`cssifyDeclaration(property, value)`](#cssifydeclarationproperty-value)
  16. * [`cssifyObject(object)`](#cssifyobjectobject)
  17. * [`hyphenateProperty(property)`](#hyphenatepropertyproperty)
  18. * [`isPrefixedProperty(property)`](#isprefixedpropertyproperty)
  19. * [`isPrefixedValue(value)`](#isprefixedvaluevalue)
  20. * [`isUnitlessProperty(property)`](#isunitlesspropertyproperty)
  21. * [`normalizeProperty(property)`](#normalizepropertyproperty)
  22. * [`resolveArrayValue(property, value)`](#resolvearrayvalueproperty-value)
  23. * [`unprefixProperty(property)`](#unprefixpropertyproperty)
  24. * [`unprefixValue(value)`](#unprefixvaluevalue)
  25. ------
  26. ### `assignStyle(base, ...extend)`
  27. Merges deep style objects similar to `Object.assign`.
  28. ```javascript
  29. import { assignStyle } from 'css-in-js-utils'
  30. assignStyle(
  31. { color: 'red', backgroundColor: 'black' },
  32. { color: 'blue' }
  33. )
  34. // => { color: 'blue', backgroundColor: 'black' }
  35. assignStyle(
  36. {
  37. color: 'red',
  38. ':hover': {
  39. backgroundColor: 'black'
  40. }
  41. },
  42. ':hover': {
  43. backgroundColor: 'blue'
  44. }
  45. }
  46. )
  47. // => { color: 'red', ':hover': { backgroundColor: 'blue' }}
  48. ```
  49. ------
  50. ### `camelCaseProperty(property)`
  51. Converts the `property` to camelCase.
  52. ```javascript
  53. import { camelCaseProperty } from 'css-in-js-utils'
  54. camelCaseProperty('padding-top')
  55. // => 'paddingTop'
  56. camelCaseProperty('-webkit-transition')
  57. // => 'WebkitTransition'
  58. ```
  59. ------
  60. ### `cssifyDeclaration(property, value)`
  61. Generates a CSS declaration (`property`:`value`) string.
  62. ```javascript
  63. import { cssifyDeclaration } from 'css-in-js-utils'
  64. cssifyDeclaration('paddingTop', '400px')
  65. // => 'padding-top:400px'
  66. cssifyDeclaration('WebkitFlex', 3)
  67. // => '-webkit-flex:3'
  68. ```
  69. ------
  70. ### `cssifyObject(object)`
  71. Generates a CSS string using all key-property pairs in `object`.
  72. It automatically removes declarations with value types other than `number` and `string`.
  73. ```javascript
  74. import { cssifyObject } from 'css-in-js-utils'
  75. cssifyObject({
  76. paddingTop: '400px',
  77. paddingBottom: undefined,
  78. WebkitFlex: 3,
  79. _anyKey: [1, 2, 4]
  80. })
  81. // => 'padding-top:400px;-webkit-flex:3'
  82. ```
  83. ------
  84. ### `hyphenateProperty(property)`
  85. Converts the `property` to hyphen-case.
  86. > Directly mirrors [hyphenate-style-name](https://github.com/rexxars/hyphenate-style-name).
  87. ```javascript
  88. import { hyphenateProperty } from 'css-in-js-utils'
  89. hyphenateProperty('paddingTop')
  90. // => 'padding-top'
  91. hyphenateProperty('WebkitTransition')
  92. // => '-webkit-transition'
  93. ```
  94. ------
  95. ### `isPrefixedProperty(property)`
  96. Checks if a `property` includes a vendor prefix.
  97. ```javascript
  98. import { isPrefixedProperty } from 'css-in-js-utils'
  99. isPrefixedProperty('paddingTop')
  100. // => false
  101. isPrefixedProperty('WebkitTransition')
  102. // => true
  103. ```
  104. ------
  105. ### `isPrefixedValue(value)`
  106. Checks if a `value` includes vendor prefixes.
  107. ```javascript
  108. import { isPrefixedValue } from 'css-in-js-utils'
  109. isPrefixedValue('200px')
  110. isPrefixedValue(200)
  111. // => false
  112. isPrefixedValue('-webkit-calc(100% - 50px)')
  113. // => true
  114. ```
  115. ------
  116. ### `isUnitlessProperty(property)`
  117. Checks if a `property` accepts unitless values.
  118. ```javascript
  119. import { isUnitlessProperty } from 'css-in-js-utils'
  120. isUnitlessProperty('width')
  121. // => false
  122. isUnitlessProperty('flexGrow')
  123. isUnitlessProperty('lineHeight')
  124. isUnitlessProperty('line-height')
  125. // => true
  126. ```
  127. ------
  128. ### `normalizeProperty(property)`
  129. Normalizes the `property` by unprefixing **and** camelCasing it.
  130. > Uses the [`camelCaseProperty`](#camelcasepropertyproperty) and [`unprefixProperty`](#unprefixpropertyproperty)-methods.
  131. ```javascript
  132. import { normalizeProperty } from 'css-in-js-utils'
  133. normalizeProperty('-webkit-transition-delay')
  134. // => 'transitionDelay'
  135. ```
  136. ------
  137. ### `resolveArrayValue(property, value)`
  138. Concatenates array values to single CSS value.
  139. > Uses the [`hyphenateProperty`](#hyphenatepropertyproperty)-method.
  140. ```javascript
  141. import { resolveArrayValue } from 'css-in-js-utils'
  142. resolveArrayValue('display', [ '-webkit-flex', 'flex' ])
  143. // => '-webkit-flex;display:flex'
  144. resolveArrayValue('paddingTop', [ 'calc(100% - 50px)', '100px' ])
  145. // => 'calc(100% - 50px);padding-top:100px'
  146. ```
  147. ------
  148. ### `unprefixProperty(property)`
  149. Removes the vendor prefix (if set) from the `property`.
  150. ```javascript
  151. import { unprefixProperty } from 'css-in-js-utils'
  152. unprefixProperty('WebkitTransition')
  153. // => 'transition'
  154. unprefixProperty('transitionDelay')
  155. // => 'transitionDelay'
  156. ```
  157. ------
  158. ### `unprefixValue(value)`
  159. Removes all vendor prefixes (if any) from the `value`.
  160. ```javascript
  161. import { unprefixValue } from 'css-in-js-utils'
  162. unprefixValue('-webkit-calc(-moz-calc(100% - 50px)/2)')
  163. // => 'calc(calc(100% - 50px)/2)'
  164. unprefixValue('100px')
  165. // => '100px'
  166. ```
  167. ## Direct Import
  168. Every utility function may be imported directly to save bundle size.
  169. ```javascript
  170. import camelCaseProperty from 'css-in-js-utils/lib/camelCaseProperty'
  171. ```
  172. ## License
  173. css-in-js-utils is licensed under the [MIT License](http://opensource.org/licenses/MIT).<br>
  174. Documentation is licensed under [Creative Common License](http://creativecommons.org/licenses/by/4.0/).<br>
  175. Created with ♥ by [@rofrischmann](http://rofrischmann.de).