fix light theme in Carbon by introducing getTheme

main
Mike Fix 6 years ago committed by Michael Fix
parent dff5013edd
commit 1994c820ea

@ -99,7 +99,7 @@ class Carbon extends React.PureComponent {
theme={config.windowTheme} theme={config.windowTheme}
code={this.props.children} code={this.props.children}
copyable={this.props.copyable} copyable={this.props.copyable}
light={config.theme.light} light={this.props.theme.light}
/> />
) : null} ) : null}
<CodeMirror <CodeMirror
@ -108,7 +108,7 @@ class Carbon extends React.PureComponent {
value={this.props.children} value={this.props.children}
options={options} options={options}
/> />
{config.watermark && <Watermark light={config.theme.light} />} {config.watermark && <Watermark light={this.props.theme.light} />}
<div className="container-bg"> <div className="container-bg">
<div className="white eliminateOnRender" /> <div className="white eliminateOnRender" />
<div className="alpha eliminateOnRender" /> <div className="alpha eliminateOnRender" />

@ -96,6 +96,8 @@ class Editor extends React.Component {
carbonNode = React.createRef() carbonNode = React.createRef()
getTheme = () => this.props.themes.find(t => t.id === this.state.theme) || DEFAULT_THEME
updateState = updates => { updateState = updates => {
this.setState(updates, () => { this.setState(updates, () => {
if (!this.gist) { if (!this.gist) {
@ -117,7 +119,10 @@ class Editor extends React.Component {
// if safari, get image from api // if safari, get image from api
const isPNG = format !== 'svg' const isPNG = format !== 'svg'
if (this.context.image && this.isSafari && isPNG) { if (this.context.image && this.isSafari && isPNG) {
const encodedState = serializeState(this.state) const themeConfig = this.getTheme()
const highlights = { ...themeConfig.highlights, ...this.state.highlights }
// TODO: pull from state highlights, or custom theme highlights, or
const encodedState = serializeState({ ...this.state, highlights })
return this.context.image(encodedState) return this.context.image(encodedState)
} }
@ -261,6 +266,13 @@ class Editor extends React.Component {
} }
updateTheme = theme => this.updateState({ theme }) updateTheme = theme => this.updateState({ theme })
updateHighlights = updates =>
this.setState(({ highlights = {} }) => ({
highlights: {
...highlights,
...updates
}
}))
createTheme = theme => { createTheme = theme => {
this.props.updateThemes(themes => [theme, ...themes]) this.props.updateThemes(themes => [theme, ...themes])
@ -284,6 +296,7 @@ class Editor extends React.Component {
render() { render() {
const { const {
theme, theme,
highlights,
language, language,
backgroundColor, backgroundColor,
backgroundImage, backgroundImage,
@ -294,16 +307,19 @@ class Editor extends React.Component {
const config = omit(this.state, ['code']) const config = omit(this.state, ['code'])
const themeConfig = this.getTheme()
return ( return (
<div className="editor"> <div className="editor">
<Toolbar> <Toolbar>
<Themes <Themes
theme={theme}
highlights={highlights}
update={this.updateTheme} update={this.updateTheme}
updateHighlights={this.updateHighlights}
remove={this.removeTheme} remove={this.removeTheme}
create={this.createTheme} create={this.createTheme}
theme={theme}
themes={this.props.themes} themes={this.props.themes}
router={this.props.router}
/> />
<Dropdown <Dropdown
icon={languageIcon} icon={languageIcon}
@ -353,6 +369,7 @@ class Editor extends React.Component {
key={language} key={language}
ref={this.carbonNode} ref={this.carbonNode}
config={this.state} config={this.state}
theme={themeConfig}
onChange={this.updateCode} onChange={this.updateCode}
loading={this.state.loading} loading={this.state.loading}
> >

@ -6,7 +6,6 @@ import { managePopout } from '../Popout'
import ThemeIcon from '../svg/Theme' import ThemeIcon from '../svg/Theme'
import RemoveIcon from '../svg/Remove' import RemoveIcon from '../svg/Remove'
import { COLORS, DEFAULT_THEME } from '../../lib/constants' import { COLORS, DEFAULT_THEME } from '../../lib/constants'
import { getRouteState } from '../../lib/routing'
const ThemeCreate = dynamic(() => import('./ThemeCreate'), { const ThemeCreate = dynamic(() => import('./ThemeCreate'), {
loading: () => null loading: () => null
@ -53,26 +52,14 @@ const getCustomName = themes =>
class Themes extends React.PureComponent { class Themes extends React.PureComponent {
state = { state = {
highlights: {},
name: '' name: ''
} }
componentDidMount() {
const { queryState } = getRouteState(this.props.router)
if (queryState.highlights) {
this.updateHighlights(queryState.highlights)
}
}
dropdown = React.createRef() dropdown = React.createRef()
static getDerivedStateFromProps(props) { static getDerivedStateFromProps(props) {
if (!props.isVisible) { if (!props.isVisible) {
const themeConfig =
(props.themes && props.themes.find(t => t.id === props.theme)) || DEFAULT_THEME
return { return {
highlights: themeConfig.highlights,
name: getCustomName(props.themes) name: getCustomName(props.themes)
} }
} }
@ -89,14 +76,6 @@ class Themes extends React.PureComponent {
} }
} }
updateHighlights = updates =>
this.setState(({ highlights }) => ({
highlights: {
...highlights,
...updates
}
}))
create = theme => { create = theme => {
this.props.toggleVisibility() this.props.toggleVisibility()
this.props.create(theme) this.props.create(theme)
@ -106,10 +85,11 @@ class Themes extends React.PureComponent {
render() { render() {
const { themes, theme, isVisible, toggleVisibility } = this.props const { themes, theme, isVisible, toggleVisibility } = this.props
const { highlights } = this.state
const themeConfig = themes.find(t => t.id === theme) || DEFAULT_THEME const themeConfig = themes.find(t => t.id === theme) || DEFAULT_THEME
const highlights = { ...themeConfig.highlights, ...this.props.highlights }
const dropdownValue = isVisible ? { name: this.state.name } : themeConfig const dropdownValue = isVisible ? { name: this.state.name } : themeConfig
const dropdownList = [ const dropdownList = [
@ -138,7 +118,7 @@ class Themes extends React.PureComponent {
themes={themes} themes={themes}
highlights={highlights} highlights={highlights}
create={this.create} create={this.create}
updateHighlights={this.updateHighlights} updateHighlights={this.props.updateHighlights}
name={this.state.name} name={this.state.name}
onInputChange={e => this.setState({ name: e.target.value })} onInputChange={e => this.setState({ name: e.target.value })}
/> />

@ -948,7 +948,7 @@ export const DEFAULT_SETTINGS = {
dropShadow: true, dropShadow: true,
dropShadowOffsetY: '20px', dropShadowOffsetY: '20px',
dropShadowBlurRadius: '68px', dropShadowBlurRadius: '68px',
theme: DEFAULT_THEME, theme: DEFAULT_THEME.id,
windowTheme: 'none', windowTheme: 'none',
language: DEFAULT_LANGUAGE, language: DEFAULT_LANGUAGE,
fontFamily: 'Hack', fontFamily: 'Hack',

Loading…
Cancel
Save