-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Manage CSSRules with "Stylesheet" instance
This update refactors the previous implementation to use an instance to track/manage/interface with `withStyle`, instead of relying purely on a privately defined CONSTANT. The UUID mechanism has also been replaced by a simple incrementing ID mechanism. This nullifies the risk of collision and is simpler :).
- Loading branch information
ItsJonQ
committed
May 2, 2018
1 parent
05bca90
commit 0301715
Showing
5 changed files
with
83 additions
and
86 deletions.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
const makeStyleSheet = () => { | ||
const state = { | ||
id: 0, | ||
CSSRules: {} | ||
} | ||
|
||
const addRule = (id, styles) => { | ||
state.CSSRules[id] = styles | ||
} | ||
|
||
const getRule = (id) => { | ||
return state.CSSRules[id] | ||
} | ||
|
||
const hasRule = (id) => { | ||
return !!getRule(id) | ||
} | ||
|
||
const removeRule = (id) => { | ||
state.CSSRules[id] = undefined | ||
} | ||
|
||
const makeRule = (CSSRules) => { | ||
state.id = state.id + 1 | ||
return { id: state.id, CSSRules } | ||
} | ||
|
||
const makeStyles = (props) => { | ||
return generateStyles(props) | ||
} | ||
|
||
return { | ||
addRule, | ||
getRule, | ||
hasRule, | ||
removeRule, | ||
makeRule, | ||
makeStyles, | ||
CSSRules: state.CSSRules | ||
} | ||
} | ||
|
||
/** | ||
* Creates the tokenized styles based. | ||
*/ | ||
export const generateStyles = ({id, props, CSSRules}) => { | ||
const parsedCSSRules = typeof CSSRules === 'function' | ||
? CSSRules(props) : CSSRules | ||
|
||
return tokenize(id, parsedCSSRules) | ||
} | ||
|
||
/** | ||
* Renders the CSSRule with tokenized with the unique ID. | ||
* | ||
* @param {string} id | ||
* @param {string} CSSRules | ||
* @returns {string} | ||
*/ | ||
export const tokenize = (id, CSSRules) => `/* ${id} */\n${CSSRules.trim()}\n` | ||
|
||
export default makeStyleSheet |
This file was deleted.
Oops, something went wrong.
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