@neovici/cosmoz-dropdown 1.0.4 → 1.3.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/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@neovici/cosmoz-dropdown",
3
- "version": "1.0.4",
3
+ "version": "1.3.0",
4
4
  "description": "A simple dropdown web component",
5
5
  "keywords": [
6
6
  "lit-html",
@@ -1,9 +1,8 @@
1
- import { html, component, useCallback, useEffect } from 'haunted';
1
+ import { html, component, useCallback } from 'haunted';
2
2
  import { usePosition } from './use-position';
3
- import { useFocus } from './use-focus';
3
+ import { useHostFocus } from './use-focus';
4
4
 
5
5
  const preventDefault = e => e.preventDefault(),
6
- fevs = ['focusin', 'focusout'],
7
6
  Content = host => {
8
7
  usePosition({ anchor: host.anchor, host, placement: host.placement });
9
8
  return html` <style>
@@ -12,21 +11,22 @@ const preventDefault = e => e.preventDefault(),
12
11
  left: -9999999999px;
13
12
  min-width: 72px;
14
13
  box-sizing: border-box;
14
+ padding: var(--cosmoz-dropdown-spacing, 0px)
15
+ }
16
+ .content {
17
+ background: var(--cosmoz-dropdown-bg-color, #fff);
18
+ box-shadow: var(--cosmoz-dropdown-box-shadow, 0px 3px 4px 2px rgba(0, 0, 0, 0.1));
19
+ }
20
+ ::slotted(*) {
21
+ display: block;
15
22
  }
16
23
  </style>
17
- <slot></slot>`;
24
+ <div class="content"><slot></slot></div>`;
18
25
  },
19
26
  Dropdown = host => {
20
27
  const { placement } = host,
21
- { active, onFocus, onToggle } = useFocus(host),
22
- anchor = useCallback(() => host.shadowRoot.querySelector('.anchor'), []);
23
- useEffect(() => {
24
- host.setAttribute('tabindex', '-1');
25
- fevs.forEach(ev => host.addEventListener(ev, onFocus));
26
- return () => {
27
- fevs.forEach(ev => host.removeEventListener(ev, onFocus));
28
- };
29
- }, []);
28
+ anchor = useCallback(() => host.shadowRoot.querySelector('.anchor'), []),
29
+ { active, onToggle } = useHostFocus(host);
30
30
  return html`
31
31
  <style>
32
32
  .anchor {
@@ -37,6 +37,11 @@ const preventDefault = e => e.preventDefault(),
37
37
  cursor: pointer;
38
38
  pointer-events: auto;
39
39
  outline: none;
40
+ background: var(--cosmoz-dropdown-button-bg-color, #101010);
41
+ color: var(--cosmoz-dropdown-button-color, #fff);
42
+ border-radius: var(--cosmoz-dropdown-button-radius, 50%);
43
+ width: var(--cosmoz-dropdown-button-width, var(--cosmoz-dropdown-button-size, 40px));
44
+ height: var(--cosmoz-dropdown-button-height, var(--cosmoz-dropdown-button-size, 40px));
40
45
  }
41
46
  </style>
42
47
  <div class="anchor" part="anchor">
package/src/use-focus.js CHANGED
@@ -7,7 +7,10 @@ export const useFocus = ({ disabled, onFocus }) => {
7
7
  const [{ focused, closed } = {}, setState] = useState(),
8
8
  active = focused && !disabled,
9
9
  meta = useMeta({ closed, onFocus }),
10
- setClosed = useCallback(closed => setState(p => ({ ...p, closed })), []),
10
+ setClosed = useCallback(
11
+ closed => setState(p => ({ ...p, closed })),
12
+ []
13
+ ),
11
14
  onToggle = useCallback(e => {
12
15
  const target = e.currentTarget;
13
16
  return isFocused(target)
@@ -40,10 +43,29 @@ export const useFocus = ({ disabled, onFocus }) => {
40
43
  active: active && !closed,
41
44
  setClosed,
42
45
  onToggle,
43
- onFocus: useCallback(e => {
44
- const focused = isFocused(e.currentTarget);
45
- setState({ focused });
46
- meta.onFocus?.(focused);
47
- }, [meta])
46
+ onFocus: useCallback(
47
+ e => {
48
+ const focused = isFocused(e.currentTarget);
49
+ setState({ focused });
50
+ meta.onFocus?.(focused);
51
+ },
52
+ [meta]
53
+ )
48
54
  };
49
55
  };
56
+
57
+ const fevs = ['focusin', 'focusout'];
58
+ export const useHostFocus = host => {
59
+ const thru = useFocus(host),
60
+ { onFocus } = thru;
61
+
62
+ useEffect(() => {
63
+ host.setAttribute('tabindex', '-1');
64
+ fevs.forEach(ev => host.addEventListener(ev, onFocus));
65
+ return () => {
66
+ fevs.forEach(ev => host.removeEventListener(ev, onFocus));
67
+ };
68
+ }, []);
69
+
70
+ return thru;
71
+ };