@neovici/cosmoz-dropdown 7.6.1-beta.1 → 7.6.1

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.
@@ -1,8 +1,9 @@
1
- import { component, css, useCallback, useEffect, useRef } from '@pionjs/pion';
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
- const setContent = useCallback((el) => {
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
- <cosmoz-dropdown-content
68
- popover
69
- id="content"
70
- part="content"
71
- exportparts="wrap, content"
72
- style="${styleMap(styles)}"
73
- ${ref(setContent)}
74
- ><slot></slot>${guard([render], () => render?.() || nothing)}</cosmoz-dropdown-content
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 };
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) => t.matches(':focus-within');
4
- const hasOpenDialogInPath = (e) => e.composedPath().some((el) => el instanceof HTMLDialogElement && el.open);
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
- fevs.forEach((ev) => host.addEventListener(ev, onFocus));
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
- fevs.forEach((ev) => host.removeEventListener(ev, onFocus));
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.6.1-beta.1",
3
+ "version": "7.6.1",
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
- "branches": [
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.8.0",
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",