@neovici/cosmoz-dropdown 7.5.0 → 7.6.1-beta.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,9 +1,8 @@
1
- import { component, css } from '@pionjs/pion';
1
+ import { component, css, useCallback, useEffect, useRef } 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';
7
6
  import { Content } from './cosmoz-dropdown-content';
8
7
  import { useFloating } from './use-floating';
9
8
  import { useHostFocus } from './use-focus';
@@ -32,12 +31,30 @@ const style = css `
32
31
  const Dropdown = (host) => {
33
32
  const { placement, strategy, middleware, render } = host;
34
33
  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
- return html ` <div class="anchor" part="anchor" ${ref(setReference)}>
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)}>
41
58
  <button
42
59
  @mousedown=${preventDefault}
43
60
  @click=${onToggle}
@@ -47,16 +64,16 @@ const Dropdown = (host) => {
47
64
  <slot name="button">...</slot>
48
65
  </button>
49
66
  </div>
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
- > `)}`;
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
+ `;
60
77
  };
61
78
  customElements.define('cosmoz-dropdown', component(Dropdown, { styleSheets: [style] }));
62
79
  export { Content, Dropdown };
@@ -40,7 +40,7 @@ const style = css `
40
40
  position: fixed;
41
41
  position-anchor: --dropdown-anchor;
42
42
  inset: unset;
43
- margin: var(--cz-spacing, 0.25rem);
43
+ margin-block: var(--cz-spacing, 0.25rem);
44
44
  position-try-fallbacks:
45
45
  flip-block,
46
46
  flip-inline,
package/dist/use-focus.js CHANGED
@@ -1,6 +1,7 @@
1
- import { useEffect, useState, useCallback } from '@pionjs/pion';
2
1
  import { useMeta } from '@neovici/cosmoz-utils/hooks/use-meta';
2
+ import { useCallback, useEffect, useState } from '@pionjs/pion';
3
3
  const isFocused = (t) => t.matches(':focus-within');
4
+ const hasOpenDialogInPath = (e) => e.composedPath().some((el) => el instanceof HTMLDialogElement && el.open);
4
5
  export const useFocus = ({ disabled, onFocus }) => {
5
6
  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) => {
6
7
  const target = e.currentTarget;
@@ -16,6 +17,9 @@ export const useFocus = ({ disabled, onFocus }) => {
16
17
  if (e.defaultPrevented) {
17
18
  return;
18
19
  }
20
+ if (e.key === 'Escape' && hasOpenDialogInPath(e)) {
21
+ return;
22
+ }
19
23
  const { closed } = meta;
20
24
  if (e.key === 'Escape' && !closed) {
21
25
  e.preventDefault();
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@neovici/cosmoz-dropdown",
3
- "version": "7.5.0",
3
+ "version": "7.6.1-beta.1",
4
4
  "description": "A simple dropdown web component",
5
5
  "keywords": [
6
6
  "lit-html",
@@ -44,7 +44,13 @@
44
44
  "@semantic-release/npm",
45
45
  "@semantic-release/git"
46
46
  ],
47
- "branch": "master",
47
+ "branches": [
48
+ "master",
49
+ {
50
+ "name": "beta",
51
+ "prerelease": true
52
+ }
53
+ ],
48
54
  "preset": "conventionalcommits"
49
55
  },
50
56
  "publishConfig": {