@flairstudio/kit 0.1.0 → 0.2.0-beta.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/CHANGELOG.md +8 -0
- package/README.md +156 -10
- package/TOKENS.md +10 -1
- package/dist/fonts.css +1 -1
- package/dist/preview-host.d.ts +39 -0
- package/dist/preview-host.js +94 -0
- package/dist/preview.d.ts +37 -0
- package/dist/preview.js +211 -0
- package/dist/tokens.css +2 -2
- package/dist/tokens.json +8 -2
- package/package.json +5 -2
package/CHANGELOG.md
CHANGED
|
@@ -2,6 +2,14 @@
|
|
|
2
2
|
|
|
3
3
|
Renaming or removing a token is a breaking change. Adding a token, or changing a value, is a minor release. Before 1.0, a breaking change bumps the minor version.
|
|
4
4
|
|
|
5
|
+
## 0.2.0-beta.0
|
|
6
|
+
|
|
7
|
+
A beta, published under npm's `next` tag. `latest` stays at 0.1.0 until 0.2.0.
|
|
8
|
+
|
|
9
|
+
- **Added:** `preview.js`, the listener that lets Flair Design show a site's page in its frame, wearing the theme being edited. Also `preview-host.js`, Flair's side, which a site's own tests can use too. Both ship with type declarations. See "Showing a site in Flair" in the README.
|
|
10
|
+
- **Added:** `layers` in `tokens.json`, and a "Cascade layers" section in `TOKENS.md`.
|
|
11
|
+
- **Breaking, for palettes:** the cascade layers are now `flair-kit.tokens, flair-kit.palette, flair-kit.components, flair-kit.preview`. Move the palette file's blocks inside `@layer flair-kit.palette { … }`, and open the file with the order line, so it works whatever order the CSS loads in. A palette left outside the layer still works on the site, but in Flair it beats the preview, so the page doesn't follow the theme being edited.
|
|
12
|
+
|
|
5
13
|
## 0.1.0
|
|
6
14
|
|
|
7
15
|
First release: colour, spacing, type, radius, layer, motion, size and font tokens; light and dark modes; opt-in Inter.
|
package/README.md
CHANGED
|
@@ -1,15 +1,15 @@
|
|
|
1
1
|
# flair-kit
|
|
2
2
|
|
|
3
3
|
Published on npm as `@flairstudio/kit`. Design tokens as plain CSS: colours with light and dark modes, spacing, type sizes, radius, layers, motion and fonts. No build step is needed to use them. The colour names are the ones the Flair Design theme tool uses.
|
|
4
|
-
- A Flair theme exported in the `data-theme` style
|
|
5
|
-
- Flair's `light-dark()` export
|
|
4
|
+
- A Flair theme exported in the `data-theme` style becomes a site's palette file once it sits inside the palette layer (see below). Until Flair's Export writes that wrapper, add it by hand.
|
|
5
|
+
- Flair's `light-dark()` export can't be used that way, because it can't follow `data-theme`.
|
|
6
6
|
|
|
7
7
|
Every token is listed in `TOKENS.md`, which ships in the package.
|
|
8
8
|
|
|
9
9
|
## Use it in a site with a build
|
|
10
10
|
|
|
11
11
|
```sh
|
|
12
|
-
npm install @flairstudio/kit@0.
|
|
12
|
+
npm install @flairstudio/kit@0.2.0-beta.0 --save-exact
|
|
13
13
|
```
|
|
14
14
|
|
|
15
15
|
Import in this order:
|
|
@@ -29,7 +29,7 @@ Install it as a dev dependency, copy `node_modules/@flairstudio/kit/dist/` into
|
|
|
29
29
|
<link rel="stylesheet" href="assets/flair-kit/tokens.css">
|
|
30
30
|
```
|
|
31
31
|
|
|
32
|
-
Line 1 of each
|
|
32
|
+
Line 1 of each generated file names its version, `/* flair-kit <version> · … */`, so a test can check that the copy matches the installed version.
|
|
33
33
|
|
|
34
34
|
## Light and dark
|
|
35
35
|
|
|
@@ -41,20 +41,28 @@ Line 1 of each CSS file names its version, `/* flair-kit 0.1.0 · … */`, so a
|
|
|
41
41
|
|
|
42
42
|
The package's colours are a default. A site's palette file replaces them, and it must:
|
|
43
43
|
|
|
44
|
+
- **open with the layer order line, then put everything inside `@layer flair-kit.palette { … }`.** The line fixes the order of the layers whichever CSS file the browser reads first, so the palette beats the package's defaults even if it loads before `tokens.css`. The layer lets Flair's preview beat the palette while it shows the page (see "Showing a site in Flair").
|
|
44
45
|
- **use the same three selectors as `tokens.css`, in this order.** The dark block must come after the light one, or `data-theme="dark"` on `<html>` stops working:
|
|
45
46
|
|
|
46
47
|
```css
|
|
47
|
-
|
|
48
|
-
[data-theme="light"] { color-scheme: light; --color-background: #…; /* … */ --gradient-angle: 110deg; }
|
|
48
|
+
@layer flair-kit.tokens, flair-kit.palette, flair-kit.components, flair-kit.preview;
|
|
49
49
|
|
|
50
|
-
|
|
50
|
+
@layer flair-kit.palette {
|
|
51
|
+
:root,
|
|
52
|
+
[data-theme="light"] { color-scheme: light; --color-background: #…; /* … */ --gradient-angle: 110deg; }
|
|
51
53
|
|
|
52
|
-
|
|
53
|
-
|
|
54
|
+
[data-theme="dark"] { color-scheme: dark; --color-background: #…; /* … */ }
|
|
55
|
+
|
|
56
|
+
@media (prefers-color-scheme: dark) {
|
|
57
|
+
:root:not([data-theme="light"]) { color-scheme: dark; --color-background: #…; /* … */ }
|
|
58
|
+
}
|
|
54
59
|
}
|
|
55
60
|
```
|
|
56
61
|
|
|
57
62
|
- **set every colour in both modes.** A colour the file leaves out falls back to the package's default. `tokens.json` lists them all under `colors.names`, so a site test can check its palette covers them.
|
|
63
|
+
- **set colours only on `:root` and on elements with `data-theme`.** A palette may use stronger selectors, such as `#page[data-palette][data-theme]`, as long as the element carries `data-theme`. A colour set on any other element, such as `body` or `.page`, isn't reached by Flair's preview: the element keeps the palette's value instead of inheriting Flair's.
|
|
64
|
+
|
|
65
|
+
A palette left outside the layer still works on the site, but in Flair it beats the preview, so the page doesn't change as the theme is edited.
|
|
58
66
|
|
|
59
67
|
Override a colour with those same three selectors. A colour set only on `:root` behaves differently:
|
|
60
68
|
- it applies to the page in every mode, so a value chosen for light mode also shows in dark mode;
|
|
@@ -63,7 +71,13 @@ Override a colour with those same three selectors. A colour set only on `:root`
|
|
|
63
71
|
|
|
64
72
|
Check colour values carefully. The colours are registered as `<color>`, so a misspelt value (`#1f2a3`, `banana`) doesn't fail loudly. The element quietly takes its parent's colour instead, which inside a `data-theme` region can mean a light colour in a dark region.
|
|
65
73
|
|
|
66
|
-
Every token declaration in the package sits in the `flair-kit.tokens` cascade layer. The `@property` registrations in `tokens.css` and the `@font-face` rules in `fonts.css` sit outside it by design, because neither needs overriding. A site's own CSS outside a layer always wins, whatever its selectors, so no override needs `!important
|
|
74
|
+
Every token declaration in the package sits in the `flair-kit.tokens` cascade layer. The `@property` registrations in `tokens.css` and the `@font-face` rules in `fonts.css` sit outside it by design, because neither needs overriding. A site's own CSS outside a layer always wins, whatever its selectors, so no override needs `!important`, and overrides stay outside a layer. From lowest to highest, the order is the package's defaults, the site's palette, components, Flair's preview, then the site's unlayered CSS:
|
|
75
|
+
|
|
76
|
+
```css
|
|
77
|
+
@layer flair-kit.tokens, flair-kit.palette, flair-kit.components, flair-kit.preview;
|
|
78
|
+
```
|
|
79
|
+
|
|
80
|
+
A site that uses its own layers declares them after flair-kit's.
|
|
67
81
|
|
|
68
82
|
## Breakpoints
|
|
69
83
|
|
|
@@ -73,6 +87,138 @@ Every token declaration in the package sits in the `flair-kit.tokens` cascade la
|
|
|
73
87
|
|
|
74
88
|
The package adds no transitions. `--dur-theme` is there for a site that fades between themes; under `prefers-reduced-motion: reduce` it becomes `0.01ms`, never `0s`, so code waiting on a transition's end event still hears it. A site that sets its own `--dur-theme` overrides that as well, so it should set it again inside its own `@media (prefers-reduced-motion: reduce)` block.
|
|
75
89
|
|
|
90
|
+
## Showing a site in Flair
|
|
91
|
+
|
|
92
|
+
Flair Design can show a site's real page in a frame, wearing the theme being edited, and follow every change live. The page needs the listener, `preview.js`, from this package. It loads only inside a frame, so visitors never download it.
|
|
93
|
+
|
|
94
|
+
### Load the listener
|
|
95
|
+
|
|
96
|
+
A site with a build, in its entry file:
|
|
97
|
+
|
|
98
|
+
```js
|
|
99
|
+
if (window.self !== window.top) import('@flairstudio/kit/preview.js').then((m) => m.listen());
|
|
100
|
+
```
|
|
101
|
+
|
|
102
|
+
The bundler puts the import in a file of its own, which loads only when the page is framed.
|
|
103
|
+
|
|
104
|
+
A site without a build, with `dist/` copied to `assets/flair-kit/`, after its stylesheet links:
|
|
105
|
+
|
|
106
|
+
```html
|
|
107
|
+
<script type="module">
|
|
108
|
+
if (window.self !== window.top) import('./assets/flair-kit/preview.js').then((m) => m.listen());
|
|
109
|
+
</script>
|
|
110
|
+
```
|
|
111
|
+
|
|
112
|
+
Outside a frame, `listen()` does nothing. Calling it again while it runs changes nothing, and it returns a function that stops it.
|
|
113
|
+
|
|
114
|
+
### Which Flair may theme the page
|
|
115
|
+
|
|
116
|
+
`listen()` takes themes only from Flair's own local addresses, `http://localhost:3000` (its dev server) and `http://localhost:4317` (its tests), which it exports as `DEFAULT_ALLOW`. Pass `allow` to replace the list, or spread the default to add to it:
|
|
117
|
+
|
|
118
|
+
```js
|
|
119
|
+
import('@flairstudio/kit/preview.js').then(({ listen, DEFAULT_ALLOW }) =>
|
|
120
|
+
listen({ allow: [...DEFAULT_ALLOW, 'https://flair.example'] }));
|
|
121
|
+
```
|
|
122
|
+
|
|
123
|
+
Addresses must match exactly: `http://127.0.0.1:3000` isn't `http://localhost:3000`. A message from any other address, or from a window that isn't the page's parent, is ignored without a word. Only colour names the package knows and `#rrggbb` values get through, so a message can't add any other CSS.
|
|
124
|
+
|
|
125
|
+
### What the listener changes
|
|
126
|
+
|
|
127
|
+
Only these, and only once Flair has sent a theme:
|
|
128
|
+
- its own `<style>`, in the `flair-kit.preview` layer, which beats the site's palette but not its unlayered overrides;
|
|
129
|
+
- `data-theme` on `<html>`, set to the mode chosen in Flair;
|
|
130
|
+
- `data-flair-preview` on `<html>`, set with the first theme;
|
|
131
|
+
- an inline `--dur-theme` on `<html>`: `0.01ms` for the first theme and while a colour is being dragged, so nothing fades.
|
|
132
|
+
|
|
133
|
+
The preview beats the palette only when the palette sits in its layer (see "A site's palette and overrides"). If a router or hydration rebuilds `<head>`, the listener puts its `<style>` back with the next theme. A Content Security Policy whose `style-src` doesn't allow inline styles blocks that `<style>`, so the page keeps its own colours in Flair. Allow inline styles on the pages Flair frames.
|
|
134
|
+
|
|
135
|
+
### Review tools step aside
|
|
136
|
+
|
|
137
|
+
A site's own review tools, such as a theme switcher, notes or a palette menu, would fight Flair's. Hide them under the attribute:
|
|
138
|
+
|
|
139
|
+
```css
|
|
140
|
+
[data-flair-preview] .review-tools { display: none; }
|
|
141
|
+
```
|
|
142
|
+
|
|
143
|
+
A script that needs to react listens for the event, fired on `document` each time a theme goes on:
|
|
144
|
+
|
|
145
|
+
```js
|
|
146
|
+
document.addEventListener('flair-kit:preview', (event) => {
|
|
147
|
+
const { mode, first } = event.detail; // 'light' or 'dark'; true for the first theme
|
|
148
|
+
});
|
|
149
|
+
```
|
|
150
|
+
|
|
151
|
+
Flair shows the page once the listeners have run and two frames have been drawn, so a change made in the handler is in place by then.
|
|
152
|
+
|
|
153
|
+
### Two rules
|
|
154
|
+
|
|
155
|
+
- **Flair sets the mode on `<html>`, and an element with its own `data-theme` keeps it.** So a page root that sets its own `data-theme`, such as `#page`, copies `event.detail.mode` into it on every event. Regions that always stay one mode keep theirs.
|
|
156
|
+
- **Set `--dur-theme` on `:root` only.** The listener's inline value on `<html>` beats it there. A value set on another element would bring the fade back during a drag.
|
|
157
|
+
|
|
158
|
+
### Flair's side: `preview-host.js`
|
|
159
|
+
|
|
160
|
+
Flair sends themes with `preview-host.js`, and a site's tests can too:
|
|
161
|
+
|
|
162
|
+
```js
|
|
163
|
+
import { connect } from '@flairstudio/kit/preview-host.js';
|
|
164
|
+
|
|
165
|
+
const { send, close } = connect(iframe, {
|
|
166
|
+
origin: 'http://localhost:5173', // the site's address
|
|
167
|
+
onReady: ({ kit, names }) => {}, // the frame's package version and colour names
|
|
168
|
+
onApplied: () => {}, // the theme is on: the frame is ready to be seen
|
|
169
|
+
onRefused: (reason) => {}, // a short code, such as 'value'
|
|
170
|
+
onMismatch: ({ host, frame }) => {}, // the frame speaks another format version
|
|
171
|
+
});
|
|
172
|
+
send({ light, dark, mode: 'light', angle: 110, instant: false });
|
|
173
|
+
```
|
|
174
|
+
|
|
175
|
+
`light` and `dark` map colour names, without the `--color-` prefix, to `#rrggbb` values. `send` throws a `TypeError` on a theme the frame would refuse. It keeps the latest theme and sends it whenever the frame says it's ready: at once, and again after a reload or a link inside the frame. It sends only the colour names the frame knows. `close()` stops listening.
|
|
176
|
+
|
|
177
|
+
### Testing a site's pages with a Flair theme
|
|
178
|
+
|
|
179
|
+
A site's Playwright tests can put a theme on its pages without Flair running. The test answers an allowed address itself with `page.route`, serving a small host page and the two scripts. The site's own server serves the page under test.
|
|
180
|
+
|
|
181
|
+
```js
|
|
182
|
+
import { test, expect } from '@playwright/test';
|
|
183
|
+
import { createRequire } from 'node:module';
|
|
184
|
+
import { dirname, join } from 'node:path';
|
|
185
|
+
|
|
186
|
+
const FLAIR = 'http://localhost:4317'; // an allowed address; nothing needs to run there
|
|
187
|
+
const SITE = 'http://localhost:5173/'; // the page under test
|
|
188
|
+
const require = createRequire(import.meta.url);
|
|
189
|
+
const dist = dirname(require.resolve('@flairstudio/kit/preview-host.js'));
|
|
190
|
+
// The package's default palette; use a Flair export's values to test a real theme
|
|
191
|
+
const { light, dark } = require('@flairstudio/kit/tokens.json').colors;
|
|
192
|
+
|
|
193
|
+
const HOST_PAGE = `<!doctype html>
|
|
194
|
+
<iframe src="${SITE}" title="Site" style="width: 1480px; height: 900px; border: 0"></iframe>
|
|
195
|
+
<script type="module">
|
|
196
|
+
import { connect } from './preview-host.js';
|
|
197
|
+
const frame = document.querySelector('iframe');
|
|
198
|
+
window.flairKit = { applied: 0, refused: [] };
|
|
199
|
+
window.flairKit.host = connect(frame, {
|
|
200
|
+
origin: new URL(frame.src).origin,
|
|
201
|
+
onApplied: () => { window.flairKit.applied++; },
|
|
202
|
+
onRefused: (reason) => { window.flairKit.refused.push(reason); },
|
|
203
|
+
});
|
|
204
|
+
</script>`;
|
|
205
|
+
|
|
206
|
+
test('the home page wears a Flair theme', async ({ page }) => {
|
|
207
|
+
await page.route(`${FLAIR}/**`, (route) => {
|
|
208
|
+
const { pathname } = new URL(route.request().url());
|
|
209
|
+
if (pathname === '/') return route.fulfill({ contentType: 'text/html', body: HOST_PAGE });
|
|
210
|
+
return route.fulfill({ path: join(dist, pathname.slice(1)) }); // preview-host.js, and the preview.js it imports
|
|
211
|
+
});
|
|
212
|
+
await page.goto(`${FLAIR}/`);
|
|
213
|
+
await page.evaluate((theme) => window.flairKit.host.send(theme), { light, dark, mode: 'dark', angle: 110 });
|
|
214
|
+
await expect.poll(() => page.evaluate(() => window.flairKit.applied)).toBeGreaterThan(0);
|
|
215
|
+
const site = page.frameLocator('iframe');
|
|
216
|
+
// check the site's page through `site`
|
|
217
|
+
});
|
|
218
|
+
```
|
|
219
|
+
|
|
220
|
+
In a TypeScript test, `window.flairKit` needs a cast inside `page.evaluate`, such as `(window as any).flairKit`.
|
|
221
|
+
|
|
76
222
|
## Versions
|
|
77
223
|
|
|
78
224
|
Pin an exact version, and read `CHANGELOG.md` (it ships in the package) before upgrading. Renaming or removing a token is a breaking change.
|
package/TOKENS.md
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
# flair-kit tokens
|
|
2
2
|
|
|
3
|
-
Version 0.
|
|
3
|
+
Version 0.2.0-beta.0. Colours change with the mode; everything else stays the same in light and dark.
|
|
4
4
|
|
|
5
5
|
## Colours
|
|
6
6
|
|
|
@@ -183,4 +183,13 @@ Under `prefers-reduced-motion: reduce`:
|
|
|
183
183
|
|
|
184
184
|
- `--dur-theme` becomes `0.01ms`
|
|
185
185
|
|
|
186
|
+
## Cascade layers
|
|
187
|
+
|
|
188
|
+
From lowest to highest, then the site's own unlayered CSS, which beats them all:
|
|
189
|
+
|
|
190
|
+
- `flair-kit.tokens`: the package's defaults, which are every token in this file
|
|
191
|
+
- `flair-kit.palette`: the site's palette file
|
|
192
|
+
- `flair-kit.components`: components
|
|
193
|
+
- `flair-kit.preview`: the theme Flair sends while it shows the page in its frame
|
|
194
|
+
|
|
186
195
|
Generated from src/tokens.ts by npm run build.
|
package/dist/fonts.css
CHANGED
|
@@ -0,0 +1,39 @@
|
|
|
1
|
+
/* flair-kit 0.2.0-beta.0 · generated from src/tokens.ts · do not edit */
|
|
2
|
+
|
|
3
|
+
import type { Colors, Mode } from './preview.js';
|
|
4
|
+
|
|
5
|
+
export { COLOR_NAMES, FORMAT_VERSION, KIT_VERSION } from './preview.js';
|
|
6
|
+
export type { ColorName, Colors, Mode, Reason } from './preview.js';
|
|
7
|
+
|
|
8
|
+
/** A theme as Flair sends it: both modes' colours, the canvas mode, the gradient angle, and whether a drag is under way */
|
|
9
|
+
export interface Theme {
|
|
10
|
+
readonly light: Colors;
|
|
11
|
+
readonly dark: Colors;
|
|
12
|
+
readonly mode: Mode;
|
|
13
|
+
/** Degrees, from 0 to 360 */
|
|
14
|
+
readonly angle: number;
|
|
15
|
+
/** True while a drag or typing is under way, so the page doesn't fade */
|
|
16
|
+
readonly instant?: boolean;
|
|
17
|
+
}
|
|
18
|
+
/** What the frame said when it became ready */
|
|
19
|
+
export interface ReadyInfo { readonly kit: string; readonly names: readonly string[] }
|
|
20
|
+
export interface ConnectOptions {
|
|
21
|
+
/** The site's origin, or any address on it */
|
|
22
|
+
readonly origin: string;
|
|
23
|
+
/** The frame is listening: its package version and the colour names it accepts */
|
|
24
|
+
readonly onReady?: (info: ReadyInfo) => void;
|
|
25
|
+
/** The frame has put the theme on and is ready to be seen */
|
|
26
|
+
readonly onApplied?: () => void;
|
|
27
|
+
/** The frame refused the theme, or shares no colour names with it ("name") */
|
|
28
|
+
readonly onRefused?: (reason: string) => void;
|
|
29
|
+
/** The frame speaks another format version, so nothing is sent */
|
|
30
|
+
readonly onMismatch?: (versions: { readonly host: number; readonly frame: unknown }) => void;
|
|
31
|
+
}
|
|
32
|
+
export interface Connection {
|
|
33
|
+
/** Checks the theme, keeps it, and sends it now and whenever the frame says ready again. Throws a TypeError on a theme the frame would refuse */
|
|
34
|
+
send(theme: Theme): void;
|
|
35
|
+
/** Stops listening to the frame */
|
|
36
|
+
close(): void;
|
|
37
|
+
}
|
|
38
|
+
/** Connects to the page in an <iframe> of a site */
|
|
39
|
+
export declare function connect(frame: HTMLIFrameElement, options: ConnectOptions): Connection;
|
|
@@ -0,0 +1,94 @@
|
|
|
1
|
+
/* flair-kit 0.2.0-beta.0 · generated from src/preview-host.js · do not edit */
|
|
2
|
+
|
|
3
|
+
// Flair's side of the preview: frames a site's page and sends it the theme being edited. A site's own tests use
|
|
4
|
+
// it too, to put a Flair theme on their pages without Flair running. The names and the checks come from
|
|
5
|
+
// ./preview.js, so both sides of the format are written once.
|
|
6
|
+
import { COLOR_NAMES, FORMAT_VERSION, KIT_VERSION, MESSAGE_TYPES, checkTheme } from './preview.js';
|
|
7
|
+
|
|
8
|
+
export { COLOR_NAMES, FORMAT_VERSION, KIT_VERSION };
|
|
9
|
+
|
|
10
|
+
/** An address as an origin. Only an http or https page can be framed and posted to */
|
|
11
|
+
function toOrigin(address) {
|
|
12
|
+
let url = null;
|
|
13
|
+
try {
|
|
14
|
+
url = new URL(address);
|
|
15
|
+
} catch {
|
|
16
|
+
// not a URL at all
|
|
17
|
+
}
|
|
18
|
+
if (!url || (url.protocol !== 'http:' && url.protocol !== 'https:')) {
|
|
19
|
+
throw new TypeError(`flair-kit: connect() needs an http or https origin, not ${JSON.stringify(String(address))}`);
|
|
20
|
+
}
|
|
21
|
+
return url.origin;
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
/**
|
|
25
|
+
* Connects to the page in `frame`, an <iframe> of a site at `options.origin`. Returns { send, close }.
|
|
26
|
+
* send(theme) keeps the latest theme and sends it whenever the frame says ready: now, after a reload, after a link.
|
|
27
|
+
*/
|
|
28
|
+
export function connect(frame, options) {
|
|
29
|
+
const origin = toOrigin(options?.origin);
|
|
30
|
+
const { onReady, onApplied, onRefused, onMismatch } = options;
|
|
31
|
+
const win = frame.ownerDocument.defaultView;
|
|
32
|
+
|
|
33
|
+
let names = null;
|
|
34
|
+
let latest = null;
|
|
35
|
+
let closed = false;
|
|
36
|
+
|
|
37
|
+
const toFrame = (message) => frame.contentWindow?.postMessage(message, origin);
|
|
38
|
+
|
|
39
|
+
const sendLatest = () => {
|
|
40
|
+
if (!latest || !names) return;
|
|
41
|
+
const shared = names.filter((name) => Object.hasOwn(latest.light, name));
|
|
42
|
+
if (shared.length === 0) {
|
|
43
|
+
onRefused?.('name');
|
|
44
|
+
return;
|
|
45
|
+
}
|
|
46
|
+
const pick = (values) => Object.fromEntries(shared.map((name) => [name, values[name]]));
|
|
47
|
+
toFrame({
|
|
48
|
+
type: MESSAGE_TYPES.theme,
|
|
49
|
+
v: FORMAT_VERSION,
|
|
50
|
+
light: pick(latest.light),
|
|
51
|
+
dark: pick(latest.dark),
|
|
52
|
+
mode: latest.mode,
|
|
53
|
+
angle: latest.angle,
|
|
54
|
+
instant: latest.instant === true,
|
|
55
|
+
});
|
|
56
|
+
};
|
|
57
|
+
|
|
58
|
+
const onMessage = (event) => {
|
|
59
|
+
if (event.origin !== origin || !event.source || event.source !== frame.contentWindow) return;
|
|
60
|
+
const { data } = event;
|
|
61
|
+
if (data === null || typeof data !== 'object') return;
|
|
62
|
+
if (data.type === MESSAGE_TYPES.ready) {
|
|
63
|
+
if (data.v !== FORMAT_VERSION) {
|
|
64
|
+
names = null;
|
|
65
|
+
onMismatch?.({ host: FORMAT_VERSION, frame: data.v });
|
|
66
|
+
return;
|
|
67
|
+
}
|
|
68
|
+
names = Array.isArray(data.names) ? data.names.filter((name) => typeof name === 'string') : [];
|
|
69
|
+
onReady?.({ kit: String(data.kit), names: [...names] });
|
|
70
|
+
sendLatest();
|
|
71
|
+
} else if (data.type === MESSAGE_TYPES.applied) {
|
|
72
|
+
onApplied?.();
|
|
73
|
+
} else if (data.type === MESSAGE_TYPES.refused) {
|
|
74
|
+
onRefused?.(String(data.reason));
|
|
75
|
+
}
|
|
76
|
+
};
|
|
77
|
+
|
|
78
|
+
win.addEventListener('message', onMessage);
|
|
79
|
+
toFrame({ type: MESSAGE_TYPES.hello, v: FORMAT_VERSION });
|
|
80
|
+
|
|
81
|
+
return {
|
|
82
|
+
send(theme) {
|
|
83
|
+
const reason = checkTheme(theme, COLOR_NAMES);
|
|
84
|
+
if (reason) throw new TypeError(`flair-kit: send() was given a theme the frame would refuse (${reason})`);
|
|
85
|
+
if (closed) return;
|
|
86
|
+
latest = { light: { ...theme.light }, dark: { ...theme.dark }, mode: theme.mode, angle: theme.angle, instant: theme.instant };
|
|
87
|
+
sendLatest();
|
|
88
|
+
},
|
|
89
|
+
close() {
|
|
90
|
+
closed = true;
|
|
91
|
+
win.removeEventListener('message', onMessage);
|
|
92
|
+
},
|
|
93
|
+
};
|
|
94
|
+
}
|
|
@@ -0,0 +1,37 @@
|
|
|
1
|
+
/* flair-kit 0.2.0-beta.0 · generated from src/tokens.ts · do not edit */
|
|
2
|
+
|
|
3
|
+
/** The package version this listener came from */
|
|
4
|
+
export declare const KIT_VERSION: string;
|
|
5
|
+
/** The message format's version. It goes up only when a field changes meaning or a new field becomes required */
|
|
6
|
+
export declare const FORMAT_VERSION: 1;
|
|
7
|
+
/** The colour names this listener accepts, without the --color- prefix, in the package's order */
|
|
8
|
+
export declare const COLOR_NAMES: readonly ["background","card","muted","secondary","foreground","card-foreground","muted-foreground","on-secondary","border","outline","primary","on-primary","primary-hover","primary-pressed","primary-text","ring","tertiary","on-tertiary","accent-2","glow","destructive","on-destructive","chart-1","chart-2","chart-3","chart-4","brand","gradient-1","gradient-2","gradient-3"];
|
|
9
|
+
/** A colour name, without the --color- prefix */
|
|
10
|
+
export type ColorName = (typeof COLOR_NAMES)[number];
|
|
11
|
+
/** A colour mode */
|
|
12
|
+
export type Mode = 'light' | 'dark';
|
|
13
|
+
/** The Flair addresses listen() allows when it's given no list: Flair's dev server and its tests */
|
|
14
|
+
export declare const DEFAULT_ALLOW: readonly ["http://localhost:3000","http://localhost:4317"];
|
|
15
|
+
/** The type of each message in the format */
|
|
16
|
+
export declare const MESSAGE_TYPES: { readonly ready: "flair-kit:ready"; readonly hello: "flair-kit:hello"; readonly theme: "flair-kit:theme"; readonly applied: "flair-kit:applied"; readonly refused: "flair-kit:refused"; };
|
|
17
|
+
/** Why a theme was refused. A refusal never echoes the values it was sent */
|
|
18
|
+
export type Reason = "type" | "version" | "mode" | "angle" | "name" | "value";
|
|
19
|
+
/** Colours by name, each a #rrggbb value */
|
|
20
|
+
export type Colors = { readonly [N in ColorName]?: string };
|
|
21
|
+
/** The detail of the flair-kit:preview event, fired on document each time a theme goes on */
|
|
22
|
+
export interface PreviewEventDetail { readonly mode: Mode; readonly first: boolean }
|
|
23
|
+
/** Options for listen() */
|
|
24
|
+
export interface ListenOptions {
|
|
25
|
+
/** The Flair addresses that may theme the page, as origins or page addresses. It replaces DEFAULT_ALLOW */
|
|
26
|
+
readonly allow?: readonly string[];
|
|
27
|
+
}
|
|
28
|
+
/** Starts listening for Flair if the page is in a frame, and returns a function that stops it. A second call while it runs returns the same function */
|
|
29
|
+
export declare function listen(options?: ListenOptions): () => void;
|
|
30
|
+
/** Checks a theme's mode, angle and colours against `names`: the first reason it fails, or null. Never throws */
|
|
31
|
+
export declare function checkTheme(theme: unknown, names: readonly string[]): Reason | null;
|
|
32
|
+
|
|
33
|
+
declare global {
|
|
34
|
+
interface DocumentEventMap {
|
|
35
|
+
'flair-kit:preview': CustomEvent<PreviewEventDetail>;
|
|
36
|
+
}
|
|
37
|
+
}
|
package/dist/preview.js
ADDED
|
@@ -0,0 +1,211 @@
|
|
|
1
|
+
/* flair-kit 0.2.0-beta.0 · generated from src/preview.js and src/tokens.ts · do not edit */
|
|
2
|
+
|
|
3
|
+
// Written by the build from src/tokens.ts and src/preview-format.ts
|
|
4
|
+
export const KIT_VERSION = "0.2.0-beta.0";
|
|
5
|
+
export const FORMAT_VERSION = 1;
|
|
6
|
+
export const COLOR_NAMES = Object.freeze(["background","card","muted","secondary","foreground","card-foreground","muted-foreground","on-secondary","border","outline","primary","on-primary","primary-hover","primary-pressed","primary-text","ring","tertiary","on-tertiary","accent-2","glow","destructive","on-destructive","chart-1","chart-2","chart-3","chart-4","brand","gradient-1","gradient-2","gradient-3"]);
|
|
7
|
+
export const DEFAULT_ALLOW = Object.freeze(["http://localhost:3000","http://localhost:4317"]);
|
|
8
|
+
export const MESSAGE_TYPES = Object.freeze({"ready":"flair-kit:ready","hello":"flair-kit:hello","theme":"flair-kit:theme","applied":"flair-kit:applied","refused":"flair-kit:refused"});
|
|
9
|
+
const LAYER_ORDER = "@layer flair-kit.tokens, flair-kit.palette, flair-kit.components, flair-kit.preview;";
|
|
10
|
+
const PREVIEW_LAYER = "flair-kit.preview";
|
|
11
|
+
|
|
12
|
+
// The listener that lets Flair show this page in its frame, wearing the theme being edited.
|
|
13
|
+
// KIT_VERSION, FORMAT_VERSION, COLOR_NAMES, DEFAULT_ALLOW, MESSAGE_TYPES, LAYER_ORDER and PREVIEW_LAYER are
|
|
14
|
+
// written above this file by the build (scripts/lib/preview.ts). Nothing here runs until listen() is called.
|
|
15
|
+
|
|
16
|
+
const HEX = /^#[0-9a-f]{6}$/i;
|
|
17
|
+
const MODES = ['light', 'dark'];
|
|
18
|
+
const EVENT = 'flair-kit:preview';
|
|
19
|
+
const ATTRIBUTE = 'data-flair-preview';
|
|
20
|
+
/** Never 0s, so code waiting on a transition's end event still hears it */
|
|
21
|
+
const HOLD = '0.01ms';
|
|
22
|
+
/** applied waits for two animation frames; this stands behind them in case the browser holds frames back */
|
|
23
|
+
const APPLIED_FALLBACK_MS = 500;
|
|
24
|
+
|
|
25
|
+
/** The running listener for each window, so a page never has two */
|
|
26
|
+
const running = new WeakMap();
|
|
27
|
+
/** The listener's one <style> in each document, kept across a stop() and a fresh listen() */
|
|
28
|
+
const sheets = new WeakMap();
|
|
29
|
+
|
|
30
|
+
/** The preview stylesheet, built only from checked names and values, in the frame's colour order */
|
|
31
|
+
function stylesheet({ light, dark, angle }) {
|
|
32
|
+
const names = COLOR_NAMES.filter((name) => Object.hasOwn(light, name));
|
|
33
|
+
const colours = (values, pad) => names.map((name) => `${pad}--color-${name}: ${values[name]};`);
|
|
34
|
+
return [
|
|
35
|
+
LAYER_ORDER,
|
|
36
|
+
`@layer ${PREVIEW_LAYER} {`,
|
|
37
|
+
' :root,',
|
|
38
|
+
' [data-theme="light"] {',
|
|
39
|
+
' color-scheme: light;',
|
|
40
|
+
...colours(light, ' '),
|
|
41
|
+
` --gradient-angle: ${angle}deg;`,
|
|
42
|
+
' }',
|
|
43
|
+
' [data-theme="dark"] {',
|
|
44
|
+
' color-scheme: dark;',
|
|
45
|
+
...colours(dark, ' '),
|
|
46
|
+
' }',
|
|
47
|
+
' @media (prefers-color-scheme: dark) {',
|
|
48
|
+
' :root:not([data-theme="light"]) {',
|
|
49
|
+
' color-scheme: dark;',
|
|
50
|
+
...colours(dark, ' '),
|
|
51
|
+
' }',
|
|
52
|
+
' }',
|
|
53
|
+
'}',
|
|
54
|
+
'',
|
|
55
|
+
].join('\n');
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
/** An address as an origin. Only an http or https page can frame this one and post to it */
|
|
59
|
+
function toOrigin(address) {
|
|
60
|
+
let url = null;
|
|
61
|
+
try {
|
|
62
|
+
url = new URL(address);
|
|
63
|
+
} catch {
|
|
64
|
+
// not a URL at all
|
|
65
|
+
}
|
|
66
|
+
if (!url || (url.protocol !== 'http:' && url.protocol !== 'https:')) {
|
|
67
|
+
throw new TypeError(`flair-kit: listen() allows only http and https addresses, not ${JSON.stringify(String(address))}`);
|
|
68
|
+
}
|
|
69
|
+
return url.origin;
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
const isColors = (value) => value !== null && typeof value === 'object' && !Array.isArray(value);
|
|
73
|
+
|
|
74
|
+
/**
|
|
75
|
+
* Checks a theme's mode, angle and colours: the first reason it fails, or null. Never throws.
|
|
76
|
+
* `names` are the colour names that may appear, without the --color- prefix.
|
|
77
|
+
*/
|
|
78
|
+
export function checkTheme(theme, names) {
|
|
79
|
+
if (theme === null || typeof theme !== 'object' || !MODES.includes(theme.mode)) return 'mode';
|
|
80
|
+
const { angle, light, dark } = theme;
|
|
81
|
+
if (typeof angle !== 'number' || !Number.isFinite(angle) || angle < 0 || angle > 360) return 'angle';
|
|
82
|
+
if (!isColors(light) || !isColors(dark)) return 'name';
|
|
83
|
+
const keys = Object.keys(light);
|
|
84
|
+
if (keys.length === 0 || keys.length !== Object.keys(dark).length || !keys.every((key) => Object.hasOwn(dark, key))) return 'name';
|
|
85
|
+
const known = new Set(names);
|
|
86
|
+
if (!keys.every((key) => known.has(key))) return 'name';
|
|
87
|
+
const isHex = (value) => typeof value === 'string' && HEX.test(value);
|
|
88
|
+
if (!keys.every((key) => isHex(light[key]) && isHex(dark[key]))) return 'value';
|
|
89
|
+
return null;
|
|
90
|
+
}
|
|
91
|
+
|
|
92
|
+
/** Why a message from the allowed parent can't be used, or null */
|
|
93
|
+
function messageReason(data) {
|
|
94
|
+
if (data === null || typeof data !== 'object') return 'type';
|
|
95
|
+
if (data.type !== MESSAGE_TYPES.theme && data.type !== MESSAGE_TYPES.hello) return 'type';
|
|
96
|
+
if (data.v !== FORMAT_VERSION) return 'version';
|
|
97
|
+
return data.type === MESSAGE_TYPES.theme ? checkTheme(data, COLOR_NAMES) : null;
|
|
98
|
+
}
|
|
99
|
+
|
|
100
|
+
const readyMessage = () => ({ type: MESSAGE_TYPES.ready, v: FORMAT_VERSION, kit: KIT_VERSION, names: [...COLOR_NAMES] });
|
|
101
|
+
|
|
102
|
+
/**
|
|
103
|
+
* Starts listening for Flair, if this page is inside a frame. Returns a function that stops it.
|
|
104
|
+
* `allow` lists the Flair addresses that may theme the page; without it, Flair's own local addresses.
|
|
105
|
+
* Calling it again while it runs returns the same stop function and changes nothing.
|
|
106
|
+
*/
|
|
107
|
+
export function listen(options = {}) {
|
|
108
|
+
const win = globalThis.window;
|
|
109
|
+
if (!win || win.self === win.top) return () => {};
|
|
110
|
+
const current = running.get(win);
|
|
111
|
+
if (current) return current.stop;
|
|
112
|
+
|
|
113
|
+
const allow = [...(options.allow ?? DEFAULT_ALLOW)].map(toOrigin);
|
|
114
|
+
const post = (message, origin) => win.parent.postMessage(message, origin);
|
|
115
|
+
|
|
116
|
+
let first = true;
|
|
117
|
+
/** True once the first theme has been drawn and its applied has gone. Until then every theme keeps the hold */
|
|
118
|
+
let firstDrawn = false;
|
|
119
|
+
let pending = null;
|
|
120
|
+
let pendingFrame = 0;
|
|
121
|
+
const waits = new Set();
|
|
122
|
+
|
|
123
|
+
/** Runs `done` once, after two animation frames or the fallback timer, whichever comes first */
|
|
124
|
+
const afterTwoFrames = (done) => {
|
|
125
|
+
const wait = { frame: 0, timer: 0 };
|
|
126
|
+
const finish = () => {
|
|
127
|
+
if (!waits.delete(wait)) return;
|
|
128
|
+
win.cancelAnimationFrame(wait.frame);
|
|
129
|
+
win.clearTimeout(wait.timer);
|
|
130
|
+
done();
|
|
131
|
+
};
|
|
132
|
+
waits.add(wait);
|
|
133
|
+
wait.frame = win.requestAnimationFrame(() => {
|
|
134
|
+
wait.frame = win.requestAnimationFrame(finish);
|
|
135
|
+
});
|
|
136
|
+
wait.timer = win.setTimeout(finish, APPLIED_FALLBACK_MS);
|
|
137
|
+
};
|
|
138
|
+
|
|
139
|
+
const apply = (theme, origin) => {
|
|
140
|
+
const doc = win.document;
|
|
141
|
+
const root = doc.documentElement;
|
|
142
|
+
let sheet = sheets.get(doc);
|
|
143
|
+
if (!sheet) {
|
|
144
|
+
sheet = doc.createElement('style');
|
|
145
|
+
sheets.set(doc, sheet);
|
|
146
|
+
}
|
|
147
|
+
// A router or hydration can rebuild <head>; put the sheet back rather than theme a detached element
|
|
148
|
+
if (!sheet.isConnected) (doc.head ?? root).append(sheet);
|
|
149
|
+
const isFirst = first;
|
|
150
|
+
first = false;
|
|
151
|
+
sheet.textContent = stylesheet(theme);
|
|
152
|
+
root.setAttribute('data-theme', theme.mode);
|
|
153
|
+
// A theme that lands before the first has been drawn keeps the hold too, or the first would fade after all
|
|
154
|
+
if (!firstDrawn || theme.instant === true) root.style.setProperty('--dur-theme', HOLD);
|
|
155
|
+
else root.style.removeProperty('--dur-theme');
|
|
156
|
+
if (isFirst) root.setAttribute(ATTRIBUTE, '');
|
|
157
|
+
doc.dispatchEvent(new win.CustomEvent(EVENT, { detail: { mode: theme.mode, first: isFirst } }));
|
|
158
|
+
afterTwoFrames(() => {
|
|
159
|
+
if (isFirst) firstDrawn = true;
|
|
160
|
+
post({ type: MESSAGE_TYPES.applied, v: FORMAT_VERSION }, origin);
|
|
161
|
+
});
|
|
162
|
+
};
|
|
163
|
+
|
|
164
|
+
/** The first theme goes on at once; later ones are coalesced, the latest once per animation frame */
|
|
165
|
+
const receiveTheme = (theme, origin) => {
|
|
166
|
+
if (first) {
|
|
167
|
+
apply(theme, origin);
|
|
168
|
+
return;
|
|
169
|
+
}
|
|
170
|
+
pending = { theme, origin };
|
|
171
|
+
if (pendingFrame) return;
|
|
172
|
+
pendingFrame = win.requestAnimationFrame(() => {
|
|
173
|
+
pendingFrame = 0;
|
|
174
|
+
const next = pending;
|
|
175
|
+
pending = null;
|
|
176
|
+
if (next) apply(next.theme, next.origin);
|
|
177
|
+
});
|
|
178
|
+
};
|
|
179
|
+
|
|
180
|
+
const onMessage = (event) => {
|
|
181
|
+
if (!allow.includes(event.origin)) return;
|
|
182
|
+
if (event.source !== win.parent) return;
|
|
183
|
+
const { data } = event;
|
|
184
|
+
const reason = messageReason(data);
|
|
185
|
+
if (reason) {
|
|
186
|
+
console.warn(`flair-kit: a preview message from Flair was refused (${reason})`);
|
|
187
|
+
post({ type: MESSAGE_TYPES.refused, v: FORMAT_VERSION, reason }, event.origin);
|
|
188
|
+
return;
|
|
189
|
+
}
|
|
190
|
+
if (data.type === MESSAGE_TYPES.hello) post(readyMessage(), event.origin);
|
|
191
|
+
else receiveTheme(data, event.origin);
|
|
192
|
+
};
|
|
193
|
+
|
|
194
|
+
const stop = () => {
|
|
195
|
+
win.removeEventListener('message', onMessage);
|
|
196
|
+
if (pendingFrame) win.cancelAnimationFrame(pendingFrame);
|
|
197
|
+
pendingFrame = 0;
|
|
198
|
+
pending = null;
|
|
199
|
+
for (const wait of waits) {
|
|
200
|
+
win.cancelAnimationFrame(wait.frame);
|
|
201
|
+
win.clearTimeout(wait.timer);
|
|
202
|
+
}
|
|
203
|
+
waits.clear();
|
|
204
|
+
if (running.get(win)?.stop === stop) running.delete(win);
|
|
205
|
+
};
|
|
206
|
+
|
|
207
|
+
running.set(win, { stop });
|
|
208
|
+
win.addEventListener('message', onMessage);
|
|
209
|
+
for (const origin of allow) post(readyMessage(), origin);
|
|
210
|
+
return stop;
|
|
211
|
+
}
|
package/dist/tokens.css
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
|
-
/* flair-kit 0.
|
|
1
|
+
/* flair-kit 0.2.0-beta.0 · generated from src/tokens.ts · do not edit */
|
|
2
2
|
|
|
3
|
-
@layer flair-kit.tokens, flair-kit.components;
|
|
3
|
+
@layer flair-kit.tokens, flair-kit.palette, flair-kit.components, flair-kit.preview;
|
|
4
4
|
|
|
5
5
|
/* Registered so colours can fade between themes; inherits: true so a data-theme region gets its own values */
|
|
6
6
|
@property --color-background { syntax: '<color>'; inherits: true; initial-value: #f6faf9; }
|
package/dist/tokens.json
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
{
|
|
2
|
-
"version": "0.
|
|
2
|
+
"version": "0.2.0-beta.0",
|
|
3
3
|
"colors": {
|
|
4
4
|
"names": [
|
|
5
5
|
"background",
|
|
@@ -592,5 +592,11 @@
|
|
|
592
592
|
},
|
|
593
593
|
"reducedMotion": {
|
|
594
594
|
"--dur-theme": "0.01ms"
|
|
595
|
-
}
|
|
595
|
+
},
|
|
596
|
+
"layers": [
|
|
597
|
+
"flair-kit.tokens",
|
|
598
|
+
"flair-kit.palette",
|
|
599
|
+
"flair-kit.components",
|
|
600
|
+
"flair-kit.preview"
|
|
601
|
+
]
|
|
596
602
|
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@flairstudio/kit",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.2.0-beta.0",
|
|
4
4
|
"description": "Design tokens as plain CSS: colours with light and dark modes, spacing, type, layers and motion.",
|
|
5
5
|
"license": "MIT",
|
|
6
6
|
"author": "FlairStudio",
|
|
@@ -10,11 +10,14 @@
|
|
|
10
10
|
"./tokens.css": "./dist/tokens.css",
|
|
11
11
|
"./fonts.css": "./dist/fonts.css",
|
|
12
12
|
"./tokens.json": "./dist/tokens.json",
|
|
13
|
+
"./preview.js": { "types": "./dist/preview.d.ts", "default": "./dist/preview.js" },
|
|
14
|
+
"./preview-host.js": { "types": "./dist/preview-host.d.ts", "default": "./dist/preview-host.js" },
|
|
13
15
|
"./package.json": "./package.json"
|
|
14
16
|
},
|
|
15
17
|
"sideEffects": ["**/*.css"],
|
|
16
18
|
"publishConfig": {
|
|
17
|
-
"access": "public"
|
|
19
|
+
"access": "public",
|
|
20
|
+
"tag": "next"
|
|
18
21
|
},
|
|
19
22
|
"scripts": {
|
|
20
23
|
"build": "node scripts/build.ts",
|