@halftone-ds/tokens 1.3.2 → 1.5.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.
@@ -1,7 +1,13 @@
1
1
  /**
2
- * NativeWind 4 / Tailwind 3 (shadcn v3 shape): bare HSL triplets in `:root` and `.dark:root`, plus the
3
- * colour + radius maps tailwind.config.js spreads in.
4
- * options: { css: 'global.css', tailwind: 'tokens/generated/tailwind.theme.js' }
2
+ * NativeWind 4 / Tailwind 3 (shadcn v3 shape): bare HSL triplets in `:root` and `.dark:root`, plus the theme
3
+ * extension Tailwind needs (colours, radius, sizes, type, shadows…).
4
+ * options: { css: 'global.css', tailwind: 'tokens/generated/tailwind.theme.js' } the whole stylesheet, and the
5
+ * extension as a map tailwind.config.js spreads in, key by key
6
+ * options: { css: 'tokens/generated/tokens.css', partial: true, preset: 'tokens/generated/tailwind.preset.js' }
7
+ * only the variables, for the app's own global.css to import first (`@import './tokens/generated/tokens.css';`
8
+ * before `@tailwind base;`: Tailwind's build inlines it, so it reaches native too), and the extension as a
9
+ * Tailwind preset the app's tailwind.config.js lists once (`presets: [require('nativewind/preset'),
10
+ * require('./tokens/generated/tailwind.preset')]`). `tailwind` and `preset` can both be written.
5
11
  */
6
12
  import { hexToHslTriplet } from '../lib/color.mjs';
7
13
  import { cssName, group, isColor, radiusCss } from '../lib/resolve.mjs';
@@ -49,11 +55,11 @@ function cssBlock(selector, tokens, shape, mode, colors) {
49
55
  }
50
56
 
51
57
  export default function nativewind3(ctx, options) {
58
+ const directives = options.partial
59
+ ? '/* Import this file first in global.css, before `@tailwind base;`. */\n'
60
+ : '@tailwind base;\n@tailwind components;\n@tailwind utilities;\n';
52
61
  const css = `/* ${ctx.header} */
53
- @tailwind base;
54
- @tailwind components;
55
- @tailwind utilities;
56
-
62
+ ${directives}
57
63
  @layer base {
58
64
  ${cssBlock(':root', ctx.semLight, { ...ctx.shape, styleShape: ctx.shape, textStyles: ctx.textStyles, extras: ctx.extras, components: ctx.components }, 'light', ctx.colors)}
59
65
 
@@ -98,7 +104,8 @@ ${cssBlock('.dark:root', ctx.semDark, { styleShape: ctx.shape, extras: ctx.extra
98
104
  const letterSpacing = Object.fromEntries(Object.keys(ctx.textStyles).map((name) => [`type-${name}`, `var(--type-${name}-tracking)`]));
99
105
  const fontWeight = Object.fromEntries(Object.keys(ctx.textStyles).map((name) => [`type-${name}`, `var(--type-${name}-weight)`]));
100
106
  // The component layer: bg-button-primary-bg, rounded-button, h-button-md, px-button-x, text-type-button,
101
- // border-button-primary-w… Each colour carries its opacity (--button-primary-bg-a).
107
+ // border-button-primary-w, gap-button-gap, border-chip… Each colour carries its opacity (--button-primary-bg-a).
108
+ // Weight, case and shadow are picked in JS from TOKENS.components (a custom font is one family per weight).
102
109
  Object.assign(colors, componentColors(ctx.components, (key, alpha) => (alpha ? `hsl(var(--${key}) / var(--${key}-a))` : `hsl(var(--${key}))`)));
103
110
  for (const name of COMPONENT_NAMES) {
104
111
  const c = ctx.components[name];
@@ -111,6 +118,10 @@ ${cssBlock('.dark:root', ctx.semDark, { styleShape: ctx.shape, extras: ctx.extra
111
118
  letterSpacing[`type-${p}`] = `var(--${p}-text-tracking)`;
112
119
  }
113
120
  Object.assign(spacing, { 'button-x-sm': 'var(--button-x-sm)', 'button-x-lg': 'var(--button-x-lg)' });
121
+ // Gaps (gap-button-gap, gap-button-gap-sm, gap-chip-gap, gap-badge-gap) and Chip and Badge borders (border-chip).
122
+ for (const name of ['button', 'chip', 'badge']) spacing[`${name}-gap`] = `var(--${name}-gap)`;
123
+ spacing['button-gap-sm'] = 'var(--button-gap-sm)';
124
+ for (const name of ['chip', 'badge']) borderWidth[name] = `var(--${name}-border-w)`;
114
125
  for (const variant of Object.keys(ctx.components.button.variants)) {
115
126
  borderWidth[`button-${variant}-w`] = `var(--button-${variant}-border-w)`;
116
127
  borderWidth[`button-${variant}-edge-w`] = `var(--button-${variant}-edge-w)`;
@@ -127,9 +138,28 @@ ${cssBlock('.dark:root', ctx.semDark, { styleShape: ctx.shape, extras: ctx.extra
127
138
  letterSpacing[`type-${name}`] = `var(--type-${name}-tracking)`;
128
139
  fontWeight[`type-${name}`] = `var(--type-${name}-weight)`;
129
140
  }
130
- const tailwind = `// ${ctx.header}\nmodule.exports = ${JSON.stringify({ colors, borderRadius, borderWidth, boxShadow, height, minHeight: height, width: height, spacing, fontSize, lineHeight, letterSpacing, fontWeight, maxWidth }, null, 2)};\n`;
131
- return [
132
- { path: options.css, contents: css },
133
- { path: options.tailwind, contents: tailwind },
134
- ];
141
+ // Motion: duration-fast|base|slow and ease-standard|emphasized|exit (springs are in lib/theme.ts MOTION).
142
+ const transitionDuration = Object.fromEntries(Object.entries(ctx.motion.durations).map(([n, ms]) => [n, `${ms}ms`]));
143
+ const transitionTimingFunction = Object.fromEntries(Object.entries(ctx.motion.easings).map(([n, c]) => [n, `cubic-bezier(${c.join(', ')})`]));
144
+ const extend = { colors, borderRadius, borderWidth, boxShadow, height, minHeight: height, width: height, spacing, fontSize, lineHeight, letterSpacing, fontWeight, maxWidth, transitionDuration, transitionTimingFunction };
145
+ const files = [{ path: options.css, contents: css }];
146
+ if (options.tailwind) files.push({ path: options.tailwind, contents: `// ${ctx.header}\nmodule.exports = ${JSON.stringify(extend, null, 2)};\n` });
147
+ if (options.preset) {
148
+ // A preset merges into the app's config: its `extend` adds to the app's own, with the hairline border NativeWind
149
+ // computes per device.
150
+ files.push({
151
+ path: options.preset,
152
+ contents: `// ${ctx.header}
153
+ // A Tailwind preset: list it in tailwind.config.js after nativewind/preset.
154
+ const { hairlineWidth } = require('nativewind/theme');
155
+
156
+ const extend = ${JSON.stringify(extend, null, 2)};
157
+ extend.borderWidth.hairline = hairlineWidth();
158
+
159
+ /** @type {import('tailwindcss').Config} */
160
+ module.exports = { darkMode: 'class', theme: { extend } };
161
+ `,
162
+ });
163
+ }
164
+ return files;
135
165
  }
@@ -6,13 +6,17 @@
6
6
  * options: { out: 'tokens/generated/web.css' }
7
7
  * density: false leave the spacing scale alone (the docs site)
8
8
  * typeset: false leave the text sizes alone (the docs site keeps its reading sizes)
9
+ * Motion: --duration-*, --ease-* and --spring-* in :root, with duration-fast|base|slow, ease-standard|emphasized|exit and
10
+ * spring-snappy|gentle classes (a spring class sets the easing and its duration together). A transition or an enter/exit
11
+ * animation without a duration class takes the theme's fast duration and standard easing, so the theme's Speed reaches
12
+ * every component, the overlays tw-animate-css draws included (it reads --tw-duration).
9
13
  */
10
14
  import { hexToOklch } from '../lib/color.mjs';
11
15
  import { group, radiusCss } from '../lib/resolve.mjs';
12
16
  import { TEXT_LINE_HEIGHT } from '../lib/theme/recipe.mjs';
13
17
  import { COMPONENT_NAMES, COMPONENTS, componentColors, componentVars } from '../lib/theme/components.mjs';
14
18
  import { hasExtras } from '../lib/theme/extras.mjs';
15
- import { extraVars, styleVars, textStyleVars, typeVars } from '../lib/theme/vars.mjs';
19
+ import { extraVars, motionVars, styleVars, textStyleVars, typeVars } from '../lib/theme/vars.mjs';
16
20
 
17
21
  const rem = (px) => (px >= 9999 ? '9999px' : `${px / 16}rem`);
18
22
  const SYSTEM_SANS = "ui-sans-serif, system-ui, -apple-system, 'Segoe UI', sans-serif";
@@ -95,12 +99,13 @@ function componentsCss(ctx) {
95
99
  ...COMPONENT_NAMES.filter((n) => n !== 'input').map((n) => ` --radius-${n}: var(--${n}-radius);`),
96
100
  ...Object.entries(colors).map(([key, css]) => ` --color-${key}: ${css};`),
97
101
  ];
98
- const utilities = COMPONENT_NAMES.map((n) => COMPONENTS[n].prefix).map(
99
- (p) => `@utility type-${p} {
102
+ // A label's weight and case are the component's (Button, Chip, Badge); a font-* or case class still wins.
103
+ const utilities = COMPONENT_NAMES.map((n) => [n, COMPONENTS[n].prefix]).map(
104
+ ([n, p]) => `@utility type-${p} {
100
105
  font-family: var(--${p}-text-font);
101
106
  font-size: var(--${p}-text-size);
102
107
  line-height: var(--tw-leading, var(--${p}-text-leading));
103
- letter-spacing: var(--tw-tracking, var(--${p}-text-tracking));
108
+ letter-spacing: var(--tw-tracking, var(--${p}-text-tracking));${COMPONENTS[n].props.includes('weight') ? `\n font-weight: var(--tw-font-weight, var(--${p}-text-weight));` : ''}${COMPONENTS[n].props.includes('case') ? `\n text-transform: var(--${p}-text-case);` : ''}
104
109
  }`
105
110
  );
106
111
  return {
@@ -111,6 +116,20 @@ function componentsCss(ctx) {
111
116
  };
112
117
  }
113
118
 
119
+ /**
120
+ * The motion classes: durations and springs (an easing class is Tailwind's own, from --ease-* in @theme). Like Tailwind's
121
+ * duration-200, they set --tw-duration (and a spring --tw-ease) too, which tw-animate-css's animate-in/out read.
122
+ */
123
+ function motionUtilities(motion) {
124
+ return [
125
+ ...Object.keys(motion.durations).map((n) => `@utility duration-${n} {\n --tw-duration: var(--duration-${n});\n transition-duration: var(--duration-${n});\n}`),
126
+ ...Object.keys(motion.springs).map(
127
+ (n) =>
128
+ `@utility spring-${n} {\n --tw-ease: var(--spring-${n});\n --tw-duration: var(--spring-${n}-duration);\n transition-timing-function: var(--spring-${n});\n transition-duration: var(--spring-${n}-duration);\n}`
129
+ ),
130
+ ].join('\n');
131
+ }
132
+
114
133
  export default function shadcnWeb(ctx, options) {
115
134
  const extras = extrasCss(ctx);
116
135
  const components = componentsCss(ctx);
@@ -132,6 +151,8 @@ export default function shadcnWeb(ctx, options) {
132
151
  }
133
152
  ${styles('light')}
134
153
  ${shadows('light')}
154
+ /* Motion: durations, easings and springs (a spring is a linear() easing and the time it takes to settle). */
155
+ ${Object.entries(motionVars(ctx.motion)).map(([k, v]) => ` ${k}: ${v};`).join('\n')}
135
156
  /* The text styles (type-h1…type-blockquote), literal at the typeset: an edited style is one variable. */
136
157
  ${Object.entries(textStyleVars(ctx.textStyles, 'oklch', (role) => FONT_ROLE[role])).map(([k, v]) => ` ${k}: ${v};`).join('\n')}
137
158
  ${vars('light')}${components.light}${extras.light}
@@ -160,18 +181,28 @@ ${ctx.radius.steps.map((s) => ` --radius-${s.name}: ${radiusCss(s)};`).join('\n
160
181
  --shadow-2xl: var(--shadow-3);
161
182
  --font-sans: var(--kit-font-body, ${SYSTEM_SANS});
162
183
  --font-heading: var(--kit-font-heading, var(--kit-font-body, ${SYSTEM_SANS}));
163
- --font-mono: var(--kit-font-mono, ${SYSTEM_MONO});${
184
+ --font-mono: var(--kit-font-mono, ${SYSTEM_MONO});
185
+ ${Object.keys(ctx.motion.easings).map((n) => ` --ease-${n}: var(--ease-${n});`).join('\n')}
186
+ /* A transition without a duration or easing class takes the theme's (Tailwind's default is 150ms, its own curve). */
187
+ --default-transition-duration: var(--duration-fast);
188
+ --default-transition-timing-function: var(--ease-standard);${
164
189
  options.typeset === false ? '' : `\n /* The theme's typeset scales every text size and its line height (text-sm, text-base…). */\n${textScale(ctx)}`
165
190
  }
166
191
  ${ctx.colors.map((c) => ` --color-${c.name}: ${color(c.name)};`).join('\n')}${components.theme}${extras.theme}
167
192
  }
168
193
 
169
194
  ${textStyleUtilities(Object.keys(ctx.textStyles))}
195
+ ${motionUtilities(ctx.motion)}
170
196
  ${components.utilities}${extras.utilities}
171
197
  @layer base {
172
198
  * {
173
199
  @apply border-border outline-ring/50;
174
200
  }
201
+ /* An enter or exit animation without a duration class (popovers, menus, tooltips) takes the theme's fast duration:
202
+ tw-animate-css reads --tw-duration, and its own fallback is a fixed 150ms. A duration-* class still wins. */
203
+ *, ::before, ::after {
204
+ --tw-duration: var(--duration-fast);
205
+ }
175
206
  body {
176
207
  @apply bg-background text-foreground;
177
208
  }
@@ -1,6 +1,6 @@
1
1
  /**
2
2
  * lib/theme.ts — semantic colours as hex per scheme, non-colour primitives, and (for Expo Router apps)
3
- * the React Navigation theme.
3
+ * the React Navigation theme, and MOTION (durations, easings and springs, typed, for Motion and Reanimated).
4
4
  * options: { out: 'lib/theme.ts', navTheme?: 'expo-router/react-navigation' | false }
5
5
  */
6
6
  import { camel, group } from '../lib/resolve.mjs';
@@ -23,12 +23,18 @@ const round = (n) => Number(n.toFixed(4));
23
23
  function componentsObject(components) {
24
24
  return COMPONENT_NAMES.map((name) => {
25
25
  const c = components[name];
26
- const text = { font: c.text.font, size: round(c.text.size), lineHeight: round(c.text.leading), tracking: c.text.tracking };
26
+ const text = { font: c.text.font, size: round(c.text.size), lineHeight: round(c.text.leading), tracking: c.text.tracking, weight: c.text.weight ?? (name === 'input' ? 400 : 500), case: c.text.case ?? 'none' };
27
+ // The icon size and the shadow ('none', '1'…'3'): native picks them in JS.
28
+ const own = { ...(c.gap ? { gap: c.gap } : {}), ...('icon' in c ? { icon: c.icon } : {}), ...('shadow' in c ? { shadow: c.shadow } : {}) };
27
29
  // The font role is typed as a role, so code can branch on it (the object is `as const`), and tracking as a
28
30
  // number: a kit compares it with 0, which wouldn't typecheck against a system's literal 0.01.
29
- const json = JSON.stringify({ height: c.h, x: c.x, radius: c.radius, text })
31
+ const json = JSON.stringify({ height: c.h, x: c.x, radius: c.radius, text, ...own })
30
32
  .replace(/"font":("\w+")/, `"font":$1 as 'heading' | 'body' | 'mono'`)
31
- .replace(/"tracking":(-?[\d.e-]+)/, '"tracking":$1 as number');
33
+ .replace(/"tracking":(-?[\d.e-]+)/, '"tracking":$1 as number')
34
+ .replace(/"weight":(\d+)/, '"weight":$1 as number')
35
+ .replace(/"case":("\w+")/, `"case":$1 as 'none' | 'uppercase'`)
36
+ .replace(/"icon":([\d.]+)/, '"icon":$1 as number')
37
+ .replace(/"shadow":("\w+")/, `"shadow":$1 as 'none' | '1' | '2' | '3'`);
32
38
  return ` ${name}: ${json},`;
33
39
  }).join('\n');
34
40
  }
@@ -95,6 +101,9 @@ ${primitiveGroup(ctx.prim, ['radius'])}
95
101
  },
96
102
  /** Lucide stroke width for icons (the Icon component's default; an explicit strokeWidth is relative to 2). */
97
103
  iconStroke: ${ctx.shape.stroke},
104
+ /** The Foundations shadows as CSS, per mode (shadow-sm / md / lg): native draws a component's shadow with them
105
+ * (React Native's boxShadow), so a see-through field shows one too. */
106
+ shadows: ${JSON.stringify(ctx.shape.shadows)},
98
107
  /** Width of \`border\` in px. */
99
108
  borderWidth: ${ctx.shape.borderWidth},
100
109
  /** The theme's styles (button: solid · soft · outline · raised; field: outline · filled · underline) and their
@@ -102,9 +111,10 @@ ${primitiveGroup(ctx.prim, ['radius'])}
102
111
  styles: ${JSON.stringify(ctx.styles)},
103
112
  button: ${JSON.stringify(ctx.shape.button)},
104
113
  field: ${JSON.stringify(ctx.shape.field)},
105
- /** The component layer (Button, Chip, Badge, Input) as numbers: heights and padding (px), corner, and the text's
106
- * font role and tracking. The classes (h-button-md, rounded-button, text-type-button…) already follow them; this is
107
- * for the touch-target floor (phones raise a height under ${TOUCH_TARGET} with hitSlop) and the font. */
114
+ /** The component layer (Button, Chip, Badge, Input) as numbers: heights, padding and gaps (px), corner, the text's
115
+ * font role, tracking, weight and case, the icon size and shadow level. The classes (h-button-md,
116
+ * rounded-button, text-type-button…) already follow the sizes; this is for the touch-target floor (phones raise a
117
+ * height under ${TOUCH_TARGET} with hitSlop) and what native picks in JS: the font, weight, case, icon and shadow. */
108
118
  components: {
109
119
  ${componentsObject(ctx.components)}
110
120
  },
@@ -134,6 +144,32 @@ ${primitiveGroup(ctx.prim, ['easing'])}
134
144
  },
135
145
  } as const;
136
146
 
147
+ /**
148
+ * The theme's motion, at its speed: durations (ms), easings (cubic-bezier) and springs (stiffness, damping, mass).
149
+ * Reanimated: \`withTiming(v, { duration: MOTION.duration.base, easing: Easing.bezier(...MOTION.easing.standard) })\`,
150
+ * \`withSpring(v, MOTION.spring.snappy)\`. Motion: \`transition={{ type: 'spring', ...MOTION.spring.snappy }}\`, or
151
+ * \`{ duration: MOTION.duration.base / 1000, ease: MOTION.easing.standard }\`. Respect reduced motion either way.
152
+ */
153
+ export const MOTION: Motion = ${JSON.stringify(
154
+ {
155
+ duration: ctx.motion.durations,
156
+ easing: ctx.motion.easings,
157
+ spring: ctx.motion.springs,
158
+ },
159
+ null,
160
+ 2
161
+ )};
162
+
163
+ export type Spring = { stiffness: number; damping: number; mass: number };
164
+ export type Motion = {
165
+ /** ms: fast for hover and small feedback, base for most transitions, slow for sheets. */
166
+ duration: { fast: number; base: number; slow: number };
167
+ /** cubic-bezier [x1, y1, x2, y2]: standard for most movement, emphasized for entering, exit for leaving. */
168
+ easing: { standard: readonly [number, number, number, number]; emphasized: readonly [number, number, number, number]; exit: readonly [number, number, number, number] };
169
+ /** snappy for presses and toggles, gentle for sheets and cards settling in. */
170
+ spring: { snappy: Spring; gentle: Spring };
171
+ };
172
+
137
173
  /** The component layer's CSS variables (what an embedded kit accepts from the docs site's live theme). */
138
174
  export const COMPONENT_VARS = ${JSON.stringify(componentVarNames())} as const;
139
175
 
@@ -164,6 +200,7 @@ export type ThemeInfo = {
164
200
  measure: string;
165
201
  button?: string;
166
202
  field?: string;
203
+ speed?: string;
167
204
  preset?: string;
168
205
  };
169
206
  };