Skip to content

Commit

Permalink
Send css method down as a prop instead of as a global
Browse files Browse the repository at this point in the history
  • Loading branch information
Maja Wichrowska committed Jan 5, 2018
1 parent da2e478 commit ae8e1f7
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 7 deletions.
17 changes: 13 additions & 4 deletions src/ThemedStyleSheet.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,9 +32,17 @@ function resolve(...styles) {
return styleInterface.resolve(styles);
}

function resolveNoRTL(...styles) {
if (styleInterface.resolveNoRTL) {
return styleInterface.resolveNoRTL(styles);
function resolveLTR(...styles) {
if (styleInterface.resolveLTR) {
return styleInterface.resolveLTR(styles);
}

return resolve(styles);
}

function resolveRTL(...styles) {
if (styleInterface.resolveRTL) {
return styleInterface.resolveRTL(styles);
}

return resolve(styles);
Expand All @@ -57,8 +65,9 @@ export default globalCache.setIfMissingThenGet(
create: createLTR,
createRTL,
get,
resolveNoRTL,
resolve,
resolveLTR,
resolveRTL,
flush,
}),
);
20 changes: 17 additions & 3 deletions src/withStyles.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ import ThemedStyleSheet from './ThemedStyleSheet';

// Add some named exports for convenience.
export const css = ThemedStyleSheet.resolve;
export const cssNoRTL = ThemedStyleSheet.resolveNoRTL;
export const withStylesPropTypes = {
styles: PropTypes.object.isRequired, // eslint-disable-line react/forbid-prop-types
theme: PropTypes.object.isRequired, // eslint-disable-line react/forbid-prop-types
Expand Down Expand Up @@ -40,6 +39,7 @@ export function withStyles(
{
stylesPropName = 'styles',
themePropName = 'theme',
cssPropName = 'css',
flushBefore = false,
pureComponent = false,
} = {},
Expand All @@ -56,9 +56,21 @@ export function withStyles(
this.maybeCreateStyles();
}

getDirection() {
return this.context[CHANNEL] && this.context[CHANNEL].getState();
}

getResolveMethod() {
const isRTL = this.getDirection() === DIRECTIONS.RTL;
if (isRTL) {
return ThemedStyleSheet.resolveRTL;
}

return ThemedStyleSheet.resolveLTR;
}

maybeCreateStyles() {
const direction = this.context[CHANNEL] && this.context[CHANNEL].getState();
const isRTL = direction === DIRECTIONS.RTL;
const isRTL = this.getDirection() === DIRECTIONS.RTL;
if (isRTL && !styleDefRTL) {
styleDefRTL = styleFn ? ThemedStyleSheet.createRTL(styleFn) : EMPTY_STYLES_FN;
} else if (!isRTL && !styleDefLTR) {
Expand Down Expand Up @@ -88,6 +100,7 @@ export function withStyles(
{...{
[themePropName]: ThemedStyleSheet.get(),
[stylesPropName]: styleDef(),
[cssPropName]: this.getResolveMethod(),
}}
/>
);
Expand All @@ -105,6 +118,7 @@ export function withStyles(
WithStyles.propTypes = deepmerge({}, WrappedComponent.propTypes);
delete WithStyles.propTypes[stylesPropName];
delete WithStyles.propTypes[themePropName];
delete WithStyles.propTypes[cssPropName];
}
if (WrappedComponent.defaultProps) {
WithStyles.defaultProps = deepmerge({}, WrappedComponent.defaultProps);
Expand Down

0 comments on commit ae8e1f7

Please sign in to comment.