Skip to content

Instantly share code, notes, and snippets.

@RoystonS
Last active May 25, 2018 14:59
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save RoystonS/15e9fcd6aed5c67eb203d01936bd110a to your computer and use it in GitHub Desktop.
Save RoystonS/15e9fcd6aed5c67eb203d01936bd110a to your computer and use it in GitHub Desktop.
A stylis plugin for integrating with rtl-css-js
// See https://github.com/thysultan/stylis.js#plugins for documentation on how stylis plugins work
function stylisPlugin(context, content) {
if (context === 1) {
// content is a single CSS rule, like 'background-color: pink'
// RTL-CSS-JS requires the key + value to be passed in separately.
const [key, value] = content.split(/:/, 2);
// TODO: this is a bit onerous: rtlcssjs doesn't expose convertProperty, only the entire object convert
// Create an object containing our one property
const rtlInput = { [key]: value };
// Get RTL-CSS-JS to process it
const rtlOutput = rtlCssJS(rtlInput);
// Extract the new key and value back out again
const keys = Object.keys(rtlOutput);
if (keys.length !== 1) {
throw new Error(`Unexpected result from rtlCssJS`);
}
const rtlKey = keys[0];
const rtlValue = rtlOutput[rtlKey];
// Reconstruct the property + value string
return `${rtlKey}: ${rtlValue}`;
} else {
return content;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment