@clarusjs/ui-primitives 0.0.0-stage → 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/LICENSE +21 -0
- package/README.md +48 -2
- package/package.json +52 -3
- 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/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 Felix Orinda
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/README.md
CHANGED
|
@@ -1,3 +1,49 @@
|
|
|
1
|
-
#
|
|
1
|
+
# @clarusjs/ui-primitives
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
Accessible behaviours for [clarusjs](https://clarusjs.com) components, as small DOM functions. Each takes an element
|
|
4
|
+
(and options) and returns how to stop it; a template hands it the element with `[use]`.
|
|
5
|
+
|
|
6
|
+
```sh
|
|
7
|
+
pnpm add @clarusjs/ui-primitives
|
|
8
|
+
```
|
|
9
|
+
|
|
10
|
+
```tscl
|
|
11
|
+
<script>
|
|
12
|
+
import { rovingFocus } from '@clarusjs/ui-primitives'
|
|
13
|
+
</script>
|
|
14
|
+
|
|
15
|
+
<div role="toolbar" aria-label="Formatting" [use]="(el) => rovingFocus(el)">
|
|
16
|
+
<button>Bold</button> <button>Italic</button>
|
|
17
|
+
</div>
|
|
18
|
+
```
|
|
19
|
+
|
|
20
|
+
| Behaviour | Does |
|
|
21
|
+
|---|---|
|
|
22
|
+
| `rovingFocus(el, { items, orientation, loop })` | one Tab stop for a group; arrow keys (RTL-aware), Home and End; disabled items skipped |
|
|
23
|
+
| `typeahead(el, { items, onMatch, timeout })` | typing jumps to the next item starting with it; a repeated key cycles |
|
|
24
|
+
| `dismiss(el, { onDismiss, exclude })` | Escape or a press outside closes it; stacked layers; a cancelable `cl:dismiss` event |
|
|
25
|
+
| `enter(el)`, `exit(el)` | `data-open` / `data-closed` with `data-starting-style` / `data-ending-style` for CSS transitions |
|
|
26
|
+
| `focusReturn(el)` | focus goes back to where it was when it stops |
|
|
27
|
+
| `position(floating, anchor, { side, align, offset, flip })` | places a popup by its anchor, in the window; `data-side` and CSS variables |
|
|
28
|
+
|
|
29
|
+
Each is its own module (all six: about 2.3 KB gzip), with no dependencies. Guide: https://clarusjs.com/guide/ui-primitives
|
|
30
|
+
|
|
31
|
+
## Parts
|
|
32
|
+
|
|
33
|
+
Headless `.tscl` components on the platform's own elements, no styles. Import them under your own names:
|
|
34
|
+
|
|
35
|
+
```tscl
|
|
36
|
+
@import uiDialog from '@clarusjs/ui-primitives/dialog.tscl'
|
|
37
|
+
```
|
|
38
|
+
|
|
39
|
+
| Part | Built on |
|
|
40
|
+
|---|---|
|
|
41
|
+
| `dialog`, `dialog-trigger`, `dialog-close` | `<dialog>` and command invokers (`commandfor`): no JavaScript |
|
|
42
|
+
| `popover`, `popover-trigger` | the `popover` attribute and CSS anchor positioning: no JavaScript |
|
|
43
|
+
| `tooltip`, `tooltip-trigger` | `popover="hint"` with anchor positioning; `interestfor` where the browser has it, else the trigger (an island) shows it on hover and focus |
|
|
44
|
+
| `menu`, `menu-trigger`, `menu-list`, `menu-item` | a popover list opened by its trigger (`popovertarget`); the menu (an island) adds focus, arrow keys, typeahead and `(select)` |
|
|
45
|
+
| `accordion-item` | `<details name>`: no JavaScript |
|
|
46
|
+
| `switch`, `checkbox` | a native checkbox in its label, `[(checked)]` |
|
|
47
|
+
| `tabs`, `tab-list`, `tab`, `tab-panel` | Radix-shaped; the parts read the selection from context, so the server writes their ARIA and `hidden`; the root is an island (`rovingFocus`) |
|
|
48
|
+
| `radio-group`, `radio` | native radios in a fieldset; the radios read the group from context; the group is an island (`[(value)]`, `(change)`) |
|
|
49
|
+
| `field` | label, input, description and error wired by id; Base UI's `data-touched`, `data-dirty`, `data-invalid` |
|
package/package.json
CHANGED
|
@@ -1,6 +1,55 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@clarusjs/ui-primitives",
|
|
3
|
-
"version": "0.
|
|
4
|
-
"
|
|
5
|
-
"
|
|
3
|
+
"version": "0.1.0",
|
|
4
|
+
"description": "Accessible behaviours for clarusjs components: roving focus, typeahead, dismiss, transitions, focus return and positioning, as small DOM functions for [use].",
|
|
5
|
+
"type": "module",
|
|
6
|
+
"license": "MIT",
|
|
7
|
+
"sideEffects": false,
|
|
8
|
+
"files": [
|
|
9
|
+
"src",
|
|
10
|
+
"LICENSE",
|
|
11
|
+
"README.md"
|
|
12
|
+
],
|
|
13
|
+
"exports": {
|
|
14
|
+
".": "./src/index.js",
|
|
15
|
+
"./*": "./src/*"
|
|
16
|
+
},
|
|
17
|
+
"publishConfig": {
|
|
18
|
+
"access": "public"
|
|
19
|
+
},
|
|
20
|
+
"peerDependencies": {
|
|
21
|
+
"clarusjs": "^0.15.0"
|
|
22
|
+
},
|
|
23
|
+
"engines": {
|
|
24
|
+
"node": "^22.12.0 || ^24.0.0 || >=26.0.0"
|
|
25
|
+
},
|
|
26
|
+
"repository": {
|
|
27
|
+
"type": "git",
|
|
28
|
+
"url": "git+https://github.com/forinda/clarusjs.git",
|
|
29
|
+
"directory": "packages/ui-primitives"
|
|
30
|
+
},
|
|
31
|
+
"keywords": [
|
|
32
|
+
"clarusjs",
|
|
33
|
+
"accessibility",
|
|
34
|
+
"headless",
|
|
35
|
+
"primitives",
|
|
36
|
+
"ui"
|
|
37
|
+
],
|
|
38
|
+
"homepage": "https://clarusjs.com/guide/ui-primitives",
|
|
39
|
+
"bugs": {
|
|
40
|
+
"url": "https://github.com/forinda/clarusjs/issues"
|
|
41
|
+
},
|
|
42
|
+
"devDependencies": {
|
|
43
|
+
"@types/node": "^26.6.6",
|
|
44
|
+
"clarusjs": "^0.15.0",
|
|
45
|
+
"happy-dom": "^21.0.4",
|
|
46
|
+
"typescript": "^7.0.2",
|
|
47
|
+
"vite": "^8.3.8",
|
|
48
|
+
"vitest": "^5.1.1"
|
|
49
|
+
},
|
|
50
|
+
"scripts": {
|
|
51
|
+
"types": "node scripts/types.mjs",
|
|
52
|
+
"test": "vitest run",
|
|
53
|
+
"typecheck": "pnpm types && tsc --noEmit"
|
|
54
|
+
}
|
|
6
55
|
}
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
{{--
|
|
2
|
+
Accordion item (#465): <details> and <summary>. Items with the same `group` are exclusive (the name attribute): one
|
|
3
|
+
open at a time. No JavaScript; find-in-page opens a closed one. Style it with details[open].
|
|
4
|
+
|
|
5
|
+
<accordion-item group="faq" [open]="true">
|
|
6
|
+
@slot('trigger') { Is it free? }
|
|
7
|
+
Yes.
|
|
8
|
+
</accordion-item>
|
|
9
|
+
--}}
|
|
10
|
+
@input group: string = ''
|
|
11
|
+
@input open: boolean = false
|
|
12
|
+
|
|
13
|
+
<details [name]="group || false" [open]="open" data-part="accordion-item">
|
|
14
|
+
<summary data-part="trigger"><slot name="trigger" /></summary>
|
|
15
|
+
<div data-part="panel"><slot /></div>
|
|
16
|
+
</details>
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
{{--
|
|
2
|
+
Checkbox (#465): a native checkbox inside its label. Bind it two ways: <checkbox label="I agree" [(checked)]="agreed" />.
|
|
3
|
+
data-checked and data-disabled on the label are for styling.
|
|
4
|
+
--}}
|
|
5
|
+
@input label: string
|
|
6
|
+
@input name: string = ''
|
|
7
|
+
@input disabled: boolean = false
|
|
8
|
+
@model checked: boolean = false
|
|
9
|
+
|
|
10
|
+
<label data-part="checkbox" [data-checked]="checked ? '' : false" [data-disabled]="disabled ? '' : false">
|
|
11
|
+
<input type="checkbox" [name]="name || false" [disabled]="disabled" [(checked)]="checked" />
|
|
12
|
+
<span data-part="label">{{ label }}</span>
|
|
13
|
+
</label>
|
|
@@ -0,0 +1,208 @@
|
|
|
1
|
+
{{--
|
|
2
|
+
Combobox (#466): pick one option, or several with [multiple]="true", from a list you can search by typing.
|
|
3
|
+
@import combobox from '@clarusjs/ui-primitives/combobox.tscl', or copy this file into your project. Bind the pick
|
|
4
|
+
with [(value)] (a string), or the picks with [(values)] (a list).
|
|
5
|
+
|
|
6
|
+
Headless: data-part names each element (combobox, label, control, chip, input, listbox, option, status), and the
|
|
7
|
+
option the keys are on has data-highlighted. Only what it needs to work is styled (the list below the input).
|
|
8
|
+
|
|
9
|
+
<combobox [id]="'country'" [label]="'Country'" [options]="countries" [(value)]="form.country" />
|
|
10
|
+
<combobox [id]="'topics'" [label]="'Topics'" [options]="topics" [multiple]="true" [(values)]="form.topics" />
|
|
11
|
+
|
|
12
|
+
Remote options: give [src] a URL; typing asks it for `src?q=<search>` (JSON: [{ value, label }]), 200 ms after the
|
|
13
|
+
last key, and the newest search wins. <combobox [id]="'city'" [label]="'City'" [src]="'/api/cities'" [(value)]="city" />
|
|
14
|
+
|
|
15
|
+
Keyboard: ↑ ↓ move, Home / End jump, Enter picks, Esc closes, Backspace in an empty search removes the last pick.
|
|
16
|
+
|
|
17
|
+
Each pick or removal sends (change) first, with the option and whether it's now selected; $info.reason is 'select',
|
|
18
|
+
'deselect' or 'remove', and $info.cancel() keeps it from happening:
|
|
19
|
+
<combobox … (change)="if (tooMany && $event.selected) $info.cancel()" />
|
|
20
|
+
Give each one its own id: its listbox and options are named after it.
|
|
21
|
+
--}}
|
|
22
|
+
@type Option = { value: string; label: string }
|
|
23
|
+
@input id: string = 'combobox'
|
|
24
|
+
@input label: string = 'Fruit'
|
|
25
|
+
@input options: Option[] = [
|
|
26
|
+
{ value: 'apple', label: 'Apple' },
|
|
27
|
+
{ value: 'apricot', label: 'Apricot' },
|
|
28
|
+
{ value: 'banana', label: 'Banana' },
|
|
29
|
+
{ value: 'cherry', label: 'Cherry' },
|
|
30
|
+
{ value: 'grape', label: 'Grape' },
|
|
31
|
+
{ value: 'mango', label: 'Mango' },
|
|
32
|
+
{ value: 'orange', label: 'Orange' },
|
|
33
|
+
{ value: 'pear', label: 'Pear' },
|
|
34
|
+
]
|
|
35
|
+
@input multiple: boolean = false
|
|
36
|
+
@input placeholder: string = 'Search…'
|
|
37
|
+
@input src: string = ''
|
|
38
|
+
@model value: string = ''
|
|
39
|
+
@model values: string[] = []
|
|
40
|
+
@event change: { value: string; label: string; selected: boolean }
|
|
41
|
+
|
|
42
|
+
<script>
|
|
43
|
+
import { json, resource } from 'clarusjs/fetch'
|
|
44
|
+
let query = state('')
|
|
45
|
+
let term = state('') // the search sent to src: the query, 200 ms after the last key
|
|
46
|
+
let known = state({}) // labels of remote options picked, by value (the next search may not list them)
|
|
47
|
+
let timer
|
|
48
|
+
let open = state(false)
|
|
49
|
+
let active = state(0)
|
|
50
|
+
// What's chosen, as a list in both modes.
|
|
51
|
+
const chosen = derived(() => (multiple ? values : value ? [value] : []))
|
|
52
|
+
// With src: the server's answer for the term, cached by it; nothing is asked until the list opens.
|
|
53
|
+
const remote = resource(
|
|
54
|
+
({ signal }) =>
|
|
55
|
+
src && open ? json(src + (src.includes('?') ? '&' : '?') + 'q=' + encodeURIComponent(term), { signal }) : false,
|
|
56
|
+
{ key: () => (src && open ? ['combobox', src, term] : false) },
|
|
57
|
+
)
|
|
58
|
+
// The options matching what's typed (by label, ignoring case), or the server's.
|
|
59
|
+
const shown = derived(() => {
|
|
60
|
+
if (src) return remote.value ?? []
|
|
61
|
+
const q = query.trim().toLowerCase()
|
|
62
|
+
return q ? options.filter((o) => o.label.toLowerCase().includes(q)) : options
|
|
63
|
+
})
|
|
64
|
+
const labelOf = (v) => known[v] ?? options.find((o) => o.value === v)?.label ?? v
|
|
65
|
+
const status = derived(() => {
|
|
66
|
+
if (!open) return ''
|
|
67
|
+
if (src && remote.loading) return 'Searching…'
|
|
68
|
+
if (src && remote.error) return "Couldn't load the options."
|
|
69
|
+
return shown.length ? '' : 'No match for "' + query + '"'
|
|
70
|
+
})
|
|
71
|
+
|
|
72
|
+
function show() {
|
|
73
|
+
open = true
|
|
74
|
+
active = Math.max(
|
|
75
|
+
0,
|
|
76
|
+
shown.findIndex((o) => chosen.includes(o.value)),
|
|
77
|
+
)
|
|
78
|
+
}
|
|
79
|
+
function pick(option, event) {
|
|
80
|
+
const selected = !(multiple && chosen.includes(option.value))
|
|
81
|
+
if (
|
|
82
|
+
!emit(
|
|
83
|
+
'change',
|
|
84
|
+
{ value: option.value, label: option.label, selected },
|
|
85
|
+
{ reason: selected ? 'select' : 'deselect', event },
|
|
86
|
+
)
|
|
87
|
+
)
|
|
88
|
+
return
|
|
89
|
+
if (src) known = { ...known, [option.value]: option.label }
|
|
90
|
+
if (multiple) {
|
|
91
|
+
values = chosen.includes(option.value) ? chosen.filter((v) => v !== option.value) : [...chosen, option.value]
|
|
92
|
+
} else {
|
|
93
|
+
value = option.value
|
|
94
|
+
open = false
|
|
95
|
+
}
|
|
96
|
+
query = ''
|
|
97
|
+
term = ''
|
|
98
|
+
}
|
|
99
|
+
function remove(v, event) {
|
|
100
|
+
if (!emit('change', { value: v, label: labelOf(v), selected: false }, { reason: 'remove', event })) return
|
|
101
|
+
values = chosen.filter((x) => x !== v)
|
|
102
|
+
}
|
|
103
|
+
function type(text) {
|
|
104
|
+
query = text
|
|
105
|
+
open = true
|
|
106
|
+
active = 0
|
|
107
|
+
clearTimeout(timer)
|
|
108
|
+
timer = setTimeout(() => (term = text), src ? 200 : 0)
|
|
109
|
+
}
|
|
110
|
+
function key(event) {
|
|
111
|
+
const last = shown.length - 1
|
|
112
|
+
if (event.key === 'ArrowDown' || event.key === 'ArrowUp') {
|
|
113
|
+
event.preventDefault()
|
|
114
|
+
if (!open) return show()
|
|
115
|
+
active = event.key === 'ArrowDown' ? Math.min(active + 1, last) : Math.max(active - 1, 0)
|
|
116
|
+
} else if (open && (event.key === 'Home' || event.key === 'End')) {
|
|
117
|
+
event.preventDefault()
|
|
118
|
+
active = event.key === 'Home' ? 0 : last
|
|
119
|
+
} else if (event.key === 'Enter' && open && shown[active]) {
|
|
120
|
+
event.preventDefault()
|
|
121
|
+
pick(shown[active], event)
|
|
122
|
+
} else if (event.key === 'Escape' && open) {
|
|
123
|
+
event.preventDefault()
|
|
124
|
+
open = false
|
|
125
|
+
query = '' // back to what's chosen
|
|
126
|
+
} else if (event.key === 'Backspace' && multiple && !query && chosen.length) {
|
|
127
|
+
remove(chosen[chosen.length - 1], event)
|
|
128
|
+
}
|
|
129
|
+
}
|
|
130
|
+
// Closes when focus leaves the whole combobox (a click outside, Tab away).
|
|
131
|
+
function leave(event) {
|
|
132
|
+
if (!event.currentTarget.contains(event.relatedTarget)) {
|
|
133
|
+
open = false
|
|
134
|
+
query = ''
|
|
135
|
+
}
|
|
136
|
+
}
|
|
137
|
+
</script>
|
|
138
|
+
|
|
139
|
+
<div data-part="combobox" (focusout)="leave($event)">
|
|
140
|
+
<label [for]="id + '-input'" data-part="label">{{ label }}</label>
|
|
141
|
+
<div data-part="control">
|
|
142
|
+
@if (multiple) {
|
|
143
|
+
@for (v of chosen; track v) {
|
|
144
|
+
<span data-part="chip"
|
|
145
|
+
>{{ labelOf(v) }}
|
|
146
|
+
<button type="button" [aria-label]="'Remove ' + labelOf(v)" (click)="remove(v, $event)">×</button></span
|
|
147
|
+
>
|
|
148
|
+
}
|
|
149
|
+
}
|
|
150
|
+
<input
|
|
151
|
+
[id]="id + '-input'"
|
|
152
|
+
role="combobox"
|
|
153
|
+
autocomplete="off"
|
|
154
|
+
data-part="input"
|
|
155
|
+
aria-autocomplete="list"
|
|
156
|
+
[aria-expanded]="open ? 'true' : 'false'"
|
|
157
|
+
[aria-controls]="id + '-list'"
|
|
158
|
+
[aria-activedescendant]="open && shown[active] ? id + '-' + shown[active].value : false"
|
|
159
|
+
[placeholder]="multiple || !chosen.length ? placeholder : false"
|
|
160
|
+
[value]="open || multiple ? query : labelOf(value)"
|
|
161
|
+
(input)="type($event.target.value)"
|
|
162
|
+
(click)="show()"
|
|
163
|
+
(keydown)="key($event)"
|
|
164
|
+
/>
|
|
165
|
+
</div>
|
|
166
|
+
<ul
|
|
167
|
+
[id]="id + '-list'"
|
|
168
|
+
role="listbox"
|
|
169
|
+
[aria-label]="label"
|
|
170
|
+
[aria-multiselectable]="multiple ? 'true' : false"
|
|
171
|
+
[hidden]="!open"
|
|
172
|
+
(mousedown)="$event.preventDefault()"
|
|
173
|
+
data-part="listbox"
|
|
174
|
+
>
|
|
175
|
+
@for (option of shown; track option.value) {
|
|
176
|
+
<li
|
|
177
|
+
[id]="id + '-' + option.value"
|
|
178
|
+
role="option"
|
|
179
|
+
[aria-selected]="chosen.includes(option.value) ? 'true' : 'false'"
|
|
180
|
+
[data-highlighted]="$index === active ? '' : false"
|
|
181
|
+
data-part="option"
|
|
182
|
+
(click)="pick(option, $event)"
|
|
183
|
+
>
|
|
184
|
+
{{ option.label }}
|
|
185
|
+
</li>
|
|
186
|
+
}
|
|
187
|
+
</ul>
|
|
188
|
+
<p role="status" data-part="status">{{ status }}</p>
|
|
189
|
+
</div>
|
|
190
|
+
|
|
191
|
+
<style scoped>
|
|
192
|
+
/* What it needs to work: the list opens below the input, over what follows. The rest is yours to style. */
|
|
193
|
+
:scope {
|
|
194
|
+
position: relative;
|
|
195
|
+
}
|
|
196
|
+
[data-part='listbox'] {
|
|
197
|
+
position: absolute;
|
|
198
|
+
top: 100%;
|
|
199
|
+
left: 0;
|
|
200
|
+
right: 0;
|
|
201
|
+
z-index: 10;
|
|
202
|
+
max-height: 240px;
|
|
203
|
+
overflow-y: auto;
|
|
204
|
+
margin: 0;
|
|
205
|
+
padding: 0;
|
|
206
|
+
list-style: none;
|
|
207
|
+
}
|
|
208
|
+
</style>
|
package/src/dialog.tscl
ADDED
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
{{--
|
|
2
|
+
Dialog (#465): a native <dialog>, so focus stays in it and the page behind is inert while it's open, Escape closes it,
|
|
3
|
+
and (dismissible, the default) so does a click outside (closedby="any"). Opened by a <dialog-trigger> and closed by a
|
|
4
|
+
<dialog-close> with the browser's command invokers: no JavaScript. Style it with dialog[open] and ::backdrop.
|
|
5
|
+
|
|
6
|
+
<dialog-trigger target="confirm">Delete…</dialog-trigger>
|
|
7
|
+
<dialog id="confirm">
|
|
8
|
+
@slot('title') { Delete this file? }
|
|
9
|
+
<p>It can't be undone.</p>
|
|
10
|
+
<dialog-close target="confirm">Cancel</dialog-close>
|
|
11
|
+
</dialog>
|
|
12
|
+
--}}
|
|
13
|
+
@input id: string
|
|
14
|
+
@input dismissible: boolean = true
|
|
15
|
+
|
|
16
|
+
<dialog [id]="id" [aria-labelledby]="id + '-title'" [closedby]="dismissible ? 'any' : 'closerequest'" data-part="dialog">
|
|
17
|
+
<h2 [id]="id + '-title'" data-part="title"><slot name="title" /></h2>
|
|
18
|
+
<slot />
|
|
19
|
+
</dialog>
|
package/src/dismiss.d.ts
ADDED
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
export type DismissOptions = {
|
|
2
|
+
/**
|
|
3
|
+
* Close it.
|
|
4
|
+
*/
|
|
5
|
+
onDismiss: (reason: 'escape-key' | 'outside-press', event: Event) => void;
|
|
6
|
+
/**
|
|
7
|
+
* Elements whose presses aren't outside (its trigger).
|
|
8
|
+
*/
|
|
9
|
+
exclude?: () => (Element | null | undefined)[];
|
|
10
|
+
};
|
|
11
|
+
/**
|
|
12
|
+
* @typedef {object} DismissOptions
|
|
13
|
+
* @property {(reason: 'escape-key' | 'outside-press', event: Event) => void} onDismiss Close it.
|
|
14
|
+
* @property {() => (Element | null | undefined)[]} [exclude] Elements whose presses aren't outside (its trigger).
|
|
15
|
+
*/
|
|
16
|
+
/**
|
|
17
|
+
* Calls `onDismiss` when Escape is pressed while `el` is the topmost layer, or when a pointer goes down outside it
|
|
18
|
+
* (and outside the layers opened after it, and `exclude()`). Before that, `el` gets a cancelable `cl:dismiss`
|
|
19
|
+
* event (`detail.reason`); preventDefault() keeps it open.
|
|
20
|
+
* @param {HTMLElement} el
|
|
21
|
+
* @param {DismissOptions} options
|
|
22
|
+
* @returns {() => void} Stops it (when the layer closes).
|
|
23
|
+
* @example
|
|
24
|
+
* @if (open) { <div [use]="(el) => dismiss(el, { onDismiss: () => (open = false) })">…</div> }
|
|
25
|
+
* @see https://clarusjs.com/guide/ui-primitives#dismiss
|
|
26
|
+
*/
|
|
27
|
+
export declare function dismiss(el: HTMLElement, options: DismissOptions): () => void;
|
package/src/dismiss.js
ADDED
|
@@ -0,0 +1,51 @@
|
|
|
1
|
+
// dismiss: close a layer (a popover, a menu, a dialog without <dialog>) on Escape or a press outside it. Layers stack:
|
|
2
|
+
// Escape closes the topmost only, and a press inside a later layer isn't outside an earlier one (Radix's
|
|
3
|
+
// DismissableLayer). A cancelable cl:dismiss event on the layer goes first, so a page can keep it open.
|
|
4
|
+
|
|
5
|
+
/** @type {HTMLElement[]} the open layers, oldest first */
|
|
6
|
+
const layers = []
|
|
7
|
+
|
|
8
|
+
/**
|
|
9
|
+
* @typedef {object} DismissOptions
|
|
10
|
+
* @property {(reason: 'escape-key' | 'outside-press', event: Event) => void} onDismiss Close it.
|
|
11
|
+
* @property {() => (Element | null | undefined)[]} [exclude] Elements whose presses aren't outside (its trigger).
|
|
12
|
+
*/
|
|
13
|
+
|
|
14
|
+
/**
|
|
15
|
+
* Calls `onDismiss` when Escape is pressed while `el` is the topmost layer, or when a pointer goes down outside it
|
|
16
|
+
* (and outside the layers opened after it, and `exclude()`). Before that, `el` gets a cancelable `cl:dismiss`
|
|
17
|
+
* event (`detail.reason`); preventDefault() keeps it open.
|
|
18
|
+
* @param {HTMLElement} el
|
|
19
|
+
* @param {DismissOptions} options
|
|
20
|
+
* @returns {() => void} Stops it (when the layer closes).
|
|
21
|
+
* @example
|
|
22
|
+
* @if (open) { <div [use]="(el) => dismiss(el, { onDismiss: () => (open = false) })">…</div> }
|
|
23
|
+
* @see https://clarusjs.com/guide/ui-primitives#dismiss
|
|
24
|
+
*/
|
|
25
|
+
export function dismiss(el, options) {
|
|
26
|
+
layers.push(el)
|
|
27
|
+
/** @param {'escape-key' | 'outside-press'} reason @param {Event} event */
|
|
28
|
+
const close = (reason, event) => {
|
|
29
|
+
if (el.dispatchEvent(new CustomEvent('cl:dismiss', { detail: { reason }, cancelable: true }))) options.onDismiss(reason, event)
|
|
30
|
+
}
|
|
31
|
+
/** @param {KeyboardEvent} event */
|
|
32
|
+
const keydown = (event) => {
|
|
33
|
+
if (event.key !== 'Escape' || layers.at(-1) !== el) return
|
|
34
|
+
close('escape-key', event)
|
|
35
|
+
}
|
|
36
|
+
/** @param {PointerEvent} event */
|
|
37
|
+
const pointerdown = (event) => {
|
|
38
|
+
const target = /** @type {Node} */ (event.target)
|
|
39
|
+
const later = layers.slice(layers.indexOf(el))
|
|
40
|
+
if (later.some((layer) => layer.contains(target))) return
|
|
41
|
+
if ((options.exclude?.() ?? []).some((x) => x?.contains(target))) return
|
|
42
|
+
close('outside-press', event)
|
|
43
|
+
}
|
|
44
|
+
document.addEventListener('keydown', keydown)
|
|
45
|
+
document.addEventListener('pointerdown', pointerdown, true)
|
|
46
|
+
return () => {
|
|
47
|
+
layers.splice(layers.indexOf(el), 1)
|
|
48
|
+
document.removeEventListener('keydown', keydown)
|
|
49
|
+
document.removeEventListener('pointerdown', pointerdown, true)
|
|
50
|
+
}
|
|
51
|
+
}
|
package/src/field.tscl
ADDED
|
@@ -0,0 +1,53 @@
|
|
|
1
|
+
{{--
|
|
2
|
+
Field (#465): a label, a native input, a description and an error, wired by id (for, aria-describedby,
|
|
3
|
+
aria-invalid). The error shows once the field has been left, or when [show] says so (after a submit). Base UI's
|
|
4
|
+
field state as data- attributes on the wrapper: data-touched, data-dirty, data-invalid, data-filled. It tells its
|
|
5
|
+
parent when it's left: (blur).
|
|
6
|
+
|
|
7
|
+
<field id="email" label="Email" type="email" [error]="errors.email" [(value)]="form.email" />
|
|
8
|
+
--}}
|
|
9
|
+
@input id: string
|
|
10
|
+
@input label: string
|
|
11
|
+
@input description: string = ''
|
|
12
|
+
@input error: string = ''
|
|
13
|
+
@input show: boolean = false
|
|
14
|
+
@input type: string = 'text'
|
|
15
|
+
@input name: string = ''
|
|
16
|
+
@input placeholder: string = ''
|
|
17
|
+
@input required: boolean = false
|
|
18
|
+
@model value: string = ''
|
|
19
|
+
@event blur
|
|
20
|
+
|
|
21
|
+
<script>
|
|
22
|
+
let touched = state(false)
|
|
23
|
+
const initial = value
|
|
24
|
+
const invalid = derived(() => !!error && (touched || show))
|
|
25
|
+
</script>
|
|
26
|
+
|
|
27
|
+
<div
|
|
28
|
+
data-part="field"
|
|
29
|
+
[data-touched]="touched ? '' : false"
|
|
30
|
+
[data-dirty]="value !== initial ? '' : false"
|
|
31
|
+
[data-filled]="value ? '' : false"
|
|
32
|
+
[data-invalid]="invalid ? '' : false"
|
|
33
|
+
>
|
|
34
|
+
<label [for]="id" data-part="label">{{ label }}</label>
|
|
35
|
+
<input
|
|
36
|
+
[id]="id"
|
|
37
|
+
[type]="type"
|
|
38
|
+
[name]="name || false"
|
|
39
|
+
[placeholder]="placeholder || false"
|
|
40
|
+
[required]="required"
|
|
41
|
+
[(value)]="value"
|
|
42
|
+
[aria-invalid]="invalid ? 'true' : false"
|
|
43
|
+
[aria-describedby]="[description ? id + '-description' : '', invalid ? id + '-error' : ''].filter(Boolean).join(' ') || false"
|
|
44
|
+
(blur)="touched = true; emit('blur')"
|
|
45
|
+
data-part="control"
|
|
46
|
+
/>
|
|
47
|
+
@if (description) {
|
|
48
|
+
<p [id]="id + '-description'" data-part="description">{{ description }}</p>
|
|
49
|
+
}
|
|
50
|
+
@if (invalid) {
|
|
51
|
+
<p [id]="id + '-error'" data-part="error" role="alert">{{ error }}</p>
|
|
52
|
+
}
|
|
53
|
+
</div>
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Remember what has focus now; the returned function puts focus back there, if it's still on the page and focus is in
|
|
3
|
+
* `el` or nowhere (a click elsewhere keeps its own focus).
|
|
4
|
+
* @param {HTMLElement} el
|
|
5
|
+
* @returns {() => void}
|
|
6
|
+
* @example
|
|
7
|
+
* @if (open) { <div [use]="focusReturn">…</div> }
|
|
8
|
+
* @see https://clarusjs.com/guide/ui-primitives#focusreturn
|
|
9
|
+
*/
|
|
10
|
+
export declare function focusReturn(el: HTMLElement): () => void;
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
// focusReturn: when a popup, menu or dialog goes, focus goes back to where it was when it opened (its trigger).
|
|
2
|
+
|
|
3
|
+
/**
|
|
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
|
+
* `el` or nowhere (a click elsewhere keeps its own focus).
|
|
6
|
+
* @param {HTMLElement} el
|
|
7
|
+
* @returns {() => void}
|
|
8
|
+
* @example
|
|
9
|
+
* @if (open) { <div [use]="focusReturn">…</div> }
|
|
10
|
+
* @see https://clarusjs.com/guide/ui-primitives#focusreturn
|
|
11
|
+
*/
|
|
12
|
+
export function focusReturn(el) {
|
|
13
|
+
const before = /** @type {HTMLElement | null} */ (document.activeElement)
|
|
14
|
+
return () => {
|
|
15
|
+
const now = document.activeElement
|
|
16
|
+
if (before?.isConnected && (!now || now === document.body || el.contains(now))) before.focus()
|
|
17
|
+
}
|
|
18
|
+
}
|
package/src/index.d.ts
ADDED
|
@@ -0,0 +1,6 @@
|
|
|
1
|
+
export { rovingFocus } from './roving-focus.js';
|
|
2
|
+
export { typeahead } from './typeahead.js';
|
|
3
|
+
export { dismiss } from './dismiss.js';
|
|
4
|
+
export { enter, exit } from './transition.js';
|
|
5
|
+
export { focusReturn } from './focus-return.js';
|
|
6
|
+
export { position } from './position.js';
|
package/src/index.js
ADDED
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
// @clarusjs/ui-primitives: behaviours for accessible components, each a function of an element that returns how to
|
|
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.js'
|
|
4
|
+
export { typeahead } from './typeahead.js'
|
|
5
|
+
export { dismiss } from './dismiss.js'
|
|
6
|
+
export { enter, exit } from './transition.js'
|
|
7
|
+
export { focusReturn } from './focus-return.js'
|
|
8
|
+
export { position } from './position.js'
|
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
{{-- Menu item (#465): one choice in a <menu-list>; its value goes to the menu's (select). data-menu names its menu. --}}
|
|
2
|
+
@context menu: { id: string }
|
|
3
|
+
@input value: string
|
|
4
|
+
@input disabled: boolean = false
|
|
5
|
+
|
|
6
|
+
<button type="button" role="menuitem" tabindex="-1" [data-value]="value" [data-menu]="menu.id" [aria-disabled]="disabled ? 'true' : false" data-part="menu-item">
|
|
7
|
+
<slot />
|
|
8
|
+
</button>
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
{{-- Menu list (#465): the popover holding the <menu-item>s, below its trigger (flipping when there's no room). --}}
|
|
2
|
+
@context menu: { id: string }
|
|
3
|
+
|
|
4
|
+
<div
|
|
5
|
+
[id]="menu.id + '-list'"
|
|
6
|
+
popover="auto"
|
|
7
|
+
role="menu"
|
|
8
|
+
[aria-labelledby]="menu.id + '-trigger'"
|
|
9
|
+
[style]="'position-anchor: --' + menu.id + '; position-area: bottom span-right; position-try-fallbacks: flip-block, flip-inline'"
|
|
10
|
+
data-part="menu-list"
|
|
11
|
+
>
|
|
12
|
+
<slot />
|
|
13
|
+
</div>
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
{{-- Menu trigger (#465): opens its <menu>'s list (popovertarget) and anchors it. No JavaScript of its own. --}}
|
|
2
|
+
@context menu: { id: string }
|
|
3
|
+
|
|
4
|
+
<button
|
|
5
|
+
type="button"
|
|
6
|
+
[id]="menu.id + '-trigger'"
|
|
7
|
+
[popovertarget]="menu.id + '-list'"
|
|
8
|
+
aria-haspopup="menu"
|
|
9
|
+
[aria-controls]="menu.id + '-list'"
|
|
10
|
+
aria-expanded="false"
|
|
11
|
+
[style]="'anchor-name: --' + menu.id"
|
|
12
|
+
data-part="trigger"
|
|
13
|
+
>
|
|
14
|
+
<slot />
|
|
15
|
+
</button>
|