@clarusjs/ui-primitives 0.3.2 → 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 +9 -9
- 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
|
@@ -3,14 +3,12 @@
|
|
|
3
3
|
/**
|
|
4
4
|
* Remember what has focus now; the returned function puts focus back there, if it's still on the page and focus is in
|
|
5
5
|
* `el` or nowhere (a click elsewhere keeps its own focus).
|
|
6
|
-
* @param {HTMLElement} el
|
|
7
|
-
* @returns {() => void}
|
|
8
6
|
* @example
|
|
9
7
|
* @if (open) { <div [use]="focusReturn">…</div> }
|
|
10
8
|
* @see https://clarusjs.com/guide/ui-primitives#focusreturn
|
|
11
9
|
*/
|
|
12
|
-
export function focusReturn(el) {
|
|
13
|
-
const before =
|
|
10
|
+
export function focusReturn(el: HTMLElement): () => void {
|
|
11
|
+
const before = document.activeElement as HTMLElement | null
|
|
14
12
|
return () => {
|
|
15
13
|
const now = document.activeElement
|
|
16
14
|
if (before?.isConnected && (!now || now === document.body || el.contains(now))) before.focus()
|
|
@@ -0,0 +1,40 @@
|
|
|
1
|
+
{{--
|
|
2
|
+
Form errors (#691): the summary a form shows after a failed submit, above it. Each message is a link that focuses
|
|
3
|
+
its field, and it's an alert, so a screen reader reads it as it appears (the forms engine focuses the first field).
|
|
4
|
+
Give it the engine's errors.
|
|
5
|
+
|
|
6
|
+
@if (tried) { <form-errors [errors]="f.errors" [ids]="{ email: 'signup-email' }" /> }
|
|
7
|
+
--}}
|
|
8
|
+
<script lang="ts">
|
|
9
|
+
const { errors, title = 'There is a problem', ids = {} } = props<{
|
|
10
|
+
/** The errors being shown, as the forms engine's f.errors has them. */
|
|
11
|
+
errors: { path: string; message: string }[]
|
|
12
|
+
/** Its heading. */
|
|
13
|
+
title?: string
|
|
14
|
+
/** Each field's id by path, where it isn't the path itself. */
|
|
15
|
+
ids?: Record<string, string>
|
|
16
|
+
}>()
|
|
17
|
+
// A link to a field scrolls to it but doesn't focus it: this does (and its label shows above it)
|
|
18
|
+
function links(el: HTMLElement): () => void {
|
|
19
|
+
const go = (event: MouseEvent) => {
|
|
20
|
+
const field = document.getElementById((event.target as Element).closest('a')?.getAttribute('href')?.slice(1) ?? '')
|
|
21
|
+
if (!field) return
|
|
22
|
+
event.preventDefault()
|
|
23
|
+
field.focus()
|
|
24
|
+
field.closest('label, [data-part="field"]')?.scrollIntoView({ block: 'center' })
|
|
25
|
+
}
|
|
26
|
+
el.addEventListener('click', go)
|
|
27
|
+
return () => el.removeEventListener('click', go)
|
|
28
|
+
}
|
|
29
|
+
</script>
|
|
30
|
+
|
|
31
|
+
<div data-part="form-errors" role="alert" tabindex="-1" [hidden]="!errors.length" [use]="links">
|
|
32
|
+
@if (errors.length) {
|
|
33
|
+
<p data-part="form-errors-title">{{ title }}</p>
|
|
34
|
+
<ul>
|
|
35
|
+
@for (e of errors; track e.path) {
|
|
36
|
+
<li><a [href]="'#' + (ids[e.path] ?? e.path)">{{ e.message }}</a></li>
|
|
37
|
+
}
|
|
38
|
+
</ul>
|
|
39
|
+
}
|
|
40
|
+
</div>
|
|
@@ -1,12 +1,12 @@
|
|
|
1
1
|
// @clarusjs/ui-primitives: behaviours for accessible components, each a function of an element that returns how to
|
|
2
2
|
// stop it, for [use]="…" in a clarusjs island. Import what you use: each is its own module, so an island ships only that.
|
|
3
|
-
export { rovingFocus } from './roving-focus.
|
|
4
|
-
export { typeahead } from './typeahead.
|
|
5
|
-
export { dismiss } from './dismiss.
|
|
6
|
-
export { enter, exit } from './transition.
|
|
7
|
-
export { focusReturn } from './focus-return.
|
|
8
|
-
export { position } from './position.
|
|
9
|
-
export { toast, dismissToast, toasts, subscribeToasts, clearToasts } from './toast.
|
|
10
|
-
export { SIDEBAR_KEY, SIDEBAR_SCRIPT, toggleSidebar } from './sidebar.
|
|
3
|
+
export { rovingFocus } from './roving-focus.ts'
|
|
4
|
+
export { typeahead } from './typeahead.ts'
|
|
5
|
+
export { dismiss } from './dismiss.ts'
|
|
6
|
+
export { enter, exit } from './transition.ts'
|
|
7
|
+
export { focusReturn } from './focus-return.ts'
|
|
8
|
+
export { position } from './position.ts'
|
|
9
|
+
export { toast, dismissToast, toasts, subscribeToasts, clearToasts } from './toast.ts'
|
|
10
|
+
export { SIDEBAR_KEY, SIDEBAR_SCRIPT, toggleSidebar } from './sidebar.ts'
|
|
11
11
|
// ponytail: resizeColumn isn't here, since all of these together are held to 3 KB gzip and it would take them past;
|
|
12
12
|
// import it as '@clarusjs/ui-primitives/resize-column.js'. Add it here with the budget raise, if that comes.
|
package/src/menu-item.tscl
CHANGED
|
@@ -1,8 +1,14 @@
|
|
|
1
1
|
{{-- Menu item (#465): one choice in a <menu-list>; its value goes to the menu's (select). data-menu names its menu. --}}
|
|
2
2
|
@context menu: { id: string }
|
|
3
|
-
|
|
4
|
-
|
|
3
|
+
<script lang="ts">
|
|
4
|
+
const props = props<{
|
|
5
|
+
/** What the menu's (select) event sends when it's chosen. */
|
|
6
|
+
value: string
|
|
7
|
+
/** Can't be chosen. */
|
|
8
|
+
disabled?: boolean
|
|
9
|
+
}>(() => ({ disabled: false }))
|
|
10
|
+
</script>
|
|
5
11
|
|
|
6
|
-
<button type="button" role="menuitem" tabindex="-1" [data-value]="value" [data-menu]="menu.id" [aria-disabled]="disabled ? 'true' : false" data-part="menu-item">
|
|
12
|
+
<button type="button" role="menuitem" tabindex="-1" [data-value]="props.value" [data-menu]="menu.id" [aria-disabled]="props.disabled ? 'true' : false" data-part="menu-item">
|
|
7
13
|
<slot />
|
|
8
14
|
</button>
|
package/src/menu-list.tscl
CHANGED
|
@@ -6,7 +6,7 @@
|
|
|
6
6
|
popover="auto"
|
|
7
7
|
role="menu"
|
|
8
8
|
[aria-labelledby]="menu.id + '-trigger'"
|
|
9
|
-
[style]="'position-anchor: --' + menu.id + '; position-area: bottom span-
|
|
9
|
+
[style]="'position-anchor: --' + menu.id + '; position-area: bottom span-x-end; position-try-fallbacks: flip-block, flip-inline'"
|
|
10
10
|
data-part="menu-list"
|
|
11
11
|
>
|
|
12
12
|
<slot />
|
package/src/menu.tscl
CHANGED
|
@@ -14,12 +14,15 @@
|
|
|
14
14
|
|
|
15
15
|
Give each one its own id: its trigger and list are named after it.
|
|
16
16
|
--}}
|
|
17
|
-
@input id: string
|
|
18
|
-
@event select: string
|
|
19
17
|
|
|
20
|
-
<script>
|
|
18
|
+
<script lang="ts">
|
|
21
19
|
import { rovingFocus, typeahead } from '@clarusjs/ui-primitives'
|
|
22
|
-
|
|
20
|
+
const props = props<{
|
|
21
|
+
/** Its id, unique on the page: its trigger and list ids are made from it. */
|
|
22
|
+
id: string
|
|
23
|
+
}>()
|
|
24
|
+
const emit = events<{ select: string }>()
|
|
25
|
+
provide('menu', { id: props.id })
|
|
23
26
|
// The trigger, list and items are the page's server HTML: wired here, this menu's only (not a menu nested in it).
|
|
24
27
|
function wire(el) {
|
|
25
28
|
const own = (node) => node.closest('[data-part="menu"]') === el
|
|
@@ -27,7 +30,7 @@
|
|
|
27
30
|
const trigger = [...el.querySelectorAll('[aria-haspopup="menu"]')].find(own)
|
|
28
31
|
if (!list) return
|
|
29
32
|
// 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"])`
|
|
33
|
+
const items = `[role="menuitem"][data-menu="${CSS.escape(props.id)}"]:not([aria-disabled="true"])`
|
|
31
34
|
// Keys typed in a nested menu are that menu's: typeahead here doesn't move focus for them.
|
|
32
35
|
let nested = false
|
|
33
36
|
const mark = (event) => (nested = !own(event.target))
|
package/src/menubar.tscl
ADDED
|
@@ -0,0 +1,69 @@
|
|
|
1
|
+
{{--
|
|
2
|
+
Menubar (#682): a row of <menu>s (File, Edit, View…), WAI-ARIA's menubar. ← → move between the menus' triggers (one
|
|
3
|
+
Tab stop); ↓ opens the focused one; inside an open menu, ← → close it and open the next one; while one is open,
|
|
4
|
+
hovering another trigger opens that one instead. Each <menu> keeps its own keys, typeahead and (select). Give each
|
|
5
|
+
trigger role="menuitem" (it lands on the button), as a menubar's items are.
|
|
6
|
+
|
|
7
|
+
<menubar label="Editor">
|
|
8
|
+
<menu id="file" (select)="run($event)"><menu-trigger role="menuitem">File</menu-trigger><menu-list>…</menu-list></menu>
|
|
9
|
+
<menu id="edit" (select)="run($event)"><menu-trigger role="menuitem">Edit</menu-trigger><menu-list>…</menu-list></menu>
|
|
10
|
+
</menubar>
|
|
11
|
+
--}}
|
|
12
|
+
<script lang="ts">
|
|
13
|
+
import { rovingFocus } from '@clarusjs/ui-primitives'
|
|
14
|
+
const { label } = props<{
|
|
15
|
+
/** What the menubar is for, as its accessible name. */
|
|
16
|
+
label: string
|
|
17
|
+
}>()
|
|
18
|
+
const TRIGGERS = '[data-part="menu"] > [aria-haspopup="menu"]'
|
|
19
|
+
function wire(el: HTMLElement): () => void {
|
|
20
|
+
// Its own menus' triggers, not those of submenus inside their lists
|
|
21
|
+
const triggers = () => [...el.querySelectorAll<HTMLElement>(TRIGGERS)].filter((t) => !t.closest('[role="menu"]'))
|
|
22
|
+
const listOf = (t: Element) => document.getElementById(t.getAttribute('popovertarget') ?? '') as HTMLElement | null
|
|
23
|
+
const isOpen = (list: HTMLElement | null) => !!list?.matches(':popover-open')
|
|
24
|
+
const openOne = () => triggers().find((t) => isOpen(listOf(t)))
|
|
25
|
+
const show = (t: HTMLElement) => {
|
|
26
|
+
const list = listOf(t)
|
|
27
|
+
if (list && !isOpen(list)) list.showPopover() // the menu island focuses its first item as it opens
|
|
28
|
+
}
|
|
29
|
+
const key = (event: KeyboardEvent) => {
|
|
30
|
+
const all = triggers()
|
|
31
|
+
const target = event.target as HTMLElement
|
|
32
|
+
// On a trigger: ↓ opens its menu (← → are rovingFocus's)
|
|
33
|
+
if (all.includes(target)) {
|
|
34
|
+
if (event.key === 'ArrowDown') { event.preventDefault(); show(target) }
|
|
35
|
+
return
|
|
36
|
+
}
|
|
37
|
+
// Inside an open menu: ← → move to the next menu, open
|
|
38
|
+
if (event.key !== 'ArrowLeft' && event.key !== 'ArrowRight') return
|
|
39
|
+
const current = openOne()
|
|
40
|
+
if (!current || !listOf(current)?.contains(target)) return
|
|
41
|
+
event.preventDefault()
|
|
42
|
+
const at = all.indexOf(current)
|
|
43
|
+
// Right to left (the nearest dir), the next menu is to the left
|
|
44
|
+
const forward = (event.key === 'ArrowRight') !== (el.closest<HTMLElement>('[dir]')?.dir === 'rtl')
|
|
45
|
+
const next = all[(at + (forward ? 1 : -1) + all.length) % all.length]!
|
|
46
|
+
listOf(current)?.hidePopover()
|
|
47
|
+
next.focus()
|
|
48
|
+
show(next)
|
|
49
|
+
}
|
|
50
|
+
// While one is open, the pointer on another trigger opens that one
|
|
51
|
+
const over = (event: PointerEvent) => {
|
|
52
|
+
const t = (event.target as Element).closest<HTMLElement>(TRIGGERS)
|
|
53
|
+
const current = openOne()
|
|
54
|
+
if (t && current && t !== current && triggers().includes(t)) { listOf(current)?.hidePopover(); t.focus(); show(t) }
|
|
55
|
+
}
|
|
56
|
+
el.addEventListener('keydown', key)
|
|
57
|
+
el.addEventListener('pointerover', over)
|
|
58
|
+
const stop = rovingFocus(el, { items: TRIGGERS, orientation: 'horizontal' })
|
|
59
|
+
return () => {
|
|
60
|
+
el.removeEventListener('keydown', key)
|
|
61
|
+
el.removeEventListener('pointerover', over)
|
|
62
|
+
stop()
|
|
63
|
+
}
|
|
64
|
+
}
|
|
65
|
+
</script>
|
|
66
|
+
|
|
67
|
+
<div role="menubar" data-part="menubar" [aria-label]="label" [use]="wire">
|
|
68
|
+
<slot />
|
|
69
|
+
</div>
|
|
@@ -0,0 +1,5 @@
|
|
|
1
|
+
// The navigation menu's context (#683): its id, which names the bar its panels are anchored to.
|
|
2
|
+
import { contextKey, type ContextKey } from 'clarusjs/runtime'
|
|
3
|
+
|
|
4
|
+
/** The navigation menu's context: its id, for the triggers and panels inside it. */
|
|
5
|
+
export const navMenuKey: ContextKey<{ id: string }> = contextKey('nav-menu')
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
{{-- Navigation menu panel (#683): a popover under the whole <nav-menu> bar (anchored to it, as wide as it), opened by its trigger. --}}
|
|
2
|
+
<script lang="ts">
|
|
3
|
+
import { navMenuKey } from './nav-menu-key.ts'
|
|
4
|
+
const { id } = props<{
|
|
5
|
+
/** Its trigger's target. */
|
|
6
|
+
id: string
|
|
7
|
+
}>()
|
|
8
|
+
const menu = context(navMenuKey)
|
|
9
|
+
</script>
|
|
10
|
+
|
|
11
|
+
<div [id]="id" popover="auto" data-part="nav-panel" [style]="'position-anchor: --' + menu.id + '; position-area: bottom span-all; width: anchor-size(width)'"><slot /></div>
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
{{-- Navigation menu trigger (#683): the button that opens its <nav-menu-panel> (popovertarget). No JavaScript of its own. --}}
|
|
2
|
+
<script lang="ts">
|
|
3
|
+
const { target } = props<{
|
|
4
|
+
/** Its panel's id. */
|
|
5
|
+
target: string
|
|
6
|
+
}>()
|
|
7
|
+
</script>
|
|
8
|
+
|
|
9
|
+
<button type="button" [popovertarget]="target" [aria-controls]="target" aria-expanded="false" data-part="nav-trigger"><slot /></button>
|
|
@@ -0,0 +1,78 @@
|
|
|
1
|
+
{{--
|
|
2
|
+
Navigation menu (#683): site navigation with dropdown panels (a mega menu). Its links stay links and its triggers
|
|
3
|
+
buttons, no menu roles (WAI-ARIA's disclosure navigation): Tab goes through them all. Each <nav-menu-trigger> opens
|
|
4
|
+
its <nav-menu-panel> as a popover, so a click, Escape, a click outside and one panel at a time are the browser's, and
|
|
5
|
+
the panels' links work without JavaScript. Every panel opens in the same place, under the whole bar. This island
|
|
6
|
+
adds hover (after a short delay; at once from one open panel to another), closing after the pointer leaves, and an
|
|
7
|
+
indicator under the open trigger that moves from one to the next.
|
|
8
|
+
|
|
9
|
+
<nav-menu id="main" label="Main">
|
|
10
|
+
<li><nav-menu-trigger target="products">Products</nav-menu-trigger><nav-menu-panel id="products">…links…</nav-menu-panel></li>
|
|
11
|
+
<li><a href="/pricing">Pricing</a></li>
|
|
12
|
+
</nav-menu>
|
|
13
|
+
--}}
|
|
14
|
+
<script lang="ts">
|
|
15
|
+
import { navMenuKey } from './nav-menu-key.ts'
|
|
16
|
+
const { id, label, openDelay = 150, closeDelay = 300 } = props<{
|
|
17
|
+
/** Names the bar its panels are anchored to; unique on the page. */
|
|
18
|
+
id: string
|
|
19
|
+
/** The navigation's name (several on a page each need one). */
|
|
20
|
+
label: string
|
|
21
|
+
/** How long the pointer rests on a trigger before its panel opens, in ms. */
|
|
22
|
+
openDelay?: number
|
|
23
|
+
/** How long after the pointer leaves the bar and the panel it closes, in ms. */
|
|
24
|
+
closeDelay?: number
|
|
25
|
+
}>()
|
|
26
|
+
provide(navMenuKey, { id })
|
|
27
|
+
|
|
28
|
+
function wire(nav: HTMLElement): () => void {
|
|
29
|
+
const triggers = () => [...nav.querySelectorAll<HTMLElement>('[data-part="nav-trigger"]')]
|
|
30
|
+
const panelOf = (t: Element) => document.getElementById(t.getAttribute('popovertarget') ?? '') as HTMLElement | null
|
|
31
|
+
const isOpen = (p: HTMLElement | null) => !!p?.matches(':popover-open')
|
|
32
|
+
const openTrigger = () => triggers().find((t) => isOpen(panelOf(t)))
|
|
33
|
+
const indicator = nav.querySelector<HTMLElement>('[data-part="nav-indicator"]')
|
|
34
|
+
let timer = 0
|
|
35
|
+
const later = (fn: () => void, ms: number) => { clearTimeout(timer); timer = window.setTimeout(fn, ms) }
|
|
36
|
+
const show = (t: HTMLElement) => { const p = panelOf(t); if (p && !isOpen(p)) p.showPopover() }
|
|
37
|
+
const hide = () => { const t = openTrigger(); if (t) panelOf(t)?.hidePopover() }
|
|
38
|
+
|
|
39
|
+
// The indicator under the open trigger; aria-expanded and data-open in step with the panels
|
|
40
|
+
const sync = () => {
|
|
41
|
+
const open = openTrigger()
|
|
42
|
+
for (const t of triggers()) t.setAttribute('aria-expanded', String(t === open))
|
|
43
|
+
nav.toggleAttribute('data-open', !!open)
|
|
44
|
+
if (indicator && open) {
|
|
45
|
+
const n = nav.getBoundingClientRect(), r = open.getBoundingClientRect()
|
|
46
|
+
indicator.style.setProperty('--ui-nav-indicator-x', `${r.left - n.left}px`)
|
|
47
|
+
indicator.style.setProperty('--ui-nav-indicator-width', `${r.width}px`)
|
|
48
|
+
}
|
|
49
|
+
}
|
|
50
|
+
const enter = (event: PointerEvent) => {
|
|
51
|
+
const t = (event.target as Element).closest<HTMLElement>('[data-part="nav-trigger"]')
|
|
52
|
+
if (!t || !nav.contains(t)) { clearTimeout(timer); return }
|
|
53
|
+
if (openTrigger()) { clearTimeout(timer); show(t) } // from one open panel to the next: at once
|
|
54
|
+
else later(() => show(t), openDelay)
|
|
55
|
+
}
|
|
56
|
+
const leave = () => later(hide, closeDelay)
|
|
57
|
+
const panels = triggers().map(panelOf).filter((p): p is HTMLElement => !!p)
|
|
58
|
+
nav.addEventListener('pointerover', enter)
|
|
59
|
+
nav.addEventListener('pointerleave', leave)
|
|
60
|
+
for (const p of panels) {
|
|
61
|
+
p.addEventListener('toggle', sync)
|
|
62
|
+
p.addEventListener('pointerenter', () => clearTimeout(timer))
|
|
63
|
+
p.addEventListener('pointerleave', leave)
|
|
64
|
+
}
|
|
65
|
+
sync()
|
|
66
|
+
return () => {
|
|
67
|
+
clearTimeout(timer)
|
|
68
|
+
nav.removeEventListener('pointerover', enter)
|
|
69
|
+
nav.removeEventListener('pointerleave', leave)
|
|
70
|
+
for (const p of panels) { p.removeEventListener('toggle', sync); p.removeEventListener('pointerleave', leave) }
|
|
71
|
+
}
|
|
72
|
+
}
|
|
73
|
+
</script>
|
|
74
|
+
|
|
75
|
+
<nav data-part="nav-menu" [aria-label]="label" [style]="'anchor-name: --' + id" [use]="wire">
|
|
76
|
+
<ul data-part="nav-list"><slot /></ul>
|
|
77
|
+
<span data-part="nav-indicator" aria-hidden="true"></span>
|
|
78
|
+
</nav>
|
|
@@ -0,0 +1,139 @@
|
|
|
1
|
+
{{--
|
|
2
|
+
Number field (#673): a native number input between − and + buttons. The browser gives it its spinbutton role, typing,
|
|
3
|
+
↑ ↓, min/max/step validation and the form value, without JavaScript; the island adds the buttons, PageUp/PageDown
|
|
4
|
+
(ten steps), Home/End (min, max) and, with [wheel], the mouse wheel while it has focus. Steps land on the step grid
|
|
5
|
+
(counted from min) and stay within min and max. Bind the number with [(value)]: null when the field is empty.
|
|
6
|
+
|
|
7
|
+
<number-field id="qty" label="Quantity" [min]="1" [max]="99" [(value)]="order.qty" />
|
|
8
|
+
|
|
9
|
+
[format] (Intl.NumberFormat options) shows the value in a locale's form beside the field and gives screen readers
|
|
10
|
+
the same text (aria-valuetext): [format]="{ style: 'currency', currency: 'EUR' }" [locale]="'de-DE'". The locale is
|
|
11
|
+
fixed ('en' unless given) so the server and the browser write the same text.
|
|
12
|
+
|
|
13
|
+
Headless: data-part names each element (number-field, label, group, decrement, input, increment, value); the root
|
|
14
|
+
has data-disabled, and data-filled when there's a value.
|
|
15
|
+
--}}
|
|
16
|
+
<script lang="ts">
|
|
17
|
+
import { effect } from 'clarusjs/runtime'
|
|
18
|
+
const { id, label, name = '', placeholder = '', min, max, step = 1, disabled = false, required = false, wheel = false, format, locale = 'en' } = props<{
|
|
19
|
+
/** Its id, unique on the page: its input id. */
|
|
20
|
+
id: string
|
|
21
|
+
/** What the field is for; also names the − and + buttons ("Decrease Quantity"). */
|
|
22
|
+
label: string
|
|
23
|
+
/** Sends the value with a form. */
|
|
24
|
+
name?: string
|
|
25
|
+
/** Shown while the field is empty: an example of what goes in it. */
|
|
26
|
+
placeholder?: string
|
|
27
|
+
/** The smallest value; the value can't go below it. */
|
|
28
|
+
min?: number
|
|
29
|
+
/** The largest value; the value can't go above it. */
|
|
30
|
+
max?: number
|
|
31
|
+
/** The step the buttons and keys move by, and the grid values snap to (counted from min, else 0). */
|
|
32
|
+
step?: number
|
|
33
|
+
/** Can't be changed or focused. */
|
|
34
|
+
disabled?: boolean
|
|
35
|
+
/** Must be filled in before the form is sent. */
|
|
36
|
+
required?: boolean
|
|
37
|
+
/** The mouse wheel steps the value while the field has focus. */
|
|
38
|
+
wheel?: boolean
|
|
39
|
+
/** Intl.NumberFormat options: shows the value in this form beside the field, and as its aria-valuetext. */
|
|
40
|
+
format?: Intl.NumberFormatOptions
|
|
41
|
+
/** The locale for format. */
|
|
42
|
+
locale?: string
|
|
43
|
+
}>()
|
|
44
|
+
/** The number, or null when the field is empty. */
|
|
45
|
+
let value = model<number | null>(() => null)
|
|
46
|
+
// The value attribute stays the starting value, which a form reset puts back; the live value is set by wire().
|
|
47
|
+
const initial = value
|
|
48
|
+
|
|
49
|
+
const formatted = derived(() => (format && value !== null ? new Intl.NumberFormat(locale, format).format(value) : ''))
|
|
50
|
+
|
|
51
|
+
// The native input's step grid (HTML's step base): counted from min, else the starting value, else 0. Values are
|
|
52
|
+
// base + k × step for whole k, and min/max bound k, so a step never lands off the grid or past a bound.
|
|
53
|
+
const base = min ?? initial ?? 0
|
|
54
|
+
/** Decimal places of n, exponent notation too (1e-7: 7). */
|
|
55
|
+
const places = (n: number): number => {
|
|
56
|
+
const [mantissa, exponent = '0'] = String(n).toLowerCase().split('e')
|
|
57
|
+
return Math.max(0, (mantissa!.split('.')[1] ?? '').length - Number(exponent))
|
|
58
|
+
}
|
|
59
|
+
const digits = Math.max(places(step), places(base))
|
|
60
|
+
const ε = 1e-9 // (0.6 − 0) / 0.2 is 2.9999999999999996
|
|
61
|
+
const lowest = min === undefined ? -Infinity : Math.ceil((min - base) / step - ε)
|
|
62
|
+
const highest = max === undefined ? Infinity : Math.floor((max - base) / step + ε)
|
|
63
|
+
const at = (k: number): number => Number((base + k * step).toFixed(digits))
|
|
64
|
+
/** n on the grid, within the admissible steps, without floating-point noise (0.1 + 0.2). */
|
|
65
|
+
function snap(n: number): number {
|
|
66
|
+
return at(Math.min(highest, Math.max(lowest, Math.round((n - base) / step))))
|
|
67
|
+
}
|
|
68
|
+
const atMin = derived(() => value !== null && lowest !== -Infinity && value <= at(lowest))
|
|
69
|
+
const atMax = derived(() => value !== null && highest !== Infinity && value >= at(highest))
|
|
70
|
+
function stepBy(steps: number): void {
|
|
71
|
+
if (disabled) return
|
|
72
|
+
value = value === null ? snap(base) : snap(value + steps * step)
|
|
73
|
+
}
|
|
74
|
+
const read = (input: HTMLInputElement): number | null => (input.value === '' || Number.isNaN(input.valueAsNumber) ? null : input.valueAsNumber)
|
|
75
|
+
|
|
76
|
+
function key(event: KeyboardEvent): void {
|
|
77
|
+
const jump: Record<string, () => void> = {
|
|
78
|
+
PageUp: () => stepBy(10),
|
|
79
|
+
PageDown: () => stepBy(-10),
|
|
80
|
+
Home: () => { if (min !== undefined && !disabled) value = min },
|
|
81
|
+
End: () => { if (max !== undefined && !disabled) value = max },
|
|
82
|
+
}
|
|
83
|
+
if (!jump[event.key]) return // ↑ ↓ and typing are the browser's
|
|
84
|
+
event.preventDefault()
|
|
85
|
+
jump[event.key]!()
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
function wire(input: HTMLInputElement): () => void {
|
|
89
|
+
const typed = () => { value = read(input) }
|
|
90
|
+
const scrolled = (event: WheelEvent) => {
|
|
91
|
+
if (!wheel || document.activeElement !== input || !event.deltaY) return
|
|
92
|
+
event.preventDefault()
|
|
93
|
+
stepBy(event.deltaY < 0 ? 1 : -1)
|
|
94
|
+
}
|
|
95
|
+
// A form reset puts back the value the field started with, without an input event.
|
|
96
|
+
const reset = (event: Event) => { if (event.target === input.form) setTimeout(typed) }
|
|
97
|
+
input.addEventListener('input', typed)
|
|
98
|
+
input.addEventListener('wheel', scrolled, { passive: false })
|
|
99
|
+
document.addEventListener('reset', reset)
|
|
100
|
+
// The value into the field only when it differs from what's typed: "1." reads as empty and must stay as typed.
|
|
101
|
+
const stop = effect(() => {
|
|
102
|
+
const now = read(input)
|
|
103
|
+
if (value !== now) input.value = value === null ? '' : String(value)
|
|
104
|
+
})
|
|
105
|
+
return () => {
|
|
106
|
+
input.removeEventListener('input', typed)
|
|
107
|
+
input.removeEventListener('wheel', scrolled)
|
|
108
|
+
document.removeEventListener('reset', reset)
|
|
109
|
+
stop()
|
|
110
|
+
}
|
|
111
|
+
}
|
|
112
|
+
</script>
|
|
113
|
+
|
|
114
|
+
<div data-part="number-field" [data-disabled]="disabled ? '' : false" [data-filled]="value !== null ? '' : false">
|
|
115
|
+
<label [for]="id" data-part="label">{{ label }}</label>
|
|
116
|
+
<div data-part="group">
|
|
117
|
+
<button type="button" data-part="decrement" [aria-label]="'Decrease ' + label" tabindex="-1" [disabled]="disabled || atMin" (click)="stepBy(-1)">−</button>
|
|
118
|
+
<input
|
|
119
|
+
[id]="id"
|
|
120
|
+
type="number"
|
|
121
|
+
data-part="input"
|
|
122
|
+
[min]="min"
|
|
123
|
+
[max]="max"
|
|
124
|
+
[step]="step"
|
|
125
|
+
[name]="name || undefined"
|
|
126
|
+
[placeholder]="placeholder || undefined"
|
|
127
|
+
[required]="required"
|
|
128
|
+
[disabled]="disabled"
|
|
129
|
+
[value]="initial ?? undefined"
|
|
130
|
+
[aria-valuetext]="formatted || undefined"
|
|
131
|
+
(keydown)="key($event)"
|
|
132
|
+
[use]="wire"
|
|
133
|
+
/>
|
|
134
|
+
<button type="button" data-part="increment" [aria-label]="'Increase ' + label" tabindex="-1" [disabled]="disabled || atMax" (click)="stepBy(1)">+</button>
|
|
135
|
+
</div>
|
|
136
|
+
@if (formatted) {
|
|
137
|
+
<output [for]="id" data-part="value" aria-hidden="true">{{ formatted }}</output>
|
|
138
|
+
}
|
|
139
|
+
</div>
|
package/src/popover-trigger.tscl
CHANGED
|
@@ -1,4 +1,9 @@
|
|
|
1
1
|
{{-- Toggles the popover whose id is `target` (popovertarget), and is its anchor: no JavaScript. --}}
|
|
2
|
-
|
|
2
|
+
<script lang="ts">
|
|
3
|
+
const props = props<{
|
|
4
|
+
/** The id of the popover it toggles. */
|
|
5
|
+
target: string
|
|
6
|
+
}>()
|
|
7
|
+
</script>
|
|
3
8
|
|
|
4
|
-
<button type="button" [popovertarget]="target" [style]="'anchor-name: --' + target" data-part="trigger"><slot /></button>
|
|
9
|
+
<button type="button" [popovertarget]="props.target" [style]="'anchor-name: --' + props.target" data-part="trigger"><slot /></button>
|
package/src/popover.tscl
CHANGED
|
@@ -7,29 +7,36 @@
|
|
|
7
7
|
<popover-trigger target="share">Share</popover-trigger>
|
|
8
8
|
<popover id="share" side="bottom" align="start">…</popover>
|
|
9
9
|
--}}
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
10
|
+
<script lang="ts">
|
|
11
|
+
const props = props<{
|
|
12
|
+
/** Its id, unique on the page: a <popover-trigger> targets it. */
|
|
13
|
+
id: string
|
|
14
|
+
/** Which side of its trigger it opens on (the opposite one when there's no room). */
|
|
15
|
+
side?: 'top' | 'bottom' | 'left' | 'right'
|
|
16
|
+
/** How it lines up with its trigger along that side: start, center or end. */
|
|
17
|
+
align?: 'start' | 'center' | 'end'
|
|
18
|
+
}>(() => ({ side: 'bottom', align: 'center' }))
|
|
19
|
+
</script>
|
|
13
20
|
|
|
14
21
|
<div
|
|
15
|
-
[id]="id"
|
|
22
|
+
[id]="props.id"
|
|
16
23
|
popover="auto"
|
|
17
24
|
data-part="popover"
|
|
18
|
-
[data-side]="side"
|
|
19
|
-
[data-align]="align"
|
|
25
|
+
[data-side]="props.side"
|
|
26
|
+
[data-align]="props.align"
|
|
20
27
|
[style]="
|
|
21
28
|
'position-anchor: --' +
|
|
22
|
-
id +
|
|
29
|
+
props.id +
|
|
23
30
|
'; position-area: ' +
|
|
24
|
-
side +
|
|
25
|
-
(align === 'center'
|
|
31
|
+
props.side +
|
|
32
|
+
(props.align === 'center'
|
|
26
33
|
? ''
|
|
27
34
|
: ' ' +
|
|
28
|
-
(side === 'top' || side === 'bottom'
|
|
29
|
-
? align === 'start'
|
|
30
|
-
? 'span-
|
|
31
|
-
: 'span-
|
|
32
|
-
: align === 'start'
|
|
35
|
+
(props.side === 'top' || props.side === 'bottom'
|
|
36
|
+
? props.align === 'start'
|
|
37
|
+
? 'span-x-end'
|
|
38
|
+
: 'span-x-start'
|
|
39
|
+
: props.align === 'start'
|
|
33
40
|
? 'span-bottom'
|
|
34
41
|
: 'span-top')) +
|
|
35
42
|
'; position-try-fallbacks: flip-block, flip-inline'
|
|
@@ -4,27 +4,28 @@
|
|
|
4
4
|
// ponytail: JavaScript always; CSS anchor positioning (position-anchor, position-try) where supported would need none.
|
|
5
5
|
// Add it when a part can say all it needs in CSS.
|
|
6
6
|
|
|
7
|
-
/**
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
*/
|
|
7
|
+
/** Where position() places the floating element, relative to its anchor. */
|
|
8
|
+
export interface PositionOptions {
|
|
9
|
+
/** Which side of the anchor. Default 'bottom'. */
|
|
10
|
+
side?: 'top' | 'bottom' | 'left' | 'right'
|
|
11
|
+
/** Along that side. Default 'center'. */
|
|
12
|
+
align?: 'start' | 'center' | 'end'
|
|
13
|
+
/** Gap from the anchor, in px. Default 4. */
|
|
14
|
+
offset?: number
|
|
15
|
+
/** The other side when there's no room on this one. Default true. */
|
|
16
|
+
flip?: boolean
|
|
17
|
+
}
|
|
14
18
|
|
|
15
19
|
/**
|
|
16
20
|
* Position `floating` (fixed) by `anchor`, now and on scroll and resize.
|
|
17
|
-
* @
|
|
18
|
-
* @param {Element} anchor
|
|
19
|
-
* @param {PositionOptions} [options]
|
|
20
|
-
* @returns {() => void} Stops following.
|
|
21
|
+
* @returns Stops following.
|
|
21
22
|
* @example
|
|
22
23
|
* @if (open) { <div [use]="(el) => position(el, trigger)">…</div> }
|
|
23
24
|
* @see https://clarusjs.com/guide/ui-primitives#position
|
|
24
25
|
*/
|
|
25
|
-
export function position(floating, anchor, options = {}) {
|
|
26
|
+
export function position(floating: HTMLElement, anchor: Element, options: PositionOptions = {}): () => void {
|
|
26
27
|
const { align = 'center', offset = 4, flip = true } = options
|
|
27
|
-
const opposite =
|
|
28
|
+
const opposite = { top: 'bottom', bottom: 'top', left: 'right', right: 'left' } as const
|
|
28
29
|
const place = () => {
|
|
29
30
|
const a = anchor.getBoundingClientRect()
|
|
30
31
|
const f = floating.getBoundingClientRect()
|
|
@@ -35,9 +36,12 @@ export function position(floating, anchor, options = {}) {
|
|
|
35
36
|
const size = side === 'top' || side === 'bottom' ? f.height : f.width
|
|
36
37
|
if (flip && room[side] < size && room[opposite[side]] > room[side]) side = opposite[side]
|
|
37
38
|
const vertical = side === 'top' || side === 'bottom'
|
|
38
|
-
const along = (start = 0, length = 0, own = 0) => (
|
|
39
|
-
|
|
40
|
-
const
|
|
39
|
+
const along = (at: string, start = 0, length = 0, own = 0) => (at === 'start' ? start : at === 'end' ? start + length - own : start + (length - own) / 2)
|
|
40
|
+
// Along a top or bottom side, start is the line's start: the right edge right to left (the nearest dir)
|
|
41
|
+
const rtl = (floating.closest('[dir]') as HTMLElement | null)?.dir === 'rtl'
|
|
42
|
+
const x = rtl && align !== 'center' ? (align === 'start' ? 'end' : 'start') : align
|
|
43
|
+
const top = side === 'bottom' ? a.bottom + offset : side === 'top' ? a.top - offset - f.height : along(align, a.top, a.height, f.height)
|
|
44
|
+
const left = side === 'right' ? a.right + offset : side === 'left' ? a.left - offset - f.width : along(x, a.left, a.width, f.width)
|
|
41
45
|
const s = floating.style
|
|
42
46
|
s.position = 'fixed'
|
|
43
47
|
s.top = `${Math.round(top)}px`
|
|
@@ -48,7 +52,7 @@ export function position(floating, anchor, options = {}) {
|
|
|
48
52
|
s.setProperty('--anchor-height', `${a.height}px`)
|
|
49
53
|
s.setProperty('--available-height', `${Math.max(vertical ? room[side] : vh, 0)}px`)
|
|
50
54
|
s.setProperty('--available-width', `${Math.max(vertical ? vw : room[side], 0)}px`)
|
|
51
|
-
s.setProperty('--transform-origin', vertical ? `${
|
|
55
|
+
s.setProperty('--transform-origin', vertical ? `${x === 'start' ? 'left' : x === 'end' ? 'right' : 'center'} ${opposite[side]}` : `${opposite[side]} center`)
|
|
52
56
|
}
|
|
53
57
|
place()
|
|
54
58
|
const options_ = { passive: true, capture: true }
|
|
@@ -0,0 +1,48 @@
|
|
|
1
|
+
{{--
|
|
2
|
+
Preview card trigger (#681): the link its <preview-card> previews, and the card's anchor. Browsers with interest
|
|
3
|
+
invokers (interestfor, made for link previews) show and hide the card themselves, with the delays as CSS
|
|
4
|
+
(interest-delay). Elsewhere this small island does: open [openDelay] ms after the pointer comes, at once on focus;
|
|
5
|
+
kept open while the pointer is on the card; closed [closeDelay] ms after it leaves both, on Escape, or on blur.
|
|
6
|
+
--}}
|
|
7
|
+
<script lang="ts">
|
|
8
|
+
const { target, href, openDelay = 600, closeDelay = 300 } = props<{
|
|
9
|
+
/** The card's id. */
|
|
10
|
+
target: string
|
|
11
|
+
/** Where the link goes. */
|
|
12
|
+
href: string
|
|
13
|
+
/** How long the pointer rests on the link before the card opens, in ms. */
|
|
14
|
+
openDelay?: number
|
|
15
|
+
/** How long after the pointer leaves the link and the card it closes, in ms. */
|
|
16
|
+
closeDelay?: number
|
|
17
|
+
}>()
|
|
18
|
+
function wire(el: HTMLAnchorElement): () => void {
|
|
19
|
+
if ('interestForElement' in el) return () => {} // the browser does it
|
|
20
|
+
const card = () => document.getElementById(target) as (HTMLElement & { showPopover(): void; hidePopover(): void }) | null
|
|
21
|
+
let timer = 0
|
|
22
|
+
const later = (fn: () => void, ms: number) => { clearTimeout(timer); timer = window.setTimeout(fn, ms) }
|
|
23
|
+
const open = () => { const c = card(); if (c && !c.matches(':popover-open')) c.showPopover() }
|
|
24
|
+
const close = () => { clearTimeout(timer); const c = card(); if (c?.matches(':popover-open')) c.hidePopover() }
|
|
25
|
+
const leave = () => later(close, closeDelay)
|
|
26
|
+
const key = (event: KeyboardEvent) => { if (event.key === 'Escape') close() }
|
|
27
|
+
// Focus leaving for the card keeps it open; anywhere else closes it
|
|
28
|
+
const blur = (event: FocusEvent) => { if (!card()?.contains(event.relatedTarget as Node)) close() }
|
|
29
|
+
const c = card()
|
|
30
|
+
const on: [EventTarget | null, string, EventListener][] = [
|
|
31
|
+
[el, 'pointerenter', () => later(open, openDelay)],
|
|
32
|
+
[el, 'pointerleave', leave],
|
|
33
|
+
[el, 'focus', open],
|
|
34
|
+
[el, 'blur', blur as EventListener],
|
|
35
|
+
[el, 'keydown', key as EventListener],
|
|
36
|
+
[c, 'pointerenter', () => clearTimeout(timer)],
|
|
37
|
+
[c, 'pointerleave', leave],
|
|
38
|
+
[c, 'keydown', key as EventListener],
|
|
39
|
+
]
|
|
40
|
+
for (const [t, type, fn] of on) t?.addEventListener(type, fn)
|
|
41
|
+
return () => {
|
|
42
|
+
clearTimeout(timer)
|
|
43
|
+
for (const [t, type, fn] of on) t?.removeEventListener(type, fn)
|
|
44
|
+
}
|
|
45
|
+
}
|
|
46
|
+
</script>
|
|
47
|
+
|
|
48
|
+
<a [href]="href" [interestfor]="target" data-part="preview-card-trigger" [style]="'anchor-name: --' + target + '; interest-delay: ' + openDelay + 'ms ' + closeDelay + 'ms'" [use]="wire"><slot /></a>
|