@stacksjs/components 0.2.465 → 0.2.467
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/dist/{ChatLauncher-pc9nqh43.stx → ChatLauncher-3zph4trh.stx} +10 -2
- package/dist/{Combobox-9xh1jgjr.stx → Combobox-9n04ec5w.stx} +16 -4
- package/dist/{CommandPalette-vvaqtqat.stx → CommandPalette-qqjdbcyg.stx} +33 -8
- package/dist/{DateRangePicker-z64qqw84.stx → DateRangePicker-4zmf34e4.stx} +13 -9
- package/dist/Dialog-pdq8rxvy.stx +162 -0
- package/dist/{Drawer-hc0jjpsw.stx → Drawer-nmq100vy.stx} +24 -128
- package/dist/{Dropdown-25160tc6.stx → Dropdown-hb8ks69x.stx} +16 -4
- package/dist/{EmailInput-ffzwvp9r.stx → EmailInput-57626391.stx} +3 -0
- package/dist/{Listbox-32gzxace.stx → Listbox-83j2q4x6.stx} +16 -4
- package/dist/{NumberInput-r2wsh8w9.stx → NumberInput-pq4k2k4k.stx} +31 -10
- package/dist/{PasswordInput-36bws0kf.stx → PasswordInput-dg41ktgd.stx} +8 -1
- package/dist/{Popover-9m42pym0.stx → Popover-khtvagcs.stx} +16 -4
- package/dist/{Select-pv44ngbe.stx → Select-61zv5tr7.stx} +98 -17
- package/dist/Tab-46mxm7f8.stx +35 -0
- package/dist/TabList-76bpx5fp.stx +154 -0
- package/dist/Table-0a4y46vd.stx +43 -0
- package/dist/TableCell-jhvsxw8b.stx +17 -0
- package/dist/TableHeader-krzcx4sv.stx +62 -0
- package/dist/{Tabs-3j2h0jjn.stx → Tabs-1swcxmfb.stx} +4 -1
- package/dist/{TextInput-h9trj9f9.stx → TextInput-nknmz9md.stx} +57 -4
- package/dist/{Textarea-k8cjs4zs.stx → Textarea-9xmhjqp8.stx} +30 -7
- package/dist/{Tooltip-z13bn0pv.stx → Tooltip-rg1c2zrc.stx} +13 -3
- package/dist/index.d.ts +2 -0
- package/dist/index.js +7 -7
- package/dist/ui/input/index.d.ts +18 -5
- package/dist/ui/select/index.d.ts +1 -1
- package/dist/ui/table/index.d.ts +4 -0
- package/dist/ui/tabs/index.d.ts +35 -3
- package/dist/utils/overlays.d.ts +39 -0
- package/package.json +3 -3
- package/src/index.ts +2 -0
- package/src/ui/chat/ChatLauncher.stx +10 -2
- package/src/ui/combobox/Combobox.stx +16 -4
- package/src/ui/command-palette/CommandPalette.stx +33 -8
- package/src/ui/date-range-picker/DateRangePicker.stx +13 -9
- package/src/ui/dialog/Dialog.stx +29 -169
- package/src/ui/dialog/README.md +5 -0
- package/src/ui/drawer/Drawer.stx +24 -128
- package/src/ui/drawer/README.md +4 -0
- package/src/ui/dropdown/Dropdown.stx +16 -4
- package/src/ui/input/EmailInput.stx +3 -0
- package/src/ui/input/NumberInput.stx +31 -10
- package/src/ui/input/PasswordInput.stx +8 -1
- package/src/ui/input/TextInput.stx +57 -4
- package/src/ui/input/index.ts +31 -5
- package/src/ui/listbox/Listbox.stx +16 -4
- package/src/ui/popover/Popover.stx +16 -4
- package/src/ui/select/README.md +59 -21
- package/src/ui/select/Select.stx +98 -17
- package/src/ui/select/index.ts +8 -1
- package/src/ui/table/README.md +69 -1
- package/src/ui/table/Table.stx +31 -2
- package/src/ui/table/TableCell.stx +8 -1
- package/src/ui/table/TableHeader.stx +47 -5
- package/src/ui/table/index.ts +9 -0
- package/src/ui/tabs/Tab.stx +35 -0
- package/src/ui/tabs/TabList.stx +154 -0
- package/src/ui/tabs/Tabs.stx +4 -1
- package/src/ui/tabs/index.ts +37 -3
- package/src/ui/textarea/Textarea.stx +30 -7
- package/src/ui/tooltip/Tooltip.stx +13 -3
- package/src/utils/overlays.ts +69 -0
- package/dist/Dialog-e9a4r6d0.stx +0 -302
- package/dist/Table-3ek3w95h.stx +0 -14
- package/dist/TableCell-8swtcq8s.stx +0 -10
- package/dist/TableHeader-qsc3e208.stx +0 -20
|
@@ -0,0 +1,154 @@
|
|
|
1
|
+
<script server>
|
|
2
|
+
/*
|
|
3
|
+
* A tab strip whose selection and panels belong to the page (stacksjs/stx#2058).
|
|
4
|
+
*
|
|
5
|
+
* <Tabs> owns both its selection and its panels: it renders every panel inside
|
|
6
|
+
* itself and remembers the tab it was told to start on. That suits a settings
|
|
7
|
+
* card. It does not suit tabs that switch between whole sections of a page,
|
|
8
|
+
* where the selection is kept in the URL or a store and each section is behind
|
|
9
|
+
* its own `:if`, so a section nobody opened never fetches its data.
|
|
10
|
+
*
|
|
11
|
+
* TabList is the behaviour alone. Its children are `role="tab"` elements, a
|
|
12
|
+
* `<Tab>` or any element with `role="tab"` and `data-value` (a `:for` row
|
|
13
|
+
* works), and it gives them what the role promises: one tab stop on the
|
|
14
|
+
* selected tab, the arrow keys, Home and End moving between them and selecting
|
|
15
|
+
* as they go, and `aria-selected` kept true on exactly one. The selection is a
|
|
16
|
+
* `v-model:value`, or `:value` with `@change`.
|
|
17
|
+
*
|
|
18
|
+
* <TabList v-model:value="tab" label="Store sections" variant="line">
|
|
19
|
+
* <Tab value="products" controls="products-panel">Products</Tab>
|
|
20
|
+
* <Tab value="sales" controls="sales-panel">Sales</Tab>
|
|
21
|
+
* </TabList>
|
|
22
|
+
* <section :if="tab === 'products'" id="products-panel" role="tabpanel">…</section>
|
|
23
|
+
*/
|
|
24
|
+
export const value = $props.value ?? ''
|
|
25
|
+
export const label = $props.label || ''
|
|
26
|
+
export const labelledby = $props.labelledby || ''
|
|
27
|
+
export const orientation = $props.orientation === 'vertical' ? 'vertical' : 'horizontal'
|
|
28
|
+
export const variant = $props.variant || 'line'
|
|
29
|
+
export const className = $props.className || ''
|
|
30
|
+
|
|
31
|
+
const variantClasses = {
|
|
32
|
+
line: 'flex gap-1 border-b border-line',
|
|
33
|
+
pills: 'inline-flex gap-1 p-1 bg-surface-raised rounded-panel',
|
|
34
|
+
none: '',
|
|
35
|
+
}
|
|
36
|
+
const directionClasses = orientation === 'vertical' ? 'flex-col' : ''
|
|
37
|
+
export const listClasses = `${variantClasses[variant] ?? variantClasses.line} ${directionClasses} ${className}`.replace(/\s+/g, ' ').trim()
|
|
38
|
+
</script>
|
|
39
|
+
|
|
40
|
+
<script client>
|
|
41
|
+
const emit = defineEmits()
|
|
42
|
+
const orientation = {{ orientation }}
|
|
43
|
+
const selected = useModel('value', { default: {{ value }}, parse: v => v == null ? '' : String(v) })
|
|
44
|
+
const listRef = useRef('listRef')
|
|
45
|
+
const mounted = state(false)
|
|
46
|
+
let observer = null
|
|
47
|
+
|
|
48
|
+
/** This list's own tabs: not a nested list's, and not a disabled one. */
|
|
49
|
+
function ownTabs(includeDisabled) {
|
|
50
|
+
const root = listRef.value
|
|
51
|
+
if (!root) return []
|
|
52
|
+
return Array.from(root.querySelectorAll('[role="tab"]')).filter(tab =>
|
|
53
|
+
tab.closest('[role="tablist"]') === root
|
|
54
|
+
&& (includeDisabled || (!tab.hasAttribute('disabled') && tab.getAttribute('aria-disabled') !== 'true')))
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
function valueOf(tab, index) {
|
|
58
|
+
const value = tab.getAttribute('data-value')
|
|
59
|
+
return value == null ? String(index) : value
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
/*
|
|
63
|
+
* One stop in the tab order, on the selected tab; the first enabled tab when
|
|
64
|
+
* nothing is selected yet, so the strip can still be reached. Run on every
|
|
65
|
+
* change of selection and of the tabs themselves, because a `:for` adds and
|
|
66
|
+
* removes them after mount.
|
|
67
|
+
*/
|
|
68
|
+
function sync() {
|
|
69
|
+
const tabs = ownTabs(true)
|
|
70
|
+
const current = selected()
|
|
71
|
+
let any = false
|
|
72
|
+
tabs.forEach((tab, index) => {
|
|
73
|
+
const on = valueOf(tab, index) === current
|
|
74
|
+
if (on) any = true
|
|
75
|
+
tab.setAttribute('aria-selected', on ? 'true' : 'false')
|
|
76
|
+
tab.setAttribute('tabindex', on ? '0' : '-1')
|
|
77
|
+
})
|
|
78
|
+
if (!any) {
|
|
79
|
+
const first = ownTabs(false)[0]
|
|
80
|
+
if (first) first.setAttribute('tabindex', '0')
|
|
81
|
+
}
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
function select(tab) {
|
|
85
|
+
const index = ownTabs(true).indexOf(tab)
|
|
86
|
+
const value = valueOf(tab, index)
|
|
87
|
+
if (value === selected()) return
|
|
88
|
+
selected.set(value)
|
|
89
|
+
emit('change', value)
|
|
90
|
+
}
|
|
91
|
+
|
|
92
|
+
function onClick(event) {
|
|
93
|
+
const tab = event.target && event.target.closest ? event.target.closest('[role="tab"]') : null
|
|
94
|
+
if (!tab || !ownTabs(false).includes(tab)) return
|
|
95
|
+
select(tab)
|
|
96
|
+
}
|
|
97
|
+
|
|
98
|
+
/*
|
|
99
|
+
* The keys the role announces. Selection follows focus (automatic
|
|
100
|
+
* activation), which is what a tab that switches a section of the page wants.
|
|
101
|
+
*/
|
|
102
|
+
function onKey(event) {
|
|
103
|
+
const tabs = ownTabs(false)
|
|
104
|
+
const from = event.target && event.target.closest ? event.target.closest('[role="tab"]') : null
|
|
105
|
+
const index = tabs.indexOf(from)
|
|
106
|
+
if (index === -1) return
|
|
107
|
+
const forward = orientation === 'vertical' ? 'ArrowDown' : 'ArrowRight'
|
|
108
|
+
const back = orientation === 'vertical' ? 'ArrowUp' : 'ArrowLeft'
|
|
109
|
+
let next = -1
|
|
110
|
+
if (event.key === forward) next = (index + 1) % tabs.length
|
|
111
|
+
else if (event.key === back) next = (index - 1 + tabs.length) % tabs.length
|
|
112
|
+
else if (event.key === 'Home') next = 0
|
|
113
|
+
else if (event.key === 'End') next = tabs.length - 1
|
|
114
|
+
if (next === -1) return
|
|
115
|
+
event.preventDefault()
|
|
116
|
+
tabs[next].focus()
|
|
117
|
+
select(tabs[next])
|
|
118
|
+
}
|
|
119
|
+
|
|
120
|
+
effect(() => {
|
|
121
|
+
selected()
|
|
122
|
+
if (mounted()) sync()
|
|
123
|
+
})
|
|
124
|
+
|
|
125
|
+
onMount(() => {
|
|
126
|
+
mounted.set(true)
|
|
127
|
+
sync()
|
|
128
|
+
const root = listRef.value
|
|
129
|
+
if (root && typeof MutationObserver !== 'undefined') {
|
|
130
|
+
observer = new MutationObserver(() => sync())
|
|
131
|
+
observer.observe(root, { childList: true, subtree: true })
|
|
132
|
+
}
|
|
133
|
+
})
|
|
134
|
+
|
|
135
|
+
onDestroy(() => {
|
|
136
|
+
if (observer) observer.disconnect()
|
|
137
|
+
observer = null
|
|
138
|
+
})
|
|
139
|
+
|
|
140
|
+
defineExpose({ selected })
|
|
141
|
+
</script>
|
|
142
|
+
|
|
143
|
+
<div
|
|
144
|
+
x-ref="listRef"
|
|
145
|
+
role="tablist"
|
|
146
|
+
aria-orientation="{{ orientation }}"
|
|
147
|
+
@if(label)aria-label="{{ label }}"@endif
|
|
148
|
+
@if(labelledby)aria-labelledby="{{ labelledby }}"@endif
|
|
149
|
+
class="{{ listClasses }}"
|
|
150
|
+
@click="onClick($event)"
|
|
151
|
+
@keydown="onKey($event)"
|
|
152
|
+
>
|
|
153
|
+
<slot />
|
|
154
|
+
</div>
|
package/src/ui/tabs/Tabs.stx
CHANGED
|
@@ -167,7 +167,10 @@ function onTabKey(event, index) {
|
|
|
167
167
|
else if (event.key === 'End') { event.preventDefault(); next = tabCount - 1 }
|
|
168
168
|
if (next !== index) {
|
|
169
169
|
selectTab(next)
|
|
170
|
-
|
|
170
|
+
// Slot mode numbers its tabs per instance; `tab-N` is the prop mode's id,
|
|
171
|
+
// so arrowing in slot mode moved the selection and left focus behind.
|
|
172
|
+
const id = hasPropTabs ? `tab-${next}` : discoveredTabs()[next]?.tabId
|
|
173
|
+
if (id) document.getElementById(id)?.focus()
|
|
171
174
|
}
|
|
172
175
|
}
|
|
173
176
|
|
package/src/ui/tabs/index.ts
CHANGED
|
@@ -1,3 +1,5 @@
|
|
|
1
|
+
export { default as Tab } from './Tab.stx'
|
|
2
|
+
export { default as TabList } from './TabList.stx'
|
|
1
3
|
export { default as TabPanel } from './TabPanel.stx'
|
|
2
4
|
export { default as Tabs } from './Tabs.stx'
|
|
3
5
|
|
|
@@ -8,7 +10,8 @@ export { default as Tabs } from './Tabs.stx'
|
|
|
8
10
|
*
|
|
9
11
|
* See stacksjs/stx#1703.
|
|
10
12
|
*/
|
|
11
|
-
|
|
13
|
+
/** One tab of the legacy prop API. Named apart from the `<Tab>` component. */
|
|
14
|
+
export interface TabDefinition {
|
|
12
15
|
label: string
|
|
13
16
|
content: string
|
|
14
17
|
icon?: string
|
|
@@ -16,11 +19,11 @@ export interface Tab {
|
|
|
16
19
|
|
|
17
20
|
export interface TabsProps {
|
|
18
21
|
/** Legacy: array of tab definitions with string content. */
|
|
19
|
-
tabs?:
|
|
22
|
+
tabs?: TabDefinition[]
|
|
20
23
|
defaultTab?: number
|
|
21
24
|
orientation?: 'horizontal' | 'vertical'
|
|
22
25
|
variant?: 'line' | 'pills' | 'enclosed'
|
|
23
|
-
onChange?: (index: number, tab:
|
|
26
|
+
onChange?: (index: number, tab: TabDefinition) => void
|
|
24
27
|
className?: string
|
|
25
28
|
}
|
|
26
29
|
|
|
@@ -48,3 +51,34 @@ export interface TabPanelProps {
|
|
|
48
51
|
icon?: string
|
|
49
52
|
className?: string
|
|
50
53
|
}
|
|
54
|
+
|
|
55
|
+
/**
|
|
56
|
+
* A tab strip whose selection and panels belong to the page: `<TabList>` with
|
|
57
|
+
* `<Tab>` children (or any `role="tab"` element with `data-value`). The list
|
|
58
|
+
* keeps one tab stop, the arrow keys, Home and End, and `aria-selected`; the
|
|
59
|
+
* page keeps its panels, behind its own `:if`. See stacksjs/stx#2058.
|
|
60
|
+
*
|
|
61
|
+
* @example
|
|
62
|
+
* ```html
|
|
63
|
+
* <TabList v-model:value="tab" label="Store sections">
|
|
64
|
+
* <Tab value="products" controls="products-panel">Products</Tab>
|
|
65
|
+
* <Tab value="sales" controls="sales-panel">Sales</Tab>
|
|
66
|
+
* </TabList>
|
|
67
|
+
* ```
|
|
68
|
+
*/
|
|
69
|
+
export interface TabListProps {
|
|
70
|
+
value?: string
|
|
71
|
+
label?: string
|
|
72
|
+
labelledby?: string
|
|
73
|
+
orientation?: 'horizontal' | 'vertical'
|
|
74
|
+
variant?: 'line' | 'pills' | 'none'
|
|
75
|
+
className?: string
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
export interface TabProps {
|
|
79
|
+
value: string
|
|
80
|
+
controls?: string
|
|
81
|
+
disabled?: boolean
|
|
82
|
+
variant?: 'line' | 'pills' | 'none'
|
|
83
|
+
className?: string
|
|
84
|
+
}
|
|
@@ -33,7 +33,14 @@ export const resize = $props.resize || 'vertical'
|
|
|
33
33
|
export const className = $props.className || ''
|
|
34
34
|
|
|
35
35
|
const resizeClasses = { none: 'resize-none', vertical: 'resize-y', horizontal: 'resize-x', both: 'resize' }
|
|
36
|
-
|
|
36
|
+
/*
|
|
37
|
+
* The padding and type read the density roles, so an
|
|
38
|
+
* app sets its own field size with --stx-control-px, --stx-control-py,
|
|
39
|
+
* --stx-control-text and --stx-control-leading instead of a className
|
|
40
|
+
* that ties with py-2 on specificity. The fallbacks are px-4 / py-2 /
|
|
41
|
+
* text-base, so nothing moves by default (stacksjs/stx#1993).
|
|
42
|
+
*/
|
|
43
|
+
const baseClasses = 'block w-full border-0 rounded-control shadow-sm ring-1 ring-inset transition-colors duration-200 focus:ring-2 focus:ring-inset px-control-x py-control-y text-control bg-field'
|
|
37
44
|
/*
|
|
38
45
|
* The two halves of the validity state, exported so the CLIENT can swap them
|
|
39
46
|
* (stacksjs/stx#2047). Toggled per class rather than as a whole string,
|
|
@@ -82,8 +89,12 @@ const emit = defineEmits()
|
|
|
82
89
|
const isError = useReactiveProp('error', {{ error }})
|
|
83
90
|
const autoResize = {{ autoResize }}
|
|
84
91
|
const maxRows = {{ maxRows }}
|
|
85
|
-
|
|
86
|
-
|
|
92
|
+
/*
|
|
93
|
+
* Two-way, like TextInput (stacksjs/stx#2059): `v-model:value="bio"` keeps the
|
|
94
|
+
* page's signal in step, with `.lazy` and `.trim` honoured. `input` is still
|
|
95
|
+
* emitted for the callers that listen for it.
|
|
96
|
+
*/
|
|
97
|
+
const inputValue = useModel('value', { default: {{ value }}, parse: (v) => v == null ? '' : String(v) })
|
|
87
98
|
|
|
88
99
|
function resizeTextarea(el) {
|
|
89
100
|
if (!autoResize) return
|
|
@@ -96,19 +107,26 @@ function resizeTextarea(el) {
|
|
|
96
107
|
}
|
|
97
108
|
|
|
98
109
|
function onInput(event) {
|
|
99
|
-
inputValue.
|
|
110
|
+
inputValue.input(event.target.value)
|
|
100
111
|
resizeTextarea(event.target)
|
|
101
|
-
emit('input', inputValue())
|
|
112
|
+
if (!inputValue.modifiers.lazy) emit('input', inputValue())
|
|
113
|
+
}
|
|
114
|
+
|
|
115
|
+
function onChange(event) {
|
|
116
|
+
inputValue.change(event.target.value)
|
|
117
|
+
if (inputValue.modifiers.lazy) emit('input', inputValue())
|
|
102
118
|
}
|
|
103
119
|
|
|
104
120
|
onMount(() => {
|
|
105
121
|
if (autoResize) {
|
|
106
|
-
|
|
122
|
+
// The instance's own id: a fixed 'textarea-input' stopped matching
|
|
123
|
+
// anything once ids became unique per instance.
|
|
124
|
+
const el = document.getElementById({{ inputId }})
|
|
107
125
|
if (el) resizeTextarea(el)
|
|
108
126
|
}
|
|
109
127
|
})
|
|
110
128
|
|
|
111
|
-
const charCount = derived(() => `${inputValue().length} / {{ maxLength || 0 }}`)
|
|
129
|
+
const charCount = derived(() => `${String(inputValue()).length} / {{ maxLength || 0 }}`)
|
|
112
130
|
|
|
113
131
|
defineExpose({ value: inputValue })
|
|
114
132
|
</script>
|
|
@@ -137,7 +155,12 @@ defineExpose({ value: inputValue })
|
|
|
137
155
|
@if(readonly)readonly@endif
|
|
138
156
|
@if(required)required@endif
|
|
139
157
|
@if(maxLength)maxlength="{{ maxLength }}"@endif
|
|
158
|
+
{{-- The live value. The served text between the tags is the default
|
|
159
|
+
value; this keeps the element in step when the page's signal moves,
|
|
160
|
+
which nothing did before -- the field had no binding at all. --}}
|
|
161
|
+
:value="inputValue()"
|
|
140
162
|
@input="onInput($event)"
|
|
163
|
+
@change="onChange($event)"
|
|
141
164
|
aria-invalid="{{ error ? 'true' : 'false' }}"
|
|
142
165
|
:aria-invalid="isError() ? 'true' : 'false'"
|
|
143
166
|
:class="{ '{{ stateInvalidClasses }}': isError(), '{{ stateValidClasses }}': !isError() }"
|
|
@@ -133,9 +133,19 @@ function hideTooltip() {
|
|
|
133
133
|
* unless this tooltip is the one showing. useEventListener drops it on
|
|
134
134
|
* teardown, so a page of tooltips does not leak one apiece.
|
|
135
135
|
*/
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
136
|
+
/*
|
|
137
|
+
* While it shows, the tooltip is a popup layer on the page's overlay stack, so
|
|
138
|
+
* Escape dismisses it first: a tooltip over a dialog's field goes, and the
|
|
139
|
+
* dialog stays (stacksjs/stx#2057). It listened on the document itself, where
|
|
140
|
+
* the dialog's listener could take the same Escape.
|
|
141
|
+
*/
|
|
142
|
+
effect(() => {
|
|
143
|
+
if (isVisible()) window.stx.overlays.open(tooltipId, { modal: false, panel: null, onEscape: hideTooltip })
|
|
144
|
+
else window.stx.overlays.close(tooltipId)
|
|
145
|
+
})
|
|
146
|
+
|
|
147
|
+
onDestroy(() => {
|
|
148
|
+
window.stx.overlays.close(tooltipId)
|
|
139
149
|
})
|
|
140
150
|
|
|
141
151
|
/*
|
|
@@ -0,0 +1,69 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The page's overlay stack, for an overlay the app draws itself.
|
|
3
|
+
*
|
|
4
|
+
* Dialog, Drawer and stxConfirm/stxAlert all go on one stack in the stx
|
|
5
|
+
* runtime (`window.stx.overlays`): the topmost overlay answers Escape and owns
|
|
6
|
+
* Tab, the page stays locked from the first open to the last close, and focus
|
|
7
|
+
* moves into an overlay and back to whatever opened it. An app's own slide-over
|
|
8
|
+
* or full-screen panel joins the same stack through these, so it nests with the
|
|
9
|
+
* library's dialogs instead of competing with them (stacksjs/stx#2057).
|
|
10
|
+
*
|
|
11
|
+
* ```ts
|
|
12
|
+
* import { closeOverlay, openOverlay } from '@stacksjs/components'
|
|
13
|
+
*
|
|
14
|
+
* effect(() => {
|
|
15
|
+
* if (panelOpen()) openOverlay('workout-panel', { panel: '#wp-panel', onEscape: close })
|
|
16
|
+
* else closeOverlay('workout-panel')
|
|
17
|
+
* })
|
|
18
|
+
* onDestroy(() => closeOverlay('workout-panel'))
|
|
19
|
+
* ```
|
|
20
|
+
*
|
|
21
|
+
* Every call is a no-op where there is no runtime (server rendering, tests
|
|
22
|
+
* without a DOM), so shared code can call them unguarded.
|
|
23
|
+
*/
|
|
24
|
+
|
|
25
|
+
export interface OverlayOptions {
|
|
26
|
+
/** Where focus is kept while this overlay is on top: an element, a selector, or a function returning one. A selector or function is looked up each time, since the panel may not be rendered yet when the overlay opens. */
|
|
27
|
+
panel: Element | string | null | (() => Element | null)
|
|
28
|
+
/** The overlay's outermost element, when it is not the panel. An overlay that is a direct child of `<body>` makes the rest of the page inert while it is on top. */
|
|
29
|
+
root?: Element | null
|
|
30
|
+
/** Escape while this overlay is on top. Leave it out for one that must be answered rather than dismissed. */
|
|
31
|
+
onEscape?: (() => void) | null
|
|
32
|
+
/** Where focus goes first. Defaults to `[autofocus]`, then the first focusable element, then the panel. */
|
|
33
|
+
initialFocus?: HTMLElement | null | (() => HTMLElement | null)
|
|
34
|
+
/** Where focus goes back to on close. Defaults to whatever had focus when it opened. */
|
|
35
|
+
opener?: Element | null
|
|
36
|
+
/** Give focus back on close. Default true. */
|
|
37
|
+
restoreFocus?: boolean
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
interface OverlayStack {
|
|
41
|
+
open: (id: string, options: OverlayOptions) => void
|
|
42
|
+
close: (id: string) => void
|
|
43
|
+
isTop: (id: string) => boolean
|
|
44
|
+
list: () => string[]
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
function stack(): OverlayStack | undefined {
|
|
48
|
+
return (globalThis as { stx?: { overlays?: OverlayStack } }).stx?.overlays
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
/** Put an overlay on top. Calling it again for an open id only updates its options. */
|
|
52
|
+
export function openOverlay(id: string, options: OverlayOptions): void {
|
|
53
|
+
stack()?.open(id, options)
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
/** Take an overlay off the stack, wherever it is in it. */
|
|
57
|
+
export function closeOverlay(id: string): void {
|
|
58
|
+
stack()?.close(id)
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
/** Whether this overlay is the one that has the keyboard. */
|
|
62
|
+
export function isTopOverlay(id: string): boolean {
|
|
63
|
+
return stack()?.isTop(id) ?? false
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
/** Open overlay ids, outermost first. */
|
|
67
|
+
export function openOverlays(): string[] {
|
|
68
|
+
return stack()?.list() ?? []
|
|
69
|
+
}
|