Repository navigation
Expand file tree
/
Copy pathindex.js
More file actions
120 lines (106 loc) · 3.01 KB
/
Copy pathindex.js
File metadata and controls
120 lines (106 loc) · 3.01 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
/* eslint react/jsx-fragments: "off" */
import React, { Fragment, createContext, useEffect, useReducer } from 'react' // Can be aliased to `preact` in host project
const Context = createContext('Dropdown')
const providers = {}
let dropdown
function useDropdownState (uid) {
const [, update] = useReducer(s => s + 1, 0)
const setDropdown = uid => {
if (uid !== dropdown) {
dropdown = uid
Object.values(providers).forEach(fn => fn())
}
}
useEffect(() => {
providers[uid] = update
return () => {
delete providers[uid]
}
}, [])
return [dropdown, setDropdown]
}
export function DropdownProvider ({ uid, children }) {
const value = useDropdownState(uid)
return <Context.Provider value={value}>{children}</Context.Provider>
}
export function Dropdown ({
uid,
buttonText = 'Select',
noWrapper = false,
Trigger,
children
}) {
if (!uid) {
throw new Error('<Dropdown> must include a uid prop.')
}
return (
<DropdownProvider uid={uid}>
<Context.Consumer>
{([dropdown, setDropdown]) => {
const isOpen = dropdown === uid
const cls = isOpen
? 'ae-dropdown-menu open'
: isOpen === false
? 'ae-dropdown-menu close'
: 'ae-dropdown-menu' // isOpen === null
const handleClick = ev => {
ev.preventDefault()
ev.stopPropagation()
setDropdown(isOpen ? null : uid)
}
return (
<Fragment>
{Trigger === undefined
? (
<button className='ae-btn-dropdown' onClick={handleClick}>
{buttonText}
</button>
)
: <Trigger className='ae-btn-dropdown' onClick={handleClick} />}
{noWrapper
? isOpen && children
: (
<div className={cls}>
{children}
</div>
)}
</Fragment>
)
}}
</Context.Consumer>
</DropdownProvider>
)
}
// DOM event to close all Dropdown's on off-click
const hasData = el => el.hasAttribute != null && el.hasAttribute('data-dropdown')
const checkClass = (className, el) => {
if ((el.classList && el.classList.contains(className)) || hasData(el)) {
return true
}
while (el.parentNode) {
el = el.parentNode
if ((el.classList && el.classList.contains(className)) || hasData(el)) {
return true
}
}
return false
}
const isClickable = (el) =>
el.tagName === 'A' ||
el.tagName === 'BUTTON'
try {
document.body.addEventListener('click', (ev) => {
if (dropdown == null || !Object.values(providers).length) {
return
}
const el = ev.target
if (checkClass('ae-btn-dropdown', el)) {
return
}
const withinDropdown = checkClass('ae-dropdown-menu', el)
if (!withinDropdown || (withinDropdown && isClickable(el))) {
dropdown = null
Object.values(providers).forEach(fn => fn())
}
})
} catch (_) {}