@stacksjs/components 0.2.955 → 0.2.958

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.
@@ -38,7 +38,7 @@ export const className = $props.className || ''
38
38
  * badge whose whole job is to distinguish would render two of its variants
39
39
  * identically. A wrong colour is worse than an unthemeable one.
40
40
  */
41
- const variantClasses = {
41
+ export const badgeColorClasses = {
42
42
  default: 'bg-surface-raised text-fg-strong',
43
43
  primary: 'bg-accent-soft text-accent-soft-ink',
44
44
  secondary: 'bg-secondary-soft text-secondary-soft-ink',
@@ -50,7 +50,7 @@ const variantClasses = {
50
50
 
51
51
  const sizeClasses = { sm: 'px-2 py-0.5 text-xs', md: 'px-2.5 py-0.5 text-sm', lg: 'px-3 py-1 text-base' }
52
52
 
53
- const dotColorClasses = {
53
+ export const badgeDotColorClasses = {
54
54
  default: 'bg-fg-soft',
55
55
  primary: 'bg-accent',
56
56
  secondary: 'bg-secondary',
@@ -60,13 +60,28 @@ const dotColorClasses = {
60
60
  info: 'bg-info',
61
61
  }
62
62
 
63
- export const badgeClasses = `inline-flex items-center gap-1.5 font-medium rounded-pill ${variantClasses[variant] || variantClasses.default} ${sizeClasses[size] || sizeClasses.md} ${className}`.trim()
64
- export const dotClasses = `w-2 h-2 rounded-full ${dotColorClasses[variant] || dotColorClasses.default}`.trim()
63
+ const initialVariant = Object.hasOwn(badgeColorClasses, variant) ? variant : 'default'
64
+ export const badgeClasses = `inline-flex items-center gap-1.5 font-medium rounded-pill ${badgeColorClasses[initialVariant]} ${sizeClasses[size] || sizeClasses.md} ${className}`.trim()
65
+ export const dotClasses = `w-2 h-2 rounded-full ${badgeDotColorClasses[initialVariant]}`.trim()
65
66
  export const removeButtonClasses = `ml-0.5 -mr-1 text-current hover:bg-black/10 dark:hover:bg-white/10 rounded-full p-0.5 transition-colors duration-150`.trim()
66
67
  </script>
67
68
 
68
69
  <script client>
69
70
  const emit = defineEmits()
71
+ const colorVariant = useReactiveProp('variant', {{ variant }})
72
+ const badgeColors = {{ badgeColorClasses }}
73
+ const dotColors = {{ badgeDotColorClasses }}
74
+ const callerClasses = {{ className }}.split(/\s+/)
75
+
76
+ function colorToggles(options, extraClasses = []) {
77
+ const tone = colorVariant()
78
+ const current = Object.hasOwn(options, tone) ? options[tone] : options.default
79
+ const retained = new Set([...current.split(' '), ...extraClasses])
80
+ return Object.fromEntries(Object.values(options).flatMap(classes => classes.split(' ').map(token => [token, retained.has(token)])))
81
+ }
82
+
83
+ const badgeColorToggles = derived(() => colorToggles(badgeColors, callerClasses))
84
+ const dotColorToggles = derived(() => colorToggles(dotColors))
70
85
 
71
86
  function onRemove() {
72
87
  emit('remove')
@@ -75,10 +90,11 @@ function onRemove() {
75
90
 
76
91
  <span
77
92
  class="{{ badgeClasses }}"
93
+ :class="badgeColorToggles()"
78
94
  @if(live)role="status" aria-live="{{ live }}"@endif
79
95
  >
80
96
  @if(dot)
81
- <span class="{{ dotClasses }}" aria-hidden="true"></span>
97
+ <span class="{{ dotClasses }}" :class="dotColorToggles()" aria-hidden="true"></span>
82
98
  @endif
83
99
 
84
100
  <slot />
@@ -0,0 +1,208 @@
1
+ <script server>
2
+ export const open = $bool($props.open)
3
+ export const initialQuery = $props.query || ''
4
+ export const className = $props.className || ''
5
+
6
+ const baseClasses = 'fixed inset-0 z-50 overflow-y-auto p-4 sm:p-6 md:p-20'
7
+ export const paletteClasses = `${baseClasses} ${className}`.trim()
8
+ export const paletteInstanceId = $uid
9
+ </script>
10
+
11
+ <script client>
12
+ const emit = defineEmits()
13
+ const paletteInstanceId = {{ paletteInstanceId }}
14
+ const isOpen = state({{ open }})
15
+ const query = state({{ initialQuery }})
16
+
17
+ function close() {
18
+ if (!isOpen()) return
19
+ isOpen.set(false)
20
+ setActive(null)
21
+ emit('close')
22
+ }
23
+
24
+ function show() {
25
+ if (isOpen()) return
26
+ isOpen.set(true)
27
+ }
28
+
29
+ function onQueryInput(event) {
30
+ query.set(event.target.value)
31
+ emit('query', event.target.value)
32
+ }
33
+
34
+ function onItemSelect(event) {
35
+ event.stopPropagation()
36
+ emit('select', event.detail)
37
+ close()
38
+ }
39
+
40
+ function panelEl() {
41
+ return document.querySelector(`[data-palette-id="${paletteInstanceId}"] [data-stx-command-palette-panel]`)
42
+ }
43
+
44
+ /*
45
+ * The keyboard, as a combobox (stacksjs/stx#2231). Focus stays in the search
46
+ * field so the user can keep typing; the command arrowed to is the ACTIVE
47
+ * option, named by aria-activedescendant on the field and marked
48
+ * aria-selected, and Enter clicks it, so it is selected exactly as a pointer
49
+ * selects it. The items used to be plain divs with only a click handler, so a
50
+ * keyboard could open the palette and close it and do nothing in between.
51
+ *
52
+ * Held as the element rather than an index: the page filters the results as
53
+ * the query changes, and an index would then point at a different command.
54
+ */
55
+ let activeItem = null
56
+
57
+ function listEl() {
58
+ return panelEl()?.querySelector('[data-stx-command-palette-list]') ?? null
59
+ }
60
+
61
+ /** The commands showing now: an item the page hid with :show or hidden is skipped. */
62
+ function itemElements() {
63
+ const list = listEl()
64
+ if (!list) return []
65
+ return Array.prototype.filter.call(list.querySelectorAll('[data-stx-command-item]'), (item) => {
66
+ for (let node = item; node && node !== list; node = node.parentElement) {
67
+ if (node.hidden || (node.style && node.style.display === 'none')) return false
68
+ }
69
+ return true
70
+ })
71
+ }
72
+
73
+ function setActive(item) {
74
+ activeItem = item
75
+ const items = itemElements()
76
+ const doc = panelEl()?.ownerDocument || document
77
+ items.forEach((option, index) => {
78
+ // aria-activedescendant needs an id that is unique on the page.
79
+ if (!option.id || doc.querySelectorAll('[id="' + option.id + '"]').length !== 1)
80
+ option.id = 'stx-command-option-' + paletteInstanceId + '-' + index
81
+ option.setAttribute('aria-selected', option === item ? 'true' : 'false')
82
+ })
83
+ const input = panelEl()?.querySelector('[data-stx-command-palette-input]')
84
+ if (!input) return
85
+ if (item) input.setAttribute('aria-activedescendant', item.id)
86
+ else input.removeAttribute('aria-activedescendant')
87
+ }
88
+
89
+ function moveActive(delta) {
90
+ const items = itemElements()
91
+ if (!items.length) return
92
+ const from = items.indexOf(activeItem)
93
+ const next = from < 0
94
+ ? (delta > 0 ? 0 : items.length - 1)
95
+ : (from + delta + items.length) % items.length
96
+ setActive(items[next])
97
+ // The list scrolls, and nothing moves focus there to bring the option in.
98
+ if (items[next].scrollIntoView) items[next].scrollIntoView({ block: 'nearest' })
99
+ }
100
+
101
+ function onPaletteKey(event) {
102
+ const key = event.key
103
+ if (key === 'ArrowDown' || key === 'ArrowUp') {
104
+ event.preventDefault()
105
+ moveActive(key === 'ArrowDown' ? 1 : -1)
106
+ }
107
+ else if (key === 'Home' || key === 'End') {
108
+ event.preventDefault()
109
+ activeItem = null
110
+ moveActive(key === 'Home' ? 1 : -1)
111
+ }
112
+ else if (key === 'Enter' && activeItem && itemElements().includes(activeItem)) {
113
+ event.preventDefault()
114
+ activeItem.click()
115
+ }
116
+ }
117
+
118
+ /*
119
+ * The results changed under the active command: keep it while it still
120
+ * shows, and point at nothing once it is gone rather than at a stale id.
121
+ */
122
+ let resultsObserver = null
123
+ onMount(() => {
124
+ const list = listEl()
125
+ if (!list || typeof MutationObserver === 'undefined') return
126
+ resultsObserver = new MutationObserver(() => {
127
+ if (activeItem && !itemElements().includes(activeItem)) setActive(null)
128
+ })
129
+ resultsObserver.observe(list, { childList: true, subtree: true, attributes: true, attributeFilter: ['style', 'hidden'] })
130
+ })
131
+
132
+ /*
133
+ * The palette takes over the page, so it is a modal layer on the page's
134
+ * overlay stack (window.stx.overlays): the page held still behind it, focus
135
+ * straight into the search field and back to whatever opened it, Tab kept
136
+ * inside, and Escape for whichever overlay is on top. It used to listen for
137
+ * Escape on the document and do none of the rest, so focus stayed behind it
138
+ * and a palette opened from a dialog closed the dialog too (stacksjs/stx#2057).
139
+ */
140
+ effect(() => {
141
+ if (isOpen()) {
142
+ window.stx.overlays.open(paletteInstanceId, {
143
+ panel: panelEl,
144
+ onEscape: close,
145
+ initialFocus: () => panelEl()?.querySelector('input'),
146
+ })
147
+ }
148
+ else {
149
+ window.stx.overlays.close(paletteInstanceId)
150
+ }
151
+ })
152
+
153
+ onDestroy(() => {
154
+ window.stx.overlays.close(paletteInstanceId)
155
+ resultsObserver?.disconnect()
156
+ })
157
+
158
+ defineExpose({ isOpen, query, show, close })
159
+ </script>
160
+
161
+ <div
162
+ :show="isOpen()"
163
+ class="{{ paletteClasses }}"
164
+ data-stx-command-palette
165
+ data-palette-id="{{ paletteInstanceId }}"
166
+ @stx:command-select="onItemSelect($event)"
167
+ >
168
+ <div class="fixed inset-0 bg-neutral-500/25 dark:bg-neutral-500/50 transition-opacity" aria-hidden="true" @click="close()"></div>
169
+
170
+ <div
171
+ data-stx-command-palette-panel
172
+ role="dialog"
173
+ aria-modal="true"
174
+ aria-label="Command palette"
175
+ tabindex="-1"
176
+ class="mx-auto max-w-xl transform divide-y divide-line overflow-hidden rounded-xl bg-panel shadow-2xl ring-1 ring-line transition-all relative"
177
+ >
178
+ <div class="relative">
179
+ <svg class="pointer-events-none absolute top-3.5 left-4 h-5 w-5 text-fg-soft" viewBox="0 0 20 20" fill="currentColor">
180
+ <path fill-rule="evenodd" d="M8 4a4 4 0 100 8 4 4 0 000-8zM2 8a6 6 0 1110.89 3.476l4.817 4.817a1 1 0 01-1.414 1.414l-4.816-4.816A6 6 0 012 8z" clip-rule="evenodd" />
181
+ </svg>
182
+ <input
183
+ type="text"
184
+ role="combobox"
185
+ aria-expanded="true"
186
+ aria-autocomplete="list"
187
+ aria-controls="stx-command-palette-list-{{ paletteInstanceId }}"
188
+ data-stx-command-palette-input
189
+ class="h-12 w-full border-0 bg-transparent pl-11 pr-4 text-fg placeholder-fg-soft focus:ring-0 sm:text-sm"
190
+ placeholder="Search..."
191
+ aria-label="Search commands"
192
+ :value="query()"
193
+ @input="onQueryInput($event)"
194
+ @keydown="onPaletteKey($event)"
195
+ />
196
+ </div>
197
+
198
+ <div
199
+ id="stx-command-palette-list-{{ paletteInstanceId }}"
200
+ role="listbox"
201
+ aria-label="Commands"
202
+ data-stx-command-palette-list
203
+ class="max-h-96 overflow-y-auto py-2"
204
+ >
205
+ <slot />
206
+ </div>
207
+ </div>
208
+ </div>
@@ -3,7 +3,8 @@ export const className = $props.className || ''
3
3
  export const value = $props.value
4
4
  export const valueJson = JSON.stringify(value === undefined ? null : value)
5
5
 
6
- const baseClasses = 'flex cursor-pointer select-none items-center px-4 py-2 text-fg hover:bg-accent-solid hover:text-accent-ink'
6
+ // aria-selected marks the command the keyboard is on (CommandPalette's onPaletteKey).
7
+ const baseClasses = 'flex cursor-pointer select-none items-center px-4 py-2 text-fg hover:bg-accent-solid hover:text-accent-ink aria-selected:bg-accent-solid aria-selected:text-accent-ink'
7
8
  export const itemClasses = `${baseClasses} ${className}`.trim()
8
9
  </script>
9
10
 
@@ -33,6 +34,8 @@ function onClick(event) {
33
34
 
34
35
  <div
35
36
  class="{{ itemClasses }}"
37
+ role="option"
38
+ aria-selected="false"
36
39
  data-stx-command-item
37
40
  @click="onClick($event)"
38
41
  >