UNPKG

@hakuna-matata-ui/styled-system

Version:

Style function for css-in-js building component libraries

1,663 lines (1,462 loc) 56.9 kB
import { isObject, isString, isNumber, isCssVar, memoizedGet, objectKeys, mergeWith, runIfFn, walkObject, pick, analyzeBreakpoints } from '@hakuna-matata-ui/utils'; function _extends() { _extends = Object.assign || function (target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i]; for (var key in source) { if (Object.prototype.hasOwnProperty.call(source, key)) { target[key] = source[key]; } } } return target; }; return _extends.apply(this, arguments); } var tokenToCSSVar = function tokenToCSSVar(scale, value) { return function (theme) { var valueStr = String(value); var key = scale ? scale + "." + valueStr : valueStr; return isObject(theme.__cssMap) && key in theme.__cssMap ? theme.__cssMap[key].varRef : value; }; }; function createTransform(options) { var scale = options.scale, transform = options.transform, compose = options.compose; var fn = function fn(value, theme) { var _transform; var _value = tokenToCSSVar(scale, value)(theme); var result = (_transform = transform == null ? void 0 : transform(_value, theme)) != null ? _transform : _value; if (compose) { result = compose(result, theme); } return result; }; return fn; } function toConfig(scale, transform) { return function (property) { var result = { property: property, scale: scale }; result.transform = createTransform({ scale: scale, transform: transform }); return result; }; } var getRtl = function getRtl(_ref) { var rtl = _ref.rtl, ltr = _ref.ltr; return function (theme) { return theme.direction === "rtl" ? rtl : ltr; }; }; function logical(options) { var property = options.property, scale = options.scale, transform = options.transform; return { scale: scale, property: getRtl(property), transform: scale ? createTransform({ scale: scale, compose: transform }) : transform }; } var _spaceXTemplate, _spaceYTemplate; /** * The CSS transform order following the upcoming spec from CSSWG * translate => rotate => scale => skew * @see https://drafts.csswg.org/css-transforms-2/#ctm * @see https://www.stefanjudis.com/blog/order-in-css-transformation-transform-functions-vs-individual-transforms/ */ var transformTemplate = ["rotate(var(--chakra-rotate, 0))", "scaleX(var(--chakra-scale-x, 1))", "scaleY(var(--chakra-scale-y, 1))", "skewX(var(--chakra-skew-x, 0))", "skewY(var(--chakra-skew-y, 0))"]; function getTransformTemplate() { return ["translateX(var(--chakra-translate-x, 0))", "translateY(var(--chakra-translate-y, 0))"].concat(transformTemplate).join(" "); } function getTransformGpuTemplate() { return ["translate3d(var(--chakra-translate-x, 0), var(--chakra-translate-y, 0), 0)"].concat(transformTemplate).join(" "); } var filterTemplate = { "--chakra-blur": "var(--chakra-empty,/*!*/ /*!*/)", "--chakra-brightness": "var(--chakra-empty,/*!*/ /*!*/)", "--chakra-contrast": "var(--chakra-empty,/*!*/ /*!*/)", "--chakra-grayscale": "var(--chakra-empty,/*!*/ /*!*/)", "--chakra-hue-rotate": "var(--chakra-empty,/*!*/ /*!*/)", "--chakra-invert": "var(--chakra-empty,/*!*/ /*!*/)", "--chakra-saturate": "var(--chakra-empty,/*!*/ /*!*/)", "--chakra-sepia": "var(--chakra-empty,/*!*/ /*!*/)", "--chakra-drop-shadow": "var(--chakra-empty,/*!*/ /*!*/)", filter: ["var(--chakra-blur)", "var(--chakra-brightness)", "var(--chakra-contrast)", "var(--chakra-grayscale)", "var(--chakra-hue-rotate)", "var(--chakra-invert)", "var(--chakra-saturate)", "var(--chakra-sepia)", "var(--chakra-drop-shadow)"].join(" ") }; var backdropFilterTemplate = { backdropFilter: ["var(--chakra-backdrop-blur)", "var(--chakra-backdrop-brightness)", "var(--chakra-backdrop-contrast)", "var(--chakra-backdrop-grayscale)", "var(--chakra-backdrop-hue-rotate)", "var(--chakra-backdrop-invert)", "var(--chakra-backdrop-opacity)", "var(--chakra-backdrop-saturate)", "var(--chakra-backdrop-sepia)"].join(" "), "--chakra-backdrop-blur": "var(--chakra-empty,/*!*/ /*!*/)", "--chakra-backdrop-brightness": "var(--chakra-empty,/*!*/ /*!*/)", "--chakra-backdrop-contrast": "var(--chakra-empty,/*!*/ /*!*/)", "--chakra-backdrop-grayscale": "var(--chakra-empty,/*!*/ /*!*/)", "--chakra-backdrop-hue-rotate": "var(--chakra-empty,/*!*/ /*!*/)", "--chakra-backdrop-invert": "var(--chakra-empty,/*!*/ /*!*/)", "--chakra-backdrop-opacity": "var(--chakra-empty,/*!*/ /*!*/)", "--chakra-backdrop-saturate": "var(--chakra-empty,/*!*/ /*!*/)", "--chakra-backdrop-sepia": "var(--chakra-empty,/*!*/ /*!*/)" }; function getRingTemplate(value) { return { "--chakra-ring-offset-shadow": "var(--chakra-ring-inset) 0 0 0 var(--chakra-ring-offset-width) var(--chakra-ring-offset-color)", "--chakra-ring-shadow": "var(--chakra-ring-inset) 0 0 0 calc(var(--chakra-ring-width) + var(--chakra-ring-offset-width)) var(--chakra-ring-color)", "--chakra-ring-width": value, boxShadow: ["var(--chakra-ring-offset-shadow)", "var(--chakra-ring-shadow)", "var(--chakra-shadow, 0 0 #0000)"].join(", ") }; } var flexDirectionTemplate = { "row-reverse": { space: "--chakra-space-x-reverse", divide: "--chakra-divide-x-reverse" }, "column-reverse": { space: "--chakra-space-y-reverse", divide: "--chakra-divide-y-reverse" } }; var owlSelector = "& > :not(style) ~ :not(style)"; var spaceXTemplate = (_spaceXTemplate = {}, _spaceXTemplate[owlSelector] = { marginInlineStart: "calc(var(--chakra-space-x) * calc(1 - var(--chakra-space-x-reverse)))", marginInlineEnd: "calc(var(--chakra-space-x) * var(--chakra-space-x-reverse))" }, _spaceXTemplate); var spaceYTemplate = (_spaceYTemplate = {}, _spaceYTemplate[owlSelector] = { marginTop: "calc(var(--chakra-space-y) * calc(1 - var(--chakra-space-y-reverse)))", marginBottom: "calc(var(--chakra-space-y) * var(--chakra-space-y-reverse))" }, _spaceYTemplate); function _setPrototypeOf(o, p) { _setPrototypeOf = Object.setPrototypeOf || function _setPrototypeOf(o, p) { o.__proto__ = p; return o; }; return _setPrototypeOf(o, p); } function _inherits(subClass, superClass) { if (typeof superClass !== "function" && superClass !== null) { throw new TypeError("Super expression must either be null or a function"); } subClass.prototype = Object.create(superClass && superClass.prototype, { constructor: { value: subClass, writable: true, configurable: true } }); if (superClass) _setPrototypeOf(subClass, superClass); } function _wrapRegExp() { _wrapRegExp = function (re, groups) { return new BabelRegExp(re, undefined, groups); }; var _super = RegExp.prototype; var _groups = new WeakMap(); function BabelRegExp(re, flags, groups) { var _this = new RegExp(re, flags); _groups.set(_this, groups || _groups.get(re)); return _setPrototypeOf(_this, BabelRegExp.prototype); } _inherits(BabelRegExp, RegExp); BabelRegExp.prototype.exec = function (str) { var result = _super.exec.call(this, str); if (result) result.groups = buildGroups(result, this); return result; }; BabelRegExp.prototype[Symbol.replace] = function (str, substitution) { if (typeof substitution === "string") { var groups = _groups.get(this); return _super[Symbol.replace].call(this, str, substitution.replace(/\$<([^>]+)>/g, function (_, name) { return "$" + groups[name]; })); } else if (typeof substitution === "function") { var _this = this; return _super[Symbol.replace].call(this, str, function () { var args = arguments; if (typeof args[args.length - 1] !== "object") { args = [].slice.call(args); args.push(buildGroups(args, _this)); } return substitution.apply(this, args); }); } else { return _super[Symbol.replace].call(this, str, substitution); } }; function buildGroups(result, re) { var g = _groups.get(re); return Object.keys(g).reduce(function (groups, name) { groups[name] = result[g[name]]; return groups; }, Object.create(null)); } return _wrapRegExp.apply(this, arguments); } var directionMap = { "to-t": "to top", "to-tr": "to top right", "to-r": "to right", "to-br": "to bottom right", "to-b": "to bottom", "to-bl": "to bottom left", "to-l": "to left", "to-tl": "to top left" }; var valueSet = new Set(Object.values(directionMap)); var globalSet = new Set(["none", "-moz-initial", "inherit", "initial", "revert", "unset"]); var trimSpace = function trimSpace(str) { return str.trim(); }; function parseGradient(value, theme) { var _regex$exec$groups, _regex$exec; if (value == null || globalSet.has(value)) return value; var regex = /*#__PURE__*/_wrapRegExp(/(^[\x2DA-Za-z]+)\(((.*))\)/g, { type: 1, values: 2 }); var _ref = (_regex$exec$groups = (_regex$exec = regex.exec(value)) == null ? void 0 : _regex$exec.groups) != null ? _regex$exec$groups : {}, type = _ref.type, values = _ref.values; if (!type || !values) return value; var _type = type.includes("-gradient") ? type : type + "-gradient"; var _values$split$map$fil = values.split(",").map(trimSpace).filter(Boolean), maybeDirection = _values$split$map$fil[0], stops = _values$split$map$fil.slice(1); if ((stops == null ? void 0 : stops.length) === 0) return value; var direction = maybeDirection in directionMap ? directionMap[maybeDirection] : maybeDirection; stops.unshift(direction); var _values = stops.map(function (stop) { // if stop is valid shorthand direction, return it if (valueSet.has(stop)) return stop; var firstStop = stop.indexOf(" "); // color stop could be `red.200 20%` based on css gradient spec var _ref2 = firstStop !== -1 ? [stop.substr(0, firstStop), stop.substr(firstStop + 1)] : [stop], _color = _ref2[0], _stop = _ref2[1]; var _stopOrFunc = isCSSFunction(_stop) ? _stop : _stop && _stop.split(" "); // else, get and transform the color token or css value var key = "colors." + _color; var color = key in theme.__cssMap ? theme.__cssMap[key].varRef : _color; return _stopOrFunc ? [color].concat(Array.isArray(_stopOrFunc) ? _stopOrFunc : [_stopOrFunc]).join(" ") : color; }); return _type + "(" + _values.join(", ") + ")"; } var isCSSFunction = function isCSSFunction(value) { return isString(value) && value.includes("(") && value.includes(")"); }; var gradientTransform = function gradientTransform(value, theme) { return parseGradient(value, theme != null ? theme : {}); }; var analyzeCSSValue = function analyzeCSSValue(value) { var num = parseFloat(value.toString()); var unit = value.toString().replace(String(num), ""); return { unitless: !unit, value: num, unit: unit }; }; var wrap = function wrap(str) { return function (value) { return str + "(" + value + ")"; }; }; var transformFunctions = { filter: function filter(value) { return value !== "auto" ? value : filterTemplate; }, backdropFilter: function backdropFilter(value) { return value !== "auto" ? value : backdropFilterTemplate; }, ring: function ring(value) { return getRingTemplate(transformFunctions.px(value)); }, bgClip: function bgClip(value) { return value === "text" ? { color: "transparent", backgroundClip: "text" } : { backgroundClip: value }; }, transform: function transform(value) { if (value === "auto") return getTransformTemplate(); if (value === "auto-gpu") return getTransformGpuTemplate(); return value; }, px: function px(value) { if (value == null) return value; var _analyzeCSSValue = analyzeCSSValue(value), unitless = _analyzeCSSValue.unitless; return unitless || isNumber(value) ? value + "px" : value; }, fraction: function fraction(value) { return !isNumber(value) || value > 1 ? value : value * 100 + "%"; }, "float": function float(value, theme) { var map = { left: "right", right: "left" }; return theme.direction === "rtl" ? map[value] : value; }, degree: function degree(value) { if (isCssVar(value) || value == null) return value; var unitless = isString(value) && !value.endsWith("deg"); return isNumber(value) || unitless ? value + "deg" : value; }, gradient: gradientTransform, blur: wrap("blur"), opacity: wrap("opacity"), brightness: wrap("brightness"), contrast: wrap("contrast"), dropShadow: wrap("drop-shadow"), grayscale: wrap("grayscale"), hueRotate: wrap("hue-rotate"), invert: wrap("invert"), saturate: wrap("saturate"), sepia: wrap("sepia"), bgImage: function bgImage(value) { if (value == null) return value; var prevent = isCSSFunction(value) || globalSet.has(value); return !prevent ? "url(" + value + ")" : value; }, outline: function outline(value) { var isNoneOrZero = String(value) === "0" || String(value) === "none"; return value !== null && isNoneOrZero ? { outline: "2px solid transparent", outlineOffset: "2px" } : { outline: value }; }, flexDirection: function flexDirection(value) { var _flexDirectionTemplat; var _ref = (_flexDirectionTemplat = flexDirectionTemplate[value]) != null ? _flexDirectionTemplat : {}, space = _ref.space, divide = _ref.divide; var result = { flexDirection: value }; if (space) result[space] = 1; if (divide) result[divide] = 1; return result; } }; var t = { borderWidths: toConfig("borderWidths"), borderStyles: toConfig("borderStyles"), colors: toConfig("colors"), borders: toConfig("borders"), radii: toConfig("radii", transformFunctions.px), space: toConfig("space", transformFunctions.px), spaceT: toConfig("space", transformFunctions.px), degreeT: function degreeT(property) { return { property: property, transform: transformFunctions.degree }; }, prop: function prop(property, scale, transform) { return _extends({ property: property, scale: scale }, scale && { transform: createTransform({ scale: scale, transform: transform }) }); }, propT: function propT(property, transform) { return { property: property, transform: transform }; }, sizes: toConfig("sizes", transformFunctions.px), sizesT: toConfig("sizes", transformFunctions.fraction), shadows: toConfig("shadows"), logical: logical, blur: toConfig("blur", transformFunctions.blur) }; var background = { background: t.colors("background"), backgroundColor: t.colors("backgroundColor"), backgroundImage: t.propT("backgroundImage", transformFunctions.bgImage), backgroundSize: true, backgroundPosition: true, backgroundRepeat: true, backgroundAttachment: true, backgroundClip: { transform: transformFunctions.bgClip }, bgSize: t.prop("backgroundSize"), bgPosition: t.prop("backgroundPosition"), bg: t.colors("background"), bgColor: t.colors("backgroundColor"), bgPos: t.prop("backgroundPosition"), bgRepeat: t.prop("backgroundRepeat"), bgAttachment: t.prop("backgroundAttachment"), bgGradient: t.propT("backgroundImage", transformFunctions.gradient), bgClip: { transform: transformFunctions.bgClip } }; Object.assign(background, { bgImage: background.backgroundImage, bgImg: background.backgroundImage }); var border = { border: t.borders("border"), borderWidth: t.borderWidths("borderWidth"), borderStyle: t.borderStyles("borderStyle"), borderColor: t.colors("borderColor"), borderRadius: t.radii("borderRadius"), borderTop: t.borders("borderTop"), borderBlockStart: t.borders("borderBlockStart"), borderTopLeftRadius: t.radii("borderTopLeftRadius"), borderStartStartRadius: t.logical({ scale: "radii", property: { ltr: "borderTopLeftRadius", rtl: "borderTopRightRadius" } }), borderEndStartRadius: t.logical({ scale: "radii", property: { ltr: "borderBottomLeftRadius", rtl: "borderBottomRightRadius" } }), borderTopRightRadius: t.radii("borderTopRightRadius"), borderStartEndRadius: t.logical({ scale: "radii", property: { ltr: "borderTopRightRadius", rtl: "borderTopLeftRadius" } }), borderEndEndRadius: t.logical({ scale: "radii", property: { ltr: "borderBottomRightRadius", rtl: "borderBottomLeftRadius" } }), borderRight: t.borders("borderRight"), borderInlineEnd: t.borders("borderInlineEnd"), borderBottom: t.borders("borderBottom"), borderBlockEnd: t.borders("borderBlockEnd"), borderBottomLeftRadius: t.radii("borderBottomLeftRadius"), borderBottomRightRadius: t.radii("borderBottomRightRadius"), borderLeft: t.borders("borderLeft"), borderInlineStart: { property: "borderInlineStart", scale: "borders" }, borderInlineStartRadius: t.logical({ scale: "radii", property: { ltr: ["borderTopLeftRadius", "borderBottomLeftRadius"], rtl: ["borderTopRightRadius", "borderBottomRightRadius"] } }), borderInlineEndRadius: t.logical({ scale: "radii", property: { ltr: ["borderTopRightRadius", "borderBottomRightRadius"], rtl: ["borderTopLeftRadius", "borderBottomLeftRadius"] } }), borderX: t.borders(["borderLeft", "borderRight"]), borderInline: t.borders("borderInline"), borderY: t.borders(["borderTop", "borderBottom"]), borderBlock: t.borders("borderBlock"), borderTopWidth: t.borderWidths("borderTopWidth"), borderBlockStartWidth: t.borderWidths("borderBlockStartWidth"), borderTopColor: t.colors("borderTopColor"), borderBlockStartColor: t.colors("borderBlockStartColor"), borderTopStyle: t.borderStyles("borderTopStyle"), borderBlockStartStyle: t.borderStyles("borderBlockStartStyle"), borderBottomWidth: t.borderWidths("borderBottomWidth"), borderBlockEndWidth: t.borderWidths("borderBlockEndWidth"), borderBottomColor: t.colors("borderBottomColor"), borderBlockEndColor: t.colors("borderBlockEndColor"), borderBottomStyle: t.borderStyles("borderBottomStyle"), borderBlockEndStyle: t.borderStyles("borderBlockEndStyle"), borderLeftWidth: t.borderWidths("borderLeftWidth"), borderInlineStartWidth: t.borderWidths("borderInlineStartWidth"), borderLeftColor: t.colors("borderLeftColor"), borderInlineStartColor: t.colors("borderInlineStartColor"), borderLeftStyle: t.borderStyles("borderLeftStyle"), borderInlineStartStyle: t.borderStyles("borderInlineStartStyle"), borderRightWidth: t.borderWidths("borderRightWidth"), borderInlineEndWidth: t.borderWidths("borderInlineEndWidth"), borderRightColor: t.colors("borderRightColor"), borderInlineEndColor: t.colors("borderInlineEndColor"), borderRightStyle: t.borderStyles("borderRightStyle"), borderInlineEndStyle: t.borderStyles("borderInlineEndStyle"), borderTopRadius: t.radii(["borderTopLeftRadius", "borderTopRightRadius"]), borderBottomRadius: t.radii(["borderBottomLeftRadius", "borderBottomRightRadius"]), borderLeftRadius: t.radii(["borderTopLeftRadius", "borderBottomLeftRadius"]), borderRightRadius: t.radii(["borderTopRightRadius", "borderBottomRightRadius"]) }; Object.assign(border, { rounded: border.borderRadius, roundedTop: border.borderTopRadius, roundedTopLeft: border.borderTopLeftRadius, roundedTopRight: border.borderTopRightRadius, roundedTopStart: border.borderStartStartRadius, roundedTopEnd: border.borderStartEndRadius, roundedBottom: border.borderBottomRadius, roundedBottomLeft: border.borderBottomLeftRadius, roundedBottomRight: border.borderBottomRightRadius, roundedBottomStart: border.borderEndStartRadius, roundedBottomEnd: border.borderEndEndRadius, roundedLeft: border.borderLeftRadius, roundedRight: border.borderRightRadius, roundedStart: border.borderInlineStartRadius, roundedEnd: border.borderInlineEndRadius, borderStart: border.borderInlineStart, borderEnd: border.borderInlineEnd, borderTopStartRadius: border.borderStartStartRadius, borderTopEndRadius: border.borderStartEndRadius, borderBottomStartRadius: border.borderEndStartRadius, borderBottomEndRadius: border.borderEndEndRadius, borderStartRadius: border.borderInlineStartRadius, borderEndRadius: border.borderInlineEndRadius, borderStartWidth: border.borderInlineStartWidth, borderEndWidth: border.borderInlineEndWidth, borderStartColor: border.borderInlineStartColor, borderEndColor: border.borderInlineEndColor, borderStartStyle: border.borderInlineStartStyle, borderEndStyle: border.borderInlineEndStyle }); /** * The prop types for border properties listed above */ var color = { color: t.colors("color"), textColor: t.colors("color"), fill: t.colors("fill"), stroke: t.colors("stroke") }; var effect = { boxShadow: t.shadows("boxShadow"), mixBlendMode: true, blendMode: t.prop("mixBlendMode"), backgroundBlendMode: true, bgBlendMode: t.prop("backgroundBlendMode"), opacity: true }; Object.assign(effect, { shadow: effect.boxShadow }); /** * Types for box and text shadow properties */ var filter = { filter: { transform: transformFunctions.filter }, blur: t.blur("--chakra-blur"), brightness: t.propT("--chakra-brightness", transformFunctions.brightness), contrast: t.propT("--chakra-contrast", transformFunctions.contrast), hueRotate: t.degreeT("--chakra-hue-rotate"), invert: t.propT("--chakra-invert", transformFunctions.invert), saturate: t.propT("--chakra-saturate", transformFunctions.saturate), dropShadow: t.propT("--chakra-drop-shadow", transformFunctions.dropShadow), backdropFilter: { transform: transformFunctions.backdropFilter }, backdropBlur: t.blur("--chakra-backdrop-blur"), backdropBrightness: t.propT("--chakra-backdrop-brightness", transformFunctions.brightness), backdropContrast: t.propT("--chakra-backdrop-contrast", transformFunctions.contrast), backdropHueRotate: t.degreeT("--chakra-backdrop-hue-rotate"), backdropInvert: t.propT("--chakra-backdrop-invert", transformFunctions.invert), backdropSaturate: t.propT("--chakra-backdrop-saturate", transformFunctions.saturate) }; var flexbox = { alignItems: true, alignContent: true, justifyItems: true, justifyContent: true, flexWrap: true, flexDirection: { transform: transformFunctions.flexDirection }, experimental_spaceX: { "static": spaceXTemplate, transform: createTransform({ scale: "space", transform: function transform(value) { return value !== null ? { "--chakra-space-x": value } : null; } }) }, experimental_spaceY: { "static": spaceYTemplate, transform: createTransform({ scale: "space", transform: function transform(value) { return value != null ? { "--chakra-space-y": value } : null; } }) }, flex: true, flexFlow: true, flexGrow: true, flexShrink: true, flexBasis: t.sizes("flexBasis"), justifySelf: true, alignSelf: true, order: true, placeItems: true, placeContent: true, placeSelf: true, gap: t.space("gap"), rowGap: t.space("rowGap"), columnGap: t.space("columnGap") }; Object.assign(flexbox, { flexDir: flexbox.flexDirection }); var grid = { gridGap: t.space("gridGap"), gridColumnGap: t.space("gridColumnGap"), gridRowGap: t.space("gridRowGap"), gridColumn: true, gridRow: true, gridAutoFlow: true, gridAutoColumns: true, gridColumnStart: true, gridColumnEnd: true, gridRowStart: true, gridRowEnd: true, gridAutoRows: true, gridTemplate: true, gridTemplateColumns: true, gridTemplateRows: true, gridTemplateAreas: true, gridArea: true }; var interactivity = { appearance: true, cursor: true, resize: true, userSelect: true, pointerEvents: true, outline: { transform: transformFunctions.outline }, outlineOffset: true, outlineColor: t.colors("outlineColor") }; var layout = { width: t.sizesT("width"), inlineSize: t.sizesT("inlineSize"), height: t.sizes("height"), blockSize: t.sizes("blockSize"), boxSize: t.sizes(["width", "height"]), minWidth: t.sizes("minWidth"), minInlineSize: t.sizes("minInlineSize"), minHeight: t.sizes("minHeight"), minBlockSize: t.sizes("minBlockSize"), maxWidth: t.sizes("maxWidth"), maxInlineSize: t.sizes("maxInlineSize"), maxHeight: t.sizes("maxHeight"), maxBlockSize: t.sizes("maxBlockSize"), d: t.prop("display"), overflow: true, overflowX: true, overflowY: true, overscrollBehavior: true, overscrollBehaviorX: true, overscrollBehaviorY: true, display: true, verticalAlign: true, boxSizing: true, boxDecorationBreak: true, "float": t.propT("float", transformFunctions["float"]), objectFit: true, objectPosition: true, visibility: true, isolation: true }; Object.assign(layout, { w: layout.width, h: layout.height, minW: layout.minWidth, maxW: layout.maxWidth, minH: layout.minHeight, maxH: layout.maxHeight, overscroll: layout.overscrollBehavior, overscrollX: layout.overscrollBehaviorX, overscrollY: layout.overscrollBehaviorY }); /** * Types for layout related CSS properties */ var list = { listStyleType: true, listStylePosition: true, listStylePos: t.prop("listStylePosition"), listStyleImage: true, listStyleImg: t.prop("listStyleImage") }; var srOnly = { border: "0px", clip: "rect(0, 0, 0, 0)", width: "1px", height: "1px", margin: "-1px", padding: "0px", overflow: "hidden", whiteSpace: "nowrap", position: "absolute" }; var srFocusable = { position: "static", width: "auto", height: "auto", clip: "auto", padding: "0", margin: "0", overflow: "visible", whiteSpace: "normal" }; var getWithPriority = function getWithPriority(theme, key, styles) { var result = {}; var obj = memoizedGet(theme, key, {}); for (var prop in obj) { var isInStyles = prop in styles && styles[prop] != null; if (!isInStyles) result[prop] = obj[prop]; } return result; }; var others = { srOnly: { transform: function transform(value) { if (value === true) return srOnly; if (value === "focusable") return srFocusable; return {}; } }, layerStyle: { processResult: true, transform: function transform(value, theme, styles) { return getWithPriority(theme, "layerStyles." + value, styles); } }, textStyle: { processResult: true, transform: function transform(value, theme, styles) { return getWithPriority(theme, "textStyles." + value, styles); } }, apply: { processResult: true, transform: function transform(value, theme, styles) { return getWithPriority(theme, value, styles); } } }; var position = { position: true, pos: t.prop("position"), zIndex: t.prop("zIndex", "zIndices"), inset: t.spaceT(["top", "right", "bottom", "left"]), insetX: t.spaceT(["left", "right"]), insetInline: t.spaceT("insetInline"), insetY: t.spaceT(["top", "bottom"]), insetBlock: t.spaceT("insetBlock"), top: t.spaceT("top"), insetBlockStart: t.spaceT("insetBlockStart"), bottom: t.spaceT("bottom"), insetBlockEnd: t.spaceT("insetBlockEnd"), left: t.spaceT("left"), insetInlineStart: t.logical({ scale: "space", property: { ltr: "left", rtl: "right" } }), right: t.spaceT("right"), insetInlineEnd: t.logical({ scale: "space", property: { ltr: "right", rtl: "left" } }) }; Object.assign(position, { insetStart: position.insetInlineStart, insetEnd: position.insetInlineEnd }); /** * Types for position CSS properties */ /** * The parser configuration for common outline properties */ var ring = { ring: { transform: transformFunctions.ring }, ringColor: t.colors("--chakra-ring-color"), ringOffset: t.prop("--chakra-ring-offset-width"), ringOffsetColor: t.colors("--chakra-ring-offset-color"), ringInset: t.prop("--chakra-ring-inset") }; var space = { margin: t.spaceT("margin"), marginTop: t.spaceT("marginTop"), marginBlockStart: t.spaceT("marginBlockStart"), marginRight: t.spaceT("marginRight"), marginInlineEnd: t.spaceT("marginInlineEnd"), marginBottom: t.spaceT("marginBottom"), marginBlockEnd: t.spaceT("marginBlockEnd"), marginLeft: t.spaceT("marginLeft"), marginInlineStart: t.spaceT("marginInlineStart"), marginX: t.spaceT(["marginInlineStart", "marginInlineEnd"]), marginInline: t.spaceT("marginInline"), marginY: t.spaceT(["marginTop", "marginBottom"]), marginBlock: t.spaceT("marginBlock"), padding: t.space("padding"), paddingTop: t.space("paddingTop"), paddingBlockStart: t.space("paddingBlockStart"), paddingRight: t.space("paddingRight"), paddingBottom: t.space("paddingBottom"), paddingBlockEnd: t.space("paddingBlockEnd"), paddingLeft: t.space("paddingLeft"), paddingInlineStart: t.space("paddingInlineStart"), paddingInlineEnd: t.space("paddingInlineEnd"), paddingX: t.space(["paddingInlineStart", "paddingInlineEnd"]), paddingInline: t.space("paddingInline"), paddingY: t.space(["paddingTop", "paddingBottom"]), paddingBlock: t.space("paddingBlock") }; Object.assign(space, { m: space.margin, mt: space.marginTop, mr: space.marginRight, me: space.marginInlineEnd, marginEnd: space.marginInlineEnd, mb: space.marginBottom, ml: space.marginLeft, ms: space.marginInlineStart, marginStart: space.marginInlineStart, mx: space.marginX, my: space.marginY, p: space.padding, pt: space.paddingTop, py: space.paddingY, px: space.paddingX, pb: space.paddingBottom, pl: space.paddingLeft, ps: space.paddingInlineStart, paddingStart: space.paddingInlineStart, pr: space.paddingRight, pe: space.paddingInlineEnd, paddingEnd: space.paddingInlineEnd }); /** * Types for space related CSS properties */ var textDecoration = { textDecorationColor: t.colors("textDecorationColor"), textDecoration: true, textDecor: { property: "textDecoration" }, textDecorationLine: true, textDecorationStyle: true, textDecorationThickness: true, textUnderlineOffset: true, textShadow: t.shadows("textShadow") }; var transform = { clipPath: true, transform: t.propT("transform", transformFunctions.transform), transformOrigin: true, translateX: t.spaceT("--chakra-translate-x"), translateY: t.spaceT("--chakra-translate-y"), skewX: t.degreeT("--chakra-skew-x"), skewY: t.degreeT("--chakra-skew-y"), scaleX: t.prop("--chakra-scale-x"), scaleY: t.prop("--chakra-scale-y"), scale: t.prop(["--chakra-scale-x", "--chakra-scale-y"]), rotate: t.degreeT("--chakra-rotate") }; var transition = { transition: true, transitionDelay: true, animation: true, willChange: true, transitionDuration: t.prop("transitionDuration", "transition.duration"), transitionProperty: t.prop("transitionProperty", "transition.property"), transitionTimingFunction: t.prop("transitionTimingFunction", "transition.easing") }; var typography = { fontFamily: t.prop("fontFamily", "fonts"), fontSize: t.prop("fontSize", "fontSizes", transformFunctions.px), fontWeight: t.prop("fontWeight", "fontWeights"), lineHeight: t.prop("lineHeight", "lineHeights"), letterSpacing: t.prop("letterSpacing", "letterSpacings"), textAlign: true, fontStyle: true, wordBreak: true, overflowWrap: true, textOverflow: true, textTransform: true, whiteSpace: true, noOfLines: { "static": { overflow: "hidden", textOverflow: "ellipsis", display: "-webkit-box", WebkitBoxOrient: "vertical", //@ts-ignore WebkitLineClamp: "var(--chakra-line-clamp)" }, property: "--chakra-line-clamp" }, isTruncated: { transform: function transform(value) { if (value === true) { return { overflow: "hidden", textOverflow: "ellipsis", whiteSpace: "nowrap" }; } } } }; /** * Types for typography related CSS properties */ function _arrayLikeToArray(arr, len) { if (len == null || len > arr.length) len = arr.length; for (var i = 0, arr2 = new Array(len); i < len; i++) arr2[i] = arr[i]; return arr2; } function _unsupportedIterableToArray(o, minLen) { if (!o) return; if (typeof o === "string") return _arrayLikeToArray(o, minLen); var n = Object.prototype.toString.call(o).slice(8, -1); if (n === "Object" && o.constructor) n = o.constructor.name; if (n === "Map" || n === "Set") return Array.from(o); if (n === "Arguments" || /^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(n)) return _arrayLikeToArray(o, minLen); } function _createForOfIteratorHelperLoose(o, allowArrayLike) { var it = typeof Symbol !== "undefined" && o[Symbol.iterator] || o["@@iterator"]; if (it) return (it = it.call(o)).next.bind(it); if (Array.isArray(o) || (it = _unsupportedIterableToArray(o)) || allowArrayLike && o && typeof o.length === "number") { if (it) o = it; var i = 0; return function () { if (i >= o.length) return { done: true }; return { done: false, value: o[i++] }; }; } throw new TypeError("Invalid attempt to iterate non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method."); } var group = { hover: function hover(selector) { return selector + ":hover &, " + selector + "[data-hover] &"; }, focus: function focus(selector) { return selector + ":focus &, " + selector + "[data-focus] &"; }, focusVisible: function focusVisible(selector) { return selector + ":focus-visible &"; }, active: function active(selector) { return selector + ":active &, " + selector + "[data-active] &"; }, disabled: function disabled(selector) { return selector + ":disabled &, " + selector + "[data-disabled] &"; }, invalid: function invalid(selector) { return selector + ":invalid &, " + selector + "[data-invalid] &"; }, checked: function checked(selector) { return selector + ":checked &, " + selector + "[data-checked] &"; }, indeterminate: function indeterminate(selector) { return selector + ":indeterminate &, " + selector + "[aria-checked=mixed] &, " + selector + "[data-indeterminate] &"; }, readOnly: function readOnly(selector) { return selector + ":read-only &, " + selector + "[readonly] &, " + selector + "[data-read-only] &"; }, expanded: function expanded(selector) { return selector + ":read-only &, " + selector + "[aria-expanded=true] &, " + selector + "[data-expanded] &"; } }; var toGroup = function toGroup(fn) { return merge(fn, "[role=group]", "[data-group]", ".group"); }; var merge = function merge(fn) { for (var _len = arguments.length, selectors = new Array(_len > 1 ? _len - 1 : 0), _key = 1; _key < _len; _key++) { selectors[_key - 1] = arguments[_key]; } return selectors.map(fn).join(", "); }; var pseudoSelectors = { /** * Styles for CSS selector `&:hover` */ _hover: "&:hover, &[data-hover]", /** * Styles for CSS Selector `&:active` */ _active: "&:active, &[data-active]", /** * Styles for CSS selector `&:focus` * */ _focus: "&:focus, &[data-focus]", /** * Styles for the highlighted state. */ _highlighted: "&[data-highlighted]", /** * Styles to apply when a child of this element has received focus * - CSS Selector `&:focus-within` */ _focusWithin: "&:focus-within", _focusVisible: "&:focus-visible", /** * Styles to apply when this element is disabled. The passed styles are applied to these CSS selectors: * - `&[aria-disabled=true]` * - `&:disabled` * - `&[data-disabled]` */ _disabled: "&[disabled], &[aria-disabled=true], &[data-disabled]", /** * Styles for CSS Selector `&:readonly` */ _readOnly: "&[aria-readonly=true], &[readonly], &[data-readonly]", /** * Styles for CSS selector `&::before` * * NOTE:When using this, ensure the `content` is wrapped in a backtick. * @example * ```jsx * <Box _before={{content:`""` }}/> * ``` */ _before: "&::before", /** * Styles for CSS selector `&::after` * * NOTE:When using this, ensure the `content` is wrapped in a backtick. * @example * ```jsx * <Box _after={{content:`""` }}/> * ``` */ _after: "&::after", _empty: "&:empty", /** * Styles to apply when the ARIA attribute `aria-expanded` is `true` * - CSS selector `&[aria-expanded=true]` */ _expanded: "&[aria-expanded=true], &[data-expanded]", /** * Styles to apply when the ARIA attribute `aria-checked` is `true` * - CSS selector `&[aria-checked=true]` */ _checked: "&[aria-checked=true], &[data-checked]", /** * Styles to apply when the ARIA attribute `aria-grabbed` is `true` * - CSS selector `&[aria-grabbed=true]` */ _grabbed: "&[aria-grabbed=true], &[data-grabbed]", /** * Styles for CSS Selector `&[aria-pressed=true]` * Typically used to style the current "pressed" state of toggle buttons */ _pressed: "&[aria-pressed=true], &[data-pressed]", /** * Styles to apply when the ARIA attribute `aria-invalid` is `true` * - CSS selector `&[aria-invalid=true]` */ _invalid: "&[aria-invalid=true], &[data-invalid]", /** * Styles for the valid state * - CSS selector `&[data-valid], &[data-state=valid]` */ _valid: "&[data-valid], &[data-state=valid]", /** * Styles for CSS Selector `&[aria-busy=true]` or `&[data-loading=true]`. * Useful for styling loading states */ _loading: "&[data-loading], &[aria-busy=true]", /** * Styles to apply when the ARIA attribute `aria-selected` is `true` * * - CSS selector `&[aria-selected=true]` */ _selected: "&[aria-selected=true], &[data-selected]", /** * Styles for CSS Selector `[hidden=true]` */ _hidden: "&[hidden], &[data-hidden]", /** * Styles for CSS Selector `&:-webkit-autofill` */ _autofill: "&:-webkit-autofill", /** * Styles for CSS Selector `&:nth-child(even)` */ _even: "&:nth-of-type(even)", /** * Styles for CSS Selector `&:nth-child(odd)` */ _odd: "&:nth-of-type(odd)", /** * Styles for CSS Selector `&:first-of-type` */ _first: "&:first-of-type", /** * Styles for CSS Selector `&:last-of-type` */ _last: "&:last-of-type", /** * Styles for CSS Selector `&:not(:first-of-type)` */ _notFirst: "&:not(:first-of-type)", /** * Styles for CSS Selector `&:not(:last-of-type)` */ _notLast: "&:not(:last-of-type)", /** * Styles for CSS Selector `&:visited` */ _visited: "&:visited", /** * Used to style the active link in a navigation * Styles for CSS Selector `&[aria-current=page]` */ _activeLink: "&[aria-current=page]", /** * Used to style the current step within a process * Styles for CSS Selector `&[aria-current=step]` */ _activeStep: "&[aria-current=step]", /** * Styles to apply when the ARIA attribute `aria-checked` is `mixed` * - CSS selector `&[aria-checked=mixed]` */ _indeterminate: "&:indeterminate, &[aria-checked=mixed], &[data-indeterminate]", /** * Styles to apply when parent is hovered */ _groupHover: toGroup(group.hover), /** * Styles to apply when parent is focused */ _groupFocus: toGroup(group.focus), _groupFocusVisible: toGroup(group.focusVisible), /** * Styles to apply when parent is active */ _groupActive: toGroup(group.active), /** * Styles to apply when parent is disabled */ _groupDisabled: toGroup(group.disabled), /** * Styles to apply when parent is invalid */ _groupInvalid: toGroup(group.invalid), /** * Styles to apply when parent is checked */ _groupChecked: toGroup(group.checked), /** * Styles for CSS Selector `&::placeholder`. */ _placeholder: "&::placeholder", /** * Styles for CSS Selector `&:fullscreen`. */ _fullScreen: "&:fullscreen", /** * Styles for CSS Selector `&::selection` */ _selection: "&::selection", /** * Styles for CSS Selector `[dir=rtl] &` * It is applied when any parent element has `dir="rtl"` */ _rtl: "[dir=rtl] &", /** * Styles for CSS Selector `@media (prefers-color-scheme: dark)` * used when the user has requested the system * use a light or dark color theme. */ _mediaDark: "@media (prefers-color-scheme: dark)", /** * Styles for when `data-theme` is applied to any parent of * this component or element. */ _dark: ".chakra-ui-dark &, [data-theme=dark] &, &[data-theme=dark]", /** * Styles for when `data-theme` is applied to any parent of * this component or element. */ _light: ".chakra-ui-light &, [data-theme=light] &, &[data-theme=light]" }; var pseudoPropNames = objectKeys(pseudoSelectors); var systemProps = mergeWith({}, background, border, color, flexbox, layout, filter, ring, interactivity, grid, others, position, effect, space, typography, textDecoration, transform, list, transition); var layoutSystem = Object.assign({}, space, layout, flexbox, grid, position); var layoutPropNames = objectKeys(layoutSystem); var propNames = [].concat(objectKeys(systemProps), pseudoPropNames); var styleProps = _extends({}, systemProps, pseudoSelectors); var isStyleProp = function isStyleProp(prop) { return prop in styleProps; }; /** * Expands an array or object syntax responsive style. * * @example * expandResponsive({ mx: [1, 2] }) * // or * expandResponsive({ mx: { base: 1, sm: 2 } }) * * // => { mx: 1, "@media(min-width:<sm>)": { mx: 2 } } */ var expandResponsive = function expandResponsive(styles) { return function (theme) { /** * Before any style can be processed, the user needs to call `toCSSVar` * which analyzes the theme's breakpoint and appends a `__breakpoints` property * to the theme with more details of the breakpoints. * * To learn more, go here: packages/utils/src/responsive.ts #analyzeBreakpoints */ if (!theme.__breakpoints) return styles; var _theme$__breakpoints = theme.__breakpoints, isResponsive = _theme$__breakpoints.isResponsive, toArrayValue = _theme$__breakpoints.toArrayValue, medias = _theme$__breakpoints.media; var computedStyles = {}; for (var key in styles) { var value = runIfFn(styles[key], theme); if (value == null) continue; // converts the object responsive syntax to array syntax value = isObject(value) && isResponsive(value) ? toArrayValue(value) : value; if (!Array.isArray(value)) { computedStyles[key] = value; continue; } var queries = value.slice(0, medias.length).length; for (var index = 0; index < queries; index += 1) { var media = medias == null ? void 0 : medias[index]; if (!media) { computedStyles[key] = value[index]; continue; } computedStyles[media] = computedStyles[media] || {}; if (value[index] == null) { continue; } computedStyles[media][key] = value[index]; } } return computedStyles; }; }; var isCSSVariableTokenValue = function isCSSVariableTokenValue(key, value) { return key.startsWith("--") && isString(value) && !isCssVar(value); }; var resolveTokenValue = function resolveTokenValue(theme, value) { var _ref, _getVar2; if (value == null) return value; var getVar = function getVar(val) { var _theme$__cssMap, _theme$__cssMap$val; return (_theme$__cssMap = theme.__cssMap) == null ? void 0 : (_theme$__cssMap$val = _theme$__cssMap[val]) == null ? void 0 : _theme$__cssMap$val.varRef; }; var getValue = function getValue(val) { var _getVar; return (_getVar = getVar(val)) != null ? _getVar : val; }; var valueSplit = value.split(",").map(function (v) { return v.trim(); }); var tokenValue = valueSplit[0], fallbackValue = valueSplit[1]; value = (_ref = (_getVar2 = getVar(tokenValue)) != null ? _getVar2 : getValue(fallbackValue)) != null ? _ref : getValue(value); return value; }; function getCss(options) { var _options$configs = options.configs, configs = _options$configs === void 0 ? {} : _options$configs, _options$pseudos = options.pseudos, pseudos = _options$pseudos === void 0 ? {} : _options$pseudos, theme = options.theme; var css = function css(stylesOrFn, nested) { if (nested === void 0) { nested = false; } var _styles = runIfFn(stylesOrFn, theme); var styles = expandResponsive(_styles)(theme); var computedStyles = {}; for (var key in styles) { var _config$transform, _config, _config2, _config3, _config4; var valueOrFn = styles[key]; /** * allows the user to pass functional values * boxShadow: theme => `0 2px 2px ${theme.colors.red}` */ var value = runIfFn(valueOrFn, theme); /** * converts pseudo shorthands to valid selector * "_hover" => "&:hover" */ if (key in pseudos) { key = pseudos[key]; } /** * allows the user to use theme tokens in css vars * { --banner-height: "sizes.md" } => { --banner-height: "var(--chakra-sizes-md)" } * * You can also provide fallback values * { --banner-height: "sizes.no-exist, 40px" } => { --banner-height: "40px" } */ if (isCSSVariableTokenValue(key, value)) { value = resolveTokenValue(theme, value); } var config = configs[key]; if (config === true) { config = { property: key }; } if (isObject(value)) { var _computedStyles$key; computedStyles[key] = (_computedStyles$key = computedStyles[key]) != null ? _computedStyles$key : {}; computedStyles[key] = mergeWith({}, computedStyles[key], css(value, true)); continue; } var rawValue = (_config$transform = (_config = config) == null ? void 0 : _config.transform == null ? void 0 : _config.transform(value, theme, _styles)) != null ? _config$transform : value; /** * Used for `layerStyle`, `textStyle` and `apply`. After getting the * styles in the theme, we need to process them since they might * contain theme tokens. * * `processResult` is the config property we pass to `layerStyle`, `textStyle` and `apply` */ rawValue = (_config2 = config) != null && _config2.processResult ? css(rawValue, true) : rawValue; /** * allows us define css properties for RTL and LTR. * * const marginStart = { * property: theme => theme.direction === "rtl" ? "marginRight": "marginLeft", * } */ var configProperty = runIfFn((_config3 = config) == null ? void 0 : _config3.property, theme); if (!nested && (_config4 = config) != null && _config4["static"]) { var staticStyles = runIfFn(config["static"], theme); computedStyles = mergeWith({}, computedStyles, staticStyles); } if (configProperty && Array.isArray(configProperty)) { for (var _iterator = _createForOfIteratorHelperLoose(configProperty), _step; !(_step = _iterator()).done;) { var property = _step.value; computedStyles[property] = rawValue; } continue; } if (configProperty) { if (configProperty === "&" && isObject(rawValue)) { computedStyles = mergeWith({}, computedStyles, rawValue); } else { computedStyles[configProperty] = rawValue; } continue; } if (isObject(rawValue)) { computedStyles = mergeWith({}, computedStyles, rawValue); continue; } computedStyles[key] = rawValue; } return computedStyles; }; return css; } var css = function css(styles) { return function (theme) { var cssFn = getCss({ theme: theme, pseudos: pseudoSelectors, configs: systemProps }); return cssFn(styles); }; }; /** * Thank you @markdalgleish for this piece of art! */ function resolveReference(operand) { if (isObject(operand) && operand.reference) { return operand.reference; } return String(operand); } var toExpression = function toExpression(operator) { for (var _len = arguments.length, operands = new Array(_len > 1 ? _len - 1 : 0), _key = 1; _key < _len; _key++) { operands[_key - 1] = arguments[_key]; } return operands.map(resolveReference).join(" " + operator + " ").replace(/calc/g, ""); }; var _add = function add() { for (var _len2 = arguments.length, operands = new Array(_len2), _key2 = 0; _key2 < _len2; _key2++) { operands[_key2] = arguments[_key2]; } return "calc(" + toExpression.apply(void 0, ["+"].concat(operands)) + ")"; }; var _subtract = function subtract() { for (var _len3 = arguments.length, operands = new Array(_len3), _key3 = 0; _key3 < _len3; _key3++) { operands[_key3] = arguments[_key3]; } return "calc(" + toExpression.apply(void 0, ["-"].concat(operands)) + ")"; }; var _multiply = function multiply() { for (var _len4 = arguments.length, operands = new Array(_len4), _key4 = 0; _key4 < _len4; _key4++) { operands[_key4] = arguments[_key4]; } return "calc(" + toExpression.apply(void 0, ["*"].concat(operands)) + ")"; }; var _divide = function divide() { for (var _len5 = arguments.length, operands = new Array(_len5), _key5 = 0; _key5 < _len5; _key5++) { operands[_key5] = arguments[_key5]; } return "calc(" + toExpression.apply(void 0, ["/"].concat(operands)) + ")"; }; var _negate = function negate(x) { var value = resolveReference(x); if (value != null && !Number.isNaN(parseFloat(value))) { return String(value).startsWith("-") ? String(value).slice(1) : "-" + value; } return _multiply(value, -