Skip to content
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

Separate out LTR/RTL create and resolve methods #37

Merged
merged 1 commit into from
Feb 6, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -63,12 +63,14 @@
"sinon-sandbox": "^1.0.2"
},
"peerDependencies": {
"aphrodite": ">=0.5.0"
"aphrodite": ">=0.5.0",
"react-with-styles": "^3.0.0"
},
"dependencies": {
"array-flatten": "^2.1.1",
"has": "^1.0.1",
"object.assign": "^4.1.0",
"object.entries": "^1.0.4",
"rtl-css-js": "^1.8.0"
}
}
28 changes: 24 additions & 4 deletions src/aphroditeInterfaceFactory.js
Original file line number Diff line number Diff line change
@@ -1,18 +1,38 @@
import { flushToStyleTag } from 'aphrodite/lib/inject';
import rtlCSSJS from 'rtl-css-js';
import entries from 'object.entries';

import resolve from './utils/resolve';
import resolveLTR from './utils/resolveLTR';
import resolveRTL from './utils/resolveRTL';

export default ({ StyleSheet, css }/* aphrodite */) => ({
create(styleHash) {
return this.createLTR(styleHash);
},

createLTR(styleHash) {
return StyleSheet.create(styleHash);
},

createRTL(styleHash) {
const styleHashRTL = {};
entries(styleHash).forEach(([styleKey, styleDef]) => {
styleHashRTL[styleKey] = rtlCSSJS(styleDef);
});

return StyleSheet.create(styleHashRTL);
},

resolve(styles) {
return resolve(css, styles);
return this.resolveLTR(styles);
},

resolveLTR(styles) {
return resolveLTR(css, styles);
},

resolveNoRTL(styles) {
return resolve(css, styles);
resolveRTL(styles) {
return resolveRTL(css, styles);
},

// Flushes all buffered styles to a style tag. Required for components
Expand Down
25 changes: 0 additions & 25 deletions src/aphroditeInterfaceWithRTLFactory.js

This file was deleted.

68 changes: 0 additions & 68 deletions src/utils/generateDirectionalStyles.js

This file was deleted.

12 changes: 2 additions & 10 deletions src/utils/resolve.js → src/utils/resolveLTR.js
Original file line number Diff line number Diff line change
@@ -1,27 +1,19 @@
/* eslint-disable no-param-reassign */
import { from as flatten } from 'array-flatten';

import separateStyles from './separateStyles';

function resetStyleDefinition(stylesObj) {
if (stylesObj.noRTL) {
stylesObj._definition = stylesObj.noRTL;
}
return stylesObj;
}

// Styles is an array of properties returned by `create()`, a POJO, or an
// array thereof. POJOs are treated as inline styles. This version of the
// resolve function explicitly does no work to flip styles for an RTL context.
// This function returns an object to be spread onto an element.
export default function resolve(css, styles) {
export default function resolveLTR(css, styles) {
const flattenedStyles = flatten(styles);

const {
aphroditeStyles,
hasInlineStyles,
inlineStyles,
} = separateStyles(flattenedStyles, resetStyleDefinition);
} = separateStyles(flattenedStyles);

const result = {};
if (aphroditeStyles.length > 0) {
Expand Down
29 changes: 29 additions & 0 deletions src/utils/resolveRTL.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
import { from as flatten } from 'array-flatten';
import rtlCSSJS from 'rtl-css-js';

import separateStyles from './separateStyles';

// Styles is an array of properties returned by `create()`, a POJO, or an
// array thereof. POJOs are treated as inline styles. This version of the
// resolve function explicitly flips inline styles for an RTL context.
// This function returns an object to be spread onto an element.
export default function resolveRTL(css, styles) {
const flattenedStyles = flatten(styles);

const {
aphroditeStyles,
hasInlineStyles,
inlineStyles,
} = separateStyles(flattenedStyles);

const result = {};
if (aphroditeStyles.length > 0) {
result.className = css(...aphroditeStyles);
}

if (hasInlineStyles) {
result.style = rtlCSSJS(inlineStyles);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could you remind me, why isn't this already captured by the interfaceFactory's call to rtlCSSJS in the createRTL function? (sorry i'm not super familiar with how this interface works)

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Right so the create method deals with stylesheets, e.g. styles that you define in the thunk that gets passed to withStyles. Inline styles can be defined on the fly and are passed in as an object to the css method itself... they are explicitly not part of the stylesheet definition and so don't get handled by the create. That's why we have to do this here as well. :)

(notice that the aphrodite styles are chill tho because the create took care of those)

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

thanks for explaining! 👍

}

return result;
}
66 changes: 0 additions & 66 deletions src/utils/resolveWithRTL.js

This file was deleted.

10 changes: 2 additions & 8 deletions src/utils/separateStyles.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import has from 'has';

// This function takes the array of styles and separates them into styles that
// are handled by Aphrodite and inline styles.
export default function separateStyles(stylesArray, aphroditeStyleTransform) {
export default function separateStyles(stylesArray) {
const aphroditeStyles = [];

// Since determining if an Object is empty requires collecting all of its
Expand All @@ -15,7 +15,7 @@ export default function separateStyles(stylesArray, aphroditeStyleTransform) {
// performance is critical. Normally we would prefer using `forEach`, but
// old-fashioned for loops are faster so that's what we have chosen here.
for (let i = 0; i < stylesArray.length; i += 1) {
let style = stylesArray[i];
const style = stylesArray[i];

// If this style is falsey, we just want to disregard it. This allows for
// syntax like:
Expand All @@ -26,12 +26,6 @@ export default function separateStyles(stylesArray, aphroditeStyleTransform) {
has(style, '_name') &&
has(style, '_definition')
) {
// This looks like a reference to an Aphrodite style object, so that's
// where it goes.
if (aphroditeStyleTransform) {
style = aphroditeStyleTransform(style);
}

aphroditeStyles.push(style);
} else {
Object.assign(inlineStyles, style);
Expand Down
36 changes: 0 additions & 36 deletions src/utils/withRTLExtension.js

This file was deleted.

5 changes: 0 additions & 5 deletions src/with-rtl.js

This file was deleted.

Loading