|
1 | | -## JS functions to build CSS functions |
| 1 | +## JavaScript functions to build CSS functions |
| 2 | + |
| 3 | +This package ships functions that return the equivalent CSS function syntax. |
| 4 | + |
| 5 | +## API |
| 6 | +Right now we ship 25 functions.<br> |
| 7 | + |
| 8 | +* `hsl(h, s, l)` |
| 9 | +* `hsla(h, s, l, a)` |
| 10 | +* `matrix(a, b, c, d, x, y)` |
| 11 | +* `matrix3d(a1, b1, c1, d1, a2, b2, c2, d2, a3, b3, c3, d3, a4, b4, c4, d4)` |
| 12 | +* `perspective(p)` |
| 13 | +* `rgb(r, g, b)` |
| 14 | +* `rgba(r, g, b, a)` |
| 15 | +* `rotate(x, y)` |
| 16 | +* `rotate3d(x, y, z)` |
| 17 | +* `rotateX(x)` |
| 18 | +* `rotateY(y)` |
| 19 | +* `rotateZ(z)` |
| 20 | +* `scale(x, y)` |
| 21 | +* `scale3d(x, y, z)` |
| 22 | +* `scaleX(x)` |
| 23 | +* `scaleY(y)` |
| 24 | +* `scaleZ(z)` |
| 25 | +* `skew(x, y)` |
| 26 | +* `skewX(x)` |
| 27 | +* `skewY(y)` |
| 28 | +* `translate(x, y)` |
| 29 | +* `translate3d(x, y, z)` |
| 30 | +* `translateX(x)` |
| 31 | +* `translateY(y)` |
| 32 | +* `translateZ(z)` |
| 33 | + |
| 34 | +### Parameter object notation |
| 35 | +All parameters can always be passed as a single objects as well.<br> |
| 36 | +The keys always match the parameter name e.g. `rotate3d({ x, y, z })` except for the following color functions: |
| 37 | + |
| 38 | +* `hsl({ hue, saturation, lightness })` |
| 39 | +* `hsla({ hue, saturation, alpha })` |
| 40 | +* `rgb({ red, green, blue })` |
| 41 | +* `rgba({ red, green, blue, alpha })` |
| 42 | + |
| 43 | +## Example |
| 44 | +```javascript |
| 45 | +import { rgba, translate3d } from 'css-functions' |
| 46 | + |
| 47 | +// => 'rgba(255, 0, 255, 0.5)' |
| 48 | +const color = rgba(255, 0, 255, 0.5) |
| 49 | +const color = rgba({ |
| 50 | + red: 255, |
| 51 | + green: 0, |
| 52 | + blue: 255, |
| 53 | + alpha: 0.5 |
| 54 | +}) |
| 55 | + |
| 56 | +// => 'translate3d(10px, 10%, 0)' |
| 57 | +const transform = translate3d('10px', '10%', 0) |
| 58 | +const transform = translate3d({ |
| 59 | + x: '10px', |
| 60 | + y: '10%', |
| 61 | + z: 0 |
| 62 | +}) |
| 63 | +``` |
| 64 | + |
| 65 | +## Multiple functions |
| 66 | +To combine multiple functions safely, it ships the `multiple` API. |
| 67 | +It safely combines both returned strings separated by a whitespace. |
2 | 68 |
|
3 | 69 | ```javascript |
4 | | - rgba(255, 255, 255, 0.5) |
5 | | - ``` |
| 70 | +import { translateX, scale, rotateX, multiple } from 'css-functions' |
6 | 71 |
|
7 | | - ```css |
8 | | - rgba(255, 255, 255, 0.5) |
9 | | - ``` |
| 72 | +// => 'translateX('10%') scale(0.5, 0.5) rotateX(180deg)' |
| 73 | +const combined = multiple( |
| 74 | + translateX('10%'), |
| 75 | + scale(0.5, 0.5), |
| 76 | + rotateX(180) |
| 77 | +) |
| 78 | +``` |
0 commit comments