@neovici/cosmoz-dropdown 1.0.1 → 1.1.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.1",
3
+ "version": "1.1.0",
4
4
  "description": "A simple dropdown web component",
5
5
  "keywords": [
6
6
  "lit-html",
@@ -16,7 +16,7 @@
16
16
  },
17
17
  "license": "Apache-2.0",
18
18
  "author": "",
19
- "main": "sr/index.js",
19
+ "main": "src/index.js",
20
20
  "directories": {
21
21
  "test": "test"
22
22
  },
@@ -1,11 +1,10 @@
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
- usePosition({ anchor: host.anchor, host });
7
+ usePosition({ anchor: host.anchor, host, placement: host.placement });
9
8
  return html` <style>
10
9
  :host {
11
10
  position: fixed;
@@ -17,15 +16,9 @@ const preventDefault = e => e.preventDefault(),
17
16
  <slot></slot>`;
18
17
  },
19
18
  Dropdown = host => {
20
- const { active, onFocus, onToggle } = useFocus(host),
21
- anchor = useCallback(() => host.shadowRoot.querySelector('.anchor'), []);
22
- useEffect(() => {
23
- host.setAttribute('tabindex', '-1');
24
- fevs.forEach(ev => host.addEventListener(ev, onFocus));
25
- return () => {
26
- fevs.forEach(ev => host.removeEventListener(ev, onFocus));
27
- };
28
- }, []);
19
+ const { placement } = host,
20
+ anchor = useCallback(() => host.shadowRoot.querySelector('.anchor'), []),
21
+ { active, onToggle } = useHostFocus(host);
29
22
  return html`
30
23
  <style>
31
24
  .anchor {
@@ -44,7 +37,7 @@ const preventDefault = e => e.preventDefault(),
44
37
  </button>
45
38
  </div>
46
39
  ${ active
47
- ? html` <cosmoz-dropdown-content .anchor=${ anchor } part="dropdown">
40
+ ? html` <cosmoz-dropdown-content part="dropdown" .anchor=${ anchor } .placement=${ placement }>
48
41
  <slot></slot>
49
42
  </cosmoz-dropdown-content>`
50
43
  : [] }
package/src/index.js CHANGED
@@ -1,2 +1,4 @@
1
1
  export * from './cosmoz-dropdown';
2
+ export * from './use-focus';
3
+ export * from './use-position';
2
4
 
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
+ };