@clarusjs/ui-primitives 0.0.0 → 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/README.md +47 -1
- package/package.json +49 -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 +53 -0
- package/src/focus-return.d.ts +10 -0
- package/src/focus-return.js +18 -0
- package/src/index.d.ts +6 -0
- package/src/index.js +8 -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/roving-focus.d.ts +39 -0
- package/src/roving-focus.js +65 -0
- package/src/select.tscl +47 -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/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,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-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-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-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>
|
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>
|
package/src/tab.tscl
ADDED
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
{{-- Tab (#465): one tab inside <tab-list>; [value] names the <tab-panel> it shows. Selected state comes from <tabs>. --}}
|
|
2
|
+
@context tabs: { id: string; value: string }
|
|
3
|
+
@input value: string
|
|
4
|
+
|
|
5
|
+
<button
|
|
6
|
+
type="button"
|
|
7
|
+
role="tab"
|
|
8
|
+
[id]="tabs.id + '-tab-' + value"
|
|
9
|
+
[aria-controls]="tabs.id + '-panel-' + value"
|
|
10
|
+
[aria-selected]="value === tabs.value ? 'true' : 'false'"
|
|
11
|
+
[tabindex]="value === tabs.value ? 0 : -1"
|
|
12
|
+
[data-value]="value"
|
|
13
|
+
data-part="tab"
|
|
14
|
+
>
|
|
15
|
+
<slot />
|
|
16
|
+
</button>
|
package/src/tabs.tscl
ADDED
|
@@ -0,0 +1,59 @@
|
|
|
1
|
+
{{--
|
|
2
|
+
Tabs (#465): a tab list over panels, Radix-shaped. The root is a small island; <tab-list>, <tab> and <tab-panel>
|
|
3
|
+
have no script, ship nothing, and read the selection from context, so the server writes every ARIA attribute and
|
|
4
|
+
hidden. Arrow keys, Home and End move between tabs and select them. Bind the selected value with [(value)];
|
|
5
|
+
(change) comes first with the new value, and $info.cancel() keeps the current tab.
|
|
6
|
+
|
|
7
|
+
<tabs id="install" label="Install" value="npm">
|
|
8
|
+
<tab-list>
|
|
9
|
+
<tab value="npm">npm</tab>
|
|
10
|
+
<tab value="pnpm">pnpm</tab>
|
|
11
|
+
</tab-list>
|
|
12
|
+
<tab-panel value="npm">npm i clarusjs</tab-panel>
|
|
13
|
+
<tab-panel value="pnpm">pnpm add clarusjs</tab-panel>
|
|
14
|
+
</tabs>
|
|
15
|
+
|
|
16
|
+
Give each one its own id: its tabs and panels are named after it.
|
|
17
|
+
--}}
|
|
18
|
+
@input id: string
|
|
19
|
+
@input label: string
|
|
20
|
+
@model value: string
|
|
21
|
+
@event change: string
|
|
22
|
+
|
|
23
|
+
<script>
|
|
24
|
+
import { effect } from 'clarusjs/runtime'
|
|
25
|
+
import { rovingFocus } from '@clarusjs/ui-primitives'
|
|
26
|
+
provide('tabs', { id, label, value })
|
|
27
|
+
// The tabs and panels are the page's server HTML (no markers of their own): wired here, by data-value and id.
|
|
28
|
+
// Only this group's: a tabs group nested in a panel has its own.
|
|
29
|
+
function wire(el) {
|
|
30
|
+
const own = (node) => node.closest('[data-part="tabs"]') === el
|
|
31
|
+
const click = (event) => {
|
|
32
|
+
const tab = event.target.closest('[role="tab"]')
|
|
33
|
+
if (!tab || !own(tab)) return
|
|
34
|
+
const next = tab.dataset.value
|
|
35
|
+
if (next !== value && emit('change', next, { reason: 'select', event })) value = next
|
|
36
|
+
}
|
|
37
|
+
el.addEventListener('click', click)
|
|
38
|
+
const list = [...el.querySelectorAll('[role="tablist"]')].find(own)
|
|
39
|
+
const stopKeys = list ? rovingFocus(list, { onMove: (tab) => tab.click() }) : () => {}
|
|
40
|
+
const stopSync = effect(() => {
|
|
41
|
+
const selected = value
|
|
42
|
+
for (const tab of el.querySelectorAll('[role="tab"]')) {
|
|
43
|
+
if (!own(tab)) continue
|
|
44
|
+
tab.setAttribute('aria-selected', String(tab.dataset.value === selected))
|
|
45
|
+
tab.tabIndex = tab.dataset.value === selected ? 0 : -1
|
|
46
|
+
}
|
|
47
|
+
for (const panel of el.querySelectorAll('[role="tabpanel"]')) {
|
|
48
|
+
if (own(panel)) panel.hidden = panel.id !== id + '-panel-' + selected
|
|
49
|
+
}
|
|
50
|
+
})
|
|
51
|
+
return () => {
|
|
52
|
+
el.removeEventListener('click', click)
|
|
53
|
+
stopKeys()
|
|
54
|
+
stopSync()
|
|
55
|
+
}
|
|
56
|
+
}
|
|
57
|
+
</script>
|
|
58
|
+
|
|
59
|
+
<div data-part="tabs" [use]="wire"><slot /></div>
|
|
@@ -0,0 +1,59 @@
|
|
|
1
|
+
{{--
|
|
2
|
+
Tooltip trigger (#465): a button described by its <tooltip>, and its anchor. Browsers with interest invokers
|
|
3
|
+
(interestfor) show and hide the tooltip themselves; elsewhere this small island does: on hover (after a short delay)
|
|
4
|
+
and focus, kept open while the pointer is on the tooltip, closed on Escape, blur or leaving.
|
|
5
|
+
--}}
|
|
6
|
+
@input target: string
|
|
7
|
+
|
|
8
|
+
<script>
|
|
9
|
+
function wire(el) {
|
|
10
|
+
if ('interestForElement' in el) return // the browser does it
|
|
11
|
+
const tip = () => document.getElementById(target)
|
|
12
|
+
let timer
|
|
13
|
+
const later = (fn, ms) => {
|
|
14
|
+
clearTimeout(timer)
|
|
15
|
+
timer = setTimeout(fn, ms)
|
|
16
|
+
}
|
|
17
|
+
const open = () => {
|
|
18
|
+
const t = tip()
|
|
19
|
+
if (t && !t.matches(':popover-open')) t.showPopover()
|
|
20
|
+
}
|
|
21
|
+
const close = () => {
|
|
22
|
+
clearTimeout(timer)
|
|
23
|
+
const t = tip()
|
|
24
|
+
if (t?.matches(':popover-open')) t.hidePopover()
|
|
25
|
+
}
|
|
26
|
+
const enter = () => later(open, 300)
|
|
27
|
+
// Focus keeps it open: leaving with the pointer closes it only when the trigger isn't focused.
|
|
28
|
+
const leave = () => document.activeElement === el || later(close, 100)
|
|
29
|
+
const key = (event) => {
|
|
30
|
+
if (event.key === 'Escape') close()
|
|
31
|
+
}
|
|
32
|
+
const t = tip()
|
|
33
|
+
const on = [
|
|
34
|
+
[el, 'pointerenter', enter],
|
|
35
|
+
[el, 'pointerleave', leave],
|
|
36
|
+
[el, 'focus', open],
|
|
37
|
+
[el, 'blur', close],
|
|
38
|
+
[el, 'keydown', key],
|
|
39
|
+
[t, 'pointerenter', () => clearTimeout(timer)],
|
|
40
|
+
[t, 'pointerleave', leave],
|
|
41
|
+
]
|
|
42
|
+
for (const [node, type, fn] of on) node?.addEventListener(type, fn)
|
|
43
|
+
return () => {
|
|
44
|
+
clearTimeout(timer)
|
|
45
|
+
for (const [node, type, fn] of on) node?.removeEventListener(type, fn)
|
|
46
|
+
}
|
|
47
|
+
}
|
|
48
|
+
</script>
|
|
49
|
+
|
|
50
|
+
<button
|
|
51
|
+
type="button"
|
|
52
|
+
[interestfor]="target"
|
|
53
|
+
[aria-describedby]="target"
|
|
54
|
+
[style]="'anchor-name: --' + target"
|
|
55
|
+
data-part="trigger"
|
|
56
|
+
[use]="wire"
|
|
57
|
+
>
|
|
58
|
+
<slot />
|
|
59
|
+
</button>
|
package/src/tooltip.tscl
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
{{--
|
|
2
|
+
Tooltip (#465): a hint popover (popover="hint": it doesn't close other popovers), placed next to its
|
|
3
|
+
<tooltip-trigger> by CSS anchor positioning, flipping when there's no room. The trigger shows it on hover and focus.
|
|
4
|
+
Short text only; nothing in it can be clicked.
|
|
5
|
+
|
|
6
|
+
<tooltip-trigger target="copy-tip">Copy</tooltip-trigger>
|
|
7
|
+
<tooltip id="copy-tip">Copy the link</tooltip>
|
|
8
|
+
--}}
|
|
9
|
+
@input id: string
|
|
10
|
+
@input side: 'top' | 'bottom' | 'left' | 'right' = 'top'
|
|
11
|
+
|
|
12
|
+
<div
|
|
13
|
+
[id]="id"
|
|
14
|
+
popover="hint"
|
|
15
|
+
role="tooltip"
|
|
16
|
+
data-part="tooltip"
|
|
17
|
+
[data-side]="side"
|
|
18
|
+
[style]="'position-anchor: --' + id + '; position-area: ' + side + '; position-try-fallbacks: flip-block, flip-inline'"
|
|
19
|
+
>
|
|
20
|
+
<slot />
|
|
21
|
+
</div>
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Mark `el` open, with data-starting-style for its first frame so CSS transitions run from there. An element rendered
|
|
3
|
+
* open on the server doesn't need it (nothing to animate in).
|
|
4
|
+
* @param {HTMLElement} el
|
|
5
|
+
* @returns {Promise<void>} When the starting style is gone.
|
|
6
|
+
* @example
|
|
7
|
+
* @if (open) { <div class="popup" [use]="enter">…</div> }
|
|
8
|
+
* @see https://clarusjs.com/guide/ui-primitives#enter-and-exit
|
|
9
|
+
*/
|
|
10
|
+
export declare function enter(el: HTMLElement): Promise<void>;
|
|
11
|
+
/**
|
|
12
|
+
* Mark `el` closing: data-closed and data-ending-style, until its animations and transitions finish. Remove or hide
|
|
13
|
+
* it after: `await exit(el)`.
|
|
14
|
+
* @param {HTMLElement} el
|
|
15
|
+
* @returns {Promise<void>}
|
|
16
|
+
* @see https://clarusjs.com/guide/ui-primitives#enter-and-exit
|
|
17
|
+
*/
|
|
18
|
+
export declare function exit(el: HTMLElement): Promise<void>;
|