@neovici/cosmoz-dropdown 7.6.1-beta.1 → 7.7.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 +20 -0
- package/dist/cosmoz-dropdown.js +13 -30
- package/dist/next/cosmoz-dropdown-next.js +15 -12
- package/dist/use-focus.js +25 -10
- package/package.json +5 -9
package/README.md
CHANGED
|
@@ -32,6 +32,8 @@ Modern dropdown using the Popover API and CSS Anchor Positioning.
|
|
|
32
32
|
| Property | Type | Default | Description |
|
|
33
33
|
| --------------- | --------- | --------------------- | ------------------------------------------------------------------------------------------------------------------------ |
|
|
34
34
|
| `placement` | `string` | `'bottom span-right'` | CSS anchor `position-area` value. See [MDN](https://developer.mozilla.org/en-US/docs/Web/CSS/position-area) for options. |
|
|
35
|
+
| `disabled` | `boolean` | `false` | Prevents the dropdown from opening. |
|
|
36
|
+
| `passthrough` | `boolean` | `false` | When `disabled` + `passthrough`, render default slot content inline instead of inside the popover. |
|
|
35
37
|
| `open-on-hover` | `boolean` | `false` | Open on pointer hover. |
|
|
36
38
|
| `open-on-focus` | `boolean` | `false` | Open when the trigger receives focus. |
|
|
37
39
|
|
|
@@ -65,6 +67,24 @@ When auto-open is enabled:
|
|
|
65
67
|
- When `open-on-focus` is active, clicking the button only opens (does not toggle)
|
|
66
68
|
- Otherwise, click works as a toggle
|
|
67
69
|
|
|
70
|
+
#### Disabled + Passthrough
|
|
71
|
+
|
|
72
|
+
When `disabled` and `passthrough` are both set, the default slot content renders in normal document flow (outside the popover). This enables using the dropdown as a conditional wrapper — popover mode when enabled, inline mode when disabled:
|
|
73
|
+
|
|
74
|
+
```html
|
|
75
|
+
<!-- Dropdown mode (enabled) -->
|
|
76
|
+
<cosmoz-dropdown-next placement="bottom span-right">
|
|
77
|
+
<button slot="button">Menu</button>
|
|
78
|
+
<div>Popover content</div>
|
|
79
|
+
</cosmoz-dropdown-next>
|
|
80
|
+
|
|
81
|
+
<!-- Inline mode (disabled + passthrough) -->
|
|
82
|
+
<cosmoz-dropdown-next disabled passthrough>
|
|
83
|
+
<button slot="button">Menu</button>
|
|
84
|
+
<div>Rendered inline, not inside a popover</div>
|
|
85
|
+
</cosmoz-dropdown-next>
|
|
86
|
+
```
|
|
87
|
+
|
|
68
88
|
#### Slots
|
|
69
89
|
|
|
70
90
|
| Slot | Description |
|
package/dist/cosmoz-dropdown.js
CHANGED
|
@@ -1,8 +1,9 @@
|
|
|
1
|
-
import { component, css
|
|
1
|
+
import { component, css } from '@pionjs/pion';
|
|
2
2
|
import { html, nothing } from 'lit-html';
|
|
3
3
|
import { guard } from 'lit-html/directives/guard.js';
|
|
4
4
|
import { ref } from 'lit-html/directives/ref.js';
|
|
5
5
|
import { styleMap } from 'lit-html/directives/style-map.js';
|
|
6
|
+
import { when } from 'lit-html/directives/when.js';
|
|
6
7
|
import { Content } from './cosmoz-dropdown-content';
|
|
7
8
|
import { useFloating } from './use-floating';
|
|
8
9
|
import { useHostFocus } from './use-focus';
|
|
@@ -31,30 +32,12 @@ const style = css `
|
|
|
31
32
|
const Dropdown = (host) => {
|
|
32
33
|
const { placement, strategy, middleware, render } = host;
|
|
33
34
|
const { active, onToggle } = useHostFocus(host);
|
|
34
|
-
const contentRef = useRef();
|
|
35
35
|
const { styles, setReference, setFloating } = useFloating({
|
|
36
36
|
placement,
|
|
37
37
|
strategy,
|
|
38
38
|
middleware,
|
|
39
39
|
});
|
|
40
|
-
|
|
41
|
-
contentRef.current = el;
|
|
42
|
-
setFloating(el);
|
|
43
|
-
}, [setFloating]);
|
|
44
|
-
useEffect(() => {
|
|
45
|
-
const content = contentRef.current;
|
|
46
|
-
if (!content) {
|
|
47
|
-
return;
|
|
48
|
-
}
|
|
49
|
-
if (active && !content.matches(':popover-open')) {
|
|
50
|
-
content.showPopover?.();
|
|
51
|
-
}
|
|
52
|
-
if (!active && content.matches(':popover-open')) {
|
|
53
|
-
content.hidePopover?.();
|
|
54
|
-
}
|
|
55
|
-
}, [active]);
|
|
56
|
-
return html `
|
|
57
|
-
<div class="anchor" part="anchor" ${ref(setReference)}>
|
|
40
|
+
return html ` <div class="anchor" part="anchor" ${ref(setReference)}>
|
|
58
41
|
<button
|
|
59
42
|
@mousedown=${preventDefault}
|
|
60
43
|
@click=${onToggle}
|
|
@@ -64,16 +47,16 @@ const Dropdown = (host) => {
|
|
|
64
47
|
<slot name="button">...</slot>
|
|
65
48
|
</button>
|
|
66
49
|
</div>
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
50
|
+
${when(active, () => html `<cosmoz-dropdown-content
|
|
51
|
+
popover
|
|
52
|
+
id="content"
|
|
53
|
+
part="content"
|
|
54
|
+
exportparts="wrap, content"
|
|
55
|
+
style="${styleMap(styles)}"
|
|
56
|
+
@connected=${(e) => e.target.showPopover?.()}
|
|
57
|
+
${ref(setFloating)}
|
|
58
|
+
><slot></slot>${guard([render], () => render?.() || nothing)}</cosmoz-dropdown-content
|
|
59
|
+
> `)}`;
|
|
77
60
|
};
|
|
78
61
|
customElements.define('cosmoz-dropdown', component(Dropdown, { styleSheets: [style] }));
|
|
79
62
|
export { Content, Dropdown };
|
|
@@ -85,7 +85,7 @@ const style = css `
|
|
|
85
85
|
}
|
|
86
86
|
`;
|
|
87
87
|
const CosmozDropdownNext = (host) => {
|
|
88
|
-
const { placement = 'bottom span-right', disabled, openOnHover, openOnFocus, } = host;
|
|
88
|
+
const { placement = 'bottom span-right', disabled, passthrough, openOnHover, openOnFocus, } = host;
|
|
89
89
|
const popoverRef = useRef();
|
|
90
90
|
const [opened, setOpened] = useProperty('opened', false);
|
|
91
91
|
// Call showPopover/hidePopover synchronously so the browser associates
|
|
@@ -146,17 +146,19 @@ const CosmozDropdownNext = (host) => {
|
|
|
146
146
|
}, []);
|
|
147
147
|
return html `
|
|
148
148
|
<slot name="button" @click=${handleClick}></slot>
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
149
|
+
${disabled && passthrough
|
|
150
|
+
? html `<slot></slot>`
|
|
151
|
+
: html `<div
|
|
152
|
+
popover
|
|
153
|
+
style="position-area: ${placement}"
|
|
154
|
+
@toggle=${onToggle}
|
|
155
|
+
@select=${close}
|
|
156
|
+
@focusout=${scheduleClose}
|
|
157
|
+
@focusin=${cancelClose}
|
|
158
|
+
${ref((el) => el && (popoverRef.current = el))}
|
|
159
|
+
>
|
|
160
|
+
<slot></slot>
|
|
161
|
+
</div>`}
|
|
160
162
|
`;
|
|
161
163
|
};
|
|
162
164
|
customElements.define('cosmoz-dropdown-next', component(CosmozDropdownNext, {
|
|
@@ -164,6 +166,7 @@ customElements.define('cosmoz-dropdown-next', component(CosmozDropdownNext, {
|
|
|
164
166
|
observedAttributes: [
|
|
165
167
|
'placement',
|
|
166
168
|
'disabled',
|
|
169
|
+
'passthrough',
|
|
167
170
|
'open-on-hover',
|
|
168
171
|
'open-on-focus',
|
|
169
172
|
],
|
package/dist/use-focus.js
CHANGED
|
@@ -1,7 +1,12 @@
|
|
|
1
1
|
import { useMeta } from '@neovici/cosmoz-utils/hooks/use-meta';
|
|
2
|
-
import { useCallback, useEffect, useState } from '@pionjs/pion';
|
|
3
|
-
const isFocused = (t) =>
|
|
4
|
-
|
|
2
|
+
import { useCallback, useEffect, useRef, useState } from '@pionjs/pion';
|
|
3
|
+
const isFocused = (t) => {
|
|
4
|
+
if (t.matches(':focus-within')) {
|
|
5
|
+
return true;
|
|
6
|
+
}
|
|
7
|
+
const popover = t.shadowRoot?.querySelector('[popover]');
|
|
8
|
+
return popover?.matches(':focus-within') ?? false;
|
|
9
|
+
};
|
|
5
10
|
export const useFocus = ({ disabled, onFocus }) => {
|
|
6
11
|
const [focusState, setState] = useState(), { focused: _focused, closed } = focusState || {}, focused = _focused && !disabled, meta = useMeta({ closed, onFocus }), setClosed = useCallback((closed) => setState((p) => ({ ...p, closed })), []), onToggle = useCallback((e) => {
|
|
7
12
|
const target = e.currentTarget;
|
|
@@ -17,9 +22,6 @@ export const useFocus = ({ disabled, onFocus }) => {
|
|
|
17
22
|
if (e.defaultPrevented) {
|
|
18
23
|
return;
|
|
19
24
|
}
|
|
20
|
-
if (e.key === 'Escape' && hasOpenDialogInPath(e)) {
|
|
21
|
-
return;
|
|
22
|
-
}
|
|
23
25
|
const { closed } = meta;
|
|
24
26
|
if (e.key === 'Escape' && !closed) {
|
|
25
27
|
e.preventDefault();
|
|
@@ -45,15 +47,28 @@ export const useFocus = ({ disabled, onFocus }) => {
|
|
|
45
47
|
}, [meta]),
|
|
46
48
|
};
|
|
47
49
|
};
|
|
48
|
-
const fevs = ['focusin', 'focusout'];
|
|
49
50
|
export const useHostFocus = (host) => {
|
|
50
51
|
const thru = useFocus(host), { onFocus } = thru;
|
|
52
|
+
const scheduleRef = useRef();
|
|
51
53
|
useEffect(() => {
|
|
52
54
|
host.setAttribute('tabindex', '0');
|
|
53
|
-
|
|
55
|
+
const onFocusIn = (e) => {
|
|
56
|
+
clearTimeout(scheduleRef.current);
|
|
57
|
+
onFocus(e);
|
|
58
|
+
};
|
|
59
|
+
const onFocusOut = (e) => {
|
|
60
|
+
clearTimeout(scheduleRef.current);
|
|
61
|
+
const currentTarget = e.currentTarget;
|
|
62
|
+
// TODO: `onFocus` only uses `e.currentTarget`, consider refactoring to accept `HTMLElement` instead of `FocusEvent`
|
|
63
|
+
scheduleRef.current = setTimeout(() => onFocus({ currentTarget }), 30);
|
|
64
|
+
};
|
|
65
|
+
host.addEventListener('focusin', onFocusIn);
|
|
66
|
+
host.addEventListener('focusout', onFocusOut);
|
|
54
67
|
return () => {
|
|
55
|
-
|
|
68
|
+
clearTimeout(scheduleRef.current);
|
|
69
|
+
host.removeEventListener('focusin', onFocusIn);
|
|
70
|
+
host.removeEventListener('focusout', onFocusOut);
|
|
56
71
|
};
|
|
57
|
-
}, []);
|
|
72
|
+
}, [onFocus]);
|
|
58
73
|
return thru;
|
|
59
74
|
};
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@neovici/cosmoz-dropdown",
|
|
3
|
-
"version": "7.
|
|
3
|
+
"version": "7.7.0",
|
|
4
4
|
"description": "A simple dropdown web component",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"lit-html",
|
|
@@ -44,13 +44,7 @@
|
|
|
44
44
|
"@semantic-release/npm",
|
|
45
45
|
"@semantic-release/git"
|
|
46
46
|
],
|
|
47
|
-
"
|
|
48
|
-
"master",
|
|
49
|
-
{
|
|
50
|
-
"name": "beta",
|
|
51
|
-
"prerelease": true
|
|
52
|
-
}
|
|
53
|
-
],
|
|
47
|
+
"branch": "master",
|
|
54
48
|
"preset": "conventionalcommits"
|
|
55
49
|
},
|
|
56
50
|
"publishConfig": {
|
|
@@ -89,10 +83,11 @@
|
|
|
89
83
|
"devDependencies": {
|
|
90
84
|
"@commitlint/cli": "^20.0.0",
|
|
91
85
|
"@commitlint/config-conventional": "^20.0.0",
|
|
92
|
-
"@neovici/cfg": "^2.
|
|
86
|
+
"@neovici/cfg": "^2.13.0",
|
|
93
87
|
"@neovici/cosmoz-button": "^1.0.0",
|
|
94
88
|
"@neovici/cosmoz-tokens": "^3.2.1",
|
|
95
89
|
"@open-wc/testing": "^4.0.0",
|
|
90
|
+
"@playwright/test": "1.60.0",
|
|
96
91
|
"@semantic-release/changelog": "^6.0.0",
|
|
97
92
|
"@semantic-release/git": "^10.0.0",
|
|
98
93
|
"@storybook/addon-docs": "^10.0.0",
|
|
@@ -106,6 +101,7 @@
|
|
|
106
101
|
"http-server": "^14.1.1",
|
|
107
102
|
"husky": "^9.0.11",
|
|
108
103
|
"lint-staged": "^16.2.7",
|
|
104
|
+
"playwright": "1.60.0",
|
|
109
105
|
"rollup-plugin-esbuild": "^6.1.1",
|
|
110
106
|
"semantic-release": "^25.0.0",
|
|
111
107
|
"shadow-dom-testing-library": "^1.13.1",
|