-
Notifications
You must be signed in to change notification settings - Fork 36
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
rewrite themer to use a react context #450
Open
NinjaBanjo
wants to merge
3
commits into
master
Choose a base branch
from
dpersons-themer-context
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 1 commit
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
import { Theme } from './utils' | ||
|
||
export declare class Themer { | ||
themeConfig: Theme | ||
|
||
constructor() | ||
|
||
set<TSection extends keyof Theme, TSectionKey extends keyof Theme[TSection]>( | ||
section: TSection, | ||
sectionKey: TSectionKey, | ||
themeValue: string | ||
): void | ||
|
||
subscribe(listener: (themeConfig?: Theme) => void): () => void | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
/* eslint-disable no-underscore-dangle */ | ||
import { cleanConfig, defaultTheme, themeTemplate, validConfigValue } from './utils' | ||
|
||
export class Themer { | ||
constructor() { | ||
this._themeConfig = defaultTheme | ||
this._onChangeListeners = [] | ||
} | ||
|
||
_callListeners() { | ||
this._onChangeListeners.forEach(listener => { | ||
listener(this._themeConfig) | ||
}) | ||
} | ||
|
||
get themeConfig() { | ||
return this._themeConfig | ||
} | ||
|
||
set themeConfig(themeConfig) { | ||
this._themeConfig = cleanConfig(themeConfig) | ||
this._callListeners() | ||
} | ||
|
||
get(section, sectionKey) { | ||
if (!this._themeConfig) { | ||
console.warn( | ||
'Snacks theme error: No themeConfig defined. Please use Themer template: ', | ||
themeTemplate | ||
) | ||
} else if (validConfigValue(section, sectionKey)) { | ||
return this._themeConfig[section][sectionKey] | ||
} | ||
} | ||
|
||
set(section, sectionKey, themeValue) { | ||
if (validConfigValue(section, sectionKey)) { | ||
this._themeConfig[section][sectionKey] = themeValue | ||
this._callListeners() | ||
} | ||
} | ||
|
||
subscribe(listener) { | ||
this._onChangeListeners.push(listener) | ||
|
||
const unsubscribe = () => { | ||
const index = this._onChangeListeners.indexOf(listener) | ||
if (index === -1) { | ||
return | ||
} | ||
this._onChangeListeners.splice(index, 1) | ||
} | ||
|
||
return unsubscribe | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
import { Context } from 'react' | ||
import { Themer } from './Themer' | ||
|
||
declare interface ThemerContextInterface { | ||
tick: number // for propogating theme updates through react | ||
themer: Themer | ||
} | ||
|
||
export declare const ThemerContext: Context<ThemerContextInterface> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
import { createContext } from 'react' | ||
import { Themer } from './Themer' | ||
|
||
export const ThemerContext = createContext({ themer: new Themer(), tick: 0 }) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
import React from 'react' | ||
import PropTypes from 'prop-types' | ||
import { Themer } from './Themer' | ||
import { ThemerContext } from './ThemerContext' | ||
|
||
export class ThemerProvider extends React.Component { | ||
static propTypes = { | ||
themer: PropTypes.instanceOf(Themer), | ||
children: PropTypes.node, | ||
} | ||
|
||
static contextType = ThemerContext | ||
|
||
state = { | ||
tick: 0, | ||
} | ||
|
||
componentDidMount() { | ||
this.unsubscribe = this.context.themer.subscribe(this.onThemeChange) | ||
} | ||
|
||
componentWillUnmount() { | ||
this.unsubscribe() | ||
} | ||
|
||
onThemeChange = () => { | ||
const { tick } = this.state | ||
// increment tick to force an update on the context | ||
this.setState({ tick: tick + 1 }) | ||
} | ||
|
||
render() { | ||
const { themer, children } = this.props | ||
const { tick } = this.state | ||
return <ThemerContext.Provider value={{ themer, tick }}>{children}</ThemerContext.Provider> | ||
} | ||
} | ||
|
||
ThemerProvider.propTypes = { | ||
themer: PropTypes.object, | ||
children: PropTypes.node, | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
technically you don't need to pass
tick
here, because you're already creating a new object every time thisrender()
method runs, which will happen after yousetState
. Context doesn't do deep comparisons to determine if the value has changed, it's referential.