You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

201 lines
5.1 KiB
JavaScript

import React from 'react'
import dynamic from 'next/dynamic'
import Dropdown from '../Dropdown'
import { managePopout } from '../Popout'
import ThemeIcon from '../svg/Theme'
import RemoveIcon from '../svg/Remove'
6 years ago
import { COLORS } from '../../lib/constants'
const ThemeCreate = dynamic(() => import('./ThemeCreate'), {
loading: () => null
})
6 years ago
const ThemeItem = ({ children, item, isSelected, remove }) => (
<div className="theme-item">
{children}
{item.custom && !isSelected && (
6 years ago
<div
role="button"
tabIndex={0}
className="icon"
onClick={e => {
e.stopPropagation()
remove(item.id)
}}
>
<RemoveIcon color={COLORS.SECONDARY} />
</div>
)}
<style jsx>
{`
.theme-item {
display: flex;
flex: 1;
justify-content: ${item.id === 'create' ? 'center' : 'space-between'};
align-items: center;
}
.icon {
display: flex;
margin-right: 6px;
}
`}
</style>
</div>
)
const themeIcon = <ThemeIcon />
class Themes extends React.PureComponent {
state = {
6 years ago
highlights: {}
}
dropdown = React.createRef()
componentDidUpdate(prevProps) {
6 years ago
if (prevProps.isVisible && !this.props.isVisible) {
this.setState({
highlights: {}
})
}
}
6 years ago
handleThemeSelected = theme => {
const { toggleVisibility, update } = this.props
6 years ago
if (theme.id === 'create') {
toggleVisibility()
this.dropdown.current.closeMenu()
} else {
6 years ago
update(theme)
}
}
6 years ago
updateHighlights = updates =>
this.setState(({ highlights }) => ({
highlights: {
6 years ago
...highlights,
...updates
}
6 years ago
}))
6 years ago
create = theme => {
this.props.toggleVisibility()
this.props.create(theme)
}
6 years ago
itemWrapper = props => <ThemeItem {...props} remove={this.props.remove} />
render() {
6 years ago
const { themes, theme, isVisible, toggleVisibility } = this.props
const highlights = { ...theme.highlights, ...this.state.highlights }
6 years ago
const dropdownValue = isVisible ? { name: '' } : { id: theme.id, name: theme.name }
const dropdownList = [
{
id: 'create',
name: 'Create +'
},
...themes
]
return (
<div className="themes">
<Dropdown
innerRef={this.dropdown}
icon={themeIcon}
disableInput={isVisible}
inputValue={dropdownValue}
selected={dropdownValue}
list={dropdownList}
itemWrapper={this.itemWrapper}
6 years ago
onChange={this.handleThemeSelected}
onOpen={isVisible && toggleVisibility}
/>
{isVisible && (
<ThemeCreate
theme={theme}
themes={themes}
highlights={highlights}
6 years ago
create={this.create}
updateHighlights={this.updateHighlights}
/>
)}
<style jsx>
{`
.themes {
position: relative;
}
:global(.CodeMirror__container .CodeMirror) {
color: ${highlights.text} !important;
background-color: ${highlights.background} !important;
}
:global(.cm-string),
:global(.cm-string-2) {
color: ${highlights.string} !important;
}
:global(.cm-comment) {
color: ${highlights.comment} !important;
}
:global(.cm-variable),
:global(.cm-variable-2),
:global(.cm-variable-3) {
color: ${highlights.variable} !important;
}
:global(.cm-number) {
color: ${highlights.number} !important;
}
:global(.cm-keyword) {
color: ${highlights.keyword} !important;
}
:global(.cm-property) {
color: ${highlights.property} !important;
}
:global(.cm-def) {
color: ${highlights.definition} !important;
}
:global(.cm-meta) {
color: ${highlights.meta} !important;
}
:global(.cm-operator) {
color: ${highlights.operator} !important;
}
:global(.cm-attribute) {
color: ${highlights.attribute} !important;
}
:global(.cm-s-dracula .CodeMirror-cursor) {
border-left: solid 2px #159588 !important;
}
:global(.cm-s-solarized) {
box-shadow: none !important;
}
:global(.cm-s-solarized.cm-s-light) {
text-shadow: #eee8d5 0 1px !important;
}
:global(.cm-s-solarized.cm-s-light .CodeMirror-linenumber),
:global(.cm-s-solarized.cm-s-light .CodeMirror-linenumbers) {
background-color: #fdf6e3 !important;
}
:global(.cm-s-solarized.cm-s-dark .CodeMirror-linenumber),
:global(.cm-s-solarized.cm-s-dark .CodeMirror-linenumbers) {
background-color: #002b36 !important;
}
`}
</style>
</div>
)
}
}
6 years ago
export default managePopout(Themes)