UNPKG

@linaria/react

Version:

Blazing fast zero-runtime CSS in JS library

151 lines (149 loc) 5.95 kB
"use strict"; var __create = Object.create; var __defProp = Object.defineProperty; var __getOwnPropDesc = Object.getOwnPropertyDescriptor; var __getOwnPropNames = Object.getOwnPropertyNames; var __getProtoOf = Object.getPrototypeOf; var __hasOwnProp = Object.prototype.hasOwnProperty; var __export = (target, all) => { for (var name in all) __defProp(target, name, { get: all[name], enumerable: true }); }; var __copyProps = (to, from, except, desc) => { if (from && typeof from === "object" || typeof from === "function") { for (let key of __getOwnPropNames(from)) if (!__hasOwnProp.call(to, key) && key !== except) __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable }); } return to; }; var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps( // If the importer is in node compatibility mode or this is not an ESM // file that has been converted to a CommonJS file using a Babel- // compatible transform (i.e. "__esModule" has not been set), then set // "default" to the CommonJS "module.exports" for node compatibility. isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target, mod )); var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod); // src/index.ts var src_exports = {}; __export(src_exports, { styled: () => styled_default }); module.exports = __toCommonJS(src_exports); // src/styled.ts var import_is_prop_valid = __toESM(require("@emotion/is-prop-valid")); var import_react = require("react"); var import_core = require("@linaria/core"); var isCapital = (ch) => ch.toUpperCase() === ch; var filterKey = (keys) => (key) => keys.indexOf(key) === -1; var omit = (obj, keys) => { const res = {}; Object.keys(obj).filter(filterKey(keys)).forEach((key) => { res[key] = obj[key]; }); return res; }; function filterProps(asIs, props, omitKeys) { const filteredProps = omit(props, omitKeys); if (!asIs) { const interopValidAttr = typeof import_is_prop_valid.default === "function" ? { default: import_is_prop_valid.default } : import_is_prop_valid.default; Object.keys(filteredProps).forEach((key) => { if (!interopValidAttr.default(key)) { delete filteredProps[key]; } }); } return filteredProps; } var warnIfInvalid = (value, componentName) => { if (process.env.NODE_ENV !== "production") { if (typeof value === "string" || // eslint-disable-next-line no-self-compare,no-restricted-globals typeof value === "number" && isFinite(value)) { return; } const stringified = typeof value === "object" ? JSON.stringify(value) : String(value); console.warn( `An interpolation evaluated to '${stringified}' in the component '${componentName}', which is probably a mistake. You should explicitly cast or transform the value to a string.` ); } }; var idx = 0; function styled(tag) { let mockedClass = ""; if (process.env.NODE_ENV === "test") { mockedClass += `mocked-styled-${idx++}`; if (tag && tag.__wyw_meta && tag.__wyw_meta.className) { mockedClass += ` ${tag.__wyw_meta.className}`; } } return (options) => { if (process.env.NODE_ENV !== "production" && process.env.NODE_ENV !== "test") { if (Array.isArray(options)) { throw new Error( 'Using the "styled" tag in runtime is not supported. Make sure you have set up the Babel plugin correctly. See https://github.com/callstack/linaria#setup' ); } } const render = (props, ref) => { const { as: component = tag, class: className = mockedClass } = props; const shouldKeepProps = options.propsAsIs === void 0 ? !(typeof component === "string" && component.indexOf("-") === -1 && !isCapital(component[0])) : options.propsAsIs; const filteredProps = filterProps(shouldKeepProps, props, [ "as", "class" ]); filteredProps.ref = ref; filteredProps.className = options.atomic ? (0, import_core.cx)(options.class, filteredProps.className || className) : (0, import_core.cx)(filteredProps.className || className, options.class); const { vars } = options; if (vars) { const style = {}; for (const name in vars) { const variable = vars[name]; const result = variable[0]; const unit = variable[1] || ""; const value = typeof result === "function" ? result(props) : result; warnIfInvalid(value, options.name); style[`--${name}`] = `${value}${unit}`; } const ownStyle = filteredProps.style || {}; const keys = Object.keys(ownStyle); if (keys.length > 0) { keys.forEach((key) => { style[key] = ownStyle[key]; }); } filteredProps.style = style; } if (tag.__wyw_meta && tag !== component) { filteredProps.as = component; return (0, import_react.createElement)(tag, filteredProps); } return (0, import_react.createElement)(component, filteredProps); }; const Result = import_react.forwardRef ? (0, import_react.forwardRef)(render) : ( // React.forwardRef won't available on older React versions and in Preact // Fallback to a innerRef prop in that case (props) => { const rest = omit(props, ["innerRef"]); return render(rest, props.innerRef); } ); Result.displayName = options.name; Result.__wyw_meta = { className: options.class || mockedClass, extends: tag }; return Result; }; } var styled_default = process.env.NODE_ENV !== "production" ? new Proxy(styled, { get(o, prop) { return o(prop); } }) : styled; // Annotate the CommonJS export names for ESM import in node: 0 && (module.exports = { styled }); //# sourceMappingURL=index.js.map