react-native-web-internals
Version:
React Native for Web
98 lines (97 loc) • 3.66 kB
JavaScript
import { styleq } from "styleq";
import { localizeStyle } from "styleq/transform-localize-style.js";
import { canUseDOM } from "../index.native.js";
import { atomic, classic, inline } from "./compiler/index.native.js";
import { createSheet } from "./dom/index.native.js";
import { preprocess } from "./preprocess.native.js";
import { validate } from "./validate.native.js";
var staticStyleMap = /* @__PURE__ */new WeakMap(),
sheet = createSheet();
function customStyleq(styles, isRTL) {
return styleq.factory({
transform(style) {
var compiledStyle = staticStyleMap.get(style);
return compiledStyle != null ? localizeStyle(compiledStyle, isRTL) : style;
}
})(styles);
}
function insertRules(compiledOrderedRules) {
compiledOrderedRules.forEach(function (param) {
var [rules, order] = param;
sheet != null && rules.forEach(function (rule) {
sheet.insert(rule, order);
});
});
}
function compileAndInsertAtomic(style) {
var [compiledStyle, compiledOrderedRules] = atomic(preprocess(style));
return insertRules(compiledOrderedRules), compiledStyle;
}
function compileAndInsertReset(style, key) {
var [compiledStyle, compiledOrderedRules] = classic(style, key);
return insertRules(compiledOrderedRules), compiledStyle;
}
var absoluteFillObject = {
position: "absolute",
left: 0,
right: 0,
top: 0,
bottom: 0
},
absoluteFill = create({
x: {
...absoluteFillObject
}
}).x;
function create(styles) {
return Object.keys(styles).forEach(function (key) {
var styleObj = styles[key];
if (styleObj != null && styleObj.$$css !== !0) {
var compiledStyles;
key.indexOf("$raw") > -1 ? compiledStyles = compileAndInsertReset(styleObj, key.split("$raw")[0]) : (process.env.NODE_ENV !== "production" && (validate(styleObj), styles[key] = Object.freeze(styleObj)), compiledStyles = compileAndInsertAtomic(styleObj)), staticStyleMap.set(styleObj, compiledStyles);
}
}), styles;
}
function compose(style1, style2) {
if (process.env.NODE_ENV !== "production") {
var len = arguments.length;
if (len > 2) {
var readableStyles = [...arguments].map(function (a) {
return flatten(a);
});
throw new Error(`StyleSheet.compose() only accepts 2 arguments, received ${len}: ${JSON.stringify(readableStyles)}`);
}
}
return style1 && style2 ? [style1, style2] : style1 || style2;
}
function flatten() {
for (var _len = arguments.length, styles = new Array(_len), _key = 0; _key < _len; _key++) styles[_key] = arguments[_key];
for (var flatArray = styles.flat(1 / 0), result = {}, i = 0; i < flatArray.length; i++) {
var style = flatArray[i];
style != null && typeof style == "object" && Object.assign(result, style);
}
return result;
}
function getSheet() {
return {
id: sheet.id,
textContent: sheet.getTextContent()
};
}
function StyleSheet(styles, options) {
var isRTL = options != null && options.writingDirection === "rtl",
styleProps = customStyleq(styles, isRTL);
return Array.isArray(styleProps) && styleProps[1] != null && (styleProps[1] = inline(preprocess(styleProps[1]), isRTL)), styleProps;
}
StyleSheet.absoluteFill = absoluteFill;
StyleSheet.absoluteFillObject = absoluteFillObject;
StyleSheet.create = create;
StyleSheet.compose = compose;
StyleSheet.flatten = flatten;
StyleSheet.getSheet = getSheet;
StyleSheet.hairlineWidth = 1;
setTimeout(function () {
canUseDOM && window.__REACT_DEVTOOLS_GLOBAL_HOOK__ && (window.__REACT_DEVTOOLS_GLOBAL_HOOK__.resolveRNStyle = StyleSheet.flatten);
}, 100);
export { StyleSheet as default, flatten };
//# sourceMappingURL=index.native.js.map