-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
73 lines (70 loc) · 1.37 KB
/
index.js
File metadata and controls
73 lines (70 loc) · 1.37 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
import { Component, h } from 'preact';
/**
* A dropdown component
* @param children is the element displayed on trigger
* @param Link is the element displayed to trigger the dropdown
* @param ...args are sent to Link
* @example
* <DropDown Link={Button}>
* <div>
* My inner content
* </div>
* </Dropdown>
*/
class Dropdown extends Component {
close() {
this.setState({ open: false });
}
toggle() {
this.setState({ open: !this.state.open });
}
constructor() {
super();
this.state = { open: false };
}
componentDidMount() {
const that = this;
addEventListener('click', ({ target }) => {
if (that.base === null)
return;
if (target===that.base.firstChild)
that.toggle();
else if (that.state.open) {
do {
if (target===that.base) return;
} while ((target=target.parentNode));
that.close();
}
});
}
componentWillUnmount() {
removeEventListener('click', this.handleClick);
}
render({ children, Link, ...args }, { open }) {
return (
<div>
<Link {...args} />
{ open ? children : null }
</div>
);
}
}
/**
* Works just like DropDown but replaces the Link with the children content
*/
class DropReplace extends Dropdown {
render({ children, Link, ...args }, { open }) {
return (
<div>
{ open ?
children
: <Link {...args} />
}
</div>
);
}
}
export {
Dropdown,
DropReplace
};