@clarusjs/ui-primitives 0.0.0 → 0.2.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +47 -1
- package/package.json +53 -4
- package/src/accordion-item.tscl +16 -0
- package/src/checkbox.tscl +13 -0
- package/src/combobox.tscl +208 -0
- package/src/dialog-close.tscl +4 -0
- package/src/dialog-trigger.tscl +4 -0
- package/src/dialog.tscl +19 -0
- package/src/dismiss.d.ts +27 -0
- package/src/dismiss.js +51 -0
- package/src/field.tscl +58 -0
- package/src/focus-return.d.ts +10 -0
- package/src/focus-return.js +18 -0
- package/src/index.d.ts +8 -0
- package/src/index.js +12 -0
- package/src/menu-item.tscl +8 -0
- package/src/menu-list.tscl +13 -0
- package/src/menu-trigger.tscl +15 -0
- package/src/menu.tscl +59 -0
- package/src/popover-trigger.tscl +4 -0
- package/src/popover.tscl +39 -0
- package/src/position.d.ts +36 -0
- package/src/position.js +61 -0
- package/src/radio-group.tscl +53 -0
- package/src/radio.tscl +10 -0
- package/src/resize-column.d.ts +18 -0
- package/src/resize-column.js +56 -0
- package/src/roving-focus.d.ts +39 -0
- package/src/roving-focus.js +65 -0
- package/src/select.tscl +47 -0
- package/src/sidebar-trigger.tscl +54 -0
- package/src/sidebar.d.ts +8 -0
- package/src/sidebar.js +21 -0
- package/src/sidebar.tscl +13 -0
- package/src/switch.tscl +14 -0
- package/src/tab-list.tscl +4 -0
- package/src/tab-panel.tscl +17 -0
- package/src/tab.tscl +16 -0
- package/src/tabs.tscl +59 -0
- package/src/toast-region.tscl +174 -0
- package/src/toast.d.ts +48 -0
- package/src/toast.js +56 -0
- package/src/toggle-group.tscl +48 -0
- package/src/toggle-item.tscl +16 -0
- package/src/toggle.tscl +10 -0
- package/src/tooltip-trigger.tscl +59 -0
- package/src/tooltip.tscl +21 -0
- package/src/transition.d.ts +18 -0
- package/src/transition.js +41 -0
- package/src/typeahead.d.ts +35 -0
- package/src/typeahead.js +54 -0
package/src/menu.tscl
ADDED
|
@@ -0,0 +1,59 @@
|
|
|
1
|
+
{{--
|
|
2
|
+
Menu (#465): a menu button. The list is a popover opened by its trigger (popovertarget), so opening, closing,
|
|
3
|
+
Escape and a click outside come from the browser, placed by CSS anchor positioning. This root is a small island: it
|
|
4
|
+
moves focus into the list when it opens, keeps aria-expanded in step, adds arrow keys, Home, End and typeahead, and
|
|
5
|
+
sends (select) with the chosen item's value, then closes ($info.cancel() keeps it open).
|
|
6
|
+
|
|
7
|
+
<menu id="file" (select)="run($event)">
|
|
8
|
+
<menu-trigger>File</menu-trigger>
|
|
9
|
+
<menu-list>
|
|
10
|
+
<menu-item value="new">New</menu-item>
|
|
11
|
+
<menu-item value="open">Open…</menu-item>
|
|
12
|
+
</menu-list>
|
|
13
|
+
</menu>
|
|
14
|
+
|
|
15
|
+
Give each one its own id: its trigger and list are named after it.
|
|
16
|
+
--}}
|
|
17
|
+
@input id: string
|
|
18
|
+
@event select: string
|
|
19
|
+
|
|
20
|
+
<script>
|
|
21
|
+
import { rovingFocus, typeahead } from '@clarusjs/ui-primitives'
|
|
22
|
+
provide('menu', { id })
|
|
23
|
+
// The trigger, list and items are the page's server HTML: wired here, this menu's only (not a menu nested in it).
|
|
24
|
+
function wire(el) {
|
|
25
|
+
const own = (node) => node.closest('[data-part="menu"]') === el
|
|
26
|
+
const list = [...el.querySelectorAll('[role="menu"]')].find(own)
|
|
27
|
+
const trigger = [...el.querySelectorAll('[aria-haspopup="menu"]')].find(own)
|
|
28
|
+
if (!list) return
|
|
29
|
+
// Its own items (each names its menu), not those of a menu nested in its list.
|
|
30
|
+
const items = `[role="menuitem"][data-menu="${CSS.escape(id)}"]:not([aria-disabled="true"])`
|
|
31
|
+
// Keys typed in a nested menu are that menu's: typeahead here doesn't move focus for them.
|
|
32
|
+
let nested = false
|
|
33
|
+
const mark = (event) => (nested = !own(event.target))
|
|
34
|
+
const toggle = (event) => {
|
|
35
|
+
const open = event.newState === 'open'
|
|
36
|
+
trigger?.setAttribute('aria-expanded', String(open))
|
|
37
|
+
if (open) list.querySelector(items)?.focus()
|
|
38
|
+
}
|
|
39
|
+
const click = (event) => {
|
|
40
|
+
const item = event.target.closest('[role="menuitem"]')
|
|
41
|
+
if (!item || !own(item) || item.getAttribute('aria-disabled') === 'true') return
|
|
42
|
+
if (emit('select', item.dataset.value, { reason: 'select', event })) list.hidePopover()
|
|
43
|
+
}
|
|
44
|
+
list.addEventListener('toggle', toggle)
|
|
45
|
+
list.addEventListener('click', click)
|
|
46
|
+
list.addEventListener('keydown', mark) // before typeahead's own listener
|
|
47
|
+
const stopKeys = rovingFocus(list, { items, orientation: 'vertical' })
|
|
48
|
+
const stopType = typeahead(list, { items, onMatch: (item) => nested || item.focus() })
|
|
49
|
+
return () => {
|
|
50
|
+
list.removeEventListener('toggle', toggle)
|
|
51
|
+
list.removeEventListener('click', click)
|
|
52
|
+
list.removeEventListener('keydown', mark)
|
|
53
|
+
stopKeys()
|
|
54
|
+
stopType()
|
|
55
|
+
}
|
|
56
|
+
}
|
|
57
|
+
</script>
|
|
58
|
+
|
|
59
|
+
<div data-part="menu" [use]="wire"><slot /></div>
|
package/src/popover.tscl
ADDED
|
@@ -0,0 +1,39 @@
|
|
|
1
|
+
{{--
|
|
2
|
+
Popover (#465): the popover attribute (the top layer, light dismiss: Escape or a click outside closes it), placed by
|
|
3
|
+
its <popover-trigger> with CSS anchor positioning, flipping when there's no room. No JavaScript. Style it with
|
|
4
|
+
:popover-open; data-side and data-align say where it was asked to go. position-area spans toward the end of the
|
|
5
|
+
other axis for align="start", and toward its start for "end".
|
|
6
|
+
|
|
7
|
+
<popover-trigger target="share">Share</popover-trigger>
|
|
8
|
+
<popover id="share" side="bottom" align="start">…</popover>
|
|
9
|
+
--}}
|
|
10
|
+
@input id: string
|
|
11
|
+
@input side: 'top' | 'bottom' | 'left' | 'right' = 'bottom'
|
|
12
|
+
@input align: 'start' | 'center' | 'end' = 'center'
|
|
13
|
+
|
|
14
|
+
<div
|
|
15
|
+
[id]="id"
|
|
16
|
+
popover="auto"
|
|
17
|
+
data-part="popover"
|
|
18
|
+
[data-side]="side"
|
|
19
|
+
[data-align]="align"
|
|
20
|
+
[style]="
|
|
21
|
+
'position-anchor: --' +
|
|
22
|
+
id +
|
|
23
|
+
'; position-area: ' +
|
|
24
|
+
side +
|
|
25
|
+
(align === 'center'
|
|
26
|
+
? ''
|
|
27
|
+
: ' ' +
|
|
28
|
+
(side === 'top' || side === 'bottom'
|
|
29
|
+
? align === 'start'
|
|
30
|
+
? 'span-right'
|
|
31
|
+
: 'span-left'
|
|
32
|
+
: align === 'start'
|
|
33
|
+
? 'span-bottom'
|
|
34
|
+
: 'span-top')) +
|
|
35
|
+
'; position-try-fallbacks: flip-block, flip-inline'
|
|
36
|
+
"
|
|
37
|
+
>
|
|
38
|
+
<slot />
|
|
39
|
+
</div>
|
|
@@ -0,0 +1,36 @@
|
|
|
1
|
+
export type PositionOptions = {
|
|
2
|
+
/**
|
|
3
|
+
* Which side of the anchor. Default 'bottom'.
|
|
4
|
+
*/
|
|
5
|
+
side?: 'top' | 'bottom' | 'left' | 'right';
|
|
6
|
+
/**
|
|
7
|
+
* Along that side. Default 'center'.
|
|
8
|
+
*/
|
|
9
|
+
align?: 'start' | 'center' | 'end';
|
|
10
|
+
/**
|
|
11
|
+
* Gap from the anchor, in px. Default 4.
|
|
12
|
+
*/
|
|
13
|
+
offset?: number;
|
|
14
|
+
/**
|
|
15
|
+
* The other side when there's no room on this one. Default true.
|
|
16
|
+
*/
|
|
17
|
+
flip?: boolean;
|
|
18
|
+
};
|
|
19
|
+
/**
|
|
20
|
+
* @typedef {object} PositionOptions
|
|
21
|
+
* @property {'top' | 'bottom' | 'left' | 'right'} [side] Which side of the anchor. Default 'bottom'.
|
|
22
|
+
* @property {'start' | 'center' | 'end'} [align] Along that side. Default 'center'.
|
|
23
|
+
* @property {number} [offset] Gap from the anchor, in px. Default 4.
|
|
24
|
+
* @property {boolean} [flip] The other side when there's no room on this one. Default true.
|
|
25
|
+
*/
|
|
26
|
+
/**
|
|
27
|
+
* Position `floating` (fixed) by `anchor`, now and on scroll and resize.
|
|
28
|
+
* @param {HTMLElement} floating
|
|
29
|
+
* @param {Element} anchor
|
|
30
|
+
* @param {PositionOptions} [options]
|
|
31
|
+
* @returns {() => void} Stops following.
|
|
32
|
+
* @example
|
|
33
|
+
* @if (open) { <div [use]="(el) => position(el, trigger)">…</div> }
|
|
34
|
+
* @see https://clarusjs.com/guide/ui-primitives#position
|
|
35
|
+
*/
|
|
36
|
+
export declare function position(floating: HTMLElement, anchor: Element, options?: PositionOptions): () => void;
|
package/src/position.js
ADDED
|
@@ -0,0 +1,61 @@
|
|
|
1
|
+
// position: place a floating element (a popover, a menu, a tooltip) next to its anchor, inside the window. Sets Base
|
|
2
|
+
// UI's data-side and data-align and CSS variables for styling: --anchor-width, --anchor-height, --available-width,
|
|
3
|
+
// --available-height, --transform-origin.
|
|
4
|
+
// ponytail: JavaScript always; CSS anchor positioning (position-anchor, position-try) where supported would need none.
|
|
5
|
+
// Add it when a part can say all it needs in CSS.
|
|
6
|
+
|
|
7
|
+
/**
|
|
8
|
+
* @typedef {object} PositionOptions
|
|
9
|
+
* @property {'top' | 'bottom' | 'left' | 'right'} [side] Which side of the anchor. Default 'bottom'.
|
|
10
|
+
* @property {'start' | 'center' | 'end'} [align] Along that side. Default 'center'.
|
|
11
|
+
* @property {number} [offset] Gap from the anchor, in px. Default 4.
|
|
12
|
+
* @property {boolean} [flip] The other side when there's no room on this one. Default true.
|
|
13
|
+
*/
|
|
14
|
+
|
|
15
|
+
/**
|
|
16
|
+
* Position `floating` (fixed) by `anchor`, now and on scroll and resize.
|
|
17
|
+
* @param {HTMLElement} floating
|
|
18
|
+
* @param {Element} anchor
|
|
19
|
+
* @param {PositionOptions} [options]
|
|
20
|
+
* @returns {() => void} Stops following.
|
|
21
|
+
* @example
|
|
22
|
+
* @if (open) { <div [use]="(el) => position(el, trigger)">…</div> }
|
|
23
|
+
* @see https://clarusjs.com/guide/ui-primitives#position
|
|
24
|
+
*/
|
|
25
|
+
export function position(floating, anchor, options = {}) {
|
|
26
|
+
const { align = 'center', offset = 4, flip = true } = options
|
|
27
|
+
const opposite = /** @type {const} */ ({ top: 'bottom', bottom: 'top', left: 'right', right: 'left' })
|
|
28
|
+
const place = () => {
|
|
29
|
+
const a = anchor.getBoundingClientRect()
|
|
30
|
+
const f = floating.getBoundingClientRect()
|
|
31
|
+
const vw = window.innerWidth
|
|
32
|
+
const vh = window.innerHeight
|
|
33
|
+
const room = { top: a.top - offset, bottom: vh - a.bottom - offset, left: a.left - offset, right: vw - a.right - offset }
|
|
34
|
+
let side = options.side ?? 'bottom'
|
|
35
|
+
const size = side === 'top' || side === 'bottom' ? f.height : f.width
|
|
36
|
+
if (flip && room[side] < size && room[opposite[side]] > room[side]) side = opposite[side]
|
|
37
|
+
const vertical = side === 'top' || side === 'bottom'
|
|
38
|
+
const along = (start = 0, length = 0, own = 0) => (align === 'start' ? start : align === 'end' ? start + length - own : start + (length - own) / 2)
|
|
39
|
+
const top = side === 'bottom' ? a.bottom + offset : side === 'top' ? a.top - offset - f.height : along(a.top, a.height, f.height)
|
|
40
|
+
const left = side === 'right' ? a.right + offset : side === 'left' ? a.left - offset - f.width : along(a.left, a.width, f.width)
|
|
41
|
+
const s = floating.style
|
|
42
|
+
s.position = 'fixed'
|
|
43
|
+
s.top = `${Math.round(top)}px`
|
|
44
|
+
s.left = `${Math.round(Math.min(Math.max(left, 0), Math.max(vw - f.width, 0)))}px`
|
|
45
|
+
floating.dataset.side = side
|
|
46
|
+
floating.dataset.align = align
|
|
47
|
+
s.setProperty('--anchor-width', `${a.width}px`)
|
|
48
|
+
s.setProperty('--anchor-height', `${a.height}px`)
|
|
49
|
+
s.setProperty('--available-height', `${Math.max(vertical ? room[side] : vh, 0)}px`)
|
|
50
|
+
s.setProperty('--available-width', `${Math.max(vertical ? vw : room[side], 0)}px`)
|
|
51
|
+
s.setProperty('--transform-origin', vertical ? `${align === 'start' ? 'left' : align === 'end' ? 'right' : 'center'} ${opposite[side]}` : `${opposite[side]} center`)
|
|
52
|
+
}
|
|
53
|
+
place()
|
|
54
|
+
const options_ = { passive: true, capture: true }
|
|
55
|
+
window.addEventListener('scroll', place, options_)
|
|
56
|
+
window.addEventListener('resize', place, options_)
|
|
57
|
+
return () => {
|
|
58
|
+
window.removeEventListener('scroll', place, options_)
|
|
59
|
+
window.removeEventListener('resize', place, options_)
|
|
60
|
+
}
|
|
61
|
+
}
|
|
@@ -0,0 +1,53 @@
|
|
|
1
|
+
{{--
|
|
2
|
+
Radio group (#465): native radios in a fieldset, so the arrow keys, one Tab stop and form submission come from the
|
|
3
|
+
browser. The group is a small island; <radio>s have no script and read the group's name and value from context, so
|
|
4
|
+
the server writes which one is checked. Bind the value with [(value)]; (change) comes first with the new value, and
|
|
5
|
+
$info.cancel() keeps the current one.
|
|
6
|
+
|
|
7
|
+
<radio-group name="size" label="Size" [(value)]="size">
|
|
8
|
+
<radio value="s" label="Small" />
|
|
9
|
+
<radio value="m" label="Medium" />
|
|
10
|
+
</radio-group>
|
|
11
|
+
--}}
|
|
12
|
+
@input name: string
|
|
13
|
+
@input label: string
|
|
14
|
+
@model value: string = ''
|
|
15
|
+
@event change: string
|
|
16
|
+
|
|
17
|
+
<script>
|
|
18
|
+
import { effect } from 'clarusjs/runtime'
|
|
19
|
+
provide('radio', { name, value })
|
|
20
|
+
// The radios are the page's server HTML: kept in step with value here, and their changes go through (change).
|
|
21
|
+
function wire(el) {
|
|
22
|
+
const own = (node) => node.closest('[data-part="radio-group"]') === el
|
|
23
|
+
const change = (event) => {
|
|
24
|
+
const input = event.target
|
|
25
|
+
if (input.type !== 'radio' || !own(input)) return
|
|
26
|
+
if (emit('change', input.value, { reason: 'select', event })) value = input.value
|
|
27
|
+
else sync(value) // cancelled: the previous one is checked again
|
|
28
|
+
}
|
|
29
|
+
const sync = (selected) => {
|
|
30
|
+
for (const input of el.querySelectorAll('input[type="radio"]')) {
|
|
31
|
+
if (!own(input)) continue
|
|
32
|
+
input.checked = input.value === selected
|
|
33
|
+
input.closest('[data-part="radio"]')?.toggleAttribute('data-checked', input.checked)
|
|
34
|
+
}
|
|
35
|
+
}
|
|
36
|
+
// A form reset checks the radios it started with, without a change event: value says which one is checked. Heard
|
|
37
|
+
// on the document, so it follows the group into another form (a fragment swap can keep it in a new one).
|
|
38
|
+
const reset = (event) => event.target === el.form && setTimeout(() => sync(value))
|
|
39
|
+
el.addEventListener('change', change)
|
|
40
|
+
document.addEventListener('reset', reset)
|
|
41
|
+
const stop = effect(() => sync(value))
|
|
42
|
+
return () => {
|
|
43
|
+
el.removeEventListener('change', change)
|
|
44
|
+
document.removeEventListener('reset', reset)
|
|
45
|
+
stop()
|
|
46
|
+
}
|
|
47
|
+
}
|
|
48
|
+
</script>
|
|
49
|
+
|
|
50
|
+
<fieldset data-part="radio-group" [use]="wire">
|
|
51
|
+
<legend data-part="legend">{{ label }}</legend>
|
|
52
|
+
<slot />
|
|
53
|
+
</fieldset>
|
package/src/radio.tscl
ADDED
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
{{-- Radio (#465): one native radio and its label, inside <radio-group>, which names it and says whether it's checked. --}}
|
|
2
|
+
@context radio: { name: string; value: string }
|
|
3
|
+
@input value: string
|
|
4
|
+
@input label: string
|
|
5
|
+
@input disabled: boolean = false
|
|
6
|
+
|
|
7
|
+
<label data-part="radio" [data-checked]="value === radio.value ? '' : false" [data-disabled]="disabled ? '' : false">
|
|
8
|
+
<input type="radio" [name]="radio.name" [value]="value" [checked]="value === radio.value" [disabled]="disabled" />
|
|
9
|
+
<span data-part="label">{{ label }}</span>
|
|
10
|
+
</label>
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Make `handle` (inside a `<th>`) a vertical separator that resizes its column: drag it, or focus it and use the
|
|
3
|
+
* arrow keys (by `step`), Home and End (to `min` and `max`). Right to left, the arrows and the drag follow the line.
|
|
4
|
+
* @param {HTMLElement} handle
|
|
5
|
+
* @param {{ min?: number, max?: number, step?: number, target?: HTMLElement, onResize?: (width: number) => void }} [options]
|
|
6
|
+
* `target`: the element to size (the handle's `<th>` unless you say).
|
|
7
|
+
* @returns {() => void}
|
|
8
|
+
* @example
|
|
9
|
+
* <th>Name <span [use]="(el) => resizeColumn(el, { onResize: (w) => save('name', w) })" aria-label="Resize Name"></span></th>
|
|
10
|
+
* @see https://clarusjs.com/guide/ui-primitives#resizecolumn
|
|
11
|
+
*/
|
|
12
|
+
export declare function resizeColumn(handle: HTMLElement, { min, max, step, target, onResize }?: {
|
|
13
|
+
min?: number;
|
|
14
|
+
max?: number;
|
|
15
|
+
step?: number;
|
|
16
|
+
target?: HTMLElement;
|
|
17
|
+
onResize?: (width: number) => void;
|
|
18
|
+
}): () => void;
|
|
@@ -0,0 +1,56 @@
|
|
|
1
|
+
// resizeColumn: a table column's resize handle, dragged or moved with the keyboard (after TanStack Table's column
|
|
2
|
+
// resizing). It sets the column's width as it goes; onResize gets the width to keep (at the end of a drag).
|
|
3
|
+
|
|
4
|
+
/**
|
|
5
|
+
* Make `handle` (inside a `<th>`) a vertical separator that resizes its column: drag it, or focus it and use the
|
|
6
|
+
* arrow keys (by `step`), Home and End (to `min` and `max`). Right to left, the arrows and the drag follow the line.
|
|
7
|
+
* @param {HTMLElement} handle
|
|
8
|
+
* @param {{ min?: number, max?: number, step?: number, target?: HTMLElement, onResize?: (width: number) => void }} [options]
|
|
9
|
+
* `target`: the element to size (the handle's `<th>` unless you say).
|
|
10
|
+
* @returns {() => void}
|
|
11
|
+
* @example
|
|
12
|
+
* <th>Name <span [use]="(el) => resizeColumn(el, { onResize: (w) => save('name', w) })" aria-label="Resize Name"></span></th>
|
|
13
|
+
* @see https://clarusjs.com/guide/ui-primitives#resizecolumn
|
|
14
|
+
*/
|
|
15
|
+
export function resizeColumn(handle, { min = 48, max = 1e4, step = 16, target, onResize } = {}) {
|
|
16
|
+
const col = /** @type {HTMLElement} */ (target ?? handle.closest('th'))
|
|
17
|
+
const sign = () => (handle.closest('[dir]')?.getAttribute('dir') === 'rtl' ? -1 : 1)
|
|
18
|
+
let width = Math.round(col.getBoundingClientRect().width)
|
|
19
|
+
let from = 0
|
|
20
|
+
let start = NaN
|
|
21
|
+
const attrs = { role: 'separator', 'aria-orientation': 'vertical', tabindex: '0', 'aria-valuemin': min, 'aria-valuemax': max }
|
|
22
|
+
for (const [k, v] of Object.entries(attrs)) if (!handle.hasAttribute(k)) handle.setAttribute(k, String(v))
|
|
23
|
+
const set = (/** @type {number} */ w, /** @type {boolean} */ done) => {
|
|
24
|
+
width = Math.round(Math.min(max, Math.max(min, w)))
|
|
25
|
+
col.style.width = width + 'px'
|
|
26
|
+
handle.setAttribute('aria-valuenow', String(width))
|
|
27
|
+
if (done) onResize?.(width)
|
|
28
|
+
}
|
|
29
|
+
handle.setAttribute('aria-valuenow', String(width))
|
|
30
|
+
/** @param {PointerEvent} e */
|
|
31
|
+
const down = (e) => {
|
|
32
|
+
if (e.button) return
|
|
33
|
+
e.preventDefault()
|
|
34
|
+
start = e.clientX
|
|
35
|
+
from = width
|
|
36
|
+
handle.setPointerCapture?.(e.pointerId)
|
|
37
|
+
}
|
|
38
|
+
/** @param {PointerEvent} e */
|
|
39
|
+
const move = (e) => start === start && set(from + (e.clientX - start) * sign(), false)
|
|
40
|
+
const up = () => {
|
|
41
|
+
if (start !== start) return
|
|
42
|
+
start = NaN
|
|
43
|
+
set(width, true)
|
|
44
|
+
}
|
|
45
|
+
/** @param {KeyboardEvent} e */
|
|
46
|
+
const key = (e) => {
|
|
47
|
+
const by = { ArrowRight: width + step * sign(), ArrowLeft: width - step * sign(), Home: min, End: max }[e.key]
|
|
48
|
+
if (by === undefined) return
|
|
49
|
+
e.preventDefault()
|
|
50
|
+
set(by, true)
|
|
51
|
+
}
|
|
52
|
+
/** @type {[string, (e: any) => void][]} */
|
|
53
|
+
const on = [['pointerdown', down], ['pointermove', move], ['pointerup', up], ['pointercancel', up], ['keydown', key]]
|
|
54
|
+
for (const [t, f] of on) handle.addEventListener(t, f)
|
|
55
|
+
return () => { for (const [t, f] of on) handle.removeEventListener(t, f) }
|
|
56
|
+
}
|
|
@@ -0,0 +1,39 @@
|
|
|
1
|
+
/** Items that can't take focus: disabled, or aria-disabled. */
|
|
2
|
+
export declare const disabled: (/** @type {Element} */ el: Element) => boolean;
|
|
3
|
+
export type RovingFocusOptions = {
|
|
4
|
+
/**
|
|
5
|
+
* Which descendants are the items (a selector). Default: tabs, menu items, options, buttons.
|
|
6
|
+
*/
|
|
7
|
+
items?: string;
|
|
8
|
+
/**
|
|
9
|
+
* Which arrows move. Default 'horizontal'.
|
|
10
|
+
*/
|
|
11
|
+
orientation?: 'horizontal' | 'vertical' | 'both';
|
|
12
|
+
/**
|
|
13
|
+
* From the last item on to the first, and back. Default true.
|
|
14
|
+
*/
|
|
15
|
+
loop?: boolean;
|
|
16
|
+
/**
|
|
17
|
+
* Called with the item an arrow key moved to (after focusing it).
|
|
18
|
+
*/
|
|
19
|
+
onMove?: (item: HTMLElement) => void;
|
|
20
|
+
};
|
|
21
|
+
/**
|
|
22
|
+
* @typedef {object} RovingFocusOptions
|
|
23
|
+
* @property {string} [items] Which descendants are the items (a selector). Default: tabs, menu items, options, buttons.
|
|
24
|
+
* @property {'horizontal' | 'vertical' | 'both'} [orientation] Which arrows move. Default 'horizontal'.
|
|
25
|
+
* @property {boolean} [loop] From the last item on to the first, and back. Default true.
|
|
26
|
+
* @property {(item: HTMLElement) => void} [onMove] Called with the item an arrow key moved to (after focusing it).
|
|
27
|
+
*/
|
|
28
|
+
/**
|
|
29
|
+
* One Tab stop for the group `el`: its current item has tabindex 0, the others -1; arrow keys (by orientation, flipped
|
|
30
|
+
* in right-to-left text), Home and End move focus between the enabled items. The current item is the one focused last,
|
|
31
|
+
* else one marked aria-selected, aria-checked, aria-pressed, aria-current or data-highlighted, else the first enabled one.
|
|
32
|
+
* @param {HTMLElement} el
|
|
33
|
+
* @param {RovingFocusOptions} [options]
|
|
34
|
+
* @returns {() => void} Stops it.
|
|
35
|
+
* @example
|
|
36
|
+
* <div role="toolbar" [use]="(el) => rovingFocus(el)">…</div>
|
|
37
|
+
* @see https://clarusjs.com/guide/ui-primitives#rovingfocus
|
|
38
|
+
*/
|
|
39
|
+
export declare function rovingFocus(el: HTMLElement, options?: RovingFocusOptions): () => void;
|
|
@@ -0,0 +1,65 @@
|
|
|
1
|
+
// rovingFocus: one Tab stop for a group (a toolbar, tabs, a menu, a listbox), arrow keys between its items.
|
|
2
|
+
// Ported from Radix's RovingFocusGroup and Base UI's composite: getFocusIntent's key map, RTL, disabled items skipped.
|
|
3
|
+
|
|
4
|
+
/** Items that can't take focus: disabled, or aria-disabled. */
|
|
5
|
+
export const disabled = (/** @type {Element} */ el) => el.hasAttribute('disabled') || el.getAttribute('aria-disabled') === 'true'
|
|
6
|
+
|
|
7
|
+
/**
|
|
8
|
+
* @typedef {object} RovingFocusOptions
|
|
9
|
+
* @property {string} [items] Which descendants are the items (a selector). Default: tabs, menu items, options, buttons.
|
|
10
|
+
* @property {'horizontal' | 'vertical' | 'both'} [orientation] Which arrows move. Default 'horizontal'.
|
|
11
|
+
* @property {boolean} [loop] From the last item on to the first, and back. Default true.
|
|
12
|
+
* @property {(item: HTMLElement) => void} [onMove] Called with the item an arrow key moved to (after focusing it).
|
|
13
|
+
*/
|
|
14
|
+
|
|
15
|
+
/**
|
|
16
|
+
* One Tab stop for the group `el`: its current item has tabindex 0, the others -1; arrow keys (by orientation, flipped
|
|
17
|
+
* in right-to-left text), Home and End move focus between the enabled items. The current item is the one focused last,
|
|
18
|
+
* else one marked aria-selected, aria-checked, aria-pressed, aria-current or data-highlighted, else the first enabled one.
|
|
19
|
+
* @param {HTMLElement} el
|
|
20
|
+
* @param {RovingFocusOptions} [options]
|
|
21
|
+
* @returns {() => void} Stops it.
|
|
22
|
+
* @example
|
|
23
|
+
* <div role="toolbar" [use]="(el) => rovingFocus(el)">…</div>
|
|
24
|
+
* @see https://clarusjs.com/guide/ui-primitives#rovingfocus
|
|
25
|
+
*/
|
|
26
|
+
export function rovingFocus(el, options = {}) {
|
|
27
|
+
const { items = '[role="tab"],[role="menuitem"],[role="menuitemradio"],[role="menuitemcheckbox"],[role="option"],button', orientation = 'horizontal', loop = true, onMove } = options
|
|
28
|
+
const all = () => /** @type {HTMLElement[]} */ ([...el.querySelectorAll(items)])
|
|
29
|
+
const enabled = () => all().filter((item) => !disabled(item))
|
|
30
|
+
/** @param {HTMLElement | undefined} current */
|
|
31
|
+
const settle = (current) => { for (const item of all()) item.tabIndex = item === current ? 0 : -1 }
|
|
32
|
+
const marked = all().find((item) => !disabled(item) && ['aria-selected', 'aria-checked', 'aria-pressed', 'aria-current', 'data-highlighted'].some((a) => item.hasAttribute(a) && item.getAttribute(a) !== 'false'))
|
|
33
|
+
settle(marked ?? enabled()[0])
|
|
34
|
+
|
|
35
|
+
/** @param {KeyboardEvent} event */
|
|
36
|
+
const keydown = (event) => {
|
|
37
|
+
const list = enabled()
|
|
38
|
+
const at = list.indexOf(/** @type {HTMLElement} */ (event.target))
|
|
39
|
+
if (at === -1) return
|
|
40
|
+
const rtl = getComputedStyle(el).direction === 'rtl' || el.closest('[dir]')?.getAttribute('dir') === 'rtl'
|
|
41
|
+
const keys = {
|
|
42
|
+
next: [...(orientation !== 'vertical' ? [rtl ? 'ArrowLeft' : 'ArrowRight'] : []), ...(orientation !== 'horizontal' ? ['ArrowDown'] : [])],
|
|
43
|
+
prev: [...(orientation !== 'vertical' ? [rtl ? 'ArrowRight' : 'ArrowLeft'] : []), ...(orientation !== 'horizontal' ? ['ArrowUp'] : [])],
|
|
44
|
+
}
|
|
45
|
+
let to = -1
|
|
46
|
+
if (event.key === 'Home') to = 0
|
|
47
|
+
else if (event.key === 'End') to = list.length - 1
|
|
48
|
+
else if (keys.next.includes(event.key)) to = loop ? (at + 1) % list.length : Math.min(at + 1, list.length - 1)
|
|
49
|
+
else if (keys.prev.includes(event.key)) to = loop ? (at - 1 + list.length) % list.length : Math.max(at - 1, 0)
|
|
50
|
+
if (to === -1) return
|
|
51
|
+
event.preventDefault()
|
|
52
|
+
const item = list[to]
|
|
53
|
+
settle(item)
|
|
54
|
+
item?.focus()
|
|
55
|
+
if (item && onMove) onMove(item)
|
|
56
|
+
}
|
|
57
|
+
/** @param {FocusEvent} event */
|
|
58
|
+
const focusin = (event) => { if (all().includes(/** @type {HTMLElement} */ (event.target))) settle(/** @type {HTMLElement} */ (event.target)) }
|
|
59
|
+
el.addEventListener('keydown', keydown)
|
|
60
|
+
el.addEventListener('focusin', focusin)
|
|
61
|
+
return () => {
|
|
62
|
+
el.removeEventListener('keydown', keydown)
|
|
63
|
+
el.removeEventListener('focusin', focusin)
|
|
64
|
+
}
|
|
65
|
+
}
|
package/src/select.tscl
ADDED
|
@@ -0,0 +1,47 @@
|
|
|
1
|
+
{{--
|
|
2
|
+
Select (#466): a native <select>, so it works with the keyboard, on phones and in forms as is. Where the browser
|
|
3
|
+
supports it, it's the customizable select (appearance: base-select): style its button, ::picker(select) and options
|
|
4
|
+
with CSS; elsewhere, the usual native one. Bind it two ways: <select-field id="size" label="Size" [options]="sizes"
|
|
5
|
+
[(value)]="form.size" />. It sends (change) with the new value.
|
|
6
|
+
--}}
|
|
7
|
+
@type Option = { value: string; label: string; disabled?: boolean }
|
|
8
|
+
@input id: string
|
|
9
|
+
@input label: string
|
|
10
|
+
@input options: Option[]
|
|
11
|
+
@input name: string = ''
|
|
12
|
+
@input disabled: boolean = false
|
|
13
|
+
@input placeholder: string = ''
|
|
14
|
+
@model value: string = ''
|
|
15
|
+
@event change: string
|
|
16
|
+
|
|
17
|
+
<script>
|
|
18
|
+
function changed(event) {
|
|
19
|
+
emit('change', event.target.value, { reason: 'select', event })
|
|
20
|
+
}
|
|
21
|
+
</script>
|
|
22
|
+
|
|
23
|
+
<div data-part="select">
|
|
24
|
+
<label [for]="id" data-part="label">{{ label }}</label>
|
|
25
|
+
<select
|
|
26
|
+
[id]="id"
|
|
27
|
+
[name]="name || false"
|
|
28
|
+
[disabled]="disabled"
|
|
29
|
+
[(value)]="value"
|
|
30
|
+
(change)="changed($event)"
|
|
31
|
+
data-part="control"
|
|
32
|
+
>
|
|
33
|
+
@if (placeholder) {
|
|
34
|
+
<option value="" disabled>{{ placeholder }}</option>
|
|
35
|
+
}
|
|
36
|
+
@for (o of options; track o.value) {
|
|
37
|
+
<option [value]="o.value" [disabled]="o.disabled ?? false" data-part="option">{{ o.label }}</option>
|
|
38
|
+
}
|
|
39
|
+
</select>
|
|
40
|
+
</div>
|
|
41
|
+
|
|
42
|
+
<style scoped>
|
|
43
|
+
select,
|
|
44
|
+
::picker(select) {
|
|
45
|
+
appearance: base-select;
|
|
46
|
+
}
|
|
47
|
+
</style>
|
|
@@ -0,0 +1,54 @@
|
|
|
1
|
+
{{--
|
|
2
|
+
Sidebar trigger: collapses the sidebar on a wide screen (min-width 768px); on a phone it is the sidebar's popover
|
|
3
|
+
invoker (popovertarget), so the browser opens it next to the button in the Tab order. aria-expanded follows both.
|
|
4
|
+
--}}
|
|
5
|
+
@input target: string
|
|
6
|
+
|
|
7
|
+
<script>
|
|
8
|
+
import { toggleSidebar } from '@clarusjs/ui-primitives'
|
|
9
|
+
const WIDE = '(min-width: 768px)'
|
|
10
|
+
let self // the button, from wire (events are delegated to the island, so currentTarget isn't it)
|
|
11
|
+
function toggle(event) {
|
|
12
|
+
const nav = document.getElementById(target)
|
|
13
|
+
if (!(nav instanceof HTMLElement) || !matchMedia(WIDE).matches) return // a phone: the popover toggles itself
|
|
14
|
+
event.preventDefault()
|
|
15
|
+
const next = toggleSidebar(nav)
|
|
16
|
+
self?.setAttribute('aria-expanded', String(next === 'open'))
|
|
17
|
+
}
|
|
18
|
+
function wire(button) {
|
|
19
|
+
self = button
|
|
20
|
+
const nav = document.getElementById(target)
|
|
21
|
+
if (!(nav instanceof HTMLElement)) return
|
|
22
|
+
const media = matchMedia(WIDE)
|
|
23
|
+
const sync = () => {
|
|
24
|
+
const open = media.matches
|
|
25
|
+
? (document.documentElement.dataset.sidebar ?? nav.dataset.state) !== 'collapsed'
|
|
26
|
+
: nav.matches(':popover-open')
|
|
27
|
+
button.setAttribute('aria-expanded', String(open))
|
|
28
|
+
}
|
|
29
|
+
// Widened while open as a phone panel: close it, so it doesn't float over the page.
|
|
30
|
+
const resized = () => {
|
|
31
|
+
if (media.matches && nav.matches(':popover-open')) nav.hidePopover()
|
|
32
|
+
sync()
|
|
33
|
+
}
|
|
34
|
+
nav.addEventListener('toggle', sync)
|
|
35
|
+
media.addEventListener?.('change', resized)
|
|
36
|
+
sync()
|
|
37
|
+
return () => {
|
|
38
|
+
nav.removeEventListener('toggle', sync)
|
|
39
|
+
media.removeEventListener?.('change', resized)
|
|
40
|
+
}
|
|
41
|
+
}
|
|
42
|
+
</script>
|
|
43
|
+
|
|
44
|
+
<button
|
|
45
|
+
type="button"
|
|
46
|
+
[aria-controls]="target"
|
|
47
|
+
[popovertarget]="target"
|
|
48
|
+
aria-expanded="true"
|
|
49
|
+
data-part="sidebar-trigger"
|
|
50
|
+
[use]="wire"
|
|
51
|
+
(click)="toggle($event)"
|
|
52
|
+
>
|
|
53
|
+
<slot />
|
|
54
|
+
</button>
|
package/src/sidebar.d.ts
ADDED
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
export declare const SIDEBAR_KEY = "ui-sidebar";
|
|
2
|
+
export declare const SIDEBAR_SCRIPT: string;
|
|
3
|
+
/**
|
|
4
|
+
* Collapse or reopen the sidebar on a wide screen, and remember it.
|
|
5
|
+
* @param {HTMLElement | null} nav the sidebar (its data-state follows)
|
|
6
|
+
* @returns {'open' | 'collapsed'} the new state
|
|
7
|
+
*/
|
|
8
|
+
export declare function toggleSidebar(nav: HTMLElement | null): 'open' | 'collapsed';
|
package/src/sidebar.js
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
// The sidebar's collapsed state, on <html data-sidebar="open|collapsed">, remembered in localStorage. SIDEBAR_SCRIPT
|
|
2
|
+
// goes inline in the <head> so a remembered collapse applies before the first paint (no jump on load).
|
|
3
|
+
|
|
4
|
+
export const SIDEBAR_KEY = 'ui-sidebar'
|
|
5
|
+
|
|
6
|
+
export const SIDEBAR_SCRIPT = `try{var v=localStorage.getItem(${JSON.stringify(SIDEBAR_KEY)});if(v==="collapsed"||v==="open")document.documentElement.setAttribute("data-sidebar",v)}catch(e){}`
|
|
7
|
+
|
|
8
|
+
/**
|
|
9
|
+
* Collapse or reopen the sidebar on a wide screen, and remember it.
|
|
10
|
+
* @param {HTMLElement | null} nav the sidebar (its data-state follows)
|
|
11
|
+
* @returns {'open' | 'collapsed'} the new state
|
|
12
|
+
*/
|
|
13
|
+
export function toggleSidebar(nav) {
|
|
14
|
+
const root = document.documentElement
|
|
15
|
+
const current = root.dataset.sidebar ?? nav?.dataset.state ?? 'open'
|
|
16
|
+
const next = current === 'collapsed' ? 'open' : 'collapsed'
|
|
17
|
+
root.dataset.sidebar = next
|
|
18
|
+
if (nav) nav.dataset.state = next
|
|
19
|
+
try { localStorage.setItem(SIDEBAR_KEY, next) } catch {}
|
|
20
|
+
return next
|
|
21
|
+
}
|
package/src/sidebar.tscl
ADDED
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
{{--
|
|
2
|
+
Sidebar: the app's navigation beside the page. On a wide screen it sits inline and <sidebar-trigger> collapses it
|
|
3
|
+
(remembered; put SIDEBAR_SCRIPT from @clarusjs/ui-primitives in your <head> so a collapse applies before paint).
|
|
4
|
+
On a phone it's a popover the trigger opens: a click outside and Escape close it. No script of its own.
|
|
5
|
+
|
|
6
|
+
<sidebar id="app-nav" label="Main">…links…</sidebar>
|
|
7
|
+
<sidebar-trigger target="app-nav">Menu</sidebar-trigger>
|
|
8
|
+
--}}
|
|
9
|
+
@input id: string
|
|
10
|
+
@input label: string = 'Sidebar'
|
|
11
|
+
@input collapsed: boolean = false
|
|
12
|
+
|
|
13
|
+
<nav [id]="id" [aria-label]="label" popover="auto" data-part="sidebar" [data-state]="collapsed ? 'collapsed' : 'open'"><slot /></nav>
|
package/src/switch.tscl
ADDED
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
{{--
|
|
2
|
+
Switch (#465): a native checkbox with role="switch" inside its label, so it submits with its form and works with the
|
|
3
|
+
keyboard as is. Bind it two ways: <switch label="Wi-Fi" [(checked)]="settings.wifi" />. data-checked and
|
|
4
|
+
data-disabled on the label are for styling.
|
|
5
|
+
--}}
|
|
6
|
+
@input label: string
|
|
7
|
+
@input name: string = ''
|
|
8
|
+
@input disabled: boolean = false
|
|
9
|
+
@model checked: boolean = false
|
|
10
|
+
|
|
11
|
+
<label data-part="switch" [data-checked]="checked ? '' : false" [data-disabled]="disabled ? '' : false">
|
|
12
|
+
<input type="checkbox" role="switch" [name]="name || false" [disabled]="disabled" [(checked)]="checked" />
|
|
13
|
+
<span data-part="label">{{ label }}</span>
|
|
14
|
+
</label>
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
{{--
|
|
2
|
+
Tab panel (#465): what one <tab> shows, inside <tabs>; [value] matches its tab. The server renders it hidden unless
|
|
3
|
+
it's the selected one, so nothing jumps when the page loads.
|
|
4
|
+
--}}
|
|
5
|
+
@context tabs: { id: string; value: string }
|
|
6
|
+
@input value: string
|
|
7
|
+
|
|
8
|
+
<div
|
|
9
|
+
role="tabpanel"
|
|
10
|
+
[id]="tabs.id + '-panel-' + value"
|
|
11
|
+
[aria-labelledby]="tabs.id + '-tab-' + value"
|
|
12
|
+
tabindex="0"
|
|
13
|
+
[hidden]="value !== tabs.value"
|
|
14
|
+
data-part="panel"
|
|
15
|
+
>
|
|
16
|
+
<slot />
|
|
17
|
+
</div>
|