@polyfen/polykit-tokens 0.0.0-stage → 0.2.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 +21 -0
- package/README.md +172 -2
- package/bin/polykit-brand.mjs +83 -0
- package/contrast.pairs.mjs +417 -0
- package/dist/_tokens.scss +492 -0
- package/dist/brand-contract.json +71 -0
- package/dist/brands/coral.css +14 -0
- package/dist/brands/coral.json +109 -0
- package/dist/brands/indigo.css +14 -0
- package/dist/brands/indigo.json +109 -0
- package/dist/brands/purple.css +14 -0
- package/dist/brands/purple.json +109 -0
- package/dist/brands/sky.css +14 -0
- package/dist/brands/sky.json +103 -0
- package/dist/brands/teal.css +14 -0
- package/dist/brands/teal.json +127 -0
- package/dist/brands/violet.css +20 -0
- package/dist/brands/violet.json +157 -0
- package/dist/index.d.ts +1439 -0
- package/dist/index.js +1436 -0
- package/dist/media.css +9 -0
- package/dist/shadcn.css +105 -0
- package/dist/tailwind.css +293 -0
- package/dist/themes/neutral.css +44 -0
- package/dist/tokens-studio.json +2405 -0
- package/dist/tokens.css +742 -0
- package/dist/tokens.dark.css +519 -0
- package/dist/tokens.json +928 -0
- package/dist/tokens.light.css +519 -0
- package/lib/brand.mjs +328 -0
- package/lib/contrast.mjs +130 -0
- package/package.json +54 -3
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 The Polyfen Group LLC
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/README.md
CHANGED
|
@@ -1,3 +1,173 @@
|
|
|
1
|
-
#
|
|
1
|
+
# @polyfen/polykit-tokens
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
Polykit design tokens: CSS variables, Sass, JavaScript, a Tailwind CSS v4 theme and a bridge for shadcn/ui, built from one W3C Design Tokens source.
|
|
4
|
+
|
|
5
|
+
## Install
|
|
6
|
+
|
|
7
|
+
```bash
|
|
8
|
+
pnpm add @polyfen/polykit-tokens
|
|
9
|
+
```
|
|
10
|
+
|
|
11
|
+
## CSS variables
|
|
12
|
+
|
|
13
|
+
```html
|
|
14
|
+
<link rel="stylesheet" href="node_modules/@polyfen/polykit-tokens/dist/tokens.css" />
|
|
15
|
+
```
|
|
16
|
+
|
|
17
|
+
```css
|
|
18
|
+
@import '@polyfen/polykit-tokens/tokens.css';
|
|
19
|
+
|
|
20
|
+
body {
|
|
21
|
+
background: var(--pk-color-canvas);
|
|
22
|
+
color: var(--pk-color-ink);
|
|
23
|
+
font: var(--pk-type-body-font-size) / var(--pk-type-body-line-height) var(--pk-font-sans);
|
|
24
|
+
}
|
|
25
|
+
```
|
|
26
|
+
|
|
27
|
+
Use the semantic tokens (`--pk-color-*`). The raw palette (`--pk-slate-700`) exists so semantic tokens can alias it; components should not use it.
|
|
28
|
+
|
|
29
|
+
| Prefix | What |
|
|
30
|
+
| ---------------------------------------------------------------- | --------------------------------------------------------- |
|
|
31
|
+
| `--pk-color-*` | Semantic colors, including `--pk-color-badge-<tone>-bg`, `-border` and `-ink` for the badge tones, and `--pk-color-on-category` (white text on a categorical fill). They change with the theme, except the categorical and frame colors. |
|
|
32
|
+
| `--pk-tint` | Mix share for tinted backgrounds. Changes with the theme. |
|
|
33
|
+
| `--pk-<hue>-<step>`, `--pk-white`, `--pk-black` | Raw palette. The same in every theme. |
|
|
34
|
+
| `--pk-space-*`, `--pk-radius-*`, `--pk-shadow-*`, `--pk-z-*` | Scales. `--pk-shadow-text-monogram` is a text shadow, not a box shadow. |
|
|
35
|
+
| `--pk-font-*`, `--pk-text-*`, `--pk-weight-*`, `--pk-leading-*` | Type scales. |
|
|
36
|
+
| `--pk-type-<role>-<property>` | Type roles: `title`, `body`, `heading-lg`, and so on. |
|
|
37
|
+
| `--pk-duration-*`, `--pk-delay-*`, `--pk-ease-*` | Motion. |
|
|
38
|
+
| `--pk-size-*`, `--pk-sidebar-*`, `--pk-topbar-*`, `--pk-panel-*` | Control sizes and layout. |
|
|
39
|
+
|
|
40
|
+
Fonts are not loaded by Polykit. Load Nunito Sans (400, 600, 700, 800, 900) yourself, from Google Fonts or `@fontsource/nunito-sans`.
|
|
41
|
+
|
|
42
|
+
## Themes
|
|
43
|
+
|
|
44
|
+
`tokens.css` follows the operating system's light or dark setting. Force a theme on any element:
|
|
45
|
+
|
|
46
|
+
```html
|
|
47
|
+
<html data-theme="dark">
|
|
48
|
+
<!-- or class="dark" -->
|
|
49
|
+
<section data-theme="light">A light island on a dark page</section>
|
|
50
|
+
</html>
|
|
51
|
+
```
|
|
52
|
+
|
|
53
|
+
| File | Contains |
|
|
54
|
+
| -------------------- | ----------------------------------------------- |
|
|
55
|
+
| `tokens.css` | Light, dark and automatic. The default. |
|
|
56
|
+
| `tokens.light.css` | Light only, for sites that never go dark. |
|
|
57
|
+
| `tokens.dark.css` | Dark only. |
|
|
58
|
+
| `themes/neutral.css` | Optional neutral slate canvas, instead of warm. |
|
|
59
|
+
|
|
60
|
+
Cool tint (the code and the file say neutral): load `themes/neutral.css` after `tokens.css`, and put `data-canvas="neutral"` next to the theme, normally on `<html>`. Tokens are resolved on the element that declares them, so the attribute must sit on the themed element (`<html>`, or an element with `data-theme`, `.light` or `.dark`); on an element with no theme of its own it does nothing. A themed island that should be neutral too repeats the attribute.
|
|
61
|
+
|
|
62
|
+
Strict contrast for form controls: the default control border is below 3:1 on light surfaces, to match the reference design. Override it for light:
|
|
63
|
+
|
|
64
|
+
```css
|
|
65
|
+
:root,
|
|
66
|
+
[data-theme='light'],
|
|
67
|
+
.light {
|
|
68
|
+
--pk-color-border-control: var(--pk-slate-400); /* 3.62:1 on white */
|
|
69
|
+
}
|
|
70
|
+
```
|
|
71
|
+
|
|
72
|
+
## Brands
|
|
73
|
+
|
|
74
|
+
A brand sets the accent: a required primary and an optional secondary. Everything else (surfaces, text, status colors) stays Polykit's. Load the brand's CSS after `tokens.css` and set `data-brand` on `<html>` or on any element; a theme switched inside a brand keeps it.
|
|
75
|
+
|
|
76
|
+
```html
|
|
77
|
+
<link rel="stylesheet" href="node_modules/@polyfen/polykit-tokens/dist/tokens.css" />
|
|
78
|
+
<link rel="stylesheet" href="brand.css" />
|
|
79
|
+
<html data-brand="acme">
|
|
80
|
+
…
|
|
81
|
+
</html>
|
|
82
|
+
```
|
|
83
|
+
|
|
84
|
+
| Token | Set by the brand |
|
|
85
|
+
| ---------------------------- | ---------------------------------------------------------------- |
|
|
86
|
+
| `--pk-color-accent` | Primary, required: location, selection, focus in fields |
|
|
87
|
+
| `-accent-hover`, `-accent-tint`, `-accent-tint-strong`, `-on-accent-tint`, `-on-accent`, `-focus-field`, `-focus-halo` | The rest of the primary, required. `on-accent` is text on a solid accent fill (the opt-in accent button), at 4.5:1 |
|
|
88
|
+
| `--pk-color-accent-secondary` and `-hover`, `-tint`, `-tint-strong`, `on-accent-secondary-tint`, `on-accent-secondary` | Optional, all six or none; without them each is the primary |
|
|
89
|
+
|
|
90
|
+
`chart-1` and the accent badge follow the primary.
|
|
91
|
+
|
|
92
|
+
Write the brand as JSON (`src/brands/teal.json` in this package is an example), then check it and write its CSS:
|
|
93
|
+
|
|
94
|
+
```sh
|
|
95
|
+
npx polykit-brand brand.json --css brand.css
|
|
96
|
+
```
|
|
97
|
+
|
|
98
|
+
The command checks the contract and measures every contrast pair that touches the accent, in both themes. A pair that falls short fails the check unless the brand file lists it under `exceptions` with a reason. Brand CSS uses `light-dark()` (Baseline 2024); pages without a brand are unaffected. Fictional examples ship in `dist/brands/`, named by their colors: `indigo`, `purple`, `sky`, `teal`, `coral`, and `violet` (with a magenta secondary). Their resolved colors are also in `raw.brands` in the JS entry.
|
|
99
|
+
|
|
100
|
+
## Tailwind CSS v4
|
|
101
|
+
|
|
102
|
+
```css
|
|
103
|
+
@import 'tailwindcss';
|
|
104
|
+
@import '@polyfen/polykit-tokens/tokens.css';
|
|
105
|
+
@import '@polyfen/polykit-tokens/tailwind.css';
|
|
106
|
+
```
|
|
107
|
+
|
|
108
|
+
Utilities are prefixed `pk-` so they never clash with shadcn's: `bg-pk-canvas`, `text-pk-ink`, `border-pk-border`, `rounded-pk-md`, `p-pk-4`, `shadow-pk-menu`, `font-pk-sans`, `text-pk-md`. Semantic colors follow the theme; the raw palette is available too (`bg-pk-slate-700`).
|
|
109
|
+
|
|
110
|
+
## shadcn/ui
|
|
111
|
+
|
|
112
|
+
Anything Polykit does not define falls back to [shadcn/ui](https://ui.shadcn.com). The bridge sets every shadcn theme variable from Polykit tokens, so shadcn components pick up Polykit's colors, radii and focus ring with no overrides:
|
|
113
|
+
|
|
114
|
+
```css
|
|
115
|
+
@import 'tailwindcss';
|
|
116
|
+
@import '@polyfen/polykit-tokens/tokens.css';
|
|
117
|
+
@import '@polyfen/polykit-tokens/shadcn.css';
|
|
118
|
+
```
|
|
119
|
+
|
|
120
|
+
shadcn's `accent` is a subtle hover surface, so it maps to `--pk-color-hover`, never to the orange accent. The full mapping is in the repository's `docs/FALLBACK.md`.
|
|
121
|
+
|
|
122
|
+
## Sass
|
|
123
|
+
|
|
124
|
+
Needs Sass's package importer (`pkg:` URLs, `new NodePackageImporter()` in the JS API). Without it, use a path to `dist/_tokens.scss`.
|
|
125
|
+
|
|
126
|
+
```scss
|
|
127
|
+
@use 'pkg:@polyfen/polykit-tokens' as pk;
|
|
128
|
+
|
|
129
|
+
.card {
|
|
130
|
+
padding: pk.$pk-space-4;
|
|
131
|
+
color: pk.$pk-color-ink; // var(--pk-color-ink): follows the theme
|
|
132
|
+
|
|
133
|
+
@include pk.pk-down(md) {
|
|
134
|
+
padding: pk.$pk-space-3;
|
|
135
|
+
}
|
|
136
|
+
}
|
|
137
|
+
```
|
|
138
|
+
|
|
139
|
+
Mixins: `pk-down($bp)` (max-width, inclusive), `pk-up($bp)` (min-width, one pixel above), `pk-min($bp)` (min-width, inclusive), `pk-media($name)` (any named query). Breakpoints: `xs` 420, `sm` 640, `md` 760, `lg` 1100, `xl` 1180.
|
|
140
|
+
|
|
141
|
+
## Named media queries
|
|
142
|
+
|
|
143
|
+
CSS variables cannot be used in media queries, so Polykit ships `@custom-media` rules for Lightning CSS and PostCSS:
|
|
144
|
+
|
|
145
|
+
```css
|
|
146
|
+
@import '@polyfen/polykit-tokens/media.css';
|
|
147
|
+
|
|
148
|
+
@media (--pk-md) {
|
|
149
|
+
/* max-width: 760px */
|
|
150
|
+
}
|
|
151
|
+
```
|
|
152
|
+
|
|
153
|
+
`--pk-xs`, `--pk-sm`, `--pk-md`, `--pk-lg` are max-width; `--pk-md-up` is `min-width: 761px`; `--pk-xl` is `min-width: 1180px`.
|
|
154
|
+
|
|
155
|
+
## JavaScript
|
|
156
|
+
|
|
157
|
+
```js
|
|
158
|
+
import { tokens, raw } from '@polyfen/polykit-tokens';
|
|
159
|
+
|
|
160
|
+
tokens.color.accent; // 'var(--pk-color-accent)'
|
|
161
|
+
raw.light.color.accent; // '#f27430'
|
|
162
|
+
raw.dark.color.accent; // '#f5925d'
|
|
163
|
+
tokens.motion.toastMs; // 4000
|
|
164
|
+
tokens.breakpoint.md; // 760
|
|
165
|
+
```
|
|
166
|
+
|
|
167
|
+
## Figma
|
|
168
|
+
|
|
169
|
+
`tokens-studio.json` imports into [Tokens Studio](https://tokens.studio) as three sets (Raw, Light, Dark) and two themes.
|
|
170
|
+
|
|
171
|
+
## Source and build
|
|
172
|
+
|
|
173
|
+
Tokens are authored as DTCG JSON in `src/` and built with Style Dictionary (`pnpm build`). Do not edit `dist/`. `CONTRAST.md` in the repository lists measured contrast for every pair that matters, and the documented exceptions.
|
|
@@ -0,0 +1,83 @@
|
|
|
1
|
+
#!/usr/bin/env node
|
|
2
|
+
/**
|
|
3
|
+
* Checks a project's brand file against Polykit's contract and contrast pairs, and writes its CSS.
|
|
4
|
+
*
|
|
5
|
+
* npx polykit-brand brand.json check: the contract, then every contrast pair
|
|
6
|
+
* npx polykit-brand brand.json --css brand.css also write the CSS (load it after tokens.css)
|
|
7
|
+
*
|
|
8
|
+
* Exits 1 when the file breaks the contract or a pair falls short without an exception in the
|
|
9
|
+
* brand file (or an exception is no longer needed). Warnings, such as an accent close to the
|
|
10
|
+
* information blue, are printed and do not fail. See the Brands page in the Polykit docs.
|
|
11
|
+
*/
|
|
12
|
+
import { readFile, writeFile } from 'node:fs/promises';
|
|
13
|
+
import path from 'node:path';
|
|
14
|
+
import { pathToFileURL } from 'node:url';
|
|
15
|
+
import {
|
|
16
|
+
brandCss,
|
|
17
|
+
brandPairs,
|
|
18
|
+
brandTokens,
|
|
19
|
+
brandWarnings,
|
|
20
|
+
pairKey,
|
|
21
|
+
readBrand,
|
|
22
|
+
} from '../lib/brand.mjs';
|
|
23
|
+
import { evaluate, failures } from '../lib/contrast.mjs';
|
|
24
|
+
|
|
25
|
+
const root = path.resolve(import.meta.dirname, '..');
|
|
26
|
+
const fmt = (ratio) => `${ratio.toFixed(2)}:1`;
|
|
27
|
+
|
|
28
|
+
export async function checkBrand(json) {
|
|
29
|
+
const tokens = JSON.parse(await readFile(path.join(root, 'dist/tokens.json'), 'utf8'));
|
|
30
|
+
const contract = JSON.parse(await readFile(path.join(root, 'dist/brand-contract.json'), 'utf8'));
|
|
31
|
+
const { pairs } = await import(pathToFileURL(path.join(root, 'contrast.pairs.mjs')).href);
|
|
32
|
+
const brand = readBrand(json, tokens.light);
|
|
33
|
+
const results = evaluate(
|
|
34
|
+
brandPairs(pairs, brand, contract.dependents),
|
|
35
|
+
brandTokens(tokens, brand, contract.dependents),
|
|
36
|
+
);
|
|
37
|
+
return {
|
|
38
|
+
brand,
|
|
39
|
+
results,
|
|
40
|
+
bad: failures(results),
|
|
41
|
+
warnings: brandWarnings(brand, tokens),
|
|
42
|
+
typeRoles: contract.typeRoles,
|
|
43
|
+
};
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
async function main(args) {
|
|
47
|
+
const file = args.find((a) => !a.startsWith('--'));
|
|
48
|
+
const cssAt = args.indexOf('--css');
|
|
49
|
+
if (!file || (cssAt !== -1 && !args[cssAt + 1])) {
|
|
50
|
+
console.error('Usage: polykit-brand <brand.json> [--css <out.css>]');
|
|
51
|
+
return 2;
|
|
52
|
+
}
|
|
53
|
+
let checked;
|
|
54
|
+
try {
|
|
55
|
+
checked = await checkBrand(JSON.parse(await readFile(file, 'utf8')));
|
|
56
|
+
} catch (error) {
|
|
57
|
+
console.error(error.message);
|
|
58
|
+
return 1;
|
|
59
|
+
}
|
|
60
|
+
const { brand, results, bad, warnings, typeRoles } = checked;
|
|
61
|
+
for (const warning of warnings) console.warn(`warning, ${warning.message}`);
|
|
62
|
+
for (const r of bad) {
|
|
63
|
+
const key = `"${pairKey({ fg: r.fg, bg: r.bg, mix: r.mix })}"`;
|
|
64
|
+
console.error(
|
|
65
|
+
r.status === 'stale-exception'
|
|
66
|
+
? `${r.theme}: ${key} passes at ${fmt(r.ratio)}; remove its exception`
|
|
67
|
+
: `${r.theme}: ${key} is ${fmt(r.ratio)}, below ${fmt(r.min)}. Fix the color, or add an exception with the reason.`,
|
|
68
|
+
);
|
|
69
|
+
}
|
|
70
|
+
if (cssAt !== -1) {
|
|
71
|
+
await writeFile(args[cssAt + 1], brandCss(brand, typeRoles));
|
|
72
|
+
console.log(`polykit-brand: wrote ${args[cssAt + 1]}`);
|
|
73
|
+
}
|
|
74
|
+
const excused = results.filter((r) => r.status === 'exception').length;
|
|
75
|
+
console.log(
|
|
76
|
+
`polykit-brand: ${brand.name}, ${brand.secondary ? 'with' : 'no'} secondary accent; ${results.length} measurements, ${excused} exceptions, ${bad.length} failures`,
|
|
77
|
+
);
|
|
78
|
+
return bad.length > 0 ? 1 : 0;
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
if (import.meta.url === pathToFileURL(process.argv[1] ?? '').href) {
|
|
82
|
+
process.exitCode = await main(process.argv.slice(2));
|
|
83
|
+
}
|