@link-foundation/ui-style 0.0.0-stage → 0.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +24 -0
- package/README.md +255 -2
- package/package.json +49 -4
- package/src/clip-path.js +98 -0
- package/src/conditions.js +167 -0
- package/src/css.js +245 -0
- package/src/diagnostics.js +48 -0
- package/src/index.d.ts +185 -0
- package/src/index.js +31 -0
- package/src/match.js +114 -0
- package/src/materials.js +24 -0
- package/src/properties.js +291 -0
- package/src/read.js +313 -0
- package/src/resolve.js +159 -0
- package/src/types.js +55 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
This is free and unencumbered software released into the public domain.
|
|
2
|
+
|
|
3
|
+
Anyone is free to copy, modify, publish, use, compile, sell, or
|
|
4
|
+
distribute this software, either in source code form or as a compiled
|
|
5
|
+
binary, for any purpose, commercial or non-commercial, and by any
|
|
6
|
+
means.
|
|
7
|
+
|
|
8
|
+
In jurisdictions that recognize copyright laws, the author or authors
|
|
9
|
+
of this software dedicate any and all copyright interest in the
|
|
10
|
+
software to the public domain. We make this dedication for the benefit
|
|
11
|
+
of the public at large and to the detriment of our heirs and
|
|
12
|
+
successors. We intend this dedication to be an overt act of
|
|
13
|
+
relinquishment in perpetuity of all present and future rights to this
|
|
14
|
+
software under copyright law.
|
|
15
|
+
|
|
16
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
|
17
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
|
18
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
|
|
19
|
+
IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR
|
|
20
|
+
OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
|
|
21
|
+
ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
|
|
22
|
+
OTHER DEALINGS IN THE SOFTWARE.
|
|
23
|
+
|
|
24
|
+
For more information, please refer to <https://unlicense.org>
|
package/README.md
CHANGED
|
@@ -1,3 +1,256 @@
|
|
|
1
|
-
#
|
|
1
|
+
# @link-foundation/ui-style
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
Style rules of the associative UI framework, written in Links Notation. The
|
|
4
|
+
package reads the rules, matches them to UI elements and resolves token
|
|
5
|
+
references through
|
|
6
|
+
[`@link-foundation/ui-tokens`](../ui-tokens/README.md). It produces one of
|
|
7
|
+
two results from the same rules:
|
|
8
|
+
|
|
9
|
+
- `compileCss(rules)`: a CSS stylesheet for the DOM renderer.
|
|
10
|
+
- `resolveStyle(rules, tokens, node, ancestors, env)`: the resolved style of
|
|
11
|
+
one element in a given state and environment, for renderers without CSS
|
|
12
|
+
(React Native, SwiftUI, Compose).
|
|
13
|
+
|
|
14
|
+
Its runtime dependencies are `@link-foundation/ui-core` and
|
|
15
|
+
`@link-foundation/ui-tokens`. Rules are parsed only by ui-core `parse`, which
|
|
16
|
+
uses the official `links-notation` parser.
|
|
17
|
+
|
|
18
|
+
```js
|
|
19
|
+
import { parse, values } from '@link-foundation/ui-core';
|
|
20
|
+
import { readTokens, resolveTokens } from '@link-foundation/ui-tokens';
|
|
21
|
+
import {
|
|
22
|
+
compileCss,
|
|
23
|
+
readStyles,
|
|
24
|
+
resolveStyle,
|
|
25
|
+
} from '@link-foundation/ui-style';
|
|
26
|
+
|
|
27
|
+
const { rules, diagnostics } = readStyles(`
|
|
28
|
+
(button-style: style
|
|
29
|
+
(selector (type button) (variant filled))
|
|
30
|
+
(background (token color.primary))
|
|
31
|
+
(color (token color.on-primary))
|
|
32
|
+
(radius (token shape.corner.medium))
|
|
33
|
+
(state hover (elevation 2))
|
|
34
|
+
(state pressed (scale 0.98)))
|
|
35
|
+
`);
|
|
36
|
+
|
|
37
|
+
console.log(compileCss(rules));
|
|
38
|
+
// ui-button[variant='filled'] {
|
|
39
|
+
// background-color: var(--color-primary);
|
|
40
|
+
// color: var(--color-on-primary);
|
|
41
|
+
// border-radius: var(--shape-corner-medium);
|
|
42
|
+
// }
|
|
43
|
+
// ui-button[variant='filled']:hover {
|
|
44
|
+
// box-shadow: var(--elevation-2);
|
|
45
|
+
// }
|
|
46
|
+
// ui-button[variant='filled']:active {
|
|
47
|
+
// transform: scale(0.98);
|
|
48
|
+
// }
|
|
49
|
+
|
|
50
|
+
const { sets } = readTokens(`
|
|
51
|
+
(material3: theme
|
|
52
|
+
(token color.primary '#6750A4')
|
|
53
|
+
(token color.on-primary '#FFFFFF')
|
|
54
|
+
(token shape.corner.medium 12px))
|
|
55
|
+
`);
|
|
56
|
+
const tokens = resolveTokens(sets, 'material3');
|
|
57
|
+
const ok = parse('(ok: button (label OK) (variant filled))').byId.get('ok');
|
|
58
|
+
const { style, sources } = resolveStyle(rules, tokens, ok, [], {
|
|
59
|
+
states: ['hover'],
|
|
60
|
+
});
|
|
61
|
+
values.toHex(style.background); // '#6750A4'
|
|
62
|
+
style.elevation; // { type: 'number', value: 2 }
|
|
63
|
+
sources.elevation; // 'button-style'
|
|
64
|
+
```
|
|
65
|
+
|
|
66
|
+
The complete example is `examples/ui-style-basics.mjs` in the repository.
|
|
67
|
+
|
|
68
|
+
## Grammar
|
|
69
|
+
|
|
70
|
+
A rule is a `style` element with an id. It has one selector, declarations,
|
|
71
|
+
and blocks that apply in a state or under a condition:
|
|
72
|
+
|
|
73
|
+
```lino
|
|
74
|
+
card-style: style
|
|
75
|
+
selector
|
|
76
|
+
type card
|
|
77
|
+
within
|
|
78
|
+
type dialog
|
|
79
|
+
padding
|
|
80
|
+
token space.medium
|
|
81
|
+
media
|
|
82
|
+
max-width 640px
|
|
83
|
+
padding
|
|
84
|
+
token space.small
|
|
85
|
+
state hover
|
|
86
|
+
opacity 0.92
|
|
87
|
+
container
|
|
88
|
+
min-width 400px
|
|
89
|
+
direction horizontal
|
|
90
|
+
media
|
|
91
|
+
prefers-color-scheme dark
|
|
92
|
+
prefers-reduced-motion reduce
|
|
93
|
+
border-color
|
|
94
|
+
token color.outline
|
|
95
|
+
state disabled
|
|
96
|
+
opacity 0.38
|
|
97
|
+
```
|
|
98
|
+
|
|
99
|
+
| Link | Meaning |
|
|
100
|
+
| ---------------------------------- | ------------------------------------------------------------------------------------------------------------- |
|
|
101
|
+
| `(selector PART...)` | The elements of the rule. All parts must match. Exactly one per rule. |
|
|
102
|
+
| `(PROPERTY VALUE...)` | A declaration (see [Properties](#properties)). When a property is declared twice, the later declaration wins. |
|
|
103
|
+
| `(state STATE BODY...)` | Declarations that apply while the element is in `STATE` (see [States](#states)). |
|
|
104
|
+
| `(media CONDITION... BODY...)` | Declarations that apply while every condition holds (see [Conditions](#conditions)). |
|
|
105
|
+
| `(container CONDITION... BODY...)` | Declarations that apply while every condition holds for the nearest size container. |
|
|
106
|
+
|
|
107
|
+
A block's `BODY` is made of declarations and further blocks, so a `state` can
|
|
108
|
+
sit inside a `media` block and the other way round. A rule with no id is read
|
|
109
|
+
as `style-N` and reported as `anonymous-rule`.
|
|
110
|
+
|
|
111
|
+
Selector parts:
|
|
112
|
+
|
|
113
|
+
| Part | Matches | CSS |
|
|
114
|
+
| ------------------ | ---------------------------------------------------------------------------------------- | ------------------ |
|
|
115
|
+
| `(type KIND)` | elements of the kind `KIND` (`button`, `card`) | `ui-KIND` |
|
|
116
|
+
| `(id ID)` | the element with the id `ID` | `#ID` |
|
|
117
|
+
| `(PROP VALUE)` | elements whose property `PROP` has the single value `VALUE` | `[PROP='VALUE']` |
|
|
118
|
+
| `(within PART...)` | elements with an ancestor that matches `PART...`. `within` can nest and can be repeated. | `ANCESTOR ELEMENT` |
|
|
119
|
+
|
|
120
|
+
The first `within` is written as a descendant combinator. Each further one is
|
|
121
|
+
written as `:is(ANCESTOR *)`, which has the same specificity. Arbitrary CSS
|
|
122
|
+
selectors, other combinators and pseudo-elements are not part of the grammar.
|
|
123
|
+
|
|
124
|
+
### CSS syntax is not accepted
|
|
125
|
+
|
|
126
|
+
In Links Notation, `:` separates an id from a link. CSS syntax therefore
|
|
127
|
+
cannot be written in bare values: `(state :hover)` reads as a link with an
|
|
128
|
+
id, and `(media (max-width: 640px))` reads as a link whose id is
|
|
129
|
+
`max-width`. Such links, and any value that contains `:` (such as
|
|
130
|
+
`'button:hover'`), are reported as `css-syntax` errors and skipped. A bare
|
|
131
|
+
`red:hover` is already a syntax error of the parser, which is reported as
|
|
132
|
+
`syntax-error`. Write the structured links instead: `(state hover)` and
|
|
133
|
+
`(media (max-width 640px))`.
|
|
134
|
+
|
|
135
|
+
## Properties
|
|
136
|
+
|
|
137
|
+
Every value is a typed value of ui-core (`values.typeValue`), a token
|
|
138
|
+
reference `(token PATH)`, or a fallback `(or (token PATH) VALUE)`.
|
|
139
|
+
`(param ...)`, element references and free text are rejected as
|
|
140
|
+
`invalid-value`, so CSS such as `url(...)` can never reach the stylesheet.
|
|
141
|
+
|
|
142
|
+
| Property | CSS | Value | Tokens |
|
|
143
|
+
| -------------- | ------------------ | ------------------------------------------------- | ---------------------- |
|
|
144
|
+
| `background` | `background-color` | colour | `color` |
|
|
145
|
+
| `color` | `color` | colour | `color` |
|
|
146
|
+
| `radius` | `border-radius` | 1 to 4 dimensions | `dimension` |
|
|
147
|
+
| `padding` | `padding` | 1 to 4 dimensions | `dimension` |
|
|
148
|
+
| `gap` | `gap` | 1 or 2 dimensions | `dimension` |
|
|
149
|
+
| `opacity` | `opacity` | number from 0 to 1 | `number` |
|
|
150
|
+
| `elevation` | `box-shadow` | whole number from 0, written `var(--elevation-N)` | none |
|
|
151
|
+
| `scale` | `transform` | number, written `scale(N)` | `number` |
|
|
152
|
+
| `font-size` | `font-size` | dimension | `dimension` |
|
|
153
|
+
| `font-weight` | `font-weight` | number from 1 to 1000 | `fontWeight`, `number` |
|
|
154
|
+
| `border-width` | `border-width` | dimension | `dimension` |
|
|
155
|
+
| `border-color` | `border-color` | colour | `color` |
|
|
156
|
+
| `direction` | `flex-direction` | `horizontal` (`row`) or `vertical` (`column`) | none |
|
|
157
|
+
|
|
158
|
+
Colours are any colour of ui-core (`'#6750A4'`, `(oklch 0.5 0.1 300)`, ...).
|
|
159
|
+
Dimensions are numbers with a unit (`12px`, `1rem`) or `0`, and they must not
|
|
160
|
+
be negative. In CSS a token reference is written `var(--PATH)`, following the
|
|
161
|
+
naming rule of ui-tokens `cssVariable` (`color.on-primary` gives
|
|
162
|
+
`var(--color-on-primary)`). A fallback is written `var(--a, VALUE)`.
|
|
163
|
+
|
|
164
|
+
## States
|
|
165
|
+
|
|
166
|
+
| State | CSS |
|
|
167
|
+
| ---------- | ---------------------------------------------------- |
|
|
168
|
+
| `hover` | `:hover` |
|
|
169
|
+
| `focus` | `:focus-visible` |
|
|
170
|
+
| `pressed` | `:active` |
|
|
171
|
+
| `disabled` | `:is(:disabled, [disabled], [aria-disabled='true'])` |
|
|
172
|
+
| `selected` | `[aria-selected='true']` |
|
|
173
|
+
| `checked` | `[aria-checked='true']` |
|
|
174
|
+
|
|
175
|
+
The names follow the Material 3 interaction states. In `resolveStyle`, the
|
|
176
|
+
current states are given as `env.states`.
|
|
177
|
+
|
|
178
|
+
## Conditions
|
|
179
|
+
|
|
180
|
+
| Block | Condition | CSS | Holds in `resolveStyle` when |
|
|
181
|
+
| ----------- | --------------------------------- | ----------------------------------------- | ----------------------------------------------- |
|
|
182
|
+
| `media` | `(min-width W)` | `@media (min-width: W)` | `env.viewport.width >= W` |
|
|
183
|
+
| `media` | `(max-width W)` | `@media (max-width: W)` | `env.viewport.width <= W` |
|
|
184
|
+
| `media` | `(prefers-color-scheme dark)` | `@media (prefers-color-scheme: dark)` | `env.colorScheme` is `dark` (default `light`) |
|
|
185
|
+
| `media` | `(prefers-reduced-motion reduce)` | `@media (prefers-reduced-motion: reduce)` | `env.reducedMotion` is `true` (default `false`) |
|
|
186
|
+
| `container` | `(min-width W)` | `@container (min-width: W)` | `env.container.width >= W` |
|
|
187
|
+
| `container` | `(max-width W)` | `@container (max-width: W)` | `env.container.width <= W` |
|
|
188
|
+
|
|
189
|
+
Widths are in `px`, `rem` or `em`. `resolveStyle` converts `rem` and `em` with
|
|
190
|
+
a root font size of 16px. A width condition never holds when the width is
|
|
191
|
+
not given. `light` and `no-preference` are accepted as well. Several
|
|
192
|
+
conditions in one block are joined with `and`.
|
|
193
|
+
|
|
194
|
+
## Cascade
|
|
195
|
+
|
|
196
|
+
For each property, `resolveStyle` takes the declaration that applies with the
|
|
197
|
+
highest specificity. Among declarations with equal specificity, it takes the
|
|
198
|
+
later one. This is the same order that a browser applies to the stylesheet
|
|
199
|
+
written by `compileCss`, so both results agree.
|
|
200
|
+
|
|
201
|
+
- Specificity is `[ids, properties + states, types]`, counted over the
|
|
202
|
+
selector and its `within` parts, as CSS counts the written selector. Media
|
|
203
|
+
and container blocks add none.
|
|
204
|
+
- Rules come in source order. A rule's own declarations come before its
|
|
205
|
+
blocks, and blocks come depth-first in source order.
|
|
206
|
+
|
|
207
|
+
So `(id ok)` beats `(type button) (variant filled)`, which beats
|
|
208
|
+
`(type button)`. A `state` block beats a later `media` block with the same
|
|
209
|
+
selector.
|
|
210
|
+
|
|
211
|
+
## API
|
|
212
|
+
|
|
213
|
+
| Function | Result |
|
|
214
|
+
| ----------------------------------------------------- | ------------------------------------------------------------------------------------------------------------------------- |
|
|
215
|
+
| `readStyles(text)` | `{ rules, diagnostics }`: every `style` element of the text. Other elements are skipped. |
|
|
216
|
+
| `compileCss(rules, { tagFor, prefix })` | The stylesheet. `tagFor(kind)` gives the element selector (default `ui-KIND`). `prefix` is the ui-tokens variable prefix. |
|
|
217
|
+
| `resolveStyle(rules, tokens, node, ancestors, env)` | `{ style, sources, diagnostics }`: typed values per property and the id of the winning rule per property. |
|
|
218
|
+
| `matches(selector, node, ancestors)` | Whether an element matches. `ancestors` go from the root to the parent. |
|
|
219
|
+
| `specificity(selector, states)`, `compareSpecificity` | The specificity tuple and its order. |
|
|
220
|
+
| `flatten(rules)` | Every rule and block as one entry, in cascade order. |
|
|
221
|
+
|
|
222
|
+
`tokens` is the result of ui-tokens `resolveTokens`, or any `Map` from a
|
|
223
|
+
token path to a typed value. `env` is
|
|
224
|
+
`{ states, viewport: { width }, container: { width }, colorScheme, reducedMotion }`.
|
|
225
|
+
All of its fields are optional. Identifiers in the CSS are escaped as by
|
|
226
|
+
`CSS.escape`, and property values are written as quoted CSS strings.
|
|
227
|
+
|
|
228
|
+
## Diagnostics
|
|
229
|
+
|
|
230
|
+
| Code | When |
|
|
231
|
+
| ------------------------------------------ | ---------------------------------------------------------------------------------------- |
|
|
232
|
+
| `css-syntax` | CSS syntax in a value: `(state :hover)`, `(media (max-width: 640px))`, `'a:b'`. |
|
|
233
|
+
| `unknown-property` | A declaration whose name is not in the table above. |
|
|
234
|
+
| `invalid-value` | A value of the wrong type, out of range, or with too many parts. |
|
|
235
|
+
| `unknown-state`, `invalid-condition` | A state outside the list, or a missing, unknown or invalid condition. |
|
|
236
|
+
| `missing-selector`, `invalid-selector` | A rule with no selector (the rule is dropped), or a selector part that cannot be read. |
|
|
237
|
+
| `duplicate-selector`, `misplaced-selector` | A second selector (the last one is used), or a selector inside a block. |
|
|
238
|
+
| `anonymous-rule`, `unexpected-value` | A rule without an id, or a value that is not a link in a rule body. |
|
|
239
|
+
| `unknown-token`, `type-mismatch` | From `resolveStyle`: a missing token, or a token of a type the property does not accept. |
|
|
240
|
+
|
|
241
|
+
Parser diagnostics of ui-core, such as `syntax-error`, are passed through.
|
|
242
|
+
|
|
243
|
+
## Tests
|
|
244
|
+
|
|
245
|
+
```bash
|
|
246
|
+
node --test packages/ui-style/tests/*.test.js
|
|
247
|
+
UPDATE_GOLDEN=1 node --test packages/ui-style/tests/ # rewrite tests/golden/*.css
|
|
248
|
+
```
|
|
249
|
+
|
|
250
|
+
`tests/golden/styles.css` is the stylesheet of the issue-1 sample
|
|
251
|
+
`docs/case-studies/issue-1/samples/styles.lino`. The CSS tests also parse
|
|
252
|
+
every stylesheet with `postcss`, which is a development dependency only.
|
|
253
|
+
|
|
254
|
+
## License
|
|
255
|
+
|
|
256
|
+
Unlicense.
|
package/package.json
CHANGED
|
@@ -1,6 +1,51 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@link-foundation/ui-style",
|
|
3
|
-
"version": "0.0.0
|
|
4
|
-
"
|
|
5
|
-
"
|
|
6
|
-
|
|
3
|
+
"version": "0.0.0",
|
|
4
|
+
"description": "Style rules of the associative UI framework written in Links Notation: selectors, states, media and container conditions, resolved against ui-tokens into a CSS stylesheet or a resolved style per element",
|
|
5
|
+
"type": "module",
|
|
6
|
+
"exports": {
|
|
7
|
+
".": {
|
|
8
|
+
"types": "./src/index.d.ts",
|
|
9
|
+
"default": "./src/index.js"
|
|
10
|
+
}
|
|
11
|
+
},
|
|
12
|
+
"types": "./src/index.d.ts",
|
|
13
|
+
"files": [
|
|
14
|
+
"src/",
|
|
15
|
+
"README.md",
|
|
16
|
+
"LICENSE"
|
|
17
|
+
],
|
|
18
|
+
"keywords": [
|
|
19
|
+
"ui",
|
|
20
|
+
"links-notation",
|
|
21
|
+
"styles",
|
|
22
|
+
"css",
|
|
23
|
+
"design-tokens",
|
|
24
|
+
"media-queries",
|
|
25
|
+
"container-queries"
|
|
26
|
+
],
|
|
27
|
+
"license": "Unlicense",
|
|
28
|
+
"repository": {
|
|
29
|
+
"type": "git",
|
|
30
|
+
"url": "git+https://github.com/link-foundation/ui.git",
|
|
31
|
+
"directory": "packages/ui-style"
|
|
32
|
+
},
|
|
33
|
+
"homepage": "https://github.com/link-foundation/ui/tree/main/packages/ui-style#readme",
|
|
34
|
+
"bugs": {
|
|
35
|
+
"url": "https://github.com/link-foundation/ui/issues"
|
|
36
|
+
},
|
|
37
|
+
"engines": {
|
|
38
|
+
"node": ">=20"
|
|
39
|
+
},
|
|
40
|
+
"dependencies": {
|
|
41
|
+
"@link-foundation/ui-core": "^0.0.0",
|
|
42
|
+
"@link-foundation/ui-tokens": "^0.0.0"
|
|
43
|
+
},
|
|
44
|
+
"devDependencies": {
|
|
45
|
+
"postcss": "^8.5.6",
|
|
46
|
+
"test-anywhere": "^0.8.48"
|
|
47
|
+
},
|
|
48
|
+
"publishConfig": {
|
|
49
|
+
"access": "public"
|
|
50
|
+
}
|
|
51
|
+
}
|
package/src/clip-path.js
ADDED
|
@@ -0,0 +1,98 @@
|
|
|
1
|
+
/** Deliberately bounded CSS geometry grammar. No URLs, strings, escapes,
|
|
2
|
+
* comments, declarations or unrecognised functions can reach CSS output. */
|
|
3
|
+
export function validClipPath(text) {
|
|
4
|
+
if (typeof text !== 'string' || text.length > 131072) {
|
|
5
|
+
return false;
|
|
6
|
+
}
|
|
7
|
+
if (/^inset\([0-9.]+px(?: round [0-9.]+px)?\)$/u.test(text)) {
|
|
8
|
+
return true;
|
|
9
|
+
}
|
|
10
|
+
if (!text.startsWith('polygon(') || !text.endsWith(')')) {
|
|
11
|
+
return false;
|
|
12
|
+
}
|
|
13
|
+
const source = text.slice(8, -1);
|
|
14
|
+
let offset = 0;
|
|
15
|
+
let depth = 0;
|
|
16
|
+
const space = () => {
|
|
17
|
+
while (/\s/u.test(source[offset] ?? '') && offset < source.length) {
|
|
18
|
+
offset++;
|
|
19
|
+
}
|
|
20
|
+
};
|
|
21
|
+
function term() {
|
|
22
|
+
space();
|
|
23
|
+
if (++depth > 24) {
|
|
24
|
+
return false;
|
|
25
|
+
}
|
|
26
|
+
let ok = false;
|
|
27
|
+
if (source.startsWith('calc(', offset)) {
|
|
28
|
+
offset += 5;
|
|
29
|
+
ok = expression();
|
|
30
|
+
space();
|
|
31
|
+
ok = ok && source[offset++] === ')';
|
|
32
|
+
} else if (source.startsWith('var(', offset)) {
|
|
33
|
+
offset += 4;
|
|
34
|
+
space();
|
|
35
|
+
const match = /^--[a-zA-Z_][\w-]*/u.exec(source.slice(offset));
|
|
36
|
+
if (match) {
|
|
37
|
+
offset += match[0].length;
|
|
38
|
+
space();
|
|
39
|
+
if (source[offset] === ',') {
|
|
40
|
+
offset++;
|
|
41
|
+
ok = term();
|
|
42
|
+
} else {
|
|
43
|
+
ok = true;
|
|
44
|
+
}
|
|
45
|
+
space();
|
|
46
|
+
ok = ok && source[offset++] === ')';
|
|
47
|
+
}
|
|
48
|
+
} else {
|
|
49
|
+
const match = /^[+-]?(?:\d+(?:\.\d*)?|\.\d+)(?:px|%|cqw|cqh)?/u.exec(
|
|
50
|
+
source.slice(offset)
|
|
51
|
+
);
|
|
52
|
+
if (match) {
|
|
53
|
+
offset += match[0].length;
|
|
54
|
+
ok = true;
|
|
55
|
+
}
|
|
56
|
+
}
|
|
57
|
+
depth--;
|
|
58
|
+
return ok;
|
|
59
|
+
}
|
|
60
|
+
function expression() {
|
|
61
|
+
if (!term()) {
|
|
62
|
+
return false;
|
|
63
|
+
}
|
|
64
|
+
space();
|
|
65
|
+
let count = 0;
|
|
66
|
+
while (['+', '-', '*', '/'].includes(source[offset])) {
|
|
67
|
+
if (++count > 256) {
|
|
68
|
+
return false;
|
|
69
|
+
}
|
|
70
|
+
offset++;
|
|
71
|
+
if (!term()) {
|
|
72
|
+
return false;
|
|
73
|
+
}
|
|
74
|
+
space();
|
|
75
|
+
}
|
|
76
|
+
return true;
|
|
77
|
+
}
|
|
78
|
+
let points = 0;
|
|
79
|
+
while (offset < source.length) {
|
|
80
|
+
if (!term()) {
|
|
81
|
+
return false;
|
|
82
|
+
}
|
|
83
|
+
const before = offset;
|
|
84
|
+
space();
|
|
85
|
+
if (offset === before || !term()) {
|
|
86
|
+
return false;
|
|
87
|
+
}
|
|
88
|
+
points++;
|
|
89
|
+
space();
|
|
90
|
+
if (offset === source.length) {
|
|
91
|
+
return points >= 3 && points <= 512;
|
|
92
|
+
}
|
|
93
|
+
if (source[offset++] !== ',' || points > 512) {
|
|
94
|
+
return false;
|
|
95
|
+
}
|
|
96
|
+
}
|
|
97
|
+
return false;
|
|
98
|
+
}
|
|
@@ -0,0 +1,167 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* States and conditions: `(state hover)`, `(media (max-width 640px))`,
|
|
3
|
+
* `(media (prefers-color-scheme dark))`, `(container (min-width 400px))`.
|
|
4
|
+
* Each is read once and then either tested against an environment
|
|
5
|
+
* (`resolveStyle`) or written as CSS (`compileCss`).
|
|
6
|
+
*/
|
|
7
|
+
|
|
8
|
+
import { values } from '@link-foundation/ui-core';
|
|
9
|
+
import { styleDiagnostic } from './diagnostics.js';
|
|
10
|
+
import { rawText } from './properties.js';
|
|
11
|
+
|
|
12
|
+
/**
|
|
13
|
+
* @typedef {import('./types.js').Condition} Condition
|
|
14
|
+
* @typedef {import('./types.js').StyleEnv} StyleEnv
|
|
15
|
+
* @typedef {import('./types.js').RawValue} RawValue
|
|
16
|
+
*/
|
|
17
|
+
|
|
18
|
+
/** The states, in the order of the issue. */
|
|
19
|
+
export const STATES = Object.freeze([
|
|
20
|
+
'hover',
|
|
21
|
+
'focus',
|
|
22
|
+
'pressed',
|
|
23
|
+
'active',
|
|
24
|
+
'disabled',
|
|
25
|
+
'selected',
|
|
26
|
+
'checked',
|
|
27
|
+
]);
|
|
28
|
+
|
|
29
|
+
/** The CSS of every state (Selectors Level 4, ARIA attributes). */
|
|
30
|
+
export const STATE_CSS = Object.freeze({
|
|
31
|
+
hover: ':hover',
|
|
32
|
+
focus: ':focus-visible',
|
|
33
|
+
pressed: ':active',
|
|
34
|
+
active: ':active',
|
|
35
|
+
disabled: `:is(:disabled, [disabled], [aria-disabled='true'])`,
|
|
36
|
+
selected: `[aria-selected='true']`,
|
|
37
|
+
checked: `[aria-checked='true']`,
|
|
38
|
+
});
|
|
39
|
+
|
|
40
|
+
const WIDTH = { type: 'dimension' };
|
|
41
|
+
const UNITS = new Set(['px', 'rem', 'em']);
|
|
42
|
+
|
|
43
|
+
/** The features of media and container conditions and their value types. */
|
|
44
|
+
export const FEATURES = Object.freeze({
|
|
45
|
+
media: {
|
|
46
|
+
'min-width': WIDTH,
|
|
47
|
+
'max-width': WIDTH,
|
|
48
|
+
'prefers-color-scheme': { type: ['enum', 'light', 'dark'] },
|
|
49
|
+
'prefers-reduced-motion': { type: ['enum', 'reduce', 'no-preference'] },
|
|
50
|
+
},
|
|
51
|
+
container: { 'min-width': WIDTH, 'max-width': WIDTH },
|
|
52
|
+
});
|
|
53
|
+
|
|
54
|
+
/** Read one `(FEATURE VALUE)` condition, or `null` when invalid. */
|
|
55
|
+
function readCondition(kind, raw) {
|
|
56
|
+
if (
|
|
57
|
+
!Array.isArray(raw) ||
|
|
58
|
+
raw.length !== 2 ||
|
|
59
|
+
typeof raw[0] !== 'string' ||
|
|
60
|
+
!Object.hasOwn(FEATURES[kind], raw[0])
|
|
61
|
+
) {
|
|
62
|
+
return null;
|
|
63
|
+
}
|
|
64
|
+
const [feature, text] = raw;
|
|
65
|
+
const { value } = values.typeValue(text, FEATURES[kind][feature]);
|
|
66
|
+
const ok =
|
|
67
|
+
value !== null &&
|
|
68
|
+
(value.type === 'enum' ||
|
|
69
|
+
(value.type === 'dimension' &&
|
|
70
|
+
UNITS.has(value.unit) &&
|
|
71
|
+
value.value >= 0));
|
|
72
|
+
return ok ? { feature, value } : null;
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
/**
|
|
76
|
+
* Whether a raw value is a condition: any leading link whose head is not a
|
|
77
|
+
* declaration or a block (`(padding ...)`, `(state ...)`), so an unknown
|
|
78
|
+
* feature such as `(min-height 10px)` is reported, not read as a property.
|
|
79
|
+
*/
|
|
80
|
+
const looksLikeCondition = (kind, raw, isBodyItem) =>
|
|
81
|
+
Array.isArray(raw) &&
|
|
82
|
+
typeof raw[0] === 'string' &&
|
|
83
|
+
(Object.hasOwn(FEATURES[kind], raw[0]) || !isBodyItem(raw[0]));
|
|
84
|
+
|
|
85
|
+
/**
|
|
86
|
+
* Split the values of a `media` or `container` block into its leading
|
|
87
|
+
* conditions and its body.
|
|
88
|
+
* @param {'media'|'container'} kind
|
|
89
|
+
* @param {RawValue[]} raws
|
|
90
|
+
* @param {Record<string, any>} at
|
|
91
|
+
* @param {import('./types.js').Diagnostic[]} diagnostics
|
|
92
|
+
* @param {(name: string) => boolean} isBodyItem whether a head starts the body
|
|
93
|
+
* @returns {{ conditions: Condition[]|null, body: RawValue[] }}
|
|
94
|
+
*/
|
|
95
|
+
export function readConditions(kind, raws, at, diagnostics, isBodyItem) {
|
|
96
|
+
const conditions = [];
|
|
97
|
+
let i = 0;
|
|
98
|
+
for (; i < raws.length; i += 1) {
|
|
99
|
+
if (!looksLikeCondition(kind, raws[i], isBodyItem)) {
|
|
100
|
+
break;
|
|
101
|
+
}
|
|
102
|
+
const condition = readCondition(kind, raws[i]);
|
|
103
|
+
if (condition === null) {
|
|
104
|
+
diagnostics.push(
|
|
105
|
+
styleDiagnostic('invalid-condition', 'error', {
|
|
106
|
+
...at,
|
|
107
|
+
block: kind,
|
|
108
|
+
text: rawText(raws[i]),
|
|
109
|
+
})
|
|
110
|
+
);
|
|
111
|
+
return { conditions: null, body: [] };
|
|
112
|
+
}
|
|
113
|
+
conditions.push(condition);
|
|
114
|
+
}
|
|
115
|
+
if (conditions.length === 0) {
|
|
116
|
+
diagnostics.push(
|
|
117
|
+
styleDiagnostic('invalid-condition', 'error', {
|
|
118
|
+
...at,
|
|
119
|
+
block: kind,
|
|
120
|
+
text: raws.length === 0 ? '' : rawText(raws[0]),
|
|
121
|
+
})
|
|
122
|
+
);
|
|
123
|
+
return { conditions: null, body: [] };
|
|
124
|
+
}
|
|
125
|
+
return { conditions, body: raws.slice(i) };
|
|
126
|
+
}
|
|
127
|
+
|
|
128
|
+
/** The root font size that `rem` and `em` widths are resolved with. */
|
|
129
|
+
export const ROOT_FONT_SIZE = 16;
|
|
130
|
+
|
|
131
|
+
/** A width in CSS pixels. */
|
|
132
|
+
const px = (dimension) =>
|
|
133
|
+
dimension.unit === 'px' ? dimension.value : dimension.value * ROOT_FONT_SIZE;
|
|
134
|
+
|
|
135
|
+
/**
|
|
136
|
+
* Whether a condition holds in an environment. A width condition never
|
|
137
|
+
* holds when the width is unknown; the colour scheme defaults to `light`
|
|
138
|
+
* and reduced motion to `false`, as in a browser without preferences.
|
|
139
|
+
* @param {'media'|'container'} kind
|
|
140
|
+
* @param {Condition} condition
|
|
141
|
+
* @param {StyleEnv} env
|
|
142
|
+
*/
|
|
143
|
+
export function conditionHolds(kind, { feature, value }, env) {
|
|
144
|
+
switch (feature) {
|
|
145
|
+
case 'prefers-color-scheme':
|
|
146
|
+
return (env.colorScheme ?? 'light') === value.value;
|
|
147
|
+
case 'prefers-reduced-motion':
|
|
148
|
+
return (env.reducedMotion === true) === (value.value === 'reduce');
|
|
149
|
+
default: {
|
|
150
|
+
const width = (kind === 'media' ? env.viewport : env.container)?.width;
|
|
151
|
+
if (typeof width !== 'number') {
|
|
152
|
+
return false;
|
|
153
|
+
}
|
|
154
|
+
return feature === 'min-width' ? width >= px(value) : width <= px(value);
|
|
155
|
+
}
|
|
156
|
+
}
|
|
157
|
+
}
|
|
158
|
+
|
|
159
|
+
/**
|
|
160
|
+
* The CSS of the conditions of a block: `(max-width: 640px) and
|
|
161
|
+
* (prefers-color-scheme: dark)`.
|
|
162
|
+
* @param {Condition[]} conditions
|
|
163
|
+
*/
|
|
164
|
+
export const conditionsCss = (conditions) =>
|
|
165
|
+
conditions
|
|
166
|
+
.map(({ feature, value }) => `(${feature}: ${values.toCss(value)})`)
|
|
167
|
+
.join(' and ');
|