UNPKG

@mui/stylis-plugin-rtl

Version:

A plugin for Material UI that provides RTL (right-to-left) support.

50 lines (47 loc) • 2.06 kB
/** * @mui/stylis-plugin-rtl v7.2.0 * * @license MIT * This source code is licensed under the MIT license found in the * LICENSE file in the root directory of this source tree. */ /* eslint-disable default-case, no-return-assign, curly, prefer-template, @typescript-eslint/no-unused-vars */ /** * Copied from https://github.com/styled-components/stylis-plugin-rtl/blob/main/src/stylis-rtl.ts * with a modification at line 67 to handle layer rules. */ // @ts-nocheck import cssjanus from 'cssjanus'; import { COMMENT, compile, DECLARATION, IMPORT, RULESET, serialize, strlen, KEYFRAMES, MEDIA, SUPPORTS, LAYER } from 'stylis'; function stringifyPreserveComments(element, index, children) { switch (element.type) { case IMPORT: case DECLARATION: case COMMENT: return element.return = element.return || element.value; case RULESET: { element.value = Array.isArray(element.props) ? element.props.join(',') : element.props; if (Array.isArray(element.children)) { element.children.forEach(x => { if (x.type === COMMENT) x.children = x.value; }); } } } const serializedChildren = serialize(Array.prototype.concat(element.children), stringifyPreserveComments); return strlen(serializedChildren) ? element.return = element.value + '{' + serializedChildren + '}' : ''; } function stylisRTLPlugin(element, index, children, callback) { if (element.type === KEYFRAMES || element.type === SUPPORTS || element.type === RULESET && (!element.parent || element.parent.type === MEDIA || element.parent.type === RULESET || element.parent.type === LAYER)) { const stringified = cssjanus.transform(stringifyPreserveComments(element, index, children)); element.children = stringified ? compile(stringified)[0].children : []; element.return = ''; } } // stable identifier that will not be dropped by minification unless the whole module // is unused Object.defineProperty(stylisRTLPlugin, 'name', { value: 'stylisRTLPlugin' }); export default stylisRTLPlugin;