-
Notifications
You must be signed in to change notification settings - Fork 167
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
perf: Generated classNames from props (#230)
Co-authored-by: Guilherme Gazzo <[email protected]>
- Loading branch information
Showing
15 changed files
with
155 additions
and
98 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
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 |
---|---|---|
@@ -1,6 +1,7 @@ | ||
// @flow | ||
|
||
export * from './tags'; | ||
export * from './selectors'; | ||
export * from './sheet'; | ||
export * from './tags'; | ||
export * from './toClassName'; | ||
export * from './transpile'; | ||
export * from './selectors'; |
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,38 @@ | ||
// @flow | ||
|
||
import { createSelector } from './selectors'; | ||
import { referenceRules } from './sheet'; | ||
import { transpile } from './transpile'; | ||
import type { cssFn, classNameFn } from './tags'; | ||
|
||
/** | ||
* Process a value as a className. | ||
* | ||
* @return a string containing a className or undefined | ||
*/ | ||
export const toClassName = (value: cssFn | classNameFn | string): ?string => { | ||
if (typeof value === 'function') { | ||
const rules = []; | ||
rules.push(value(rules)); | ||
|
||
const content = rules.filter(Boolean).join('') || undefined; | ||
|
||
if (!content) { | ||
return undefined; | ||
} | ||
|
||
const className = (value: classNameFn).className || createSelector(content)[0]; | ||
const encodedClassName = className.replace(/@|#|:/g, (char) => `\\${ char }`); | ||
|
||
const parsedRules = transpile(`.${ encodedClassName }`, content); | ||
referenceRules(parsedRules); | ||
|
||
return className; | ||
} | ||
|
||
if (typeof value === 'string' && value) { | ||
return value; | ||
} | ||
|
||
return undefined; | ||
}; |
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,23 @@ | ||
export const createPropType = (getValue) => { | ||
const propType = (props, propName, componentName) => { | ||
const propValue = props[propName]; | ||
|
||
if (propValue === undefined || getValue(propValue) !== undefined) { | ||
return; | ||
} | ||
|
||
return new Error(`Invalid value for prop \`${ propName }\` supplied to \`${ componentName }\`: \`${ propValue }\``); | ||
}; | ||
|
||
propType.isRequired = (props, propName, componentName) => { | ||
const propValue = props[propName]; | ||
|
||
if (propValue !== undefined && getValue(propValue) !== undefined) { | ||
return; | ||
} | ||
|
||
return new Error(`Invalid value for prop \`${ propName }\` supplied to \`${ componentName }\`: \`${ propValue }\``); | ||
}; | ||
|
||
return propType; | ||
}; |
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,3 @@ | ||
import { memoize } from './memoize'; | ||
|
||
export const cssSupports = memoize((value) => typeof window !== 'undefined' && window.CSS && window.CSS.supports(value)); |
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
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