@chakra-ui/styled-system
Version:
Style function for css-in-js building component libraries
159 lines (125 loc) • 4.71 kB
JavaScript
import { isCssVar, isObject, isString, mergeWith as merge, runIfFn } from "@chakra-ui/utils";
import { pseudoSelectors } from "./pseudos";
import { systemProps as systemPropConfigs } from "./system";
import { expandResponsive } from "./utils/expand-responsive";
var isCSSVariableTokenValue = (key, value) => key.startsWith("--") && isString(value) && !isCssVar(value);
var resolveTokenValue = (theme, value) => {
var _ref, _getVar2;
if (value == null) return value;
var 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 = val => {
var _getVar;
return (_getVar = getVar(val)) != null ? _getVar : val;
};
var valueSplit = value.split(",").map(v => v.trim());
var [tokenValue, fallbackValue] = valueSplit;
value = (_ref = (_getVar2 = getVar(tokenValue)) != null ? _getVar2 : getValue(fallbackValue)) != null ? _ref : getValue(value);
return value;
};
export function getCss(options) {
var {
configs = {},
pseudos = {},
theme
} = options;
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] = merge({}, 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 = merge({}, computedStyles, staticStyles);
}
if (configProperty && Array.isArray(configProperty)) {
for (var property of configProperty) {
computedStyles[property] = rawValue;
}
continue;
}
if (configProperty) {
if (configProperty === "&" && isObject(rawValue)) {
computedStyles = merge({}, computedStyles, rawValue);
} else {
computedStyles[configProperty] = rawValue;
}
continue;
}
if (isObject(rawValue)) {
computedStyles = merge({}, computedStyles, rawValue);
continue;
}
computedStyles[key] = rawValue;
}
return computedStyles;
};
return css;
}
export var css = styles => theme => {
var cssFn = getCss({
theme,
pseudos: pseudoSelectors,
configs: systemPropConfigs
});
return cssFn(styles);
};
//# sourceMappingURL=css.js.map