@neovici/cosmoz-dropdown 1.0.3 → 1.2.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.
- package/package.json +1 -1
- package/src/cosmoz-dropdown.js +17 -14
- package/src/use-focus.js +28 -6
package/package.json
CHANGED
package/src/cosmoz-dropdown.js
CHANGED
|
@@ -1,31 +1,29 @@
|
|
|
1
|
-
import { html, component, useCallback
|
|
1
|
+
import { html, component, useCallback } from 'haunted';
|
|
2
2
|
import { usePosition } from './use-position';
|
|
3
|
-
import {
|
|
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;
|
|
12
11
|
left: -9999999999px;
|
|
13
12
|
min-width: 72px;
|
|
14
13
|
box-sizing: border-box;
|
|
14
|
+
background: var(--cosmoz-dropdown-bg-color, #fff);
|
|
15
|
+
box-shadow: var(--cosmoz-dropdown-box-shadow, 0px 3px 4px 2px rgba(0, 0, 0, 0.1));
|
|
16
|
+
}
|
|
17
|
+
::slotted(*) {
|
|
18
|
+
display: block;
|
|
15
19
|
}
|
|
16
20
|
</style>
|
|
17
21
|
<slot></slot>`;
|
|
18
22
|
},
|
|
19
23
|
Dropdown = host => {
|
|
20
|
-
const {
|
|
21
|
-
anchor = useCallback(() => host.shadowRoot.querySelector('.anchor'), [])
|
|
22
|
-
|
|
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
|
-
}, []);
|
|
24
|
+
const { placement } = host,
|
|
25
|
+
anchor = useCallback(() => host.shadowRoot.querySelector('.anchor'), []),
|
|
26
|
+
{ active, onToggle } = useHostFocus(host);
|
|
29
27
|
return html`
|
|
30
28
|
<style>
|
|
31
29
|
.anchor {
|
|
@@ -36,6 +34,11 @@ const preventDefault = e => e.preventDefault(),
|
|
|
36
34
|
cursor: pointer;
|
|
37
35
|
pointer-events: auto;
|
|
38
36
|
outline: none;
|
|
37
|
+
background: var(--cosmoz-dropdown-button-bg-color, #101010);
|
|
38
|
+
color: var(--cosmoz-dropdown-button-color, #fff);
|
|
39
|
+
border-radius: var(--cosmoz-dropdown-button-radius, 50%);
|
|
40
|
+
width: var(--cosmoz-dropdown-button-width, var(--cosmoz-dropdown-button-size, 40px));
|
|
41
|
+
height: var(--cosmoz-dropdown-button-height, var(--cosmoz-dropdown-button-size, 40px));
|
|
39
42
|
}
|
|
40
43
|
</style>
|
|
41
44
|
<div class="anchor" part="anchor">
|
|
@@ -44,7 +47,7 @@ const preventDefault = e => e.preventDefault(),
|
|
|
44
47
|
</button>
|
|
45
48
|
</div>
|
|
46
49
|
${ active
|
|
47
|
-
? html` <cosmoz-dropdown-content .anchor=${ anchor }
|
|
50
|
+
? html` <cosmoz-dropdown-content part="dropdown" .anchor=${ anchor } .placement=${ placement }>
|
|
48
51
|
<slot></slot>
|
|
49
52
|
</cosmoz-dropdown-content>`
|
|
50
53
|
: [] }
|
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(
|
|
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(
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
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
|
+
};
|