@kodentuzo/spotstrap 0.0.0-stage → 0.1.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 +77 -2
- package/dist/js/index.d.ts +17 -0
- package/dist/js/spotstrap.js +3 -0
- package/dist/js/spotstrap.js.map +7 -0
- package/dist/recipes/index.d.ts +86 -0
- package/dist/recipes/index.js +211 -0
- package/dist/recipes/sizes.d.ts +21 -0
- package/dist/spotstrap.css +1694 -0
- package/dist/themes/kodentuzo.css +128 -0
- package/dist/themes/librelifts.css +128 -0
- package/dist/themes/librevitalis.css +128 -0
- package/dist/themes/neutral.css +128 -0
- package/dist/themes/sokobanko.css +135 -0
- package/dist/themes/spotcraft.css +128 -0
- package/dist/themes/tagtape.css +128 -0
- package/dist/themes/vinskiano.css +128 -0
- package/dist/tokens.json +2473 -0
- package/package.json +81 -4
- package/src/components/button.css +168 -0
- package/src/components/disclosure.css +235 -0
- package/src/components/field.css +173 -0
- package/src/components/menu.css +129 -0
- package/src/components/overlay.css +160 -0
- package/src/components/toggle.css +219 -0
- package/src/dark-class.css +19 -0
- package/src/js/index.ts +246 -0
- package/src/native.css +17 -0
- package/src/recipes/index.ts +262 -0
- package/src/recipes/sizes.ts +20 -0
- package/src/tailwind.css +67 -0
- package/src/themes/kodentuzo.css +136 -0
- package/src/themes/librelifts.css +136 -0
- package/src/themes/librevitalis.css +136 -0
- package/src/themes/neutral.css +137 -0
- package/src/themes/sokobanko.css +144 -0
- package/src/themes/spotcraft.css +136 -0
- package/src/themes/tagtape.css +136 -0
- package/src/themes/vinskiano.css +136 -0
- package/src/tokens.css +42 -0
package/src/js/index.ts
ADDED
|
@@ -0,0 +1,246 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @kodentuzo/spotstrap/js: keyboard and ARIA behavior for Menu, Listbox, Combobox
|
|
3
|
+
* and Tabs, built on Zag.js state machines (docs/decisions/0004-style-state-not-runtime.md).
|
|
4
|
+
* Framework apps keep their own headless library and don't need this.
|
|
5
|
+
*
|
|
6
|
+
* <script type="module">
|
|
7
|
+
* import { init } from "@kodentuzo/spotstrap/js"
|
|
8
|
+
* init()
|
|
9
|
+
* </script>
|
|
10
|
+
*
|
|
11
|
+
* Components are found by `data-spotstrap="menu|listbox|combobox|tabs"` and their parts by
|
|
12
|
+
* `data-part="…"`. A menu written with `popover` and `commandfor` works without this
|
|
13
|
+
* script; init() swaps those for Zag's behavior.
|
|
14
|
+
*/
|
|
15
|
+
|
|
16
|
+
import * as combobox from '@zag-js/combobox'
|
|
17
|
+
import * as menu from '@zag-js/menu'
|
|
18
|
+
import * as select from '@zag-js/select'
|
|
19
|
+
import * as tabs from '@zag-js/tabs'
|
|
20
|
+
import { normalizeProps, spreadProps, VanillaMachine } from '@zag-js/vanilla'
|
|
21
|
+
|
|
22
|
+
export type Cleanup = () => void
|
|
23
|
+
|
|
24
|
+
let count = 0
|
|
25
|
+
const nextId = (kind: string) => `spotstrap-${kind}-${++count}`
|
|
26
|
+
|
|
27
|
+
/** The parts that belong to this component, not to one nested inside it. */
|
|
28
|
+
function parts(root: Element, name: string): HTMLElement[] {
|
|
29
|
+
return [...root.querySelectorAll<HTMLElement>(`[data-part="${name}"]`)].filter(
|
|
30
|
+
(el) => el.closest('[data-spotstrap]') === root,
|
|
31
|
+
)
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
function part(root: Element, name: string): HTMLElement {
|
|
35
|
+
const el = parts(root, name)[0]
|
|
36
|
+
if (!el)
|
|
37
|
+
throw new Error(`spotstrap: ${root.getAttribute('data-spotstrap')} needs a data-part="${name}"`)
|
|
38
|
+
return el
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
/** Zag positions a wrapper around the content; add one when the markup has none. */
|
|
42
|
+
function positioner(root: Element, content: HTMLElement): HTMLElement {
|
|
43
|
+
const existing = parts(root, 'positioner')[0]
|
|
44
|
+
if (existing) return existing
|
|
45
|
+
const wrapper = document.createElement('div')
|
|
46
|
+
wrapper.dataset.part = 'positioner'
|
|
47
|
+
content.before(wrapper)
|
|
48
|
+
wrapper.append(content)
|
|
49
|
+
return wrapper
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
/** Drop the no-script fallback: Zag opens, closes and positions the content now. */
|
|
53
|
+
function dropNativePopover(trigger: HTMLElement, content: HTMLElement) {
|
|
54
|
+
for (const attr of ['commandfor', 'command', 'popovertarget']) trigger.removeAttribute(attr)
|
|
55
|
+
content.removeAttribute('popover')
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
/** One option per `[data-part="item"]`: its value, its text, and whether it's disabled. */
|
|
59
|
+
function items(root: Element) {
|
|
60
|
+
return parts(root, 'item').map((el) => ({
|
|
61
|
+
el,
|
|
62
|
+
value: el.dataset.value ?? el.textContent?.trim() ?? '',
|
|
63
|
+
label: el.dataset.label ?? el.textContent?.trim() ?? '',
|
|
64
|
+
disabled: el.hasAttribute('data-disabled') || el.getAttribute('aria-disabled') === 'true',
|
|
65
|
+
}))
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
type Item = ReturnType<typeof items>[number]
|
|
69
|
+
|
|
70
|
+
/** Root props without Zag's id, so the page's own id on the component stays. */
|
|
71
|
+
function rootProps(props: Record<string, unknown>): Record<string, unknown> {
|
|
72
|
+
const { id: _, ...rest } = props
|
|
73
|
+
return rest
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
function emit(root: Element, name: string, detail: unknown) {
|
|
77
|
+
root.dispatchEvent(new CustomEvent(`spotstrap:${name}`, { bubbles: true, detail }))
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
function run<S extends { service: unknown }>(
|
|
81
|
+
machine: S & { start(): void; stop(): void; subscribe(fn: () => void): () => void },
|
|
82
|
+
render: () => void,
|
|
83
|
+
): Cleanup {
|
|
84
|
+
machine.start()
|
|
85
|
+
render()
|
|
86
|
+
const unsubscribe = machine.subscribe(render)
|
|
87
|
+
return () => {
|
|
88
|
+
unsubscribe()
|
|
89
|
+
machine.stop()
|
|
90
|
+
}
|
|
91
|
+
}
|
|
92
|
+
|
|
93
|
+
function initMenu(root: HTMLElement): Cleanup {
|
|
94
|
+
const trigger = part(root, 'trigger')
|
|
95
|
+
const content = part(root, 'content')
|
|
96
|
+
dropNativePopover(trigger, content)
|
|
97
|
+
const wrapper = positioner(root, content)
|
|
98
|
+
const machine = new VanillaMachine(menu.machine, {
|
|
99
|
+
id: root.id || nextId('menu'),
|
|
100
|
+
onSelect: ({ value }) => emit(root, 'select', { value }),
|
|
101
|
+
})
|
|
102
|
+
return run(machine, () => {
|
|
103
|
+
const api = menu.connect(machine.service, normalizeProps)
|
|
104
|
+
spreadProps(trigger, api.getTriggerProps())
|
|
105
|
+
spreadProps(wrapper, api.getPositionerProps())
|
|
106
|
+
spreadProps(content, api.getContentProps())
|
|
107
|
+
for (const item of items(root)) {
|
|
108
|
+
spreadProps(item.el, api.getItemProps({ value: item.value, disabled: item.disabled }))
|
|
109
|
+
}
|
|
110
|
+
})
|
|
111
|
+
}
|
|
112
|
+
|
|
113
|
+
function initListbox(root: HTMLElement): Cleanup {
|
|
114
|
+
const all = items(root)
|
|
115
|
+
const trigger = part(root, 'trigger')
|
|
116
|
+
const content = part(root, 'content')
|
|
117
|
+
const wrapper = positioner(root, content)
|
|
118
|
+
const valueText = parts(root, 'value-text')[0]
|
|
119
|
+
const label = parts(root, 'label')[0]
|
|
120
|
+
// A hidden <select> carries the value in forms; it needs an option per item.
|
|
121
|
+
const hidden = document.createElement('select')
|
|
122
|
+
for (const i of all) hidden.append(new Option(i.label, i.value))
|
|
123
|
+
root.append(hidden)
|
|
124
|
+
const selected = all.filter((i) => i.el.hasAttribute('data-selected')).map((i) => i.value)
|
|
125
|
+
const machine = new VanillaMachine(select.machine, {
|
|
126
|
+
id: root.id || nextId('listbox'),
|
|
127
|
+
name: root.dataset.name,
|
|
128
|
+
defaultValue: selected,
|
|
129
|
+
collection: select.collection<Item>({
|
|
130
|
+
items: all,
|
|
131
|
+
itemToValue: (i) => i.value,
|
|
132
|
+
itemToString: (i) => i.label,
|
|
133
|
+
isItemDisabled: (i) => i.disabled,
|
|
134
|
+
}),
|
|
135
|
+
onValueChange: ({ value }) => emit(root, 'change', { value }),
|
|
136
|
+
})
|
|
137
|
+
for (const i of all) i.el.removeAttribute('data-selected')
|
|
138
|
+
return run(machine, () => {
|
|
139
|
+
const api = select.connect(machine.service, normalizeProps)
|
|
140
|
+
spreadProps(root, rootProps(api.getRootProps()))
|
|
141
|
+
if (label) spreadProps(label, api.getLabelProps())
|
|
142
|
+
spreadProps(trigger, api.getTriggerProps())
|
|
143
|
+
if (valueText) {
|
|
144
|
+
spreadProps(valueText, api.getValueTextProps())
|
|
145
|
+
valueText.textContent = api.valueAsString || valueText.dataset.placeholder || ''
|
|
146
|
+
}
|
|
147
|
+
spreadProps(wrapper, api.getPositionerProps())
|
|
148
|
+
spreadProps(content, api.getContentProps())
|
|
149
|
+
spreadProps(hidden, api.getHiddenSelectProps())
|
|
150
|
+
for (const item of all) spreadProps(item.el, api.getItemProps({ item }))
|
|
151
|
+
})
|
|
152
|
+
}
|
|
153
|
+
|
|
154
|
+
function initCombobox(root: HTMLElement): Cleanup {
|
|
155
|
+
const all = items(root)
|
|
156
|
+
const input = part(root, 'input') as HTMLInputElement
|
|
157
|
+
const control = parts(root, 'control')[0] ?? input.parentElement ?? root
|
|
158
|
+
const content = part(root, 'content')
|
|
159
|
+
const wrapper = positioner(root, content)
|
|
160
|
+
const label = parts(root, 'label')[0]
|
|
161
|
+
const empty = parts(root, 'empty')[0]
|
|
162
|
+
const collectionOf = (list: Item[]) =>
|
|
163
|
+
combobox.collection<Item>({
|
|
164
|
+
items: list,
|
|
165
|
+
itemToValue: (i) => i.value,
|
|
166
|
+
itemToString: (i) => i.label,
|
|
167
|
+
isItemDisabled: (i) => i.disabled,
|
|
168
|
+
})
|
|
169
|
+
const matches = (query: string) => {
|
|
170
|
+
const q = query.trim().toLocaleLowerCase()
|
|
171
|
+
return q ? all.filter((i) => i.label.toLocaleLowerCase().includes(q)) : all
|
|
172
|
+
}
|
|
173
|
+
const machine = new VanillaMachine(combobox.machine, {
|
|
174
|
+
id: root.id || nextId('combobox'),
|
|
175
|
+
name: root.dataset.name,
|
|
176
|
+
collection: collectionOf(all),
|
|
177
|
+
openOnClick: true,
|
|
178
|
+
onInputValueChange: ({ inputValue }) => {
|
|
179
|
+
machine.updateProps({ collection: collectionOf(matches(inputValue)) })
|
|
180
|
+
},
|
|
181
|
+
onValueChange: ({ value }) => emit(root, 'change', { value }),
|
|
182
|
+
})
|
|
183
|
+
return run(machine, () => {
|
|
184
|
+
const api = combobox.connect(machine.service, normalizeProps)
|
|
185
|
+
const shown = new Set(matches(api.inputValue).map((i) => i.value))
|
|
186
|
+
spreadProps(root, rootProps(api.getRootProps()))
|
|
187
|
+
if (label) spreadProps(label, api.getLabelProps())
|
|
188
|
+
spreadProps(control, api.getControlProps())
|
|
189
|
+
spreadProps(input, api.getInputProps())
|
|
190
|
+
spreadProps(wrapper, api.getPositionerProps())
|
|
191
|
+
spreadProps(content, api.getContentProps())
|
|
192
|
+
for (const item of all) {
|
|
193
|
+
spreadProps(item.el, api.getItemProps({ item }))
|
|
194
|
+
item.el.hidden = !shown.has(item.value)
|
|
195
|
+
}
|
|
196
|
+
if (empty) empty.hidden = shown.size > 0
|
|
197
|
+
})
|
|
198
|
+
}
|
|
199
|
+
|
|
200
|
+
function initTabs(root: HTMLElement): Cleanup {
|
|
201
|
+
const list = part(root, 'list')
|
|
202
|
+
const triggers = parts(root, 'trigger')
|
|
203
|
+
const panels = parts(root, 'content')
|
|
204
|
+
const value = (el: HTMLElement) => el.dataset.value ?? ''
|
|
205
|
+
const selected = triggers.find((t) => t.hasAttribute('data-selected')) ?? triggers[0]
|
|
206
|
+
const machine = new VanillaMachine(tabs.machine, {
|
|
207
|
+
id: root.id || nextId('tabs'),
|
|
208
|
+
defaultValue: selected ? value(selected) : null,
|
|
209
|
+
onValueChange: ({ value }) => emit(root, 'change', { value }),
|
|
210
|
+
})
|
|
211
|
+
for (const t of triggers) t.removeAttribute('data-selected')
|
|
212
|
+
return run(machine, () => {
|
|
213
|
+
const api = tabs.connect(machine.service, normalizeProps)
|
|
214
|
+
spreadProps(root, rootProps(api.getRootProps()))
|
|
215
|
+
spreadProps(list, api.getListProps())
|
|
216
|
+
for (const t of triggers) spreadProps(t, api.getTriggerProps({ value: value(t) }))
|
|
217
|
+
for (const p of panels) spreadProps(p, api.getContentProps({ value: value(p) }))
|
|
218
|
+
})
|
|
219
|
+
}
|
|
220
|
+
|
|
221
|
+
const kinds: Record<string, (root: HTMLElement) => Cleanup> = {
|
|
222
|
+
tabs: initTabs,
|
|
223
|
+
menu: initMenu,
|
|
224
|
+
listbox: initListbox,
|
|
225
|
+
combobox: initCombobox,
|
|
226
|
+
}
|
|
227
|
+
|
|
228
|
+
const started = new WeakMap<Element, Cleanup>()
|
|
229
|
+
|
|
230
|
+
/** Starts every component under `root` that isn't running yet. Returns a function that stops them. */
|
|
231
|
+
export function init(root: ParentNode = document): Cleanup {
|
|
232
|
+
const cleanups: Cleanup[] = []
|
|
233
|
+
for (const el of root.querySelectorAll<HTMLElement>('[data-spotstrap]')) {
|
|
234
|
+
const start = kinds[el.dataset.spotstrap ?? '']
|
|
235
|
+
if (!start || started.has(el)) continue
|
|
236
|
+
const stop = start(el)
|
|
237
|
+
started.set(el, stop)
|
|
238
|
+
cleanups.push(() => {
|
|
239
|
+
stop()
|
|
240
|
+
started.delete(el)
|
|
241
|
+
})
|
|
242
|
+
}
|
|
243
|
+
return () => {
|
|
244
|
+
for (const stop of cleanups) stop()
|
|
245
|
+
}
|
|
246
|
+
}
|
package/src/native.css
ADDED
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
/*
|
|
2
|
+
* Spotstrap for React Native with Uniwind: sizes and the type scale, without the web's base
|
|
3
|
+
* layer and components. Import it after Uniwind, then a theme:
|
|
4
|
+
*
|
|
5
|
+
* @import "tailwindcss";
|
|
6
|
+
* @import "uniwind";
|
|
7
|
+
* @import "@kodentuzo/spotstrap/native.css";
|
|
8
|
+
* @import "@kodentuzo/spotstrap/themes/neutral.css";
|
|
9
|
+
*
|
|
10
|
+
* Then build components from @kodentuzo/spotstrap/recipes. Fonts: React Native needs one family name per
|
|
11
|
+
* font, so set --font-sans and friends in your own @theme after the theme.
|
|
12
|
+
*/
|
|
13
|
+
|
|
14
|
+
@import "./tokens.css";
|
|
15
|
+
|
|
16
|
+
/* The recipes' classes, so Uniwind generates them. */
|
|
17
|
+
@source "./recipes";
|
|
@@ -0,0 +1,262 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @kodentuzo/spotstrap/recipes: the components as Uniwind class strings for React Native, built from the
|
|
3
|
+
* same tokens as the web (docs/decisions/0002-package-and-publishing.md).
|
|
4
|
+
*
|
|
5
|
+
* React Native has no :hover, :checked or ::after, so state comes in as options, and small
|
|
6
|
+
* targets get a `hitSlop` instead of an overlay. Every class is written out in full, since
|
|
7
|
+
* Tailwind and Uniwind find classes by scanning source for literal strings; the pixel
|
|
8
|
+
* values match src/tokens.css (a test checks).
|
|
9
|
+
*
|
|
10
|
+
* const b = button({ variant: 'outline', pressed })
|
|
11
|
+
* <Pressable className={b.className} hitSlop={b.hitSlop}>
|
|
12
|
+
* <Text className={b.textClassName}>Export…</Text>
|
|
13
|
+
* </Pressable>
|
|
14
|
+
*/
|
|
15
|
+
|
|
16
|
+
import { box, compact, control, slop, switchSize } from './sizes.ts'
|
|
17
|
+
|
|
18
|
+
export * as sizes from './sizes.ts'
|
|
19
|
+
|
|
20
|
+
export interface Recipe {
|
|
21
|
+
className: string
|
|
22
|
+
hitSlop?: { top: number; bottom: number; left: number; right: number }
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
export interface TextRecipe extends Recipe {
|
|
26
|
+
textClassName: string
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
const join = (...parts: (string | false | undefined)[]) => parts.filter(Boolean).join(' ')
|
|
30
|
+
|
|
31
|
+
// Button ---------------------------------------------------------------------------------
|
|
32
|
+
|
|
33
|
+
export type ButtonVariant = 'default' | 'secondary' | 'outline' | 'ghost' | 'link' | 'destructive'
|
|
34
|
+
|
|
35
|
+
const buttonColors: Record<ButtonVariant, { box: string; text: string }> = {
|
|
36
|
+
default: { box: 'bg-primary', text: 'text-primary-foreground' },
|
|
37
|
+
secondary: { box: 'bg-secondary', text: 'text-secondary-foreground' },
|
|
38
|
+
outline: { box: 'bg-background border border-border', text: 'text-foreground' },
|
|
39
|
+
ghost: { box: 'bg-transparent', text: 'text-foreground' },
|
|
40
|
+
link: { box: 'bg-transparent', text: 'text-foreground underline' },
|
|
41
|
+
destructive: { box: 'bg-destructive', text: 'text-destructive-foreground' },
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
export interface ButtonOptions {
|
|
45
|
+
variant?: ButtonVariant
|
|
46
|
+
size?: 'default' | 'compact'
|
|
47
|
+
icon?: boolean
|
|
48
|
+
pressed?: boolean
|
|
49
|
+
disabled?: boolean
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
export function button({
|
|
53
|
+
variant = 'default',
|
|
54
|
+
size = 'default',
|
|
55
|
+
icon = false,
|
|
56
|
+
pressed = false,
|
|
57
|
+
disabled = false,
|
|
58
|
+
}: ButtonOptions = {}): TextRecipe {
|
|
59
|
+
const height = size === 'compact' ? compact : control
|
|
60
|
+
const colors = buttonColors[variant]
|
|
61
|
+
return {
|
|
62
|
+
className: join(
|
|
63
|
+
'flex-row items-center justify-center gap-2 rounded-lg',
|
|
64
|
+
icon
|
|
65
|
+
? size === 'compact'
|
|
66
|
+
? 'h-[36px] w-[36px]'
|
|
67
|
+
: 'h-[44px] w-[44px]'
|
|
68
|
+
: size === 'compact'
|
|
69
|
+
? 'min-h-[36px]'
|
|
70
|
+
: 'min-h-[44px]',
|
|
71
|
+
!icon && (variant === 'link' ? 'px-1' : size === 'compact' ? 'px-3' : 'px-4'),
|
|
72
|
+
colors.box,
|
|
73
|
+
pressed && 'opacity-85 scale-[0.97]',
|
|
74
|
+
disabled && 'opacity-50',
|
|
75
|
+
),
|
|
76
|
+
textClassName: join(
|
|
77
|
+
size === 'compact' ? 'text-subhead' : 'text-body',
|
|
78
|
+
'font-medium',
|
|
79
|
+
colors.text,
|
|
80
|
+
),
|
|
81
|
+
hitSlop: slop(icon ? height : control, height),
|
|
82
|
+
}
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
// Fields ---------------------------------------------------------------------------------
|
|
86
|
+
|
|
87
|
+
export function field(): Recipe {
|
|
88
|
+
return { className: 'gap-1.5' }
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
export function label({ disabled = false } = {}): Recipe {
|
|
92
|
+
return { className: join('text-subhead font-medium text-foreground', disabled && 'opacity-50') }
|
|
93
|
+
}
|
|
94
|
+
|
|
95
|
+
export function fieldDescription(): Recipe {
|
|
96
|
+
return { className: 'text-footnote text-muted-foreground' }
|
|
97
|
+
}
|
|
98
|
+
|
|
99
|
+
export function fieldError(): Recipe {
|
|
100
|
+
return { className: 'text-footnote text-destructive' }
|
|
101
|
+
}
|
|
102
|
+
|
|
103
|
+
export interface InputOptions {
|
|
104
|
+
focused?: boolean
|
|
105
|
+
invalid?: boolean
|
|
106
|
+
disabled?: boolean
|
|
107
|
+
multiline?: boolean
|
|
108
|
+
}
|
|
109
|
+
|
|
110
|
+
/** For a TextInput. Text is 17pt (body), above the 16 iOS needs not to zoom. */
|
|
111
|
+
export function input({
|
|
112
|
+
focused = false,
|
|
113
|
+
invalid = false,
|
|
114
|
+
disabled = false,
|
|
115
|
+
multiline = false,
|
|
116
|
+
}: InputOptions = {}): Recipe {
|
|
117
|
+
return {
|
|
118
|
+
className: join(
|
|
119
|
+
multiline ? 'min-h-[108px]' : 'min-h-[44px]',
|
|
120
|
+
'px-3 py-2.5 rounded-lg border',
|
|
121
|
+
'bg-background text-foreground text-body',
|
|
122
|
+
invalid ? 'border-destructive' : focused ? 'border-ring' : 'border-input',
|
|
123
|
+
disabled && 'opacity-50',
|
|
124
|
+
),
|
|
125
|
+
}
|
|
126
|
+
}
|
|
127
|
+
|
|
128
|
+
// Checkbox, radio, switch ----------------------------------------------------------------
|
|
129
|
+
|
|
130
|
+
/** A row holding a control and its label; the whole row is the target. */
|
|
131
|
+
export function choice({ disabled = false } = {}): Recipe {
|
|
132
|
+
return {
|
|
133
|
+
className: join('flex-row items-center gap-3 min-h-[44px]', disabled && 'opacity-50'),
|
|
134
|
+
}
|
|
135
|
+
}
|
|
136
|
+
|
|
137
|
+
export interface ToggleOptions {
|
|
138
|
+
checked?: boolean
|
|
139
|
+
disabled?: boolean
|
|
140
|
+
}
|
|
141
|
+
|
|
142
|
+
export function checkbox({ checked = false, disabled = false }: ToggleOptions = {}): Recipe {
|
|
143
|
+
return {
|
|
144
|
+
className: join(
|
|
145
|
+
'h-[20px] w-[20px] items-center justify-center rounded-sm border-[1.5px]',
|
|
146
|
+
checked ? 'bg-primary border-primary' : 'bg-background border-muted-foreground',
|
|
147
|
+
disabled && 'opacity-50',
|
|
148
|
+
),
|
|
149
|
+
hitSlop: slop(box),
|
|
150
|
+
}
|
|
151
|
+
}
|
|
152
|
+
|
|
153
|
+
export function radio({ checked = false, disabled = false }: ToggleOptions = {}): Recipe {
|
|
154
|
+
return {
|
|
155
|
+
className: join(
|
|
156
|
+
'h-[20px] w-[20px] items-center justify-center rounded-full border-[1.5px]',
|
|
157
|
+
checked ? 'bg-primary border-primary' : 'bg-background border-muted-foreground',
|
|
158
|
+
disabled && 'opacity-50',
|
|
159
|
+
),
|
|
160
|
+
hitSlop: slop(box),
|
|
161
|
+
}
|
|
162
|
+
}
|
|
163
|
+
|
|
164
|
+
/** The check or dot inside a checked checkbox or radio. */
|
|
165
|
+
export function toggleMark(): Recipe {
|
|
166
|
+
return { className: 'text-primary-foreground' }
|
|
167
|
+
}
|
|
168
|
+
|
|
169
|
+
/** A switch you draw yourself; React Native's own Switch also fits the size. */
|
|
170
|
+
export function switchTrack({ checked = false, disabled = false }: ToggleOptions = {}): Recipe {
|
|
171
|
+
return {
|
|
172
|
+
className: join(
|
|
173
|
+
'h-[31px] w-[51px] rounded-full p-0.5',
|
|
174
|
+
checked ? 'bg-primary items-end' : 'bg-muted items-start',
|
|
175
|
+
disabled && 'opacity-50',
|
|
176
|
+
),
|
|
177
|
+
hitSlop: slop(switchSize.width, switchSize.height),
|
|
178
|
+
}
|
|
179
|
+
}
|
|
180
|
+
|
|
181
|
+
export function switchThumb(): Recipe {
|
|
182
|
+
return { className: 'h-[27px] w-[27px] rounded-full bg-white shadow-sm' }
|
|
183
|
+
}
|
|
184
|
+
|
|
185
|
+
export function radioGroup(): Recipe {
|
|
186
|
+
return { className: 'gap-[12px]' }
|
|
187
|
+
}
|
|
188
|
+
|
|
189
|
+
// Menus, tabs, sheets --------------------------------------------------------------------
|
|
190
|
+
|
|
191
|
+
export function menu(): Recipe {
|
|
192
|
+
return { className: 'p-1 rounded-lg border border-border bg-popover shadow-lg' }
|
|
193
|
+
}
|
|
194
|
+
|
|
195
|
+
export interface ItemOptions {
|
|
196
|
+
highlighted?: boolean
|
|
197
|
+
selected?: boolean
|
|
198
|
+
destructive?: boolean
|
|
199
|
+
disabled?: boolean
|
|
200
|
+
compact?: boolean
|
|
201
|
+
}
|
|
202
|
+
|
|
203
|
+
export function menuItem({
|
|
204
|
+
highlighted = false,
|
|
205
|
+
destructive = false,
|
|
206
|
+
disabled = false,
|
|
207
|
+
compact: isCompact = false,
|
|
208
|
+
}: ItemOptions = {}): TextRecipe {
|
|
209
|
+
return {
|
|
210
|
+
className: join(
|
|
211
|
+
'flex-row items-center gap-3 px-3 rounded-md',
|
|
212
|
+
isCompact ? 'min-h-[36px]' : 'min-h-[44px]',
|
|
213
|
+
highlighted && 'bg-accent',
|
|
214
|
+
disabled && 'opacity-50',
|
|
215
|
+
),
|
|
216
|
+
textClassName: join(
|
|
217
|
+
'text-body',
|
|
218
|
+
destructive
|
|
219
|
+
? 'text-destructive'
|
|
220
|
+
: highlighted
|
|
221
|
+
? 'text-accent-foreground'
|
|
222
|
+
: 'text-popover-foreground',
|
|
223
|
+
),
|
|
224
|
+
hitSlop: isCompact ? slop(control, compact) : undefined,
|
|
225
|
+
}
|
|
226
|
+
}
|
|
227
|
+
|
|
228
|
+
export function tabList(): Recipe {
|
|
229
|
+
return { className: 'flex-row gap-1 border-b border-border' }
|
|
230
|
+
}
|
|
231
|
+
|
|
232
|
+
export function tab({ selected = false, disabled = false } = {}): TextRecipe {
|
|
233
|
+
return {
|
|
234
|
+
className: join(
|
|
235
|
+
'min-h-[44px] px-3.5 items-center justify-center border-b-2',
|
|
236
|
+
selected ? 'border-foreground' : 'border-transparent',
|
|
237
|
+
disabled && 'opacity-50',
|
|
238
|
+
),
|
|
239
|
+
textClassName: join(
|
|
240
|
+
'text-body font-medium',
|
|
241
|
+
selected ? 'text-foreground' : 'text-muted-foreground',
|
|
242
|
+
),
|
|
243
|
+
}
|
|
244
|
+
}
|
|
245
|
+
|
|
246
|
+
/** The sheet a dialog becomes on a phone: at the bottom, rounded on top. */
|
|
247
|
+
export function sheet(): Recipe {
|
|
248
|
+
return { className: 'gap-4 p-6 rounded-t-xl border border-b-0 border-border bg-popover' }
|
|
249
|
+
}
|
|
250
|
+
|
|
251
|
+
export function sheetTitle(): Recipe {
|
|
252
|
+
return { className: 'text-title-3 font-semibold text-popover-foreground' }
|
|
253
|
+
}
|
|
254
|
+
|
|
255
|
+
export function sheetDescription(): Recipe {
|
|
256
|
+
return { className: 'text-body text-muted-foreground' }
|
|
257
|
+
}
|
|
258
|
+
|
|
259
|
+
/** Sheet actions stack full width; put the primary action first, Cancel last. */
|
|
260
|
+
export function sheetActions(): Recipe {
|
|
261
|
+
return { className: 'gap-[12px]' }
|
|
262
|
+
}
|
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
// The touch sizes from src/tokens.css and src/components/toggle.css, in points, for React
|
|
2
|
+
// Native, which has no rem and no ::after to enlarge a hit area. A test checks they match.
|
|
3
|
+
|
|
4
|
+
/** A control's height: buttons, inputs, list rows, tabs. */
|
|
5
|
+
export const control = 44
|
|
6
|
+
/** A compact control's height; its hit area is still 44. */
|
|
7
|
+
export const compact = 36
|
|
8
|
+
/** Space between two targets. */
|
|
9
|
+
export const gap = 12
|
|
10
|
+
/** A checkbox or radio box. */
|
|
11
|
+
export const box = 20
|
|
12
|
+
/** Apple's switch. */
|
|
13
|
+
export const switchSize = { width: 51, height: 31, thumb: 27 } as const
|
|
14
|
+
|
|
15
|
+
/** The hitSlop that grows a `size`-point target to `control` points on every side. */
|
|
16
|
+
export function slop(width: number, height = width) {
|
|
17
|
+
const x = Math.max(0, Math.ceil((control - width) / 2))
|
|
18
|
+
const y = Math.max(0, Math.ceil((control - height) / 2))
|
|
19
|
+
return { top: y, bottom: y, left: x, right: x }
|
|
20
|
+
}
|
package/src/tailwind.css
ADDED
|
@@ -0,0 +1,67 @@
|
|
|
1
|
+
/*
|
|
2
|
+
* Spotstrap for projects with Tailwind v4. Import it after Tailwind, then one theme:
|
|
3
|
+
*
|
|
4
|
+
* @import "tailwindcss";
|
|
5
|
+
* @import "@kodentuzo/spotstrap/tailwind.css";
|
|
6
|
+
* @import "@kodentuzo/spotstrap/themes/neutral.css";
|
|
7
|
+
*
|
|
8
|
+
* Themes define the tokens (docs/decisions/0001-token-contract.md); this file defines how
|
|
9
|
+
* they're used. Colors follow the system's light or dark setting; import
|
|
10
|
+
* "@kodentuzo/spotstrap/dark-class.css" after this file to follow a `.dark` class instead.
|
|
11
|
+
*/
|
|
12
|
+
|
|
13
|
+
/* Sizes and the type scale, shared with React Native (native.css). */
|
|
14
|
+
@import "./tokens.css";
|
|
15
|
+
|
|
16
|
+
/* Components (docs/components.md). */
|
|
17
|
+
@import "./components/button.css";
|
|
18
|
+
@import "./components/field.css";
|
|
19
|
+
@import "./components/toggle.css";
|
|
20
|
+
@import "./components/overlay.css";
|
|
21
|
+
@import "./components/menu.css";
|
|
22
|
+
@import "./components/disclosure.css";
|
|
23
|
+
|
|
24
|
+
/* Themes set colors under `light` and `dark`. Tailwind has `dark` built in. */
|
|
25
|
+
@custom-variant light (@media (prefers-color-scheme: light));
|
|
26
|
+
|
|
27
|
+
/* Older shadcn components use `font-heading`; it's the display font. */
|
|
28
|
+
@theme inline {
|
|
29
|
+
--font-heading: var(--font-display);
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
@layer base {
|
|
33
|
+
:root {
|
|
34
|
+
color-scheme: light dark;
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
/* A page that picks a scheme with a class (dark-class.css, or the prebuilt themes). */
|
|
38
|
+
:root.light {
|
|
39
|
+
color-scheme: light;
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
:root.dark {
|
|
43
|
+
color-scheme: dark;
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
*,
|
|
47
|
+
::before,
|
|
48
|
+
::after {
|
|
49
|
+
border-color: var(--color-border);
|
|
50
|
+
outline-color: color-mix(in oklab, var(--color-ring) 50%, transparent);
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
body {
|
|
54
|
+
background-color: var(--color-background);
|
|
55
|
+
color: var(--color-foreground);
|
|
56
|
+
font-family: var(--font-sans);
|
|
57
|
+
font-size: var(--text-body);
|
|
58
|
+
line-height: var(--text-body--line-height);
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
/* iOS Safari zooms into text inputs smaller than 16px. */
|
|
62
|
+
input,
|
|
63
|
+
select,
|
|
64
|
+
textarea {
|
|
65
|
+
font-size: max(16px, 1em);
|
|
66
|
+
}
|
|
67
|
+
}
|