UNPKG

@nekobird/styled-chemistry

Version:
763 lines (699 loc) 23.9 kB
import React, { createContext, useContext } from 'react'; import { ThemeProvider } from 'styled-components'; function _typeof(obj) { "@babel/helpers - typeof"; if (typeof Symbol === "function" && typeof Symbol.iterator === "symbol") { _typeof = function (obj) { return typeof obj; }; } else { _typeof = function (obj) { return obj && typeof Symbol === "function" && obj.constructor === Symbol && obj !== Symbol.prototype ? "symbol" : typeof obj; }; } return _typeof(obj); } var isNumber = function isNumber(value) { return typeof value === 'number' && !isNaN(value); }; var isInteger = function isInteger(value) { return isNumber(value) && value % 1 === 0; }; var isStringOrNumber = function isStringOrNumber(value) { return isNumber(value) || typeof value === 'string'; }; var isStringNumberOrNull = function isStringNumberOrNull(value) { return isStringOrNumber(value) || value === null; }; var isValidArrayIndex = function isValidArrayIndex(value) { return isInteger(value) && value >= 0; }; var isSet = function isSet(set) { return set && 'set' in set && _typeof(set.set) === 'object' && Array.isArray(set.set); }; var aliasIsSet = function aliasIsSet(set) { return function (key) { return typeof key === 'string' && 'alias' in set && _typeof(set.alias) === 'object' && isValidArrayIndex(set.alias[key]); }; }; var getSetValueIndex = function getSetValueIndex(set) { return function (key) { return isValidArrayIndex(key) ? key : aliasIsSet(set)(key) ? set.alias[key] : typeof key === 'undefined' ? isValidArrayIndex(set["default"]) ? set["default"] : 0 : null; }; }; var toString = function toString(value) { return isNumber(value) ? value.toString() : value; }; var toArray = function toArray(value) { return Array.isArray(value) ? value : [value]; }; var isValidArrayWithItems = function isValidArrayWithItems(value) { return _typeof(value) === 'object' && Array.isArray(value) && value.length > 0; }; var memo = function memo(fn, cache) { return function () { for (var _len = arguments.length, args = new Array(_len), _key = 0; _key < _len; _key++) { args[_key] = arguments[_key]; } var key = JSON.stringify(args); if (cache.has(key)) { return cache.get(key); } var value = fn.apply(void 0, args); cache.set(key, value); return value; }; }; var createGetFunctionFromSet = function createGetFunctionFromSet(elementSet) { var get = function get(key) { if (!isValidArrayWithItems(elementSet.set)) { return null; } var index = getSetValueIndex(elementSet)(key); var value = isValidArrayIndex(index) ? elementSet.set[index] : null; return isStringOrNumber(value) ? toString(value) : null; }; return memo(get, new Map()); }; var createGetFunctionFromSuperSet = function createGetFunctionFromSuperSet(elementSuperSet) { return function (name) { return createGetFunctionFromSet(elementSuperSet[name]); }; }; var createGetFunctionsFromElements = function createGetFunctionsFromElements(elements) { return { borderStyle: createGetFunctionFromSet(elements.borderStyles), borderWidth: createGetFunctionFromSet(elements.borderWidths), breakpoint: createGetFunctionFromSet(elements.breakpoints), color: createGetFunctionFromSuperSet(elements.colors), fontFamily: createGetFunctionFromSet(elements.fontFamilies), fontSize: createGetFunctionFromSet(elements.fontSizes), fontWeight: createGetFunctionFromSet(elements.fontWeights), image: createGetFunctionFromSet(elements.images), letterSpacing: createGetFunctionFromSet(elements.letterSpacings), lineHeight: createGetFunctionFromSet(elements.lineHeights), opacity: createGetFunctionFromSet(elements.opacities), radius: createGetFunctionFromSet(elements.radii), size: createGetFunctionFromSet(elements.sizes), space: createGetFunctionFromSet(elements.spaces), time: createGetFunctionFromSet(elements.times), timingFunction: createGetFunctionFromSet(elements.timingFunctions), zIndex: createGetFunctionFromSet(elements.zIndices) }; }; var createGetFunctionFromSet$1 = function createGetFunctionFromSet(elementGet) { return function (compoundSet) { var get = function get(key) { if (!isValidArrayWithItems(compoundSet.set)) { return null; } var index = getSetValueIndex(compoundSet)(key); var value = isValidArrayIndex(index) ? compoundSet.set[index] : null; if (typeof value === 'function') { var result = value(elementGet); return isStringOrNumber(result) ? toString(result) : null; } return null; }; return memo(get, new Map()); }; }; var createCompoundGetFunctionFromSuperSet = function createCompoundGetFunctionFromSuperSet(elementGet) { return function (compoundSuperSet) { return function (name) { return createGetFunctionFromSet$1(elementGet)(compoundSuperSet[name]); }; }; }; var createGetFunctionsFromCompounds = function createGetFunctionsFromCompounds(elementGet) { return function (compounds) { return Object.keys(compounds).reduce(function (accumulator, name) { var compound = compounds[name]; accumulator[name] = isSet(compound) ? createGetFunctionFromSet$1(elementGet)(compound) : createCompoundGetFunctionFromSuperSet(elementGet)(compound); return accumulator; }, {}); }; }; var createGetFunctionFromSet$2 = function createGetFunctionFromSet(elementGet) { return function (compoundGet) { return function (mixtureSet) { var get = function get(key) { if (!isValidArrayWithItems(mixtureSet.set)) { return null; } var index = getSetValueIndex(mixtureSet)(key); var value = isValidArrayIndex(index) ? mixtureSet.set[index] : null; if (typeof value === 'function') { var result = value(elementGet, compoundGet); return isStringOrNumber(result) ? toString(result) : null; } return null; }; return memo(get, new Map()); }; }; }; var createGetFunctionFromSuperSet$1 = function createGetFunctionFromSuperSet(elementGet) { return function (compoundGet) { return function (mixtureSuperSet) { return function (name) { return createGetFunctionFromSet$2(elementGet)(compoundGet)(mixtureSuperSet[name]); }; }; }; }; var createGetFunctionsFromMixtures = function createGetFunctionsFromMixtures(elementGet) { return function (compoundGet) { return function (mixtures) { return Object.keys(mixtures).reduce(function (accumulator, name) { var mixture = mixtures[name]; accumulator[name] = isSet(mixture) ? createGetFunctionFromSet$2(elementGet)(compoundGet)(mixture) : createGetFunctionFromSuperSet$1(elementGet)(compoundGet)(mixture); return accumulator; }, {}); }; }; }; var createTheme = function createTheme(elements) { var elementGet = createGetFunctionsFromElements(elements); return function () { var compounds = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {}; var compoundGet = createGetFunctionsFromCompounds(elementGet)(compounds); return function () { var mixtures = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {}; var mixtureGet = createGetFunctionsFromMixtures(elementGet)(compoundGet)(mixtures); return { elements: elementGet, compounds: compoundGet, mixtures: mixtureGet, original: { elements: elements, compounds: compounds, mixtures: mixtures } }; }; }; }; const StyleChemistryContext = createContext(null); const StyleChemistryProvider = ({ children, theme }) => (React.createElement(StyleChemistryContext.Provider, { value: theme }, React.createElement(ThemeProvider, { theme: theme }, children))); const useStyleChemistryTheme = () => useContext(StyleChemistryContext); var PROPS_TO_STYLE_MAP_DEFAULT_CONFIG = { enableBreakpointMapping: true, mediaRule: function mediaRule(a) { return "@media only screen and (min-width: ".concat(a, ")"); } }; var isSuperSetFunctionValueArray = function isSuperSetFunctionValueArray(value) { return Array.isArray(value) && value.length === 2 && typeof value[0] === 'string' && isStringNumberOrNull(value[1]); }; var mapStylePropertiesToValue = function mapStylePropertiesToValue(styleProperties) { return function (value) { return isStringOrNumber(value) ? styleProperties.map(function (property) { return "".concat(property, ": ").concat(value, ";"); }).join("\n") : null; }; }; var computePropValueWithSuperSetGetFunction = function computePropValueWithSuperSetGetFunction(get) { return function (value) { var result = isSuperSetFunctionValueArray(value) ? isStringOrNumber(value[1]) ? get(value[0])(value[1]) : get(value[0])() : typeof value === 'string' ? get(value)() : null; return result !== null ? toString(result) : null; }; }; var computePropValueWithSetGetFunction = function computePropValueWithSetGetFunction(get) { return function (value) { var result = isStringOrNumber(value) ? get(value) : get(); return result !== null ? toString(result) : null; }; }; var mapSettingIsSuperSet = function mapSettingIsSuperSet(mapSetting) { return 'isSuperSet' in mapSetting && typeof mapSetting.isSuperSet === 'boolean' && mapSetting.isSuperSet === true; }; var mapPropToStyleWithBreakpoints = function mapPropToStyleWithBreakpoints(mapSetting) { var compute = mapSettingHasGetFunction(mapSetting) ? mapSettingIsSuperSet(mapSetting) ? computePropValueWithSuperSetGetFunction(mapSetting.get) : computePropValueWithSetGetFunction(mapSetting.get) : function (a) { return a; }; return function (value) { var result = toArray(value).map(function (a) { return compute(a); }); return mapSetting.styleProperties ? result.map(mapStylePropertiesToValue(mapSetting.styleProperties)) : result; }; }; var mapSettingHasGetFunction = function mapSettingHasGetFunction(mapSetting) { return 'get' in mapSetting && typeof mapSetting.get === 'function'; }; var mapPropToStyle = function mapPropToStyle(mapSetting) { var compute = mapSettingHasGetFunction(mapSetting) ? mapSettingIsSuperSet(mapSetting) ? computePropValueWithSuperSetGetFunction(mapSetting.get) : computePropValueWithSetGetFunction(mapSetting.get) : function (a) { return a; }; return function (value) { return mapSetting.styleProperties ? mapStylePropertiesToValue(mapSetting.styleProperties)(compute(value)) : compute(value); }; }; var createStyleProps = function createStyleProps() { var config = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : PROPS_TO_STYLE_MAP_DEFAULT_CONFIG; return function (map) { return function (theme) { return function (props) { var mapArray = map(theme); if (config.enableBreakpointMapping && isValidArrayWithItems(props.breakpoints)) { var resolvedBreakpoints = props.breakpoints.map(function (b) { return theme.elements.breakpoint(b) || b; }).sort(); var styleValues = mapArray.reduce(function (result, setting) { setting.propNames.forEach(function (name) { typeof props[name] !== 'undefined' && result.push(mapPropToStyleWithBreakpoints(setting)(props[name])); }); return result; }, []); return resolvedBreakpoints.reduce(function (result, breakpoint, index) { if (isStringOrNumber(breakpoint)) { var style = styleValues.map(function (style) { return style[index + 1]; }).join("\n"); if (style.trim()) { result += "\n ".concat(config.mediaRule(toString(breakpoint)), " {\n ").concat(style, "\n }\n "); } } return result; }, styleValues.map(function (style) { return style[0]; }).join("\n")); } return mapArray.reduce(function (result, setting) { setting.propNames.forEach(function (name) { typeof props[name] !== 'undefined' && result.push(mapPropToStyle(setting)(props[name])); }); return result; }, []).join("\n"); }; }; }; }; var mapPropsToStyles = function mapPropsToStyles(theme) { return [{ propNames: ['background'], styleProperties: ['background'] }, { get: theme.elements.image, propNames: ['backgroundImage'], styleProperties: ['background-image'] }, { propNames: ['backgroundSize'], styleProperties: ['background-size'] }, { propNames: ['backgroundPosition'], styleProperties: ['background-position'] }, { propNames: ['backgroundRepeat'], styleProperties: ['background-repeat'] }]; }; var background = createStyleProps()(mapPropsToStyles); var mapPropsToStyles$1 = function mapPropsToStyles(theme) { return [{ propNames: ['border'], styleProperties: ['border'] }, { get: theme.elements.borderWidth, propNames: ['borderWidth'], styleProperties: ['border-width'] }, { get: theme.elements.borderStyle, propNames: ['borderStyle'], styleProperties: ['border-style'] }, { get: theme.elements.color, isSuperSet: true, propNames: ['borderColor'], styleProperties: ['border-color'] }, { get: theme.elements.radius, propNames: ['borderRadius'], styleProperties: ['border-radius'] }, { propNames: ['borderTop'], styleProperties: ['border-top'] }, { get: theme.elements.borderWidth, propNames: ['borderTopWidth'], styleProperties: ['border-top-width'] }, { get: theme.elements.borderStyle, propNames: ['borderTopStyle'], styleProperties: ['border-top-style'] }, { get: theme.elements.color, isSuperSet: true, propNames: ['borderTopColor'], styleProperties: ['border-top-color'] }, { get: theme.elements.radius, propNames: ['borderTopLeftRadius'], styleProperties: ['border-top-left-radius'] }, { get: theme.elements.radius, propNames: ['borderTopRightRadius'], styleProperties: ['border-top-right-radius'] }, { propNames: ['borderRight'], styleProperties: ['border-right'] }, { get: theme.elements.borderWidth, propNames: ['borderRightWidth'], styleProperties: ['border-right-width'] }, { get: theme.elements.borderStyle, propNames: ['borderRightStyle'], styleProperties: ['border-right-style'] }, { get: theme.elements.color, isSuperSet: true, propNames: ['borderRightColor'], styleProperties: ['border-right-color'] }, { propNames: ['borderBottom'], styleProperties: ['border-bottom'] }, { get: theme.elements.borderWidth, propNames: ['borderBottomWidth'], styleProperties: ['border-bottom-width'] }, { get: theme.elements.borderStyle, propNames: ['borderBottomStyle'], styleProperties: ['border-bottom-style'] }, { get: theme.elements.color, isSuperSet: true, propNames: ['borderBottomColor'], styleProperties: ['border-bottom-color'] }, { get: theme.elements.radius, propNames: ['borderBottomLeftRadius'], styleProperties: ['border-bottom-left-radius'] }, { get: theme.elements.radius, propNames: ['borderBottomRightRadius'], styleProperties: ['border-bottom-right-radius'] }, { propNames: ['borderLeft'], styleProperties: ['border-left'] }, { get: theme.elements.borderWidth, propNames: ['borderLeftWidth'], styleProperties: ['border-left-width'] }, { get: theme.elements.borderStyle, propNames: ['borderLeftStyle'], styleProperties: ['border-left-style'] }, { get: theme.elements.color, isSuperSet: true, propNames: ['borderLeftColor'], styleProperties: ['border-left-color'] }, { propNames: ['borderX'], styleProperties: ['border-left', 'border-right'] }, { propNames: ['borderY'], styleProperties: ['border-top', 'border-bottom'] }]; }; var border = createStyleProps()(mapPropsToStyles$1); var mapPropsToStyles$2 = function mapPropsToStyles(theme) { return [{ get: theme.elements.color, isSuperSet: true, propNames: ['color'], styleProperties: ['color'] }, { get: theme.elements.color, isSuperSet: true, propNames: ['bg', 'backgroundColor'], styleProperties: ['background-color'] }, { get: theme.elements.opacity, isSuperSet: false, propNames: ['opacity'], styleProperties: ['opacity'] }]; }; var color = createStyleProps()(mapPropsToStyles$2); var mapPropsToStyles$3 = function mapPropsToStyles(theme) { return [{ propNames: ['alignItems'], styleProperties: ['align-items'] }, { propNames: ['alignContent'], styleProperties: ['align-content'] }, { propNames: ['justifyItems'], styleProperties: ['justify-items'] }, { propNames: ['justifyContent'], styleProperties: ['justify-content'] }, { propNames: ['flexWrap'], styleProperties: ['flex-wrap'] }, { propNames: ['flexDirection'], styleProperties: ['flex-direction'] }, { propNames: ['flex'], styleProperties: ['flex'] }, { propNames: ['flexGrow'], styleProperties: ['flex-grow'] }, { propNames: ['flexShrink'], styleProperties: ['flex-shrink'] }, { propNames: ['flexBasis'], styleProperties: ['flex-basis'] }, { propNames: ['justifySelf'], styleProperties: ['justify-self'] }, { propNames: ['alignSelf'], styleProperties: ['align-self'] }, { propNames: ['order'], styleProperties: ['order'] }]; }; var flexbox = createStyleProps()(mapPropsToStyles$3); var mapPropsToStyles$4 = function mapPropsToStyles(theme) { return [{ get: theme.elements.space, isSuperSet: false, propNames: ['gridGap'], styleProperties: ['grid-gap'] }, { get: theme.elements.space, isSuperSet: false, propNames: ['gridRowGap'], styleProperties: ['grid-row-gap'] }, { get: theme.elements.space, isSuperSet: false, propNames: ['gridColumnGap'], styleProperties: ['grid-column-gap'] }, { propNames: ['gridColumn'], styleProperties: ['grid-column'] }, { propNames: ['gridRow'], styleProperties: ['grid-row'] }, { propNames: ['gridArea'], styleProperties: ['grid-area'] }, { propNames: ['gridAutoFlow'], styleProperties: ['grid-auto-flow'] }, { propNames: ['gridAutoRows'], styleProperties: ['grid-auto-rows'] }, { propNames: ['gridAutoColumns'], styleProperties: ['grid-auto-columns'] }, { propNames: ['gridTemplateRows'], styleProperties: ['grid-template-rows'] }, { propNames: ['gridTemplateColumns'], styleProperties: ['grid-template-columns'] }, { propNames: ['gridTemplateAreas'], styleProperties: ['grid-template-areas'] }]; }; var grid = createStyleProps()(mapPropsToStyles$4); var mapPropsToStyles$5 = function mapPropsToStyles(theme) { return [{ get: theme.elements.size, propNames: ['width'], styleProperties: ['width'] }, { get: theme.elements.size, propNames: ['height'], styleProperties: ['height'] }, { get: theme.elements.size, propNames: ['minWidth'], styleProperties: ['min-width'] }, { get: theme.elements.size, propNames: ['maxWidth'], styleProperties: ['max-width'] }, { get: theme.elements.size, propNames: ['minHeight'], styleProperties: ['min-height'] }, { get: theme.elements.size, propNames: ['maxHeight'], styleProperties: ['max-height'] }, { get: theme.elements.size, propNames: ['size'], styleProperties: ['width', 'height'] }, { propNames: ['display'], styleProperties: ['display'] }, { propNames: ['verticalAlign'], styleProperties: ['vertical-align'] }, { propNames: ['overflow'], styleProperties: ['overflow'] }, { propNames: ['overflowX'], styleProperties: ['overflow-x'] }, { propNames: ['overflowY'], styleProperties: ['overflow-y'] }]; }; var layout = createStyleProps()(mapPropsToStyles$5); var mapPropsToStyles$6 = function mapPropsToStyles(theme) { return [{ propNames: ['position'], styleProperties: ['position'] }, { get: theme.elements.zIndex, propNames: ['zIndex'], styleProperties: ['z-index'] }, { get: theme.elements.space, propNames: ['top'], styleProperties: ['top'] }, { get: theme.elements.space, propNames: ['right'], styleProperties: ['right'] }, { get: theme.elements.space, propNames: ['bottom'], styleProperties: ['bottom'] }, { get: theme.elements.space, propNames: ['left'], styleProperties: ['left'] }]; }; var position = createStyleProps()(mapPropsToStyles$6); var mapPropsToStyles$7 = function mapPropsToStyles(theme) { return [{ propNames: ['text-shadow'], styleProperties: ['text-shadow'] }, { propNames: ['box-shadow'], styleProperties: ['box-shadow'] }]; }; var shadow = createStyleProps()(mapPropsToStyles$7); var mapPropsToStyles$8 = function mapPropsToStyles(theme) { return [{ get: theme.elements.space, propNames: ['m', 'margin'], styleProperties: ['margin'] }, { get: theme.elements.space, propNames: ['mx', 'marginX'], styleProperties: ['margin-left', 'margin-right'] }, { get: theme.elements.space, propNames: ['my', 'marginY'], styleProperties: ['margin-top', 'margin-bottom'] }, { get: theme.elements.space, propNames: ['mt', 'marginTop'], styleProperties: ['margin-top'] }, { get: theme.elements.space, propNames: ['mb', 'marginBottom'], styleProperties: ['margin-bottom'] }, { get: theme.elements.space, propNames: ['ml', 'marginLeft'], styleProperties: ['margin-left'] }, { propNames: ['mr', 'marginRight'], get: theme.elements.space, styleProperties: ['margin-right'] }, { get: theme.elements.space, propNames: ['p', 'padding'], styleProperties: ['padding'] }, { get: theme.elements.space, propNames: ['px', 'paddingX'], styleProperties: ['padding-left', 'padding-right'] }, { get: theme.elements.space, propNames: ['py', 'paddingY'], styleProperties: ['padding-top', 'padding-bottom'] }, { get: theme.elements.space, propNames: ['pl', 'paddingLeft'], styleProperties: ['padding-left'] }, { get: theme.elements.space, propNames: ['pr', 'paddingRight'], styleProperties: ['padding-right'] }]; }; var space = createStyleProps()(mapPropsToStyles$8); var mapPropsToStyles$9 = function mapPropsToStyles(theme) { return [{ get: theme.elements.fontFamily, propNames: ['fontFamily'], styleProperties: ['font-family'] }, { get: theme.elements.fontSize, propNames: ['fontSize'], styleProperties: ['font-size'] }, { get: theme.elements.fontWeight, propNames: ['fontWeight'], styleProperties: ['font-weight'] }, { get: theme.elements.lineHeight, propNames: ['lineHeight'], styleProperties: ['line-height'] }, { get: theme.elements.letterSpacing, propNames: ['letterSpacing'], styleProperties: ['letter-spacing'] }, { propNames: ['textAlign'], styleProperties: ['text-align'] }, { propNames: ['fontStyle'], styleProperties: ['font-style'] }]; }; var typography = createStyleProps()(mapPropsToStyles$9); export { PROPS_TO_STYLE_MAP_DEFAULT_CONFIG, StyleChemistryContext, StyleChemistryProvider, background, border, color, createStyleProps, createTheme, flexbox, grid, layout, position, shadow, space, typography, useStyleChemistryTheme }; //# sourceMappingURL=styled-chemistry.js.map