postcss-rtl
Version:
PostCSS plugin for RTL-optimizations
39 lines (28 loc) • 973 B
JavaScript
function unprefixed(prop) {
return prop.replace(/^-\w+-/, '');
}
const {rtlifyDecl} = require('./decls');
const {rtlifyRule} = require('./rules');
const isKeyframeRule = (rule) => rule.type === 'atrule' && unprefixed(rule.name) === 'keyframes';
const isKeyframeAlreadyProcessed = (rule) => !!rule.params.match(/-ltr$|-rtl$/);
const isKeyframeSymmetric = (rule) => !rtlifyRule(rule);
const rtlifyKeyframe = (rule, options) => {
const ruleName = rule.params;
rule.params = `${ruleName}-ltr`;
if (!options.onlyDirection || options.onlyDirection === 'rtl') {
const rtlRule = rule.cloneAfter({params: `${ruleName}-rtl`});
rtlRule.walkDecls((decl) => {
const rtl = rtlifyDecl(decl);
decl.value = rtl ? rtl.value : decl.value;
});
}
if (options.onlyDirection && options.onlyDirection === 'rtl') {
rule.remove();
}
};
module.exports = {
isKeyframeRule,
isKeyframeAlreadyProcessed,
isKeyframeSymmetric,
rtlifyKeyframe,
};