-
Notifications
You must be signed in to change notification settings - Fork 96
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
Add directional StyleSheet
creation based on context
#117
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -16,19 +16,27 @@ function registerInterface(interfaceToRegister) { | |
styleInterface = interfaceToRegister; | ||
} | ||
|
||
function create(makeFromTheme) { | ||
function create(makeFromTheme, createWithDirection) { | ||
// Get an id to associate with this stylesheet | ||
const id = internalId; | ||
internalId += 1; | ||
|
||
const { theme, styles } = styleTheme; | ||
styles[id] = styleInterface.create(makeFromTheme(theme)); | ||
styles[id] = createWithDirection(makeFromTheme(theme)); | ||
|
||
makeFromThemes[id] = makeFromTheme; | ||
|
||
return () => styleTheme.styles[id]; | ||
} | ||
|
||
function createLTR(makeFromTheme) { | ||
return create(makeFromTheme, styleInterface.create); | ||
} | ||
|
||
function createRTL(makeFromTheme) { | ||
return create(makeFromTheme, styleInterface.createRTL || styleInterface.create); | ||
} | ||
|
||
function get() { | ||
return styleTheme.theme; | ||
} | ||
|
@@ -59,7 +67,8 @@ export default globalCache.setIfMissingThenGet( | |
() => ({ | ||
registerTheme, | ||
registerInterface, | ||
create, | ||
create: createLTR, | ||
createRTL, | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. should we also add There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I don't know how much sense that makes if |
||
get, | ||
resolveNoRTL, | ||
resolve, | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -3,6 +3,9 @@ import PropTypes from 'prop-types'; | |
import hoistNonReactStatics from 'hoist-non-react-statics'; | ||
import deepmerge from 'deepmerge'; | ||
|
||
import { CHANNEL, DIRECTIONS } from 'react-with-direction/dist/constants'; | ||
import brcastShape from 'react-with-direction/dist/proptypes/brcast'; | ||
|
||
import ThemedStyleSheet from './ThemedStyleSheet'; | ||
|
||
// Add some named exports for convenience. | ||
|
@@ -28,6 +31,10 @@ function baseClass(pureComponent) { | |
return React.Component; | ||
} | ||
|
||
const contextTypes = { | ||
[CHANNEL]: brcastShape, | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. it's unfortunate we're not using There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I mean it would add an extra virtual DOM node to ... every single component in our tree... which doesn't seem good. :/ There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Maybe we could export more things from react-with-direction to avoid as much duplication as possible? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Perhaps we could export There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I'll think about implementing this in a follow up There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yes, we should definitely do that at a minimum. |
||
}; | ||
|
||
export function withStyles( | ||
styleFn, | ||
{ | ||
|
@@ -37,18 +44,28 @@ export function withStyles( | |
pureComponent = false, | ||
} = {}, | ||
) { | ||
let styleDef; | ||
let styleDefLTR; | ||
let styleDefRTL; | ||
const BaseClass = baseClass(pureComponent); | ||
|
||
return function withStylesHOC(WrappedComponent) { | ||
// NOTE: Use a class here so components are ref-able if need be: | ||
// eslint-disable-next-line react/prefer-stateless-function | ||
class WithStyles extends BaseClass { | ||
componentWillMount() { | ||
// defer StyleSheet creation to run-time | ||
if (!styleDef) { | ||
styleDef = styleFn ? ThemedStyleSheet.create(styleFn) : EMPTY_STYLES_FN; | ||
this.maybeCreateStyles(); | ||
} | ||
|
||
maybeCreateStyles() { | ||
const direction = this.context[CHANNEL] && this.context[CHANNEL].getState(); | ||
const isRTL = direction === DIRECTIONS.RTL; | ||
if (isRTL && !styleDefRTL) { | ||
styleDefRTL = styleFn ? ThemedStyleSheet.createRTL(styleFn) : EMPTY_STYLES_FN; | ||
} else if (!isRTL && !styleDefLTR) { | ||
styleDefLTR = styleFn ? ThemedStyleSheet.create(styleFn) : EMPTY_STYLES_FN; | ||
} | ||
|
||
return isRTL ? styleDefRTL : styleDefLTR; | ||
} | ||
|
||
render() { | ||
|
@@ -63,6 +80,8 @@ export function withStyles( | |
ThemedStyleSheet.flush(); | ||
} | ||
|
||
const styleDef = this.maybeCreateStyles(); | ||
|
||
return ( | ||
<WrappedComponent | ||
{...this.props} | ||
|
@@ -81,6 +100,7 @@ export function withStyles( | |
|
||
WithStyles.WrappedComponent = WrappedComponent; | ||
WithStyles.displayName = `withStyles(${wrappedComponentName})`; | ||
WithStyles.contextTypes = contextTypes; | ||
if (WrappedComponent.propTypes) { | ||
WithStyles.propTypes = deepmerge({}, WrappedComponent.propTypes); | ||
delete WithStyles.propTypes[stylesPropName]; | ||
|
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.
let's make this both a dep and a peer dep - a peer dep so there's no conflict in case they use a different version of
react-with-direction
, and "also a dep" so npm 3+ users don't have to manually add it to package.json.