@fabricator-ui/react-native 0.0.0-stage → 0.10.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.
Files changed (46) hide show
  1. package/README.md +8 -2
  2. package/dist/LICENSE.md +25 -0
  3. package/dist/NOTICE.md +24 -0
  4. package/dist/lib/data-variants.ts +23 -0
  5. package/dist/lib/density-scale.ts +208 -0
  6. package/dist/lib/fabricator/css-object.ts +2 -0
  7. package/dist/lib/fabricator/density-scale.ts +210 -0
  8. package/dist/lib/fabricator/density-variables.ts +37 -0
  9. package/dist/lib/fabricator/motion-speeds.ts +566 -0
  10. package/dist/lib/fabricator/radius.ts +240 -0
  11. package/dist/lib/feedback.ts +124 -0
  12. package/dist/lib/field-frame.ts +53 -0
  13. package/dist/lib/field-model.ts +112 -0
  14. package/dist/lib/fluid-hover-model.ts +142 -0
  15. package/dist/lib/fluid-hover.tsx +788 -0
  16. package/dist/lib/icon-stroke.ts +38 -0
  17. package/dist/lib/motion-tokens.ts +97 -0
  18. package/dist/lib/motion.ts +292 -0
  19. package/dist/lib/overlay-model.ts +205 -0
  20. package/dist/lib/press-wash.tsx +73 -0
  21. package/dist/lib/render.tsx +53 -0
  22. package/dist/lib/select-model.ts +87 -0
  23. package/dist/lib/sheet-model.ts +101 -0
  24. package/dist/lib/sheet-surface.tsx +429 -0
  25. package/dist/lib/sound-recipes.ts +449 -0
  26. package/dist/lib/spring-model.ts +214 -0
  27. package/dist/lib/surface-color.tsx +27 -0
  28. package/dist/lib/text-children.ts +43 -0
  29. package/dist/styles.css +610 -0
  30. package/dist/ui/badge.tsx +108 -0
  31. package/dist/ui/button.tsx +285 -0
  32. package/dist/ui/card.tsx +365 -0
  33. package/dist/ui/checkbox.tsx +230 -0
  34. package/dist/ui/collapsible.tsx +194 -0
  35. package/dist/ui/density.tsx +79 -0
  36. package/dist/ui/dialog.tsx +232 -0
  37. package/dist/ui/dropdown-menu.tsx +687 -0
  38. package/dist/ui/fabricator-root.tsx +68 -0
  39. package/dist/ui/input.tsx +63 -0
  40. package/dist/ui/label.tsx +22 -0
  41. package/dist/ui/select.tsx +616 -0
  42. package/dist/ui/switch.tsx +177 -0
  43. package/dist/ui/tabs.tsx +272 -0
  44. package/dist/ui/text.tsx +125 -0
  45. package/dist/ui/textarea.tsx +65 -0
  46. package/package.json +54 -4
package/README.md CHANGED
@@ -1,3 +1,9 @@
1
- # Temporary Holding Version
1
+ # Fabricator UI for React Native
2
2
 
3
- This version is a temporary placeholder for this package. An operational version to replace this has been submitted for review and is awaiting a staged release.
3
+ In an Expo SDK 58 app, use `fabricator-ui init --distribution package` to install the package and its native peers, set up Uniwind and wire the router layout. For manual setup, load `@fabricator-ui/react-native/styles.css` in the app's Uniwind CSS entry, import that entry from the app root, and mount `FabricatorRoot` from `@fabricator-ui/react-native/fabricator-root` around the navigator.
4
+
5
+ ```tsx
6
+ import { Button } from "@fabricator-ui/react-native/button"
7
+ ```
8
+
9
+ The preview includes Badge, Button, Card, Checkbox, Dialog, Dropdown Menu, Input, Label, Select, Switch, Tabs and Textarea. Package components are generated from the same React Native registry that serves copy-in installs. Update the package in your Expo app workspace. Use [copy-in](https://fabricator.sharphaw.com/docs/installation/install-methods) to edit component source or use the broader native catalog.
@@ -0,0 +1,25 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2026 Fabricator UI contributors
4
+
5
+ Copyright (c) 2023 shadcn
6
+
7
+ Copyright (c) 2026 Micka Touillaud (Fluid Functionalism)
8
+
9
+ Permission is hereby granted, free of charge, to any person obtaining a copy
10
+ of this software and associated documentation files (the "Software"), to deal
11
+ in the Software without restriction, including without limitation the rights
12
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
13
+ copies of the Software, and to permit persons to whom the Software is
14
+ furnished to do so, subject to the following conditions:
15
+
16
+ The above copyright notice and this permission notice shall be included in all
17
+ copies or substantial portions of the Software.
18
+
19
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
20
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
21
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
22
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
23
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
24
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
25
+ SOFTWARE.
package/dist/NOTICE.md ADDED
@@ -0,0 +1,24 @@
1
+ # Notice
2
+
3
+ Fabricator UI is built on [shadcn/ui](https://github.com/shadcn-ui/ui) by shadcn, used under the MIT License.
4
+
5
+ - The component registry (`apps/web/registry`), examples (`apps/web/examples`), style maps (`apps/web/registry/styles`), the registry build script and large parts of the documentation website are derived from `shadcn-ui/ui` (`apps/v4`). The pinned upstream commit is recorded in [`upstream.lock.json`](upstream.lock.json).
6
+ - The `fabricator-ui` CLI delegates to the official [`shadcn`](https://www.npmjs.com/package/shadcn) CLI.
7
+
8
+ The original copyright notice is preserved in [`LICENSE.md`](LICENSE.md).
9
+
10
+ SwiftUI Calendar selection rules are adapted from [React DayPicker](https://github.com/gpbl/react-day-picker), MIT License, Copyright (c) 2014-2026 Giampaolo Bellavite <io@gpbl.dev> and contributors. Its MIT permission and warranty terms are the same terms preserved in `LICENSE.md`.
11
+
12
+ Fabricator UI is an independent project and is not affiliated with or endorsed by shadcn or Vercel.
13
+
14
+ Fabricator Icons (`packages/icons*`) vendors the outline icons of [Lucide](https://lucide.dev) (ISC License, Copyright (c) 2026 Lucide Icons and Contributors; some icons derive from [Feather](https://feathericons.com), MIT License, Copyright (c) 2013-present Cole Bemis) in `packages/icons-core/sources/lucide/`, and ports parts of Lucide's build tooling. Brand mark geometry comes from [Simple Icons](https://simpleicons.org) (CC0-1.0) and [gilbarbara/logos](https://github.com/gilbarbara/logos) (CC0-1.0) through [Iconify](https://iconify.design), and the brand colour resolution is shared with SharpOS. The marks themselves are trademarks of their owners; see `packages/icons/BRANDS.md`, which also gives the licences and credits of the few marks that have their own (PHP's logo is CC BY-SA 4.0, by Colin Viebrock; Tux is by Larry Ewing and The GIMP). Motion's mark is copied from motion.dev. The animations in `@fabricator-ui/icons/animated` come from [lucide-animated](https://lucide-animated.com) (MIT License, Copyright (c) 2024-2026 pqoqubbw), vendored in `packages/icons-core/sources/animated/`.
15
+
16
+ The React Native components (`apps/web/registry/native`) are built on [RN Primitives](https://github.com/roninoss/rn-primitives) (MIT) and style themselves with [Uniwind](https://github.com/uni-stack/uniwind) (MIT). [React Native Reusables](https://github.com/founded-labs/react-native-reusables) (MIT) was the reference for how shadcn's patterns translate to React Native.
17
+
18
+ The Canvas (`registry/fabricator/shared/ui/canvas*.tsx`) draws with [React Flow](https://reactflow.dev) (`@xyflow/react`, MIT License, Copyright (c) 2019-2026 webkid GmbH). Its edge path geometry (`registry/fabricator/shared/lib/canvas-geometry.ts`) is ported from React Flow's `@xyflow/system` under the same licence. The Canvas hides React Flow's attribution link and credits it here and on its docs page.
19
+
20
+ The Color Picker (`registry/fabricator/*/ui/color-picker.tsx`, `registry/fabricator/shared/lib/color-model.ts`) is adapted from the Color Picker of [Fluid Functionalism](https://www.fluidfunctionalism.com) (MIT License, Copyright (c) 2026 Micka Touillaud).
21
+
22
+ The Video Player's demo clip (`apps/web/public/media/sintel-trailer.*`) is the trailer for [Sintel](https://durian.blender.org), Copyright (c) Blender Foundation, used under [CC BY 3.0](https://creativecommons.org/licenses/by/3.0/). It is re-encoded, with a poster frame taken from it and captions transcribed from its dialogue, with a Portuguese translation. The Audio Player's demo (`sintel-trailer.m4a`, `sintel-trailer-cover.jpg`) is its soundtrack and a square frame from it.
23
+
24
+ The file and folder icons in the `/files` entries come from [Charmed Icons](https://github.com/littensy/charmed-icons) v0.10.0 (MIT License, Copyright 2024 Littensy), vendored in `packages/icons-core/sources/files/`. Its full licence is included in each icon package's `LICENSE.md`.
@@ -0,0 +1,23 @@
1
+ const DATA_VARIANT = /data-\[([a-z-]+)=([^\]]+)\]:/g
2
+
3
+ /**
4
+ * The classes that apply to an element with these attributes, for reading a style
5
+ * off a class string: `useResolveClassNames` can't see the element's props, so it
6
+ * never matches `data-[…]:`. Conflicts are the caller's to merge.
7
+ */
8
+ export function applyDataVariants(
9
+ className: string,
10
+ attributes: Record<string, unknown>
11
+ ) {
12
+ return className
13
+ .split(/\s+/)
14
+ .flatMap((token) => {
15
+ let isMatch = true
16
+ const applied = token.replace(DATA_VARIANT, (_, name, value) => {
17
+ if (String(attributes[`data-${name}`]) !== value) isMatch = false
18
+ return ""
19
+ })
20
+ return isMatch && applied !== "" ? [applied] : []
21
+ })
22
+ .join(" ")
23
+ }
@@ -0,0 +1,208 @@
1
+ // What a control's `size` means on a device held in the hand, and which rung of the
2
+ // scale a device starts on. No React Native imports, so it stays testable anywhere.
3
+
4
+ import {
5
+ DENSITIES,
6
+ DENSITY_ICON_MIN,
7
+ isDensityName,
8
+ type DensityName,
9
+ type Density as DensityTokens,
10
+ } from "./fabricator/density-scale"
11
+
12
+ /** The touch target a control reaches, with `hitSlop` where it's drawn smaller. */
13
+ const TOUCH_TARGET = 44
14
+
15
+ /** Where a window stops being a phone: the App Shell changes layout here too. */
16
+ export const TABLET_MIN_WIDTH = 768
17
+
18
+ /**
19
+ * A tablet's base: 40pt controls with the phone's 15pt text and 18pt icons. The
20
+ * desktop is 36pt and the phone 44pt. There's room for lighter chrome, and a tablet
21
+ * is read from further away, so the text stays while the controls come down;
22
+ * `hitSlop` keeps them at 44pt for a finger. Rows are as tall as controls (`30`).
23
+ */
24
+ export const TABLET_DENSITY: DensityTokens = {
25
+ space: {
26
+ 4: 4,
27
+ 6: 8,
28
+ 8: 10,
29
+ 10: 12,
30
+ 12: 14,
31
+ 14: 16,
32
+ 16: 18,
33
+ 20: 24,
34
+ 24: 28,
35
+ 28: 32,
36
+ 30: 40,
37
+ 32: 36,
38
+ 36: 40,
39
+ 40: 44,
40
+ 48: 52,
41
+ },
42
+ text: DENSITIES.spacious.text,
43
+ icon: DENSITIES.spacious.icon,
44
+ }
45
+
46
+ /** A tablet's `size="sm"`: 34pt and 14pt text, for toolbars and dense rows. */
47
+ export const TABLET_SMALL_DENSITY: DensityTokens = {
48
+ space: {
49
+ 4: 4,
50
+ 6: 6,
51
+ 8: 8,
52
+ 10: 10,
53
+ 12: 12,
54
+ 14: 14,
55
+ 16: 14,
56
+ 20: 20,
57
+ 24: 24,
58
+ 28: 28,
59
+ 30: 30,
60
+ 32: 32,
61
+ 36: 34,
62
+ 40: 38,
63
+ 48: 46,
64
+ },
65
+ text: { 11: 12, 12: 12, 13: 14, 14: 15, 16: 16, 24: 24, 32: 30 },
66
+ icon: { 14: 14, 16: 16, 18: 18 },
67
+ }
68
+
69
+ /** A tablet's `size="lg"`: 48pt and 16pt text, for a screen's main action. */
70
+ export const TABLET_LARGE_DENSITY: DensityTokens = {
71
+ space: {
72
+ 4: 4,
73
+ 6: 8,
74
+ 8: 10,
75
+ 10: 12,
76
+ 12: 14,
77
+ 14: 16,
78
+ 16: 22,
79
+ 20: 26,
80
+ 24: 30,
81
+ 28: 40,
82
+ 30: 48,
83
+ 32: 44,
84
+ 36: 48,
85
+ 40: 52,
86
+ 48: 60,
87
+ },
88
+ text: { 11: 13, 12: 14, 13: 16, 14: 17, 16: 20, 24: 30, 32: 40 },
89
+ icon: { 14: 18, 16: 20, 18: 22 },
90
+ }
91
+
92
+ /**
93
+ * A step above spacious, for a `size="lg"` control on a phone: 52pt and 17pt text,
94
+ * the size of a screen's main action. Native only: a website's largest is spacious.
95
+ */
96
+ export const LARGE_DENSITY: DensityTokens = {
97
+ space: {
98
+ 4: 4,
99
+ 6: 10,
100
+ 8: 12,
101
+ 10: 14,
102
+ 12: 16,
103
+ 14: 18,
104
+ 16: 24,
105
+ 20: 28,
106
+ 24: 32,
107
+ 28: 44,
108
+ 30: 46,
109
+ 32: 48,
110
+ 36: 52,
111
+ 40: 56,
112
+ 48: 64,
113
+ },
114
+ text: { 11: 13, 12: 14, 13: 17, 14: 18, 16: 20, 24: 30, 32: 40 },
115
+ icon: { 14: 18, 16: 20, 18: 22 },
116
+ }
117
+
118
+ const NATIVE_DENSITIES = {
119
+ large: LARGE_DENSITY,
120
+ tablet: TABLET_DENSITY,
121
+ "tablet-sm": TABLET_SMALL_DENSITY,
122
+ "tablet-lg": TABLET_LARGE_DENSITY,
123
+ } as const
124
+
125
+ export type SizeDensity = DensityName | keyof typeof NATIVE_DENSITIES
126
+
127
+ export function getDensityTokens(density: SizeDensity): DensityTokens {
128
+ return density in NATIVE_DENSITIES
129
+ ? NATIVE_DENSITIES[density as keyof typeof NATIVE_DENSITIES]
130
+ : DENSITIES[density as DensityName]
131
+ }
132
+
133
+ const CONTROL_HEIGHTS = new Map<number, SizeDensity>([
134
+ [DENSITIES.compact.space[36], "compact"],
135
+ [TABLET_SMALL_DENSITY.space[36], "tablet-sm"],
136
+ [TABLET_DENSITY.space[36], "tablet"],
137
+ [DENSITIES.spacious.space[36], "spacious"],
138
+ [TABLET_LARGE_DENSITY.space[36], "tablet-lg"],
139
+ [LARGE_DENSITY.space[36], "large"],
140
+ ])
141
+
142
+ /** Which density a control height belongs to: what `--density-36` says it's in. */
143
+ export function getDensityName(controlHeight: number): SizeDensity {
144
+ return CONTROL_HEIGHTS.get(controlHeight) ?? "default"
145
+ }
146
+
147
+ const isTabletRung = (density: SizeDensity) =>
148
+ density === "tablet" || density === "tablet-sm" || density === "tablet-lg"
149
+
150
+ /**
151
+ * The rung each density takes on a device, where it differs from the
152
+ * website's. The website's densities are sized for a pointer; on a touch
153
+ * screen Default is the platform's own size (a phone's 44pt controls, a
154
+ * tablet's 40pt) and Compact and Spacious a step either side of it.
155
+ */
156
+ const DEVICE_RUNGS: Record<
157
+ "phone" | "tablet",
158
+ Partial<Record<DensityName, SizeDensity>>
159
+ > = {
160
+ phone: { compact: "tablet", default: "spacious", spacious: "large" },
161
+ tablet: { compact: "tablet-sm", default: "tablet" },
162
+ }
163
+
164
+ /**
165
+ * The rung a device takes in place of its look's density, if any. A window
166
+ * narrower than a tablet (Split View) is a phone's.
167
+ */
168
+ export function getDeviceDensity(
169
+ density: SizeDensity,
170
+ windowWidth: number
171
+ ): SizeDensity | undefined {
172
+ if (!isDensityName(density)) return undefined
173
+ const device = windowWidth >= TABLET_MIN_WIDTH ? "tablet" : "phone"
174
+ return DEVICE_RUNGS[device][density]
175
+ }
176
+
177
+ /**
178
+ * The density a `size` pins a control to. On the website `sm` is compact and `lg`
179
+ * spacious. At spacious, a phone's base, that would drop `sm` to 28pt and 12pt text
180
+ * and leave `lg` the same as the default, so there they're a step either side, and
181
+ * a tablet's base has its own steps.
182
+ */
183
+ export function getSizeDensity(
184
+ size: string | null | undefined,
185
+ around: SizeDensity
186
+ ): SizeDensity | undefined {
187
+ if (size !== "sm" && size !== "lg") return undefined
188
+ if (isTabletRung(around)) return size === "sm" ? "tablet-sm" : "tablet-lg"
189
+ const isPhoneBase = around === "spacious" || around === "large"
190
+ if (size === "sm") return isPhoneBase ? "default" : "compact"
191
+ return isPhoneBase ? "large" : "spacious"
192
+ }
193
+
194
+ /**
195
+ * The fixed floor below `sm`, which a tablet's distance takes a step up. Its
196
+ * icon stays at the 16pt floor for a component's own icons.
197
+ */
198
+ export function getXsSize(around: SizeDensity) {
199
+ return {
200
+ height: isTabletRung(around) ? 28 : 24,
201
+ icon: DENSITY_ICON_MIN,
202
+ }
203
+ }
204
+
205
+ /** The `hitSlop` that takes a control this tall to a full touch target. */
206
+ export function getTouchSlop(height: number) {
207
+ return Math.max(0, Math.ceil((TOUCH_TARGET - height) / 2))
208
+ }
@@ -0,0 +1,2 @@
1
+ // A CSS object in the shape of a shadcn registry item's `css` field.
2
+ export type CssObject = { [selector: string]: CssObject | string }
@@ -0,0 +1,210 @@
1
+ // Fabricator density: how much room the components take.
2
+ //
3
+ // Three densities set the same tokens: compact (dashboards, toolbars, dense
4
+ // tables), default (apps) and spacious (websites and landing pages). Default
5
+ // is the one in :root; `data-density="<name>"` on <html> or on any element
6
+ // switches everything inside it, and the `density-<name>` utilities do the
7
+ // same from a class (so `md:density-spacious` works). A component's
8
+ // `size="sm"` pins it to compact and `size="lg"` to spacious.
9
+ //
10
+ // Components size themselves with the tokens, never fixed pixels. Every token
11
+ // is named after its default value: `h-(--density-36)` is 36px at default,
12
+ // 28px compact and 44px spacious; `px-(--density-16)`, `gap-(--density-6)`,
13
+ // `text-(length:--density-text-13)` and `size-(--density-icon-16)` work the
14
+ // same way, except that a component's own icons are never drawn under 16px:
15
+ // `--density-icon-16` and `-18` are floored there. `--density-icon-14` is the
16
+ // small step, for icons that only mark something (separators, small
17
+ // chevrons, metadata), and may go below it. They are CSS variables rather than theme keys, so class merging (cn)
18
+ // knows them: a consumer's `h-12` still replaces `h-(--density-36)`.
19
+ // `registry/fabricator/__tests__/density-usage.test.ts` fails on fixed
20
+ // control sizes in the style maps.
21
+
22
+ import type { CssObject } from "./css-object"
23
+ import { RADIUS_ROLE_VARS } from "./radius"
24
+
25
+ export const DENSITY_NAMES = ["compact", "default", "spacious"] as const
26
+
27
+ export type DensityName = (typeof DENSITY_NAMES)[number]
28
+
29
+ export const DEFAULT_DENSITY: DensityName = "default"
30
+
31
+ export function isDensityName(value: unknown): value is DensityName {
32
+ return DENSITY_NAMES.some((name) => name === value)
33
+ }
34
+
35
+ /** Spacing and size steps, named by their default size in px. */
36
+ export const DENSITY_SPACE_STEPS = [
37
+ 4, 6, 8, 10, 12, 14, 16, 20, 24, 28, 30, 32, 36, 40, 48,
38
+ ] as const
39
+
40
+ /** Text sizes, named by their default size in px. */
41
+ export const DENSITY_TEXT_STEPS = [11, 12, 13, 14, 16, 24, 32] as const
42
+
43
+ /** Icon sizes, named by their default size in px. */
44
+ export const DENSITY_ICON_STEPS = [14, 16, 18] as const
45
+
46
+ /**
47
+ * The smallest icon a component draws for itself (`--density-icon-16` and up),
48
+ * in px, at any density: smaller glyphs stop reading on a control.
49
+ */
50
+ export const DENSITY_ICON_MIN = 16
51
+
52
+ export type DensitySpaceStep = (typeof DENSITY_SPACE_STEPS)[number]
53
+ export type DensityTextStep = (typeof DENSITY_TEXT_STEPS)[number]
54
+ export type DensityIconStep = (typeof DENSITY_ICON_STEPS)[number]
55
+
56
+ export type Density = {
57
+ /** Px per spacing step. */
58
+ space: Record<DensitySpaceStep, number>
59
+ /** Px per text step. */
60
+ text: Record<DensityTextStep, number>
61
+ /** Px per icon step. */
62
+ icon: Record<DensityIconStep, number>
63
+ }
64
+
65
+ export const DENSITIES: Record<DensityName, Density> = {
66
+ // About a fifth smaller: 28px controls and menu rows, 12px text, and
67
+ // component icons at the 16px floor. Tracks keep 24px items with 2px of
68
+ // padding.
69
+ compact: {
70
+ space: {
71
+ 4: 2,
72
+ 6: 4,
73
+ 8: 6,
74
+ 10: 8,
75
+ 12: 10,
76
+ 14: 12,
77
+ 16: 12,
78
+ 20: 16,
79
+ 24: 20,
80
+ 28: 24,
81
+ 30: 26,
82
+ 32: 28,
83
+ 36: 28,
84
+ 40: 32,
85
+ 48: 40,
86
+ },
87
+ text: { 11: 11, 12: 12, 13: 12, 14: 13, 16: 15, 24: 20, 32: 28 },
88
+ icon: { 14: 12, 16: 16, 18: 16 },
89
+ },
90
+ // 36px controls, 13px text: Fabricator's own sizes.
91
+ default: {
92
+ space: {
93
+ 4: 4,
94
+ 6: 6,
95
+ 8: 8,
96
+ 10: 10,
97
+ 12: 12,
98
+ 14: 14,
99
+ 16: 16,
100
+ 20: 20,
101
+ 24: 24,
102
+ 28: 28,
103
+ 30: 30,
104
+ 32: 32,
105
+ 36: 36,
106
+ 40: 40,
107
+ 48: 48,
108
+ },
109
+ text: { 11: 11, 12: 12, 13: 13, 14: 14, 16: 16, 24: 24, 32: 32 },
110
+ icon: { 14: 14, 16: 16, 18: 18 },
111
+ },
112
+ // About a fifth larger: 44px controls (the touch target size), 15px text,
113
+ // 18px icons.
114
+ spacious: {
115
+ space: {
116
+ 4: 4,
117
+ 6: 8,
118
+ 8: 10,
119
+ 10: 12,
120
+ 12: 14,
121
+ 14: 16,
122
+ 16: 20,
123
+ 20: 24,
124
+ 24: 28,
125
+ 28: 36,
126
+ 30: 38,
127
+ 32: 40,
128
+ 36: 44,
129
+ 40: 48,
130
+ 48: 56,
131
+ },
132
+ text: { 11: 12, 12: 13, 13: 15, 14: 16, 16: 18, 24: 28, 32: 36 },
133
+ icon: { 14: 16, 16: 18, 18: 20 },
134
+ },
135
+ }
136
+
137
+ const toRem = (px: number) => `${px / 16}rem`
138
+
139
+ /**
140
+ * The stroke, in grid units, an icon of `px` pixels draws when none is
141
+ * given: Fabricator Icons' `getAutoStrokeWidth` for its 1.75 stroke, written
142
+ * out because this file also ships to apps without the web icon package
143
+ * (density.test.ts holds them equal). CSS can't divide by the size, so the
144
+ * style maps read it per icon step from `--density-icon-stroke-<step>`.
145
+ */
146
+ export function getIconStroke(px: number) {
147
+ return Number(((1.75 * 4 * (px + 18)) / (7 * px)).toFixed(3))
148
+ }
149
+
150
+ /** A density's tokens, keyed by name without the `--`. */
151
+ export function toDensityVars(density: Density): Record<string, string> {
152
+ return {
153
+ ...Object.fromEntries(
154
+ DENSITY_SPACE_STEPS.map((step) => [
155
+ `density-${step}`,
156
+ toRem(density.space[step]),
157
+ ])
158
+ ),
159
+ ...Object.fromEntries(
160
+ DENSITY_TEXT_STEPS.map((step) => [
161
+ `density-text-${step}`,
162
+ `${density.text[step]}px`,
163
+ ])
164
+ ),
165
+ ...Object.fromEntries(
166
+ DENSITY_ICON_STEPS.map((step) => [
167
+ `density-icon-${step}`,
168
+ toRem(density.icon[step]),
169
+ ])
170
+ ),
171
+ ...Object.fromEntries(
172
+ DENSITY_ICON_STEPS.map((step) => [
173
+ `density-icon-stroke-${step}`,
174
+ String(getIconStroke(density.icon[step])),
175
+ ])
176
+ ),
177
+ }
178
+ }
179
+
180
+ const vars = (entries: Record<string, string>) =>
181
+ Object.fromEntries(
182
+ Object.entries(entries).map(([name, value]) => [`--${name}`, value])
183
+ )
184
+
185
+ // A scope's tokens plus the corner roles, which derive from its control
186
+ // height and must be declared again where it changes (radius.ts).
187
+ const scopeVars = (name: DensityName) =>
188
+ vars({ ...toDensityVars(DENSITIES[name]), ...RADIUS_ROLE_VARS })
189
+
190
+ /**
191
+ * Density switches and utilities, in the shape of a registry item's `css`
192
+ * field. The selectors are one notch more specific than :root, so they win
193
+ * on <html>, and plain attribute selectors anywhere else.
194
+ */
195
+ export const DENSITY_CSS: CssObject = {
196
+ ...Object.fromEntries(
197
+ DENSITY_NAMES.map((name) => [
198
+ `:root[data-density="${name}"], [data-density="${name}"]`,
199
+ scopeVars(name),
200
+ ])
201
+ ),
202
+ ...Object.fromEntries(
203
+ DENSITY_NAMES.map((name) => [`@utility density-${name}`, scopeVars(name)])
204
+ ),
205
+ }
206
+
207
+ /** The default density's tokens, for :root. */
208
+ export const DENSITY_ROOT_VARS: Record<string, string> = toDensityVars(
209
+ DENSITIES[DEFAULT_DENSITY]
210
+ )
@@ -0,0 +1,37 @@
1
+ import {
2
+ DENSITY_ICON_STEPS,
3
+ DENSITY_SPACE_STEPS,
4
+ DENSITY_TEXT_STEPS,
5
+ getIconStroke,
6
+ type Density,
7
+ } from "./density-scale"
8
+
9
+ /** A density's size tokens as numbers, the form React Native reads. */
10
+ export function getDensityVariables(density: Density) {
11
+ return {
12
+ ...Object.fromEntries(
13
+ DENSITY_SPACE_STEPS.map((step) => [
14
+ `--density-${step}`,
15
+ density.space[step],
16
+ ])
17
+ ),
18
+ ...Object.fromEntries(
19
+ DENSITY_TEXT_STEPS.map((step) => [
20
+ `--density-text-${step}`,
21
+ density.text[step],
22
+ ])
23
+ ),
24
+ ...Object.fromEntries(
25
+ DENSITY_ICON_STEPS.map((step) => [
26
+ `--density-icon-${step}`,
27
+ density.icon[step],
28
+ ])
29
+ ),
30
+ ...Object.fromEntries(
31
+ DENSITY_ICON_STEPS.map((step) => [
32
+ `--density-icon-stroke-${step}`,
33
+ getIconStroke(density.icon[step]),
34
+ ])
35
+ ),
36
+ } as Record<string, number>
37
+ }