@bwui/vue 0.1.9 → 0.1.12

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.
@@ -0,0 +1,182 @@
1
+ import { defineComponent, h, nextTick, onBeforeUnmount, onMounted, ref, watch, } from 'vue';
2
+ import { cn } from './cn';
3
+ const FOCUSABLE = 'a[href], button:not([disabled]), input:not([disabled]):not([type="hidden"]), select:not([disabled]), textarea:not([disabled]), summary, [tabindex]:not([tabindex="-1"]), [contenteditable="true"]';
4
+ let popoverSeq = 0;
5
+ function closeIcon() {
6
+ return h('svg', {
7
+ class: 'bw-icon',
8
+ viewBox: '0 0 24 24',
9
+ fill: 'none',
10
+ stroke: 'currentColor',
11
+ 'stroke-width': '2',
12
+ 'stroke-linecap': 'round',
13
+ 'stroke-linejoin': 'round',
14
+ 'aria-hidden': 'true',
15
+ }, [h('path', { d: 'M18 6 6 18' }), h('path', { d: 'm6 6 12 12' })]);
16
+ }
17
+ /**
18
+ * Popover (docs/components/popover.md). Slots: `trigger`, the default (body)
19
+ * and `footer`. `v-model:open` carries the state (or leave it unbound for an
20
+ * uncontrolled popover). Escape with focus on the trigger or in the panel, and
21
+ * a press outside, close it; every close returns focus to the trigger only if
22
+ * it was inside the panel or on the trigger (POP-035). The panel needs a name:
23
+ * pass `title` or `aria-label` (POP-051).
24
+ */
25
+ export const Popover = defineComponent({
26
+ name: 'BwPopover',
27
+ inheritAttrs: false,
28
+ props: {
29
+ id: String,
30
+ placement: { type: String, default: 'bottom' },
31
+ title: String,
32
+ ariaLabel: String,
33
+ closable: { type: Boolean, default: false },
34
+ // POP-053 / POP-076: required when closable; no English default.
35
+ closeLabel: {
36
+ type: String,
37
+ default: undefined,
38
+ validator(value, props) {
39
+ if (!props.closable)
40
+ return true;
41
+ return typeof value === 'string' && value.trim().length > 0;
42
+ },
43
+ },
44
+ triggerVariant: { type: String, default: 'secondary' },
45
+ triggerSize: { type: String, default: 'md' },
46
+ triggerLabel: String,
47
+ open: { type: Boolean, default: undefined },
48
+ defaultOpen: { type: Boolean, default: false },
49
+ },
50
+ emits: ['update:open', 'open-change'],
51
+ setup(props, { attrs, emit, slots }) {
52
+ if (props.closable && !(typeof props.closeLabel === 'string' && props.closeLabel.trim())) {
53
+ throw new Error('BwPopover: closeLabel is required when closable is true (POP-053).');
54
+ }
55
+ popoverSeq += 1;
56
+ const panelId = props.id ?? `bw-popover-v${popoverSeq}`;
57
+ const titleId = `${panelId}-title`;
58
+ const internal = ref(props.defaultOpen);
59
+ const isOpen = () => (props.open !== undefined ? props.open : internal.value);
60
+ const root = ref(null);
61
+ const trigger = ref(null);
62
+ const panel = ref(null);
63
+ function change(next, reason) {
64
+ if (props.open === undefined)
65
+ internal.value = next;
66
+ emit('update:open', next);
67
+ emit('open-change', next, reason);
68
+ }
69
+ // Every keyboard and trigger close routes through here (POP-035).
70
+ function close(reason) {
71
+ const active = document.activeElement;
72
+ const restore = active === trigger.value || Boolean(panel.value?.contains(active));
73
+ if (restore && trigger.value?.isConnected)
74
+ trigger.value.focus();
75
+ change(false, reason);
76
+ }
77
+ // POP-031: focus moves into the panel on open, not on first mount.
78
+ watch(() => isOpen(), async (open, wasOpen) => {
79
+ if (!open || wasOpen)
80
+ return;
81
+ await nextTick();
82
+ const el = panel.value;
83
+ if (!el)
84
+ return;
85
+ const marked = el.querySelector('[data-bw-autofocus]');
86
+ const all = Array.from(el.querySelectorAll(FOCUSABLE)).filter((node) => !node.closest('[hidden]'));
87
+ const others = all.filter((node) => !node.classList.contains('bw-popover__close'));
88
+ const target = marked ?? others[0] ?? all[0] ?? el;
89
+ target.focus();
90
+ });
91
+ // POP-033: a pointerdown outside the root closes it.
92
+ function onPointerDown(event) {
93
+ if (!isOpen())
94
+ return;
95
+ const el = root.value;
96
+ if (!el || el.contains(event.target))
97
+ return;
98
+ // The press moves focus after this handler. Close without touching focus
99
+ // now; if focus was inside and the press left it nowhere, return it to
100
+ // the trigger once the press has settled.
101
+ const hadFocus = Boolean(panel.value?.contains(document.activeElement));
102
+ change(false, 'outside');
103
+ if (hadFocus) {
104
+ setTimeout(() => {
105
+ const now = document.activeElement;
106
+ if ((!now || now === document.body) && trigger.value?.isConnected)
107
+ trigger.value.focus();
108
+ }, 0);
109
+ }
110
+ }
111
+ onMounted(() => document.addEventListener('pointerdown', onPointerDown));
112
+ onBeforeUnmount(() => document.removeEventListener('pointerdown', onPointerDown));
113
+ // POP-032: Escape acts only with focus on the trigger or inside the panel,
114
+ // and stops propagation so an enclosing overlay does not also close.
115
+ function onKeydown(event) {
116
+ if (event.key !== 'Escape' || !isOpen())
117
+ return;
118
+ const active = document.activeElement;
119
+ if (active !== trigger.value && !panel.value?.contains(active))
120
+ return;
121
+ event.preventDefault();
122
+ event.stopPropagation();
123
+ close('escape');
124
+ }
125
+ function onPanelClick(event) {
126
+ const control = event.target.closest('[data-bw-popover-close]');
127
+ if (control && panel.value?.contains(control))
128
+ close('close-button');
129
+ }
130
+ return () => {
131
+ const open = isOpen();
132
+ const named = props.title ? { 'aria-labelledby': titleId } : { 'aria-label': props.ariaLabel };
133
+ const variant = props.triggerVariant;
134
+ return h('div', {
135
+ ...attrs,
136
+ ref: root,
137
+ class: cn('bw-popover', `bw-popover--${props.placement}`, String(attrs.class || '')),
138
+ ...(open ? { 'data-bw-open': '' } : {}),
139
+ onKeydown,
140
+ }, [
141
+ h('button', {
142
+ ref: trigger,
143
+ type: 'button',
144
+ class: cn('bw-popover__trigger', 'bw-btn', `bw-btn--${variant}`, `bw-btn--size-${props.triggerSize}`, props.triggerLabel && 'bw-btn--icon-only'),
145
+ 'aria-haspopup': 'dialog',
146
+ 'aria-expanded': open ? 'true' : 'false',
147
+ 'aria-controls': panelId,
148
+ 'aria-label': props.triggerLabel,
149
+ onClick: () => (open ? close('trigger') : change(true, 'trigger')),
150
+ }, slots.trigger?.()),
151
+ h('div', {
152
+ ref: panel,
153
+ id: panelId,
154
+ role: 'dialog',
155
+ ...named,
156
+ tabindex: -1,
157
+ hidden: !open,
158
+ class: 'bw-popover__panel',
159
+ onClick: onPanelClick,
160
+ }, [
161
+ props.title || props.closable
162
+ ? h('div', { class: 'bw-popover__header' }, [
163
+ props.title
164
+ ? h('h2', { class: 'bw-popover__title', id: titleId }, props.title)
165
+ : null,
166
+ props.closable
167
+ ? h('button', {
168
+ type: 'button',
169
+ class: 'bw-popover__close',
170
+ 'aria-label': props.closeLabel,
171
+ 'data-bw-popover-close': '',
172
+ }, [closeIcon()])
173
+ : null,
174
+ ])
175
+ : null,
176
+ h('div', { class: 'bw-popover__body' }, slots.default?.()),
177
+ slots.footer ? h('div', { class: 'bw-popover__footer' }, slots.footer()) : null,
178
+ ]),
179
+ ]);
180
+ };
181
+ },
182
+ });