@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.
Files changed (76) hide show
  1. package/package.json +9 -9
  2. package/src/accordion-item.tscl +9 -3
  3. package/src/autocomplete.tscl +115 -0
  4. package/src/checkbox-group-key.ts +5 -0
  5. package/src/checkbox-group.tscl +96 -0
  6. package/src/checkbox-item.tscl +18 -0
  7. package/src/checkbox.tscl +15 -7
  8. package/src/chip-input.tscl +24 -15
  9. package/src/combobox.tscl +44 -35
  10. package/src/context-menu.tscl +100 -0
  11. package/src/date-field.tscl +140 -0
  12. package/src/date-segments.ts +55 -0
  13. package/src/dialog-close.tscl +7 -2
  14. package/src/dialog-trigger.tscl +7 -2
  15. package/src/dialog.tscl +10 -4
  16. package/src/{dismiss.js → dismiss.ts} +15 -18
  17. package/src/drag-drop.ts +116 -0
  18. package/src/field.tscl +42 -27
  19. package/src/fieldset.tscl +37 -0
  20. package/src/{focus-return.js → focus-return.ts} +2 -4
  21. package/src/form-errors.tscl +40 -0
  22. package/src/{index.js → index.ts} +8 -8
  23. package/src/menu-item.tscl +9 -3
  24. package/src/menu-list.tscl +1 -1
  25. package/src/menu.tscl +8 -5
  26. package/src/menubar.tscl +69 -0
  27. package/src/nav-menu-key.ts +5 -0
  28. package/src/nav-menu-panel.tscl +11 -0
  29. package/src/nav-menu-trigger.tscl +9 -0
  30. package/src/nav-menu.tscl +78 -0
  31. package/src/number-field.tscl +139 -0
  32. package/src/popover-trigger.tscl +7 -2
  33. package/src/popover.tscl +21 -14
  34. package/src/{position.js → position.ts} +21 -17
  35. package/src/preview-card-trigger.tscl +48 -0
  36. package/src/preview-card.tscl +27 -0
  37. package/src/radio-group.tscl +12 -7
  38. package/src/radio.tscl +13 -6
  39. package/src/{resize-column.js → resize-column.ts} +12 -16
  40. package/src/{roving-focus.js → roving-focus.ts} +19 -20
  41. package/src/select.tscl +27 -17
  42. package/src/sidebar-trigger.tscl +9 -6
  43. package/src/{sidebar.js → sidebar.ts} +5 -3
  44. package/src/sidebar.tscl +11 -4
  45. package/src/sortable-list.tscl +50 -0
  46. package/src/swipe.ts +79 -0
  47. package/src/switch.tscl +15 -7
  48. package/src/tab-panel.tscl +9 -4
  49. package/src/tab.tscl +11 -6
  50. package/src/tabs.tscl +12 -7
  51. package/src/time-field.tscl +139 -0
  52. package/src/toast-region.tscl +10 -4
  53. package/src/toast.ts +61 -0
  54. package/src/toggle-group.tscl +15 -9
  55. package/src/toggle-item.tscl +13 -7
  56. package/src/toggle.tscl +4 -1
  57. package/src/toolbar-separator.tscl +9 -0
  58. package/src/toolbar.tscl +30 -0
  59. package/src/tooltip-trigger.tscl +10 -6
  60. package/src/tooltip.tscl +11 -5
  61. package/src/{transition.js → transition.ts} +3 -6
  62. package/src/{typeahead.js → typeahead.ts} +20 -20
  63. package/src/virtual-grid.ts +100 -0
  64. package/src/virtual-keyboard.ts +36 -0
  65. package/src/virtual-list.ts +82 -0
  66. package/src/dismiss.d.ts +0 -27
  67. package/src/focus-return.d.ts +0 -10
  68. package/src/index.d.ts +0 -8
  69. package/src/position.d.ts +0 -36
  70. package/src/resize-column.d.ts +0 -18
  71. package/src/roving-focus.d.ts +0 -39
  72. package/src/sidebar.d.ts +0 -8
  73. package/src/toast.d.ts +0 -48
  74. package/src/toast.js +0 -56
  75. package/src/transition.d.ts +0 -18
  76. package/src/typeahead.d.ts +0 -35
@@ -0,0 +1,27 @@
1
+ {{--
2
+ Preview card (#681): a card that opens while a link is hovered or focused (a link preview), placed next to its
3
+ <preview-card-trigger> by CSS anchor positioning, flipping when there's no room. A hint popover: it doesn't close
4
+ other popovers. Supplementary, like a tooltip with more in it: the link works on its own, so put nothing in the card
5
+ that someone needs and can't get from the link.
6
+
7
+ <preview-card-trigger target="repo-card" href="https://github.com/forinda/clarusjs">clarusjs</preview-card-trigger>
8
+ <preview-card id="repo-card">…</preview-card>
9
+ --}}
10
+ <script lang="ts">
11
+ const { id, side = 'bottom' } = props<{
12
+ /** Its id, unique on the page: a <preview-card-trigger> targets it. */
13
+ id: string
14
+ /** Which side of the link it opens on (the other one when there's no room). */
15
+ side?: 'top' | 'bottom' | 'left' | 'right'
16
+ }>()
17
+ </script>
18
+
19
+ <div
20
+ [id]="id"
21
+ popover="hint"
22
+ data-part="preview-card"
23
+ [data-side]="side"
24
+ [style]="'position-anchor: --' + id + '; position-area: ' + side + '; position-try-fallbacks: flip-block, flip-inline'"
25
+ >
26
+ <slot />
27
+ </div>
@@ -9,14 +9,19 @@
9
9
  <radio value="m" label="Medium" />
10
10
  </radio-group>
11
11
  --}}
12
- @input name: string
13
- @input label: string
14
- @model value: string = ''
15
- @event change: string
16
12
 
17
- <script>
13
+ <script lang="ts">
18
14
  import { effect } from 'clarusjs/runtime'
19
- provide('radio', { name, value })
15
+ const props = props<{
16
+ /** Sends the value with a form, under this name; its radios share it. */
17
+ name: string
18
+ /** Names the group for screen readers (its legend). */
19
+ label: string
20
+ }>()
21
+ /** The selected radio's value, bound two ways with [(value)]. */
22
+ let value = model<string>(() => '')
23
+ const emit = events<{ change: string }>()
24
+ provide('radio', { name: props.name, value })
20
25
  // The radios are the page's server HTML: kept in step with value here, and their changes go through (change).
21
26
  function wire(el) {
22
27
  const own = (node) => node.closest('[data-part="radio-group"]') === el
@@ -48,6 +53,6 @@
48
53
  </script>
49
54
 
50
55
  <fieldset data-part="radio-group" [use]="wire">
51
- <legend data-part="legend">{{ label }}</legend>
56
+ <legend data-part="legend">{{ props.label }}</legend>
52
57
  <slot />
53
58
  </fieldset>
package/src/radio.tscl CHANGED
@@ -1,10 +1,17 @@
1
1
  {{-- Radio (#465): one native radio and its label, inside <radio-group>, which names it and says whether it's checked. --}}
2
2
  @context radio: { name: string; value: string }
3
- @input value: string
4
- @input label: string
5
- @input disabled: boolean = false
3
+ <script lang="ts">
4
+ const props = props<{
5
+ /** What the group's value becomes when it's selected. */
6
+ value: string
7
+ /** The radio's label. */
8
+ label: string
9
+ /** Can't be used or focused. */
10
+ disabled?: boolean
11
+ }>(() => ({ disabled: false }))
12
+ </script>
6
13
 
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>
14
+ <label data-part="radio" [data-checked]="props.value === radio.value ? '' : false" [data-disabled]="props.disabled ? '' : false">
15
+ <input type="radio" [name]="radio.name" [value]="props.value" [checked]="props.value === radio.value" [disabled]="props.disabled" />
16
+ <span data-part="label">{{ props.label }}</span>
10
17
  </label>
@@ -4,53 +4,49 @@
4
4
  /**
5
5
  * Make `handle` (inside a `<th>`) a vertical separator that resizes its column: drag it, or focus it and use the
6
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}
7
+ * @param options `target`: the element to size (the handle's `<th>` unless you say).
11
8
  * @example
12
9
  * <th>Name <span [use]="(el) => resizeColumn(el, { onResize: (w) => save('name', w) })" aria-label="Resize Name"></span></th>
13
10
  * @see https://clarusjs.com/guide/ui-primitives#resizecolumn
14
11
  */
15
- export function resizeColumn(handle, { min = 48, max = 1e4, step = 16, target, onResize } = {}) {
16
- const col = /** @type {HTMLElement} */ (target ?? handle.closest('th'))
12
+ export function resizeColumn(
13
+ handle: HTMLElement,
14
+ { min = 48, max = 1e4, step = 16, target, onResize }: { min?: number, max?: number, step?: number, target?: HTMLElement, onResize?: (width: number) => void } = {},
15
+ ): () => void {
16
+ const col = (target ?? handle.closest('th')) as HTMLElement
17
17
  const sign = () => (handle.closest('[dir]')?.getAttribute('dir') === 'rtl' ? -1 : 1)
18
18
  let width = Math.round(col.getBoundingClientRect().width)
19
19
  let from = 0
20
20
  let start = NaN
21
21
  const attrs = { role: 'separator', 'aria-orientation': 'vertical', tabindex: '0', 'aria-valuemin': min, 'aria-valuemax': max }
22
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) => {
23
+ const set = (w: number, done: boolean) => {
24
24
  width = Math.round(Math.min(max, Math.max(min, w)))
25
25
  col.style.width = width + 'px'
26
26
  handle.setAttribute('aria-valuenow', String(width))
27
27
  if (done) onResize?.(width)
28
28
  }
29
29
  handle.setAttribute('aria-valuenow', String(width))
30
- /** @param {PointerEvent} e */
31
- const down = (e) => {
30
+ const down = (e: PointerEvent) => {
32
31
  if (e.button) return
33
32
  e.preventDefault()
34
33
  start = e.clientX
35
34
  from = width
36
35
  handle.setPointerCapture?.(e.pointerId)
37
36
  }
38
- /** @param {PointerEvent} e */
39
- const move = (e) => start === start && set(from + (e.clientX - start) * sign(), false)
37
+ const move = (e: PointerEvent) => start === start && set(from + (e.clientX - start) * sign(), false)
40
38
  const up = () => {
41
39
  if (start !== start) return
42
40
  start = NaN
43
41
  set(width, true)
44
42
  }
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]
43
+ const key = (e: KeyboardEvent) => {
44
+ const by = ({ ArrowRight: width + step * sign(), ArrowLeft: width - step * sign(), Home: min, End: max } as Record<string, number | undefined>)[e.key]
48
45
  if (by === undefined) return
49
46
  e.preventDefault()
50
47
  set(by, true)
51
48
  }
52
- /** @type {[string, (e: any) => void][]} */
53
- const on = [['pointerdown', down], ['pointermove', move], ['pointerup', up], ['pointercancel', up], ['keydown', key]]
49
+ const on: [string, (e: any) => void][] = [['pointerdown', down], ['pointermove', move], ['pointerup', up], ['pointercancel', up], ['keydown', key]]
54
50
  for (const [t, f] of on) handle.addEventListener(t, f)
55
51
  return () => { for (const [t, f] of on) handle.removeEventListener(t, f) }
56
52
  }
@@ -2,40 +2,40 @@
2
2
  // Ported from Radix's RovingFocusGroup and Base UI's composite: getFocusIntent's key map, RTL, disabled items skipped.
3
3
 
4
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'
5
+ export const disabled = (el: Element) => el.hasAttribute('disabled') || el.getAttribute('aria-disabled') === 'true'
6
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
- */
7
+ /** Which items rovingFocus() moves between, and how. */
8
+ export interface RovingFocusOptions {
9
+ /** Which descendants are the items (a selector). Default: tabs, menu items, options, buttons. */
10
+ items?: string
11
+ /** Which arrows move. Default 'horizontal'. */
12
+ orientation?: 'horizontal' | 'vertical' | 'both'
13
+ /** From the last item on to the first, and back. Default true. */
14
+ loop?: boolean
15
+ /** Called with the item an arrow key moved to (after focusing it). */
16
+ onMove?: (item: HTMLElement) => void
17
+ }
14
18
 
15
19
  /**
16
20
  * One Tab stop for the group `el`: its current item has tabindex 0, the others -1; arrow keys (by orientation, flipped
17
21
  * in right-to-left text), Home and End move focus between the enabled items. The current item is the one focused last,
18
22
  * 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.
23
+ * @returns Stops it.
22
24
  * @example
23
25
  * <div role="toolbar" [use]="(el) => rovingFocus(el)">…</div>
24
26
  * @see https://clarusjs.com/guide/ui-primitives#rovingfocus
25
27
  */
26
- export function rovingFocus(el, options = {}) {
28
+ export function rovingFocus(el: HTMLElement, options: RovingFocusOptions = {}): () => void {
27
29
  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)])
30
+ const all = () => [...el.querySelectorAll(items)] as HTMLElement[]
29
31
  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 settle = (current: HTMLElement | undefined) => { for (const item of all()) item.tabIndex = item === current ? 0 : -1 }
32
33
  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
34
  settle(marked ?? enabled()[0])
34
35
 
35
- /** @param {KeyboardEvent} event */
36
- const keydown = (event) => {
36
+ const keydown = (event: KeyboardEvent) => {
37
37
  const list = enabled()
38
- const at = list.indexOf(/** @type {HTMLElement} */ (event.target))
38
+ const at = list.indexOf(event.target as HTMLElement)
39
39
  if (at === -1) return
40
40
  const rtl = getComputedStyle(el).direction === 'rtl' || el.closest('[dir]')?.getAttribute('dir') === 'rtl'
41
41
  const keys = {
@@ -54,8 +54,7 @@ export function rovingFocus(el, options = {}) {
54
54
  item?.focus()
55
55
  if (item && onMove) onMove(item)
56
56
  }
57
- /** @param {FocusEvent} event */
58
- const focusin = (event) => { if (all().includes(/** @type {HTMLElement} */ (event.target))) settle(/** @type {HTMLElement} */ (event.target)) }
57
+ const focusin = (event: FocusEvent) => { if (all().includes(event.target as HTMLElement)) settle(event.target as HTMLElement) }
59
58
  el.addEventListener('keydown', keydown)
60
59
  el.addEventListener('focusin', focusin)
61
60
  return () => {
package/src/select.tscl CHANGED
@@ -4,36 +4,46 @@
4
4
  with CSS; elsewhere, the usual native one. Bind it two ways: <select-field id="size" label="Size" [options]="sizes"
5
5
  [(value)]="form.size" />. It sends (change) with the new value.
6
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
7
 
17
- <script>
8
+ <script lang="ts">
9
+ type Option = { value: string; label: string; disabled?: boolean }
10
+ const props = props<{
11
+ /** Its id, unique on the page: the select's id. */
12
+ id: string
13
+ /** The field's label. */
14
+ label: string
15
+ /** What can be picked: each has a value, a label and optionally disabled. */
16
+ options: Option[]
17
+ /** Sends the value with a form, under this name. */
18
+ name?: string
19
+ /** Can't be used or focused. */
20
+ disabled?: boolean
21
+ /** A disabled first option shown while nothing is picked. */
22
+ placeholder?: string
23
+ }>(() => ({ name: '', disabled: false, placeholder: '' }))
24
+ /** The selected option's value, bound two ways with [(value)]. */
25
+ let value = model<string>(() => '')
26
+ const emit = events<{ change: string }>()
27
+
18
28
  function changed(event) {
19
29
  emit('change', event.target.value, { reason: 'select', event })
20
30
  }
21
31
  </script>
22
32
 
23
33
  <div data-part="select">
24
- <label [for]="id" data-part="label">{{ label }}</label>
34
+ <label [for]="props.id" data-part="label">{{ props.label }}</label>
25
35
  <select
26
- [id]="id"
27
- [name]="name || false"
28
- [disabled]="disabled"
36
+ [id]="props.id"
37
+ [name]="props.name || false"
38
+ [disabled]="props.disabled"
29
39
  [(value)]="value"
30
40
  (change)="changed($event)"
31
41
  data-part="control"
32
42
  >
33
- @if (placeholder) {
34
- <option value="" disabled>{{ placeholder }}</option>
43
+ @if (props.placeholder) {
44
+ <option value="" disabled>{{ props.placeholder }}</option>
35
45
  }
36
- @for (o of options; track o.value) {
46
+ @for (o of props.options; track o.value) {
37
47
  <option [value]="o.value" [disabled]="o.disabled ?? false" data-part="option">{{ o.label }}</option>
38
48
  }
39
49
  </select>
@@ -2,14 +2,17 @@
2
2
  Sidebar trigger: collapses the sidebar on a wide screen (min-width 768px); on a phone it is the sidebar's popover
3
3
  invoker (popovertarget), so the browser opens it next to the button in the Tab order. aria-expanded follows both.
4
4
  --}}
5
- @input target: string
6
5
 
7
- <script>
6
+ <script lang="ts">
8
7
  import { toggleSidebar } from '@clarusjs/ui-primitives'
8
+ const props = props<{
9
+ /** The id of the sidebar it collapses or opens. */
10
+ target: string
11
+ }>()
9
12
  const WIDE = '(min-width: 768px)'
10
13
  let self // the button, from wire (events are delegated to the island, so currentTarget isn't it)
11
14
  function toggle(event) {
12
- const nav = document.getElementById(target)
15
+ const nav = document.getElementById(props.target)
13
16
  if (!(nav instanceof HTMLElement) || !matchMedia(WIDE).matches) return // a phone: the popover toggles itself
14
17
  event.preventDefault()
15
18
  const next = toggleSidebar(nav)
@@ -17,7 +20,7 @@
17
20
  }
18
21
  function wire(button) {
19
22
  self = button
20
- const nav = document.getElementById(target)
23
+ const nav = document.getElementById(props.target)
21
24
  if (!(nav instanceof HTMLElement)) return
22
25
  const media = matchMedia(WIDE)
23
26
  const sync = () => {
@@ -43,8 +46,8 @@
43
46
 
44
47
  <button
45
48
  type="button"
46
- [aria-controls]="target"
47
- [popovertarget]="target"
49
+ [aria-controls]="props.target"
50
+ [popovertarget]="props.target"
48
51
  aria-expanded="true"
49
52
  data-part="sidebar-trigger"
50
53
  [use]="wire"
@@ -1,16 +1,18 @@
1
1
  // The sidebar's collapsed state, on <html data-sidebar="open|collapsed">, remembered in localStorage. SIDEBAR_SCRIPT
2
2
  // goes inline in the <head> so a remembered collapse applies before the first paint (no jump on load).
3
3
 
4
+ /** The localStorage key the sidebar's collapsed state is remembered under. */
4
5
  export const SIDEBAR_KEY = 'ui-sidebar'
5
6
 
7
+ /** An inline script for the page's <head> (from transformHead in src/head.ts): applies a remembered collapse before the first paint. */
6
8
  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
9
 
8
10
  /**
9
11
  * 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
+ * @param nav the sidebar (its data-state follows)
13
+ * @returns the new state
12
14
  */
13
- export function toggleSidebar(nav) {
15
+ export function toggleSidebar(nav: HTMLElement | null): 'open' | 'collapsed' {
14
16
  const root = document.documentElement
15
17
  const current = root.dataset.sidebar ?? nav?.dataset.state ?? 'open'
16
18
  const next = current === 'collapsed' ? 'open' : 'collapsed'
package/src/sidebar.tscl CHANGED
@@ -6,8 +6,15 @@
6
6
  <sidebar id="app-nav" label="Main">…links…</sidebar>
7
7
  <sidebar-trigger target="app-nav">Menu</sidebar-trigger>
8
8
  --}}
9
- @input id: string
10
- @input label: string = 'Sidebar'
11
- @input collapsed: boolean = false
9
+ <script lang="ts">
10
+ const props = props<{
11
+ /** Its id, unique on the page: a <sidebar-trigger> targets it. */
12
+ id: string
13
+ /** Names the navigation for screen readers. */
14
+ label?: string
15
+ /** Starts collapsed (on a wide screen). */
16
+ collapsed?: boolean
17
+ }>(() => ({ label: 'Sidebar', collapsed: false }))
18
+ </script>
12
19
 
13
- <nav [id]="id" [aria-label]="label" popover="auto" data-part="sidebar" [data-state]="collapsed ? 'collapsed' : 'open'"><slot /></nav>
20
+ <nav [id]="props.id" [aria-label]="props.label" popover="auto" data-part="sidebar" [data-state]="props.collapsed ? 'collapsed' : 'open'"><slot /></nav>
@@ -0,0 +1,50 @@
1
+ {{--
2
+ Sortable list (#685): reorder a list by dragging (pointer or touch) or with the keyboard: Space picks an item up, ↑ ↓
3
+ move it, Space drops it, Escape puts it back, each step announced (drag-drop.js). Bind the order with [(value)], a
4
+ list of values; [labels] gives each one its text (the value itself otherwise). Items slide to their new places (FLIP),
5
+ except for visitors who ask for less motion.
6
+
7
+ <sortable-list label="Priorities" [labels]="names" [(value)]="order" />
8
+ --}}
9
+ <script lang="ts">
10
+ import { dragDrop } from './drag-drop.ts'
11
+ const { label, labels = {} } = props<{
12
+ /** What the list is, as its accessible name (and the zone's name in announcements). */
13
+ label: string
14
+ /** Each value's text. */
15
+ labels?: Record<string, string>
16
+ }>()
17
+ /** The values, in their order. */
18
+ let value = model<string[]>(() => [])
19
+
20
+ const still = () => matchMedia('(prefers-reduced-motion: reduce)').matches
21
+ function wire(el: HTMLElement): () => void {
22
+ return dragDrop(el, {
23
+ onDrop: ({ item, index }) => {
24
+ const moved = item.dataset.drag!
25
+ const rows = () => [...el.querySelectorAll<HTMLElement>('[data-drag]')]
26
+ const before = new Map(rows().map((r) => [r, r.getBoundingClientRect().top]))
27
+ const next = value.filter((v) => v !== moved)
28
+ next.splice(index, 0, moved)
29
+ value = next
30
+ // FLIP: each row from where it was to where it is now
31
+ if (still()) return
32
+ queueMicrotask(() => {
33
+ for (const r of rows()) {
34
+ const from = before.get(r)
35
+ const dy = from === undefined ? 0 : from - r.getBoundingClientRect().top
36
+ if (dy) r.animate?.([{ transform: `translateY(${dy}px)` }, { transform: 'none' }], { duration: 200, easing: 'ease' })
37
+ }
38
+ })
39
+ },
40
+ })
41
+ }
42
+ </script>
43
+
44
+ <div data-part="sortable-list" [use]="wire">
45
+ <ul data-drop="list" [aria-label]="label" data-part="sortable-items">
46
+ @for (v of value; track v) {
47
+ <li data-part="sortable-item" [data-drag]="v" tabindex="0">{{ labels[v] ?? v }}</li>
48
+ }
49
+ </ul>
50
+ </div>
package/src/swipe.ts ADDED
@@ -0,0 +1,79 @@
1
+ // Swipe (#686): an element dragged along one axis by the pointer or a finger; released far enough (or fast), it's
2
+ // swiped. Dismiss a toast, reveal a row's actions, drag a sheet closed. A gesture is only a shortcut (WCAG 2.5.1):
3
+ // whatever a swipe does, a button or key must do too. Not in the package's index: import
4
+ // '@clarusjs/ui-primitives/swipe.js'.
5
+
6
+ /** Which way a swipe went. */
7
+ export type Direction = 'left' | 'right' | 'up' | 'down'
8
+
9
+ /** What swipe() calls once a swipe passes its threshold, and on which axis. */
10
+ export interface SwipeOptions {
11
+ /** Swiped: close it, remove it, mark it open. */
12
+ onSwipe: (direction: Direction) => void
13
+ /** Default 'x'. */
14
+ axis?: 'x' | 'y'
15
+ /** The ways it moves; default both ways along the axis. */
16
+ directions?: Direction[]
17
+ /** How far, as a share of its size, counts as a swipe. Default 0.4. */
18
+ threshold?: number
19
+ }
20
+
21
+ /**
22
+ * `el` follows the pointer along `axis` (data-swipe = the axis, data-swiping while it moves) and, let go past the
23
+ * threshold or faster than 0.5 px/ms, calls onSwipe; then it goes back to its place (the transform is cleared), so
24
+ * onSwipe decides where it ends up. Presses on buttons, links and fields stay clicks.
25
+ * @returns Stops it.
26
+ */
27
+ export function swipe(el: HTMLElement, options: SwipeOptions): () => void {
28
+ const { onSwipe, axis = 'x', threshold = 0.4 } = options
29
+ const x = axis === 'x'
30
+ const ways = options.directions ?? (x ? ['left', 'right'] : ['up', 'down'])
31
+ const way = (d: number) => (x ? (d > 0 ? 'right' : 'left') : d > 0 ? 'down' : 'up') as Direction
32
+ let start: number | null = null
33
+ let t0 = 0, d = 0
34
+ const at = (event: PointerEvent) => (x ? event.clientX : event.clientY)
35
+ el.dataset.swipe = axis
36
+ const on: [string, (event: any) => void][] = [
37
+ ['pointerdown', (event: PointerEvent) => {
38
+ if (event.button || (event.target as Element).closest('button, a, input, select, textarea, label')) return
39
+ start = at(event); t0 = event.timeStamp; d = 0
40
+ }],
41
+ ['pointermove', (event: PointerEvent) => {
42
+ if (start === null) return
43
+ const v = at(event) - start
44
+ d = v && ways.includes(way(v)) ? v : 0
45
+ if (!el.hasAttribute('data-swiping')) {
46
+ if (Math.abs(d) < 4) return
47
+ el.setPointerCapture?.(event.pointerId)
48
+ el.setAttribute('data-swiping', '')
49
+ }
50
+ el.style.transform = d ? `translate${x ? 'X' : 'Y'}(${d}px)` : ''
51
+ }],
52
+ ['pointerup', (event: PointerEvent) => {
53
+ if (start === null) return
54
+ start = null
55
+ const moved = el.hasAttribute('data-swiping')
56
+ el.removeAttribute('data-swiping')
57
+ const far = Math.abs(d) > (x ? el.offsetWidth : el.offsetHeight) * threshold
58
+ const fast = Math.abs(d) / Math.max(1, event.timeStamp - t0) > 0.5
59
+ if (moved && d && (far || fast)) onSwipe(way(d))
60
+ el.style.transform = ''
61
+ }],
62
+ ['pointercancel', () => { start = null; el.removeAttribute('data-swiping'); el.style.transform = '' }],
63
+ ]
64
+ for (const [type, fn] of on) el.addEventListener(type, fn)
65
+ return () => { for (const [type, fn] of on) el.removeEventListener(type, fn) }
66
+ }
67
+
68
+ /** The way each side's sheet is dragged closed. */
69
+ const AWAY = { left: 'left', right: 'right', top: 'up', bottom: 'down' } as const
70
+
71
+ /**
72
+ * A sheet (a dialog with data-side) dragged toward its side closes. Escape and its close button still do.
73
+ * @returns Stops it.
74
+ */
75
+ export function swipeToClose(dialog: HTMLDialogElement): () => void {
76
+ const side = (dialog.dataset.side ?? 'right') as keyof typeof AWAY
77
+ const away = AWAY[side] ?? 'right'
78
+ return swipe(dialog, { axis: side === 'top' || side === 'bottom' ? 'y' : 'x', directions: [away], onSwipe: () => dialog.close() })
79
+ }
package/src/switch.tscl CHANGED
@@ -3,12 +3,20 @@
3
3
  keyboard as is. Bind it two ways: <switch label="Wi-Fi" [(checked)]="settings.wifi" />. data-checked and
4
4
  data-disabled on the label are for styling.
5
5
  --}}
6
- @input label: string
7
- @input name: string = ''
8
- @input disabled: boolean = false
9
- @model checked: boolean = false
6
+ <script lang="ts">
7
+ const props = props<{
8
+ /** The switch's label. */
9
+ label: string
10
+ /** Sends the value with a form, under this name. */
11
+ name?: string
12
+ /** Can't be used or focused. */
13
+ disabled?: boolean
14
+ }>(() => ({ name: '', disabled: false }))
15
+ /** Whether it's on, bound two ways with [(checked)]. */
16
+ let checked = model<boolean>(() => false)
17
+ </script>
10
18
 
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>
19
+ <label data-part="switch" [data-checked]="checked ? '' : false" [data-disabled]="props.disabled ? '' : false">
20
+ <input type="checkbox" role="switch" [name]="props.name || false" [disabled]="props.disabled" [(checked)]="checked" />
21
+ <span data-part="label">{{ props.label }}</span>
14
22
  </label>
@@ -3,14 +3,19 @@
3
3
  it's the selected one, so nothing jumps when the page loads.
4
4
  --}}
5
5
  @context tabs: { id: string; value: string }
6
- @input value: string
6
+ <script lang="ts">
7
+ const props = props<{
8
+ /** The value of the <tab> it belongs to; it shows while that tab is selected. */
9
+ value: string
10
+ }>()
11
+ </script>
7
12
 
8
13
  <div
9
14
  role="tabpanel"
10
- [id]="tabs.id + '-panel-' + value"
11
- [aria-labelledby]="tabs.id + '-tab-' + value"
15
+ [id]="tabs.id + '-panel-' + props.value"
16
+ [aria-labelledby]="tabs.id + '-tab-' + props.value"
12
17
  tabindex="0"
13
- [hidden]="value !== tabs.value"
18
+ [hidden]="props.value !== tabs.value"
14
19
  data-part="panel"
15
20
  >
16
21
  <slot />
package/src/tab.tscl CHANGED
@@ -1,15 +1,20 @@
1
1
  {{-- Tab (#465): one tab inside <tab-list>; [value] names the <tab-panel> it shows. Selected state comes from <tabs>. --}}
2
2
  @context tabs: { id: string; value: string }
3
- @input value: string
3
+ <script lang="ts">
4
+ const props = props<{
5
+ /** Its value: names the <tab-panel> it shows and is what <tabs>' value becomes when it's selected. */
6
+ value: string
7
+ }>()
8
+ </script>
4
9
 
5
10
  <button
6
11
  type="button"
7
12
  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
+ [id]="tabs.id + '-tab-' + props.value"
14
+ [aria-controls]="tabs.id + '-panel-' + props.value"
15
+ [aria-selected]="props.value === tabs.value ? 'true' : 'false'"
16
+ [tabindex]="props.value === tabs.value ? 0 : -1"
17
+ [data-value]="props.value"
13
18
  data-part="tab"
14
19
  >
15
20
  <slot />
package/src/tabs.tscl CHANGED
@@ -15,15 +15,20 @@
15
15
 
16
16
  Give each one its own id: its tabs and panels are named after it.
17
17
  --}}
18
- @input id: string
19
- @input label: string
20
- @model value: string
21
- @event change: string
22
18
 
23
- <script>
19
+ <script lang="ts">
24
20
  import { effect } from 'clarusjs/runtime'
25
21
  import { rovingFocus } from '@clarusjs/ui-primitives'
26
- provide('tabs', { id, label, value })
22
+ const props = props<{
23
+ /** Its id, unique on the page: its tabs' and panels' ids are made from it. */
24
+ id: string
25
+ /** Names the tab list for screen readers. */
26
+ label: string
27
+ }>()
28
+ /** The selected tab's value, bound two ways with [(value)]. */
29
+ let value = model<string>()
30
+ const emit = events<{ change: string }>()
31
+ provide('tabs', { id: props.id, label: props.label, value })
27
32
  // The tabs and panels are the page's server HTML (no markers of their own): wired here, by data-value and id.
28
33
  // Only this group's: a tabs group nested in a panel has its own.
29
34
  function wire(el) {
@@ -45,7 +50,7 @@
45
50
  tab.tabIndex = tab.dataset.value === selected ? 0 : -1
46
51
  }
47
52
  for (const panel of el.querySelectorAll('[role="tabpanel"]')) {
48
- if (own(panel)) panel.hidden = panel.id !== id + '-panel-' + selected
53
+ if (own(panel)) panel.hidden = panel.id !== props.id + '-panel-' + selected
49
54
  }
50
55
  })
51
56
  return () => {