forked from cerebral/overmind
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.tsx
More file actions
118 lines (109 loc) · 2.67 KB
/
index.tsx
File metadata and controls
118 lines (109 loc) · 2.67 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
import * as React from 'react'
import {
Wrapper,
Selector,
Chevron,
Dropdown,
Option,
TSImage,
TsImageWrapper,
} from './elements'
import * as ReactImage from '../../images/react.png'
import * as VueImage from '../../images/vue.png'
import * as TsImage from '../../images/ts.png'
import * as TsImageGrayscale from '../../images/ts-grayscale.png'
import Icon from '../Icon'
import { getTypescript, getTheme } from '../../utils'
type State = {
isOpen: boolean
}
type Props = {
selectedTheme: string
above?: boolean
}
class ViewSelector extends React.Component<Props, State> {
state = {
isOpen: false,
}
selector: Node
onSelectorClick = () => {
if (this.state.isOpen) {
document.removeEventListener('click', this.onDocumentClick)
} else {
document.addEventListener('click', this.onDocumentClick)
}
this.setState((state) => ({
isOpen: !state.isOpen,
}))
}
onDocumentClick = (event: MouseEvent) => {
let node = event.target as Node
while (node.parentNode) {
if (node === this.selector) {
return
}
node = node.parentNode
}
this.onSelectorClick()
}
onTsClick(event) {
event.stopPropagation()
if (getTypescript()) {
localStorage.removeItem('typescript')
} else {
localStorage.setItem('typescript', 'true')
}
window.rerender()
}
selectTheme(theme) {
localStorage.setItem('theme', theme)
window.rerender()
}
render() {
const { isOpen } = this.state
const options = {
react: (
<React.Fragment>
<img src={ReactImage} width={25} />React
</React.Fragment>
),
vue: (
<React.Fragment>
<img src={VueImage} width={25} />Vue
</React.Fragment>
),
}
return (
<Wrapper
onClick={this.onSelectorClick}
innerRef={(node) => {
this.selector = node
}}
>
<TsImageWrapper onClick={this.onTsClick}>
{getTypescript() ? (
<TSImage src={TsImage} width="20" height="20" />
) : (
<TSImage src={TsImageGrayscale} width="20" height="20" grayscale />
)}
</TsImageWrapper>
<Selector isOpen={isOpen}>
{options[getTheme()]}
<Chevron>
<Icon>chevron-down</Icon>
</Chevron>
</Selector>
{this.state.isOpen ? (
<Dropdown>
{Object.keys(options).map((theme) => (
<Option key={theme} onClick={() => this.selectTheme(theme)}>
{options[theme]}
</Option>
))}
</Dropdown>
) : null}
</Wrapper>
)
}
}
export default ViewSelector