Last active
May 25, 2018 14:59
-
-
Save RoystonS/15e9fcd6aed5c67eb203d01936bd110a to your computer and use it in GitHub Desktop.
A stylis plugin for integrating with rtl-css-js
This file contains hidden or 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
// 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