|
1 | | -# Stachine |
2 | | - |
3 | | -> A TypeScript state machine with events and effects |
4 | | -
|
5 | | -``` |
6 | | -deno add @dldc/stachine |
7 | | -``` |
8 | | - |
9 | | -## Gist |
10 | | - |
11 | | -```ts |
12 | | -import { createStachine } from "@dldc/stachine"; |
13 | | - |
14 | | -type State = { state: "Home" } | { state: "Bed" } | { state: "Work" } | { |
15 | | - state: "Error"; |
16 | | -}; |
17 | | -type Action = { action: "Commute" } | { action: "Wake" } | { action: "Sleep" }; |
18 | | - |
19 | | -const machine = createStachine<State, Action>({ |
20 | | - initialState: { state: "Home" }, |
21 | | - createErrorState: () => ({ state: "Error" }), |
22 | | - states: { |
23 | | - Home: { |
24 | | - actions: { |
25 | | - Commute: () => ({ state: "Work" }), |
26 | | - Sleep: () => ({ state: "Bed" }), |
27 | | - }, |
28 | | - }, |
29 | | - Work: { |
30 | | - actions: { |
31 | | - Commute: () => ({ state: "Home" }), |
32 | | - }, |
33 | | - }, |
34 | | - Bed: { |
35 | | - actions: { |
36 | | - Wake: () => ({ state: "Home" }), |
37 | | - }, |
38 | | - }, |
39 | | - Error: {}, |
40 | | - }, |
41 | | -}); |
| 1 | +# @dldc/css-builder |
| 2 | + |
| 3 | +> A utility for building CSS expressions like `calc`, `clamp`, etc. |
| 4 | +
|
| 5 | +## Installation |
| 6 | + |
| 7 | +```bash |
| 8 | +deno add @dldc/css-builder |
| 9 | +``` |
| 10 | + |
| 11 | +## Usage |
| 12 | + |
| 13 | +This library provides builder functions to construct CSS math expressions and a |
| 14 | +`serialize` function to convert them to CSS strings. |
| 15 | + |
| 16 | +```typescript |
| 17 | +import { add, clamp, multiply, serialize } from "@dldc/css-builder"; |
| 18 | + |
| 19 | +// Build a calc expression |
| 20 | +const result = add("10px", multiply(2, "5px")); |
| 21 | +serialize(result); // "calc(10px + 2*5px)" |
| 22 | + |
| 23 | +// Build a clamp expression |
| 24 | +const responsive = clamp("10px", "5vw", "100px"); |
| 25 | +serialize(responsive); // "clamp(10px,5vw,100px)" |
| 26 | +``` |
| 27 | + |
| 28 | +## API |
| 29 | + |
| 30 | +### Builder Functions |
| 31 | + |
| 32 | +All builder functions accept numbers, strings (like `"10px"`, `"50%"`, |
| 33 | +`"var(--size)"`), or other expressions. |
| 34 | + |
| 35 | +#### `add(...values)` |
| 36 | + |
| 37 | +Creates a `calc()` addition expression. |
| 38 | + |
| 39 | +```typescript |
| 40 | +serialize(add(10, 20, 30)); // "calc(10 + 20 + 30)" |
| 41 | +serialize(add("10px", "2rem")); // "calc(10px + 2rem)" |
| 42 | +``` |
| 43 | + |
| 44 | +#### `substract(...values)` |
| 45 | + |
| 46 | +Creates a `calc()` subtraction expression. |
| 47 | + |
| 48 | +```typescript |
| 49 | +serialize(substract(100, 20, 10)); // "calc(100 - 20 - 10)" |
| 50 | +serialize(substract("100%", "20px")); // "calc(100% - 20px)" |
| 51 | +``` |
| 52 | + |
| 53 | +#### `multiply(...values)` |
| 54 | + |
| 55 | +Creates a `calc()` multiplication expression. |
| 56 | + |
| 57 | +```typescript |
| 58 | +serialize(multiply(2, 3, 4)); // "calc(2*3*4)" |
| 59 | +serialize(multiply("10px", 2)); // "calc(10px*2)" |
| 60 | +``` |
| 61 | + |
| 62 | +#### `divide(...values)` |
| 63 | + |
| 64 | +Creates a `calc()` division expression. |
| 65 | + |
| 66 | +```typescript |
| 67 | +serialize(divide(100, 2, 5)); // "calc(100/2/5)" |
| 68 | +serialize(divide("100%", 3)); // "calc(100%/3)" |
| 69 | +``` |
| 70 | + |
| 71 | +#### `min(...values)` |
| 72 | + |
| 73 | +Creates a `min()` expression. |
| 74 | + |
| 75 | +```typescript |
| 76 | +serialize(min(10, "20px", "50%")); // "min(10,20px,50%)" |
| 77 | +``` |
| 78 | + |
| 79 | +#### `max(...values)` |
| 80 | + |
| 81 | +Creates a `max()` expression. |
| 82 | + |
| 83 | +```typescript |
| 84 | +serialize(max(10, "20px", "50%")); // "max(10,20px,50%)" |
| 85 | +``` |
| 86 | + |
| 87 | +#### `clamp(min, preferred, max)` |
| 88 | + |
| 89 | +Creates a `clamp()` expression. Use `"none"` for unbounded min or max. |
| 90 | + |
| 91 | +```typescript |
| 92 | +serialize(clamp(10, "50%", "100px")); // "clamp(10,50%,100px)" |
| 93 | +serialize(clamp("none", "50%", "none")); // "clamp(none,50%,none)" |
| 94 | +``` |
| 95 | + |
| 96 | +#### `exp(value)` |
| 97 | + |
| 98 | +Creates an `exp()` expression. |
| 99 | + |
| 100 | +```typescript |
| 101 | +serialize(exp(2)); // "exp(2)" |
| 102 | +serialize(exp("2.5")); // "exp(2.5)" |
| 103 | +``` |
| 104 | + |
| 105 | +### Composing Expressions |
| 106 | + |
| 107 | +Builder functions can be composed together: |
| 108 | + |
| 109 | +```typescript |
| 110 | +const result = add("10px", multiply(2, "5vw"), clamp(0, "20%", "50px")); |
| 111 | +serialize(result); |
| 112 | +// "calc(10px + 2*5vw + clamp(0,20%,50px))" |
| 113 | +``` |
| 114 | + |
| 115 | +### `serialize(expression)` |
| 116 | + |
| 117 | +Converts an expression to a CSS string. |
| 118 | + |
| 119 | +```typescript |
| 120 | +const expr = multiply(add(10, 20), 2); |
| 121 | +const css = serialize(expr); // "calc((10 + 20)*2)" |
42 | 122 | ``` |
0 commit comments