@clarusjs/ui-primitives 0.3.3 → 0.4.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/package.json +6 -6
- package/src/accordion-item.tscl +9 -3
- package/src/autocomplete.tscl +115 -0
- package/src/checkbox-group-key.ts +5 -0
- package/src/checkbox-group.tscl +96 -0
- package/src/checkbox-item.tscl +18 -0
- package/src/checkbox.tscl +15 -7
- package/src/chip-input.tscl +24 -15
- package/src/combobox.tscl +44 -35
- package/src/context-menu.tscl +100 -0
- package/src/date-field.tscl +140 -0
- package/src/date-segments.ts +55 -0
- package/src/dialog-close.tscl +7 -2
- package/src/dialog-trigger.tscl +7 -2
- package/src/dialog.tscl +10 -4
- package/src/{dismiss.js → dismiss.ts} +15 -18
- package/src/drag-drop.ts +116 -0
- package/src/field.tscl +42 -27
- package/src/fieldset.tscl +37 -0
- package/src/{focus-return.js → focus-return.ts} +2 -4
- package/src/form-errors.tscl +40 -0
- package/src/{index.js → index.ts} +8 -8
- package/src/menu-item.tscl +9 -3
- package/src/menu-list.tscl +1 -1
- package/src/menu.tscl +8 -5
- package/src/menubar.tscl +69 -0
- package/src/nav-menu-key.ts +5 -0
- package/src/nav-menu-panel.tscl +11 -0
- package/src/nav-menu-trigger.tscl +9 -0
- package/src/nav-menu.tscl +78 -0
- package/src/number-field.tscl +139 -0
- package/src/popover-trigger.tscl +7 -2
- package/src/popover.tscl +21 -14
- package/src/{position.js → position.ts} +21 -17
- package/src/preview-card-trigger.tscl +48 -0
- package/src/preview-card.tscl +27 -0
- package/src/radio-group.tscl +12 -7
- package/src/radio.tscl +13 -6
- package/src/{resize-column.js → resize-column.ts} +12 -16
- package/src/{roving-focus.js → roving-focus.ts} +19 -20
- package/src/select.tscl +27 -17
- package/src/sidebar-trigger.tscl +9 -6
- package/src/{sidebar.js → sidebar.ts} +5 -3
- package/src/sidebar.tscl +11 -4
- package/src/sortable-list.tscl +50 -0
- package/src/swipe.ts +79 -0
- package/src/switch.tscl +15 -7
- package/src/tab-panel.tscl +9 -4
- package/src/tab.tscl +11 -6
- package/src/tabs.tscl +12 -7
- package/src/time-field.tscl +139 -0
- package/src/toast-region.tscl +10 -4
- package/src/toast.ts +61 -0
- package/src/toggle-group.tscl +15 -9
- package/src/toggle-item.tscl +13 -7
- package/src/toggle.tscl +4 -1
- package/src/toolbar-separator.tscl +9 -0
- package/src/toolbar.tscl +30 -0
- package/src/tooltip-trigger.tscl +10 -6
- package/src/tooltip.tscl +11 -5
- package/src/{transition.js → transition.ts} +3 -6
- package/src/{typeahead.js → typeahead.ts} +20 -20
- package/src/virtual-grid.ts +100 -0
- package/src/virtual-keyboard.ts +36 -0
- package/src/virtual-list.ts +82 -0
- package/src/dismiss.d.ts +0 -27
- package/src/focus-return.d.ts +0 -10
- package/src/index.d.ts +0 -8
- package/src/position.d.ts +0 -36
- package/src/resize-column.d.ts +0 -18
- package/src/roving-focus.d.ts +0 -39
- package/src/sidebar.d.ts +0 -8
- package/src/toast.d.ts +0 -48
- package/src/toast.js +0 -56
- package/src/transition.d.ts +0 -18
- package/src/typeahead.d.ts +0 -35
|
@@ -0,0 +1,139 @@
|
|
|
1
|
+
{{--
|
|
2
|
+
Time field (#679): a time typed in segments (hour, minute, optional second, and AM/PM where the locale uses a 12-hour
|
|
3
|
+
clock), the date field's keys: ↑ ↓ step (wrapping), PageUp/PageDown by ten, Home/End to the ends; digits fill a
|
|
4
|
+
segment and move on; on AM/PM, ↑ ↓ switch it and "a" or "p" set it. Bind [(value)]: 24-hour 'HH:MM' (or 'HH:MM:SS'
|
|
5
|
+
with [seconds]) once every segment is filled, '' until then. [name] sends it with a form. The locale is fixed
|
|
6
|
+
('en-US' unless given) so the server and the browser write the same segments.
|
|
7
|
+
|
|
8
|
+
<time-field id="start" label="Starts at" name="start" [(value)]="event.start" />
|
|
9
|
+
--}}
|
|
10
|
+
<script lang="ts">
|
|
11
|
+
import { untrack } from 'clarusjs/runtime'
|
|
12
|
+
import { step, timeOrder, typeDigit } from './date-segments.ts'
|
|
13
|
+
const { id, label, name = '', locale = 'en-US', seconds = false, disabled = false } = props<{
|
|
14
|
+
/** Its id, unique on the page: its label and segment ids are made from it. */
|
|
15
|
+
id: string
|
|
16
|
+
/** The field's name, for the group and its segments. */
|
|
17
|
+
label: string
|
|
18
|
+
/** Sends the value ('HH:MM', 24-hour) with a form. */
|
|
19
|
+
name?: string
|
|
20
|
+
/** The segments' order, separators, and 12- or 24-hour clock. */
|
|
21
|
+
locale?: string
|
|
22
|
+
/** A seconds segment; the value is then 'HH:MM:SS'. */
|
|
23
|
+
seconds?: boolean
|
|
24
|
+
/** Can't be edited or focused. */
|
|
25
|
+
disabled?: boolean
|
|
26
|
+
}>()
|
|
27
|
+
/** 'HH:MM' or 'HH:MM:SS' (24-hour), or '' while a segment is empty. */
|
|
28
|
+
let value = model<string>(() => '')
|
|
29
|
+
|
|
30
|
+
type Kind = 'hour' | 'minute' | 'second' | 'dayPeriod'
|
|
31
|
+
const order = timeOrder(locale, seconds) as ({ type: Kind } | { type: 'literal'; text: string })[]
|
|
32
|
+
const twelve = order.some((p) => p.type === 'dayPeriod')
|
|
33
|
+
// The locale's own AM and PM
|
|
34
|
+
const periods = [Date.UTC(2001, 0, 1, 1), Date.UTC(2001, 0, 1, 13)].map((t) =>
|
|
35
|
+
new Intl.DateTimeFormat(locale, { hour: 'numeric', hour12: true, timeZone: 'UTC' }).formatToParts(t).find((p) => p.type === 'dayPeriod')?.value ?? '')
|
|
36
|
+
type Parts = Record<Kind, number | null>
|
|
37
|
+
const read = (time: string): Parts => {
|
|
38
|
+
const m = /^(\d{2}):(\d{2})(?::(\d{2}))?$/.exec(time)
|
|
39
|
+
if (!m) return { hour: null, minute: null, second: null, dayPeriod: null }
|
|
40
|
+
const h = Number(m[1])
|
|
41
|
+
return { hour: twelve ? h % 12 || 12 : h, minute: Number(m[2]), second: m[3] ? Number(m[3]) : seconds ? 0 : null, dayPeriod: twelve ? (h >= 12 ? 1 : 0) : null }
|
|
42
|
+
}
|
|
43
|
+
let parts = state(read(value))
|
|
44
|
+
const pad = (n: number) => String(n).padStart(2, '0')
|
|
45
|
+
const time = derived(() => {
|
|
46
|
+
const { hour, minute, second, dayPeriod } = parts
|
|
47
|
+
if (hour === null || minute === null || (seconds && second === null) || (twelve && dayPeriod === null)) return ''
|
|
48
|
+
const h = twelve ? (hour % 12) + (dayPeriod ? 12 : 0) : hour
|
|
49
|
+
return `${pad(h)}:${pad(minute)}${seconds ? `:${pad(second!)}` : ''}`
|
|
50
|
+
})
|
|
51
|
+
|
|
52
|
+
const low = (k: Kind) => (k === 'hour' && twelve ? 1 : 0)
|
|
53
|
+
const high = (k: Kind) => (k === 'hour' ? (twelve ? 12 : 23) : k === 'dayPeriod' ? 1 : 59)
|
|
54
|
+
const PLACEHOLDER: Record<Kind, string> = { hour: 'hh', minute: 'mm', second: 'ss', dayPeriod: '--' }
|
|
55
|
+
const NAME: Record<Kind, string> = { hour: 'hour', minute: 'minute', second: 'second', dayPeriod: 'AM/PM' }
|
|
56
|
+
const shown = (k: Kind) => (parts[k] === null ? PLACEHOLDER[k] : k === 'dayPeriod' ? periods[parts[k]!]! : pad(parts[k]!))
|
|
57
|
+
const spoken = (k: Kind) => (parts[k] === null ? 'Empty' : k === 'dayPeriod' ? periods[parts[k]!]! : String(parts[k]))
|
|
58
|
+
|
|
59
|
+
function set(k: Kind, n: number | null): void {
|
|
60
|
+
parts = { ...parts, [k]: n }
|
|
61
|
+
value = time
|
|
62
|
+
}
|
|
63
|
+
// The parent's new value into the segments; only value is followed (as in the date field)
|
|
64
|
+
effect(() => {
|
|
65
|
+
const next = value
|
|
66
|
+
untrack(() => { if (next !== time) parts = read(next) })
|
|
67
|
+
})
|
|
68
|
+
|
|
69
|
+
let typed = ''
|
|
70
|
+
function key(event: KeyboardEvent, k: Kind): void {
|
|
71
|
+
if (disabled) return
|
|
72
|
+
const self = (event.target as Element).closest<HTMLElement>('[role="spinbutton"]')!
|
|
73
|
+
const segments = [...(self.parentElement?.querySelectorAll<HTMLElement>('[role="spinbutton"]') ?? [])]
|
|
74
|
+
const at = segments.indexOf(self)
|
|
75
|
+
const move = (by: number) => segments[at + by]?.focus()
|
|
76
|
+
const rtl = self.closest<HTMLElement>('[dir]')?.dir === 'rtl'
|
|
77
|
+
if (k === 'dayPeriod' && /^[ap]$/i.test(event.key)) {
|
|
78
|
+
event.preventDefault()
|
|
79
|
+
set(k, event.key.toLowerCase() === 'a' ? 0 : 1)
|
|
80
|
+
return
|
|
81
|
+
}
|
|
82
|
+
if (/^\d$/.test(event.key) && k !== 'dayPeriod') {
|
|
83
|
+
event.preventDefault()
|
|
84
|
+
const t = typeDigit(typed, event.key, low(k), high(k), 2)
|
|
85
|
+
typed = t.buffer
|
|
86
|
+
set(k, t.value)
|
|
87
|
+
if (t.done) move(1)
|
|
88
|
+
return
|
|
89
|
+
}
|
|
90
|
+
const keys: Record<string, () => void> = {
|
|
91
|
+
ArrowUp: () => set(k, step(parts[k], 1, low(k), high(k))),
|
|
92
|
+
ArrowDown: () => set(k, step(parts[k], -1, low(k), high(k))),
|
|
93
|
+
PageUp: () => set(k, step(parts[k], 10, low(k), high(k))),
|
|
94
|
+
PageDown: () => set(k, step(parts[k], -10, low(k), high(k))),
|
|
95
|
+
Home: () => set(k, low(k)),
|
|
96
|
+
End: () => set(k, high(k)),
|
|
97
|
+
// Right to left (the nearest dir), the next segment is to the left
|
|
98
|
+
ArrowRight: () => move(rtl ? -1 : 1),
|
|
99
|
+
ArrowLeft: () => move(rtl ? 1 : -1),
|
|
100
|
+
Backspace: () => { typed = typed.slice(0, -1); set(k, typed ? Number(typed) : null) },
|
|
101
|
+
Delete: () => { typed = ''; set(k, null) },
|
|
102
|
+
}
|
|
103
|
+
if (!keys[event.key]) return
|
|
104
|
+
event.preventDefault()
|
|
105
|
+
if (event.key !== 'Backspace') typed = ''
|
|
106
|
+
keys[event.key]!()
|
|
107
|
+
}
|
|
108
|
+
const leave = () => { typed = '' }
|
|
109
|
+
</script>
|
|
110
|
+
|
|
111
|
+
<div data-part="time-field" role="group" [aria-labelledby]="id + '-label'" [data-filled]="time ? '' : false" [data-disabled]="disabled ? '' : false">
|
|
112
|
+
<span [id]="id + '-label'" data-part="label">{{ label }}</span>
|
|
113
|
+
<div data-part="segments">
|
|
114
|
+
@for (seg of order; track $index) {
|
|
115
|
+
@if (seg.type === 'literal') {
|
|
116
|
+
<span data-part="literal" aria-hidden="true">{{ seg.text }}</span>
|
|
117
|
+
} @else {
|
|
118
|
+
<span
|
|
119
|
+
role="spinbutton"
|
|
120
|
+
data-part="segment"
|
|
121
|
+
[data-segment]="seg.type"
|
|
122
|
+
[tabindex]="disabled ? -1 : 0"
|
|
123
|
+
[aria-label]="NAME[seg.type]"
|
|
124
|
+
[aria-valuemin]="low(seg.type)"
|
|
125
|
+
[aria-valuemax]="high(seg.type)"
|
|
126
|
+
[aria-valuenow]="parts[seg.type] ?? undefined"
|
|
127
|
+
[aria-valuetext]="spoken(seg.type)"
|
|
128
|
+
[aria-disabled]="disabled ? 'true' : undefined"
|
|
129
|
+
[data-placeholder]="parts[seg.type] === null ? '' : false"
|
|
130
|
+
(keydown)="key($event, seg.type)"
|
|
131
|
+
(blur)="leave()"
|
|
132
|
+
>{{ shown(seg.type) }}</span>
|
|
133
|
+
}
|
|
134
|
+
}
|
|
135
|
+
</div>
|
|
136
|
+
@if (name) {
|
|
137
|
+
<input type="hidden" [name]="name" [value]="time" />
|
|
138
|
+
}
|
|
139
|
+
</div>
|
package/src/toast-region.tscl
CHANGED
|
@@ -4,15 +4,19 @@
|
|
|
4
4
|
for each toast and fills the parts it finds: data-part="toast-message" (the text), "toast-action" (removed when the
|
|
5
5
|
toast has none), "toast-close", "toast-icon" (a default icon when it's empty). Static markup: it's copied, not run. A polite live region: screen readers
|
|
6
6
|
announce new messages without moving focus. Each message stays its duration (paused while hovered or focused);
|
|
7
|
-
Escape
|
|
7
|
+
Escape or a swipe sideways dismisses one; an action runs and dismisses.
|
|
8
8
|
|
|
9
9
|
<toast-region /> … import { toast } from '@clarusjs/ui-primitives'; toast('Saved')
|
|
10
10
|
[label] names the region ("Notifications"): give a second region on a page its own.
|
|
11
11
|
--}}
|
|
12
|
-
@input label: string = 'Notifications'
|
|
13
12
|
|
|
14
|
-
<script>
|
|
13
|
+
<script lang="ts">
|
|
15
14
|
import { dismissToast, subscribeToasts, toasts } from '@clarusjs/ui-primitives'
|
|
15
|
+
import { swipe } from './swipe.ts'
|
|
16
|
+
const props = props<{
|
|
17
|
+
/** Names the region for screen readers; give a second region on a page its own. */
|
|
18
|
+
label?: string
|
|
19
|
+
}>(() => ({ label: 'Notifications' }))
|
|
16
20
|
const KINDS = { success: 'Success:', error: 'Error:', info: 'Info:', warning: 'Warning:' }
|
|
17
21
|
const PATHS = {
|
|
18
22
|
success: 'M5 12l5 5 9-10',
|
|
@@ -152,6 +156,8 @@
|
|
|
152
156
|
item.addEventListener('keydown', (event) => {
|
|
153
157
|
if (event.key === 'Escape') leave(t.id, item)
|
|
154
158
|
})
|
|
159
|
+
// Swiped sideways, it's dismissed (a shortcut for its close button and Escape)
|
|
160
|
+
swipe(item, { onSwipe: () => leave(t.id, item) })
|
|
155
161
|
list.append(item)
|
|
156
162
|
start(t, item)
|
|
157
163
|
}
|
|
@@ -164,7 +170,7 @@
|
|
|
164
170
|
}
|
|
165
171
|
</script>
|
|
166
172
|
|
|
167
|
-
<section [aria-label]="label" data-part="toast-region" [use]="wire">
|
|
173
|
+
<section [aria-label]="props.label" data-part="toast-region" [use]="wire">
|
|
168
174
|
<template data-type="toast"><slot name="toast" /></template>
|
|
169
175
|
<template data-type="success"><slot name="success" /></template>
|
|
170
176
|
<template data-type="error"><slot name="error" /></template>
|
package/src/toast.ts
ADDED
|
@@ -0,0 +1,61 @@
|
|
|
1
|
+
// Toasts: short messages any island can send with toast('Saved'); <toast-region> (once, in the layout) shows them.
|
|
2
|
+
// The list lives on globalThis, so every island's copy of this module shares it however the bundle is split.
|
|
3
|
+
|
|
4
|
+
/** A button in a toast. */
|
|
5
|
+
export interface ToastAction {
|
|
6
|
+
/** The button's text. */
|
|
7
|
+
label: string
|
|
8
|
+
/** Runs when it's pressed; the toast is dismissed after. */
|
|
9
|
+
onClick: () => void
|
|
10
|
+
}
|
|
11
|
+
/** A toast in the queue, as toasts() returns it. */
|
|
12
|
+
export interface Toast {
|
|
13
|
+
/** Its number, unique in the queue: dismissToast(id). */
|
|
14
|
+
id: number
|
|
15
|
+
/** The text shown (as text, never markup). */
|
|
16
|
+
message: string
|
|
17
|
+
/** A button in the toast; a toast with one stays until dismissed. */
|
|
18
|
+
action?: ToastAction
|
|
19
|
+
/** Milliseconds shown (paused while hovered or focused); Infinity: until dismissed. */
|
|
20
|
+
duration: number
|
|
21
|
+
/** Its kind: styled by data-type, with an icon and the kind in words. */
|
|
22
|
+
type?: 'success' | 'error' | 'info' | 'warning'
|
|
23
|
+
/** false: no icon. */
|
|
24
|
+
icon?: boolean
|
|
25
|
+
}
|
|
26
|
+
const store: { items: Toast[], listeners: Set<() => void>, next: number } = (globalThis as any).__clToasts ??= { items: [], listeners: new Set(), next: 1 }
|
|
27
|
+
const notify = () => { for (const listener of store.listeners) listener() }
|
|
28
|
+
|
|
29
|
+
/**
|
|
30
|
+
* Show a message for `duration` ms (5000 by default; a toast with an action stays until dismissed, so there's time
|
|
31
|
+
* to reach it), with at most one action. Polite: it never takes focus.
|
|
32
|
+
* @returns its id, for dismissToast()
|
|
33
|
+
*/
|
|
34
|
+
export function toast(message: string, options: { action?: ToastAction, duration?: number, type?: 'success' | 'error' | 'info' | 'warning', icon?: boolean } = {}): number {
|
|
35
|
+
const id = store.next++
|
|
36
|
+
store.items = [...store.items, { id, message, action: options.action, type: options.type, icon: options.icon, duration: options.duration ?? (options.action ? Infinity : 5000) }]
|
|
37
|
+
notify()
|
|
38
|
+
return id
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
/** Removes a toast now (its action ran, or it was closed). */
|
|
42
|
+
export function dismissToast(id: number) {
|
|
43
|
+
store.items = store.items.filter((t) => t.id !== id)
|
|
44
|
+
notify()
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
/** Every toast, oldest first. */
|
|
48
|
+
export const toasts = (): Toast[] => store.items
|
|
49
|
+
|
|
50
|
+
/** Calls `listener` now and on every change; returns how to stop. @param listener */
|
|
51
|
+
export function subscribeToasts(listener: () => void) {
|
|
52
|
+
store.listeners.add(listener)
|
|
53
|
+
listener()
|
|
54
|
+
return () => { store.listeners.delete(listener) }
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
/** Removes every toast (tests, or a page that resets). */
|
|
58
|
+
export function clearToasts() {
|
|
59
|
+
store.items = []
|
|
60
|
+
notify()
|
|
61
|
+
}
|
package/src/toggle-group.tscl
CHANGED
|
@@ -8,23 +8,29 @@
|
|
|
8
8
|
<toggle-item value="center">Center</toggle-item>
|
|
9
9
|
</toggle-group>
|
|
10
10
|
--}}
|
|
11
|
-
@input label: string
|
|
12
|
-
@input type: 'single' | 'multiple' = 'single'
|
|
13
|
-
@model value: string = ''
|
|
14
|
-
@model values: string[] = []
|
|
15
11
|
|
|
16
|
-
<script>
|
|
12
|
+
<script lang="ts">
|
|
17
13
|
import { effect } from 'clarusjs/runtime'
|
|
18
14
|
import { rovingFocus } from '@clarusjs/ui-primitives'
|
|
19
|
-
|
|
20
|
-
|
|
15
|
+
const props = props<{
|
|
16
|
+
/** Names the group for screen readers. */
|
|
17
|
+
label: string
|
|
18
|
+
/** single keeps one toggle pressed (bind [(value)]); multiple lets each toggle on its own (bind [(values)]). */
|
|
19
|
+
type?: 'single' | 'multiple'
|
|
20
|
+
}>(() => ({ type: 'single' }))
|
|
21
|
+
/** The pressed toggle's value (single), bound two ways with [(value)]. */
|
|
22
|
+
let value = model<string>(() => '')
|
|
23
|
+
/** The pressed toggles' values (multiple), bound two ways with [(values)]. */
|
|
24
|
+
let values = model<string[]>(() => [])
|
|
25
|
+
provide('toggleGroup', { type: props.type, value, values })
|
|
26
|
+
const isOn = (v) => (props.type === 'multiple' ? values.includes(v) : value === v)
|
|
21
27
|
function wire(el) {
|
|
22
28
|
const own = (node) => node.closest('[data-part="toggle-group"]') === el
|
|
23
29
|
const click = (event) => {
|
|
24
30
|
const item = event.target.closest('[data-part="toggle-item"]')
|
|
25
31
|
if (!item || !own(item) || item.disabled) return
|
|
26
32
|
const v = item.dataset.value
|
|
27
|
-
if (type === 'multiple') values = values.includes(v) ? values.filter((x) => x !== v) : [...values, v]
|
|
33
|
+
if (props.type === 'multiple') values = values.includes(v) ? values.filter((x) => x !== v) : [...values, v]
|
|
28
34
|
else if (value !== v) value = v
|
|
29
35
|
}
|
|
30
36
|
el.addEventListener('click', click)
|
|
@@ -45,4 +51,4 @@
|
|
|
45
51
|
}
|
|
46
52
|
</script>
|
|
47
53
|
|
|
48
|
-
<div role="group" [aria-label]="label" data-part="toggle-group" [data-type]="type" [use]="wire"><slot /></div>
|
|
54
|
+
<div role="group" [aria-label]="props.label" data-part="toggle-group" [data-type]="props.type" [use]="wire"><slot /></div>
|
package/src/toggle-item.tscl
CHANGED
|
@@ -1,15 +1,21 @@
|
|
|
1
1
|
{{-- Toggle item: one toggle inside <toggle-group>; [value] is what it adds to the group's value. --}}
|
|
2
2
|
@context toggleGroup: { type: string; value: string; values: string[] }
|
|
3
|
-
|
|
4
|
-
|
|
3
|
+
<script lang="ts">
|
|
4
|
+
const props = props<{
|
|
5
|
+
/** What it adds to the group's value when pressed. */
|
|
6
|
+
value: string
|
|
7
|
+
/** Can't be pressed or focused. */
|
|
8
|
+
disabled?: boolean
|
|
9
|
+
}>(() => ({ disabled: false }))
|
|
10
|
+
</script>
|
|
5
11
|
|
|
6
12
|
<button
|
|
7
13
|
type="button"
|
|
8
|
-
[aria-pressed]="String(toggleGroup.type === 'multiple' ? toggleGroup.values.includes(value) : toggleGroup.value === value)"
|
|
9
|
-
[data-state]="(toggleGroup.type === 'multiple' ? toggleGroup.values.includes(value) : toggleGroup.value === value) ? 'on' : 'off'"
|
|
10
|
-
[tabindex]="toggleGroup.type === 'single' && toggleGroup.value !== '' && toggleGroup.value !== value ? -1 : 0"
|
|
11
|
-
[data-value]="value"
|
|
12
|
-
[disabled]="disabled"
|
|
14
|
+
[aria-pressed]="String(toggleGroup.type === 'multiple' ? toggleGroup.values.includes(props.value) : toggleGroup.value === props.value)"
|
|
15
|
+
[data-state]="(toggleGroup.type === 'multiple' ? toggleGroup.values.includes(props.value) : toggleGroup.value === props.value) ? 'on' : 'off'"
|
|
16
|
+
[tabindex]="toggleGroup.type === 'single' && toggleGroup.value !== '' && toggleGroup.value !== props.value ? -1 : 0"
|
|
17
|
+
[data-value]="props.value"
|
|
18
|
+
[disabled]="props.disabled"
|
|
13
19
|
data-part="toggle-item"
|
|
14
20
|
>
|
|
15
21
|
<slot />
|
package/src/toggle.tscl
CHANGED
|
@@ -3,7 +3,10 @@
|
|
|
3
3
|
|
|
4
4
|
<toggle [(pressed)]="bold" aria-label="Bold">B</toggle>
|
|
5
5
|
--}}
|
|
6
|
-
|
|
6
|
+
<script lang="ts">
|
|
7
|
+
/** Whether it's on, bound two ways with [(pressed)]. */
|
|
8
|
+
let pressed = model<boolean>(() => false)
|
|
9
|
+
</script>
|
|
7
10
|
|
|
8
11
|
<button type="button" [aria-pressed]="String(pressed)" [data-state]="pressed ? 'on' : 'off'" data-part="toggle" (click)="pressed = !pressed">
|
|
9
12
|
<slot />
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
{{-- Toolbar separator (#676): a line between groups of controls, across the toolbar (vertical in a horizontal one). --}}
|
|
2
|
+
<script lang="ts">
|
|
3
|
+
const { orientation = 'vertical' } = props<{
|
|
4
|
+
/** The line's own direction: vertical between buttons in a row. */
|
|
5
|
+
orientation?: 'horizontal' | 'vertical'
|
|
6
|
+
}>()
|
|
7
|
+
</script>
|
|
8
|
+
|
|
9
|
+
<div role="separator" data-part="toolbar-separator" [aria-orientation]="orientation"></div>
|
package/src/toolbar.tscl
ADDED
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
{{--
|
|
2
|
+
Toolbar (#676): a row of controls that is one Tab stop; the arrow keys move between its buttons, links, checkboxes and
|
|
3
|
+
radios (rovingFocus: Home and End too, flipped in right-to-left text). Text fields and selects keep their own arrow
|
|
4
|
+
keys, and their own Tab stop. Put single <toggle>s in it rather than a <toggle-group>: the toolbar is the one stop.
|
|
5
|
+
|
|
6
|
+
<toolbar label="Formatting">
|
|
7
|
+
<toggle aria-label="Bold" [(pressed)]="bold">B</toggle>
|
|
8
|
+
<toolbar-separator />
|
|
9
|
+
<button type="button">Undo</button>
|
|
10
|
+
</toolbar>
|
|
11
|
+
--}}
|
|
12
|
+
<script lang="ts">
|
|
13
|
+
import { rovingFocus } from '@clarusjs/ui-primitives'
|
|
14
|
+
const { label, orientation = 'horizontal', loop = true } = props<{
|
|
15
|
+
/** What the toolbar is for, as its accessible name. */
|
|
16
|
+
label: string
|
|
17
|
+
/** Which arrow keys move: left and right, or up and down. */
|
|
18
|
+
orientation?: 'horizontal' | 'vertical'
|
|
19
|
+
/** From the last control on to the first, and back. */
|
|
20
|
+
loop?: boolean
|
|
21
|
+
}>()
|
|
22
|
+
const ITEMS = 'button, a[href], [role="button"], input[type="checkbox"], input[type="radio"]'
|
|
23
|
+
function wire(el: HTMLElement): () => void {
|
|
24
|
+
return rovingFocus(el, { items: ITEMS, orientation, loop })
|
|
25
|
+
}
|
|
26
|
+
</script>
|
|
27
|
+
|
|
28
|
+
<div role="toolbar" data-part="toolbar" [aria-label]="label" [aria-orientation]="orientation" [data-orientation]="orientation" [use]="wire">
|
|
29
|
+
<slot />
|
|
30
|
+
</div>
|
package/src/tooltip-trigger.tscl
CHANGED
|
@@ -3,12 +3,16 @@
|
|
|
3
3
|
(interestfor) show and hide the tooltip themselves; elsewhere this small island does: on hover (after a short delay)
|
|
4
4
|
and focus, kept open while the pointer is on the tooltip, closed on Escape, blur or leaving.
|
|
5
5
|
--}}
|
|
6
|
-
@input target: string
|
|
7
6
|
|
|
8
|
-
<script>
|
|
7
|
+
<script lang="ts">
|
|
8
|
+
const props = props<{
|
|
9
|
+
/** The id of the tooltip it describes. */
|
|
10
|
+
target: string
|
|
11
|
+
}>()
|
|
12
|
+
|
|
9
13
|
function wire(el) {
|
|
10
14
|
if ('interestForElement' in el) return // the browser does it
|
|
11
|
-
const tip = () => document.getElementById(target)
|
|
15
|
+
const tip = () => document.getElementById(props.target)
|
|
12
16
|
let timer
|
|
13
17
|
const later = (fn, ms) => {
|
|
14
18
|
clearTimeout(timer)
|
|
@@ -49,9 +53,9 @@
|
|
|
49
53
|
|
|
50
54
|
<button
|
|
51
55
|
type="button"
|
|
52
|
-
[interestfor]="target"
|
|
53
|
-
[aria-describedby]="target"
|
|
54
|
-
[style]="'anchor-name: --' + target"
|
|
56
|
+
[interestfor]="props.target"
|
|
57
|
+
[aria-describedby]="props.target"
|
|
58
|
+
[style]="'anchor-name: --' + props.target"
|
|
55
59
|
data-part="trigger"
|
|
56
60
|
[use]="wire"
|
|
57
61
|
>
|
package/src/tooltip.tscl
CHANGED
|
@@ -6,16 +6,22 @@
|
|
|
6
6
|
<tooltip-trigger target="copy-tip">Copy</tooltip-trigger>
|
|
7
7
|
<tooltip id="copy-tip">Copy the link</tooltip>
|
|
8
8
|
--}}
|
|
9
|
-
|
|
10
|
-
|
|
9
|
+
<script lang="ts">
|
|
10
|
+
const props = props<{
|
|
11
|
+
/** Its id, unique on the page: a <tooltip-trigger> targets it. */
|
|
12
|
+
id: string
|
|
13
|
+
/** Which side of its trigger it shows on (the opposite one when there's no room). */
|
|
14
|
+
side?: 'top' | 'bottom' | 'left' | 'right'
|
|
15
|
+
}>(() => ({ side: 'top' }))
|
|
16
|
+
</script>
|
|
11
17
|
|
|
12
18
|
<div
|
|
13
|
-
[id]="id"
|
|
19
|
+
[id]="props.id"
|
|
14
20
|
popover="hint"
|
|
15
21
|
role="tooltip"
|
|
16
22
|
data-part="tooltip"
|
|
17
|
-
[data-side]="side"
|
|
18
|
-
[style]="'position-anchor: --' + id + '; position-area: ' + side + '; position-try-fallbacks: flip-block, flip-inline'"
|
|
23
|
+
[data-side]="props.side"
|
|
24
|
+
[style]="'position-anchor: --' + props.id + '; position-area: ' + props.side + '; position-try-fallbacks: flip-block, flip-inline'"
|
|
19
25
|
>
|
|
20
26
|
<slot />
|
|
21
27
|
</div>
|
|
@@ -8,13 +8,12 @@ const frame = () => new Promise((done) => requestAnimationFrame(() => done(undef
|
|
|
8
8
|
/**
|
|
9
9
|
* Mark `el` open, with data-starting-style for its first frame so CSS transitions run from there. An element rendered
|
|
10
10
|
* open on the server doesn't need it (nothing to animate in).
|
|
11
|
-
* @
|
|
12
|
-
* @returns {Promise<void>} When the starting style is gone.
|
|
11
|
+
* @returns When the starting style is gone.
|
|
13
12
|
* @example
|
|
14
13
|
* @if (open) { <div class="popup" [use]="enter">…</div> }
|
|
15
14
|
* @see https://clarusjs.com/guide/ui-primitives#enter-and-exit
|
|
16
15
|
*/
|
|
17
|
-
export async function enter(el) {
|
|
16
|
+
export async function enter(el: HTMLElement): Promise<void> {
|
|
18
17
|
el.removeAttribute('data-closed')
|
|
19
18
|
el.removeAttribute('data-ending-style')
|
|
20
19
|
el.setAttribute('data-open', '')
|
|
@@ -27,11 +26,9 @@ export async function enter(el) {
|
|
|
27
26
|
/**
|
|
28
27
|
* Mark `el` closing: data-closed and data-ending-style, until its animations and transitions finish. Remove or hide
|
|
29
28
|
* it after: `await exit(el)`.
|
|
30
|
-
* @param {HTMLElement} el
|
|
31
|
-
* @returns {Promise<void>}
|
|
32
29
|
* @see https://clarusjs.com/guide/ui-primitives#enter-and-exit
|
|
33
30
|
*/
|
|
34
|
-
export async function exit(el) {
|
|
31
|
+
export async function exit(el: HTMLElement): Promise<void> {
|
|
35
32
|
el.removeAttribute('data-open')
|
|
36
33
|
el.setAttribute('data-closed', '')
|
|
37
34
|
el.setAttribute('data-ending-style', '')
|
|
@@ -2,46 +2,46 @@
|
|
|
2
2
|
// getNextMatch: the search starts after the current item, and a key repeated ("bbb") cycles through the items
|
|
3
3
|
// starting with it.
|
|
4
4
|
|
|
5
|
-
/**
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
*/
|
|
5
|
+
/** Which items typeahead() searches, by their text. */
|
|
6
|
+
export interface TypeaheadOptions {
|
|
7
|
+
/** Which descendants are the items (a selector). Default: menu items and options. */
|
|
8
|
+
items?: string
|
|
9
|
+
/** What to do with the match. Default: focus it. */
|
|
10
|
+
onMatch?: (item: HTMLElement) => void
|
|
11
|
+
/** An item's text to match. Default: its textContent. */
|
|
12
|
+
text?: (item: HTMLElement) => string
|
|
13
|
+
/** How long a pause ends the search, in ms. Default 500. */
|
|
14
|
+
timeout?: number
|
|
15
|
+
}
|
|
12
16
|
|
|
13
17
|
/**
|
|
14
18
|
* Typing a character in `el` moves to the next item whose text starts with what was typed since the last pause.
|
|
15
|
-
* @
|
|
16
|
-
* @param {TypeaheadOptions} [options]
|
|
17
|
-
* @returns {() => void} Stops it.
|
|
19
|
+
* @returns Stops it.
|
|
18
20
|
* @example
|
|
19
21
|
* <ul role="listbox" [use]="[(el) => rovingFocus(el, { orientation: 'vertical' }), (el) => typeahead(el)]">…</ul>
|
|
20
22
|
* @see https://clarusjs.com/guide/ui-primitives#typeahead
|
|
21
23
|
*/
|
|
22
|
-
export function typeahead(el, options = {}) {
|
|
24
|
+
export function typeahead(el: HTMLElement, options: TypeaheadOptions = {}): () => void {
|
|
23
25
|
const { items = '[role="menuitem"],[role="menuitemradio"],[role="menuitemcheckbox"],[role="option"],li', timeout = 500 } = options
|
|
24
|
-
const text = options.text ?? ((
|
|
25
|
-
const onMatch = options.onMatch ?? ((
|
|
26
|
+
const text = options.text ?? ((item: HTMLElement) => item.textContent?.trim() ?? '')
|
|
27
|
+
const onMatch = options.onMatch ?? ((item: HTMLElement) => item.focus())
|
|
26
28
|
let search = ''
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
/** @param {KeyboardEvent} event */
|
|
30
|
-
const keydown = (event) => {
|
|
29
|
+
let timer: ReturnType<typeof setTimeout> | undefined
|
|
30
|
+
const keydown = (event: KeyboardEvent) => {
|
|
31
31
|
if (event.key.length !== 1 || event.ctrlKey || event.metaKey || event.altKey) return
|
|
32
32
|
if (event.key === ' ' && !search) return // a space starts nothing: it activates the item
|
|
33
33
|
clearTimeout(timer)
|
|
34
34
|
timer = setTimeout(() => (search = ''), timeout)
|
|
35
35
|
search += event.key.toLowerCase()
|
|
36
|
-
const list =
|
|
37
|
-
const current = list.indexOf(
|
|
36
|
+
const list = ([...el.querySelectorAll(items)] as HTMLElement[]).filter((item) => !item.hasAttribute('disabled') && item.getAttribute('aria-disabled') !== 'true')
|
|
37
|
+
const current = list.indexOf(document.activeElement as HTMLElement)
|
|
38
38
|
// "bbb" is "b" pressed three times: cycle through the b's rather than search for "bbb".
|
|
39
39
|
const repeated = [...search].every((ch) => ch === search[0])
|
|
40
40
|
const query = repeated ? search[0] : search
|
|
41
41
|
// From the item after the current one (from the current one while still typing a longer search), around.
|
|
42
42
|
const from = repeated || current === -1 ? current + 1 : current
|
|
43
43
|
const order = list.map((_, i) => list[(from + i) % list.length])
|
|
44
|
-
const match = order.find((item) => item && text(item).toLowerCase().startsWith(
|
|
44
|
+
const match = order.find((item) => item && text(item).toLowerCase().startsWith(query as string))
|
|
45
45
|
if (!match) return
|
|
46
46
|
event.preventDefault()
|
|
47
47
|
if (match !== document.activeElement) onMatch(match)
|
|
@@ -0,0 +1,100 @@
|
|
|
1
|
+
// Virtual grid (#688): a big table or spreadsheet renders only the cells in view, rows and columns. Like
|
|
2
|
+
// virtual-list.js, it works out which ones and the app renders them; rows have one height and columns their widths
|
|
3
|
+
// (measured rows are VirtualList's). The keyboard is WAI-ARIA's grid. Not in the package's index: import
|
|
4
|
+
// '@clarusjs/ui-primitives/virtual-grid.js'.
|
|
5
|
+
import { effect } from 'clarusjs/runtime'
|
|
6
|
+
|
|
7
|
+
/** A span of rows or columns to render: from start to end (exclusive), and the px before and after it. */
|
|
8
|
+
export type Span = { start: number; end: number; before: number; after: number }
|
|
9
|
+
/** The rows and columns to render, as virtualGrid() reports them. */
|
|
10
|
+
export type GridRange = { rows: Span; cols: Span }
|
|
11
|
+
|
|
12
|
+
/** The grid's size, cell sizes, and what virtualGrid() calls as the visible range changes. */
|
|
13
|
+
export interface VirtualGridOptions {
|
|
14
|
+
/** How many rows (a signal read here updates the range). */
|
|
15
|
+
rows: () => number
|
|
16
|
+
/** How many columns. */
|
|
17
|
+
cols: number
|
|
18
|
+
/** The rows and columns to render, and the px before and after each. */
|
|
19
|
+
onRange: (range: GridRange) => void
|
|
20
|
+
/** A row's height in px. Default 36. */
|
|
21
|
+
rowSize?: number
|
|
22
|
+
/** A column's width in px, or each column's. Default 120. */
|
|
23
|
+
colSize?: number | number[]
|
|
24
|
+
/** Rows and columns rendered past each edge. Default 2. */
|
|
25
|
+
overscan?: number
|
|
26
|
+
/** The px a sticky header covers at the top, kept clear when scrolling a cell into view. */
|
|
27
|
+
stickyTop?: number
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
/**
|
|
31
|
+
* The cells from `start` to `end` (exclusive) of a run of `n` whose i-th starts at `off(i)`, in view from `from` for
|
|
32
|
+
* `length` px; searching from `first`.
|
|
33
|
+
*/
|
|
34
|
+
function span(off: (i: number) => number, n: number, from: number, length: number, overscan: number, first: number): Span {
|
|
35
|
+
first = Math.min(n, first)
|
|
36
|
+
while (first < n && off(first + 1) <= from) first++
|
|
37
|
+
let last = first
|
|
38
|
+
while (last < n && off(last) < from + length) last++
|
|
39
|
+
const start = Math.max(0, first - overscan), end = Math.min(n, last + overscan)
|
|
40
|
+
return { start, end, before: off(start), after: off(n) - off(end) }
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
/**
|
|
44
|
+
* `viewport` scrolls; cells inside it carry data-row and data-col (their place in the whole grid). ←→↑↓ move focus by
|
|
45
|
+
* cell (← → swapped right to left, by the nearest dir), Home and End to the row's ends (with Ctrl, the grid's corners), PageUp and PageDown by a screen of rows.
|
|
46
|
+
* Cells need tabindex="-1" and the viewport tabindex="0": focus on it goes to the current cell, one Tab stop.
|
|
47
|
+
* @returns Stops it.
|
|
48
|
+
*/
|
|
49
|
+
export function virtualGrid(viewport: HTMLElement, options: VirtualGridOptions): () => void {
|
|
50
|
+
const { rows, cols, onRange, rowSize = 36, colSize = 120, overscan = 2, stickyTop = 0 } = options
|
|
51
|
+
let n = 0, key = '', row = 0, col = 0
|
|
52
|
+
// Right to left (the nearest dir), columns run leftward: scrollLeft counts down from 0, and ← is the next column
|
|
53
|
+
const sign = () => ((viewport.closest('[dir]') as HTMLElement | null)?.dir === 'rtl' ? -1 : 1)
|
|
54
|
+
const left = () => Math.abs(viewport.scrollLeft)
|
|
55
|
+
const height = () => viewport.clientHeight - stickyTop
|
|
56
|
+
const y = (i: number) => i * rowSize
|
|
57
|
+
// ponytail: per-column widths are summed from the first (O(columns²) per update); fine for hundreds of columns
|
|
58
|
+
const x = (i: number) => { if (typeof colSize === 'number') return i * colSize; let w = 0; for (let k = 0; k < i; k++) w += colSize[k] ?? 0; return w }
|
|
59
|
+
const update = () => {
|
|
60
|
+
const range = {
|
|
61
|
+
rows: span(y, n, viewport.scrollTop, height(), overscan, Math.floor(viewport.scrollTop / rowSize)),
|
|
62
|
+
cols: span(x, cols, left(), viewport.clientWidth, overscan, 0),
|
|
63
|
+
}
|
|
64
|
+
const now = JSON.stringify(range)
|
|
65
|
+
if (now !== key) { key = now; onRange(range) }
|
|
66
|
+
}
|
|
67
|
+
const cell = () => viewport.querySelector(`[data-row="${row}"][data-col="${col}"]`) as HTMLElement | null
|
|
68
|
+
/** The scroll that shows a..b in a view of `length` px from `from`. */
|
|
69
|
+
const into = (a: number, b: number, from: number, length: number) => (a < from ? a : b > from + length ? b - length : from)
|
|
70
|
+
const show = () => {
|
|
71
|
+
viewport.scrollTop = into(y(row), y(row + 1), viewport.scrollTop, height())
|
|
72
|
+
viewport.scrollLeft = sign() * into(x(col), x(col + 1), left(), viewport.clientWidth)
|
|
73
|
+
update()
|
|
74
|
+
requestAnimationFrame(() => cell()?.focus())
|
|
75
|
+
}
|
|
76
|
+
const stop = effect(() => { n = rows(); update() })
|
|
77
|
+
const KEYS: Record<string, (ctrl: boolean, page: number) => void> = {
|
|
78
|
+
ArrowDown: () => row++, ArrowUp: () => row--, ArrowRight: () => { col += sign() }, ArrowLeft: () => { col -= sign() },
|
|
79
|
+
PageDown: (_, p) => { row += p }, PageUp: (_, p) => { row -= p },
|
|
80
|
+
Home: (ctrl) => { col = 0; if (ctrl) row = 0 }, End: (ctrl) => { col = cols - 1; if (ctrl) row = n - 1 },
|
|
81
|
+
}
|
|
82
|
+
const on: [string, (event: any) => void][] = [
|
|
83
|
+
['scroll', update],
|
|
84
|
+
['keydown', (event: KeyboardEvent) => {
|
|
85
|
+
const move = KEYS[event.key]
|
|
86
|
+
if (!move || !n) return
|
|
87
|
+
event.preventDefault()
|
|
88
|
+
move(event.ctrlKey, Math.max(1, Math.floor(height() / rowSize)))
|
|
89
|
+
row = Math.max(0, Math.min(n - 1, row)); col = Math.max(0, Math.min(cols - 1, col))
|
|
90
|
+
show()
|
|
91
|
+
}],
|
|
92
|
+
['focusin', (event: FocusEvent) => {
|
|
93
|
+
const t = event.target as HTMLElement
|
|
94
|
+
if (t === viewport) show() // Tab into the grid: to its current cell
|
|
95
|
+
else if (t.dataset.row) { row = Number(t.dataset.row); col = Number(t.dataset.col) }
|
|
96
|
+
}],
|
|
97
|
+
]
|
|
98
|
+
for (const [type, fn] of on) viewport.addEventListener(type, fn)
|
|
99
|
+
return () => { stop(); for (const [type, fn] of on) viewport.removeEventListener(type, fn) }
|
|
100
|
+
}
|