@clarusjs/ui-primitives 0.2.0 → 0.3.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/README.md CHANGED
@@ -3,6 +3,8 @@
3
3
  Accessible behaviours for [clarusjs](https://clarusjs.com) components, as small DOM functions. Each takes an element
4
4
  (and options) and returns how to stop it; a template hands it the element with `[use]`.
5
5
 
6
+ Styled components built on them, ready to copy into your project: [ui.clarusjs.com](https://ui.clarusjs.com).
7
+
6
8
  ```sh
7
9
  pnpm add @clarusjs/ui-primitives
8
10
  ```
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@clarusjs/ui-primitives",
3
- "version": "0.2.0",
3
+ "version": "0.3.0",
4
4
  "description": "Accessible behaviours for clarusjs components: roving focus, typeahead, dismiss, transitions, focus return and positioning, as small DOM functions for [use].",
5
5
  "type": "module",
6
6
  "license": "MIT",
@@ -22,7 +22,7 @@
22
22
  "access": "public"
23
23
  },
24
24
  "peerDependencies": {
25
- "clarusjs": "^0.16.0"
25
+ "clarusjs": "^0.17.0"
26
26
  },
27
27
  "engines": {
28
28
  "node": "^22.12.0 || ^24.0.0 || >=26.0.0"
@@ -45,7 +45,7 @@
45
45
  },
46
46
  "devDependencies": {
47
47
  "@types/node": "^26.6.6",
48
- "clarusjs": "^0.16.0",
48
+ "clarusjs": "^0.17.0",
49
49
  "happy-dom": "^21.0.4",
50
50
  "typescript": "^7.0.2",
51
51
  "vite": "^8.3.8",
@@ -0,0 +1,77 @@
1
+ {{--
2
+ Chip input: values typed into one field, each becoming a chip (tags, email addresses). Enter or a comma adds what's
3
+ typed; Backspace in the empty field removes the last chip; each chip's × removes it. Bind the list with
4
+ [(values)]; with [name], a hidden input per value sends them with a form.
5
+
6
+ <chip-input [id]="'tags'" [label]="'Tags'" [name]="'tag'" [(values)]="post.tags" />
7
+
8
+ Headless: data-part names each element (chip-input, label, control, chip, input, hint). Each add or removal sends
9
+ (change) first, with the value and whether it's now in; $info.reason is 'add' or 'remove', and $info.cancel() keeps it
10
+ from happening: <chip-input … (change)="if ($event.added && !valid($event.value)) $info.cancel()" />
11
+ --}}
12
+ @input id: string = 'chips'
13
+ @input label: string = 'Tags'
14
+ @input name: string = ''
15
+ @input placeholder: string = 'Add…'
16
+ @input hint: string = 'Enter or comma adds; Backspace removes the last.'
17
+ @model values: string[] = []
18
+ @event change: { value: string; added: boolean }
19
+
20
+ <script>
21
+ let text = state('')
22
+
23
+ function add(raw) {
24
+ const value = raw.trim()
25
+ text = ''
26
+ if (!value || values.includes(value)) return
27
+ if (!emit('change', { value, added: true }, { reason: 'add' })) return
28
+ values = [...values, value]
29
+ }
30
+ function remove(value, event) {
31
+ if (!emit('change', { value, added: false }, { reason: 'remove', event })) return
32
+ values = values.filter((v) => v !== value)
33
+ }
34
+ function key(event) {
35
+ if (event.key === 'Enter' || event.key === ',') {
36
+ event.preventDefault()
37
+ add(text)
38
+ } else if (event.key === 'Backspace' && !text && values.length) {
39
+ remove(values[values.length - 1], event)
40
+ }
41
+ }
42
+ // A comma typed or pasted in: everything before it becomes chips.
43
+ function type(next) {
44
+ const parts = next.split(',')
45
+ const rest = parts.pop()
46
+ for (const p of parts) add(p)
47
+ text = rest
48
+ }
49
+ </script>
50
+
51
+ <div data-part="chip-input">
52
+ <label [for]="id + '-input'" data-part="label">{{ label }}</label>
53
+ <div data-part="control">
54
+ @for (v of values; track v) {
55
+ <span data-part="chip"
56
+ >{{ v }} <button type="button" [aria-label]="'Remove ' + v" (click)="remove(v, $event)">×</button></span
57
+ >
58
+ }
59
+ <input
60
+ [id]="id + '-input'"
61
+ data-part="input"
62
+ autocomplete="off"
63
+ [aria-describedby]="id + '-hint'"
64
+ [placeholder]="placeholder"
65
+ [value]="text"
66
+ (input)="type($event.target.value)"
67
+ (keydown)="key($event)"
68
+ (blur)="add(text)"
69
+ />
70
+ </div>
71
+ <p [id]="id + '-hint'" data-part="hint">{{ hint }}</p>
72
+ @if (name) {
73
+ @for (v of values; track v) {
74
+ <input type="hidden" [name]="name" [value]="v" />
75
+ }
76
+ }
77
+ </div>
package/src/combobox.tscl CHANGED
@@ -11,6 +11,10 @@
11
11
 
12
12
  Remote options: give [src] a URL; typing asks it for `src?q=<search>` (JSON: [{ value, label }]), 200 ms after the
13
13
  last key, and the newest search wins. <combobox [id]="'city'" [label]="'City'" [src]="'/api/cities'" [(value)]="city" />
14
+ Or answer each search yourself (an API client, IndexedDB, a worker): (search) brings the term and an AbortSignal (a
15
+ newer search aborts it); respond with the options or a promise of them, from an island's script:
16
+ <combobox [id]="'lang'" (search)="$event.respond(findLanguages($event.term, $event.signal))" />
17
+ Either way, answers are cached by search (by src, or by id for (search)).
14
18
 
15
19
  Keyboard: ↑ ↓ move, Home / End jump, Enter picks, Esc closes, Backspace in an empty search removes the last pick.
16
20
 
@@ -38,6 +42,7 @@
38
42
  @model value: string = ''
39
43
  @model values: string[] = []
40
44
  @event change: { value: string; label: string; selected: boolean }
45
+ @event search: { term: string; signal: AbortSignal; respond: (options: Option[] | Promise<Option[]>) => void }
41
46
 
42
47
  <script>
43
48
  import { json, resource } from 'clarusjs/fetch'
@@ -49,23 +54,37 @@
49
54
  let active = state(0)
50
55
  // What's chosen, as a list in both modes.
51
56
  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.
57
+ // Answered by a (search) listener (known once one responds) or by src: the answer for the term, cached by it;
58
+ // nothing is asked until the list opens.
59
+ // A plain flag, not state: the fetcher runs while the options are read, where state can't be written.
60
+ let answered = false
61
+ const remotely = () => answered || !!src
53
62
  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) },
63
+ ({ signal }) => {
64
+ if (!open) return false
65
+ let answer
66
+ emit('search', { term, signal, respond: (options) => (answer = options) })
67
+ if (answer !== undefined) {
68
+ answered = true
69
+ return Promise.resolve(answer)
70
+ }
71
+ return src ? json(src + (src.includes('?') ? '&' : '?') + 'q=' + encodeURIComponent(term), { signal }) : false
72
+ },
73
+ { key: () => (open ? ['combobox', src || '#' + id, term] : false) },
57
74
  )
58
75
  // The options matching what's typed (by label, ignoring case), or the server's.
59
76
  const shown = derived(() => {
60
- if (src) return remote.value ?? []
77
+ const answer = remote.value // read first, always: the first (search) answer arrives after this runs
78
+ if (remotely()) return answer ?? []
61
79
  const q = query.trim().toLowerCase()
62
80
  return q ? options.filter((o) => o.label.toLowerCase().includes(q)) : options
63
81
  })
64
82
  const labelOf = (v) => known[v] ?? options.find((o) => o.value === v)?.label ?? v
65
83
  const status = derived(() => {
66
84
  if (!open) return ''
67
- if (src && remote.loading) return 'Searching…'
68
- if (src && remote.error) return "Couldn't load the options."
85
+ const { loading, error } = remote
86
+ if (remotely() && loading) return 'Searching…'
87
+ if (remotely() && error) return "Couldn't load the options."
69
88
  return shown.length ? '' : 'No match for "' + query + '"'
70
89
  })
71
90
 
@@ -86,7 +105,7 @@
86
105
  )
87
106
  )
88
107
  return
89
- if (src) known = { ...known, [option.value]: option.label }
108
+ if (remotely()) known = { ...known, [option.value]: option.label }
90
109
  if (multiple) {
91
110
  values = chosen.includes(option.value) ? chosen.filter((v) => v !== option.value) : [...chosen, option.value]
92
111
  } else {
@@ -105,7 +124,7 @@
105
124
  open = true
106
125
  active = 0
107
126
  clearTimeout(timer)
108
- timer = setTimeout(() => (term = text), src ? 200 : 0)
127
+ timer = setTimeout(() => (term = text), remotely() ? 200 : 0)
109
128
  }
110
129
  function key(event) {
111
130
  const last = shown.length - 1