UNPKG

@zenkit/styles

Version:

ZenKit components for impliments styles

432 lines (349 loc) 15.6 kB
(function (global, factory) { typeof exports === 'object' && typeof module !== 'undefined' ? factory(exports, require('jss-plugin-rule-value-function'), require('jss-plugin-global'), require('jss-plugin-nested'), require('jss-plugin-camel-case'), require('jss-plugin-default-unit'), require('jss-plugin-vendor-prefixer'), require('jss-plugin-props-sort'), require('@emotion/hash'), require('react'), require('jss'), require('hoist-non-react-statics')) : typeof define === 'function' && define.amd ? define(['exports', 'jss-plugin-rule-value-function', 'jss-plugin-global', 'jss-plugin-nested', 'jss-plugin-camel-case', 'jss-plugin-default-unit', 'jss-plugin-vendor-prefixer', 'jss-plugin-props-sort', '@emotion/hash', 'react', 'jss', 'hoist-non-react-statics'], factory) : (global = global || self, factory(global.styles = {}, global.jssPluginRuleValueFunction, global.jssPluginGlobal, global.jssPluginNested, global.jssPluginCamelCase, global.jssPluginDefaultUnit, global.jssPluginVendorPrefixer, global.jssPluginPropsSort, global.emotionHash, global.react, global.jss, global.hoistNonReactStatics)); }(this, function (exports, functions, global$2, nested, camelCase, defaultUnit, vendorPrefixer, propsSort, hash, React, jss$1, hoistNonReactStatic) { 'use strict'; functions = functions && functions.hasOwnProperty('default') ? functions['default'] : functions; global$2 = global$2 && global$2.hasOwnProperty('default') ? global$2['default'] : global$2; nested = nested && nested.hasOwnProperty('default') ? nested['default'] : nested; camelCase = camelCase && camelCase.hasOwnProperty('default') ? camelCase['default'] : camelCase; defaultUnit = defaultUnit && defaultUnit.hasOwnProperty('default') ? defaultUnit['default'] : defaultUnit; vendorPrefixer = vendorPrefixer && vendorPrefixer.hasOwnProperty('default') ? vendorPrefixer['default'] : vendorPrefixer; propsSort = propsSort && propsSort.hasOwnProperty('default') ? propsSort['default'] : propsSort; hash = hash && hash.hasOwnProperty('default') ? hash['default'] : hash; React = React && React.hasOwnProperty('default') ? React['default'] : React; hoistNonReactStatic = hoistNonReactStatic && hoistNonReactStatic.hasOwnProperty('default') ? hoistNonReactStatic['default'] : hoistNonReactStatic; function createDefaultTheme() { return {}; } function createDefaultJssPreset() { return { plugins: [functions(), global$2(), nested(), camelCase(), defaultUnit(), typeof window === 'undefined' ? null : vendorPrefixer(), propsSort()] }; } var global$1 = (typeof global !== "undefined" ? global : typeof self !== "undefined" ? self : typeof window !== "undefined" ? window : {}); if (typeof global$1.setTimeout === 'function') ; if (typeof global$1.clearTimeout === 'function') ; // from https://github.com/kumavis/browser-process-hrtime/blob/master/index.js var performance = global$1.performance || {}; var performanceNow = performance.now || performance.mozNow || performance.msNow || performance.oNow || performance.webkitNow || function(){ return (new Date()).getTime() }; var escapeRegex = /([[\].#*$><+~=|^:(),"'`\s])/g; function safePrefix(classNamePrefix) { var prefix = String(classNamePrefix); return prefix.replace(escapeRegex, '-'); } var themeHashCache = {}; function createGenerateClassName() { var options = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {}; var _options$dangerouslyU = options.dangerouslyUseGlobalCSS, dangerouslyUseGlobalCSS = _options$dangerouslyU === void 0 ? false : _options$dangerouslyU, _options$productionPr = options.productionPrefix, productionPrefix = _options$productionPr === void 0 ? 'Z' : _options$productionPr, _options$seed = options.seed, seed = _options$seed === void 0 ? '' : _options$seed; var ruleCounter = 0; return function (rule, styleSheet) { var isStatic = !styleSheet.options.link; if (dangerouslyUseGlobalCSS && styleSheet && styleSheet.options.name && isStatic) { return "".concat(safePrefix(styleSheet.options.name), "-").concat(rule.key); } var suffix; if (isStatic) { var themeHash = themeHashCache[styleSheet.options.theme]; if (!themeHash) { themeHash = hash(JSON.stringify(styleSheet.options.theme)); themeHashCache[styleSheet.options.theme] = themeHash; } var raw = styleSheet.rules.raw[rule.key]; suffix = hash("".concat(themeHash).concat(rule.key).concat(JSON.stringify(raw))); } if (!suffix) { ruleCounter += 1; suffix = ruleCounter; } { if (styleSheet && styleSheet.options.classNamePrefix) { return "".concat(safePrefix(styleSheet.options.classNamePrefix), "-").concat(rule.key, "-").concat(seed).concat(suffix); } return "".concat(rule.key, "-").concat(seed).concat(suffix); } return "".concat(productionPrefix).concat(seed).concat(suffix); }; } function _objectWithoutPropertiesLoose(source, excluded) { if (source == null) return {}; var target = {}; var sourceKeys = Object.keys(source); var key, i; for (i = 0; i < sourceKeys.length; i++) { key = sourceKeys[i]; if (excluded.indexOf(key) >= 0) continue; target[key] = source[key]; } return target; } var objectWithoutPropertiesLoose = _objectWithoutPropertiesLoose; function _objectWithoutProperties(source, excluded) { if (source == null) return {}; var target = objectWithoutPropertiesLoose(source, excluded); var key, i; if (Object.getOwnPropertySymbols) { var sourceSymbolKeys = Object.getOwnPropertySymbols(source); for (i = 0; i < sourceSymbolKeys.length; i++) { key = sourceSymbolKeys[i]; if (excluded.indexOf(key) >= 0) continue; if (!Object.prototype.propertyIsEnumerable.call(source, key)) continue; target[key] = source[key]; } } return target; } var objectWithoutProperties = _objectWithoutProperties; function createCommonjsModule(fn, module) { return module = { exports: {} }, fn(module, module.exports), module.exports; } var _extends_1 = createCommonjsModule(function (module) { function _extends() { module.exports = _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); } module.exports = _extends; }); var jss = jss$1.create(createDefaultJssPreset()); var generateClassName = createGenerateClassName(); var disableGeneration = false; var theme = createDefaultTheme(); var cache = typeof window === 'undefined' ? new Map() : null; var manager = new Map(); var registry = null; var StylesContext = React.createContext({ jss: jss, generateClassName: generateClassName, disableGeneration: disableGeneration, theme: theme, cache: cache, manager: manager, registry: registry }); function mergeOuterLocalTheme(outerTheme, localTheme) { if (typeof localTheme === 'function') { return localTheme(outerTheme); } return _extends_1({}, outerTheme, localTheme); } function StylesProvider(_ref) { var children = _ref.children, localTheme = _ref.theme, localOptions = objectWithoutProperties(_ref, ["children", "theme"]); if (!children) return; return React.createElement(StylesContext.Consumer, null, function (outerOptions) { var theme = localTheme ? mergeOuterLocalTheme(outerOptions.theme, localTheme) : outerOptions.theme; return React.createElement(StylesContext.Provider, { value: _extends_1({}, outerOptions, localOptions, { theme: theme }) }, children); }); } var counter = -1e9; function increment() { counter += 1; return counter; } var fnNameMatchRegex = /^\s*function(?:\s|\s*\/\*.*\*\/\s*)+([^(\s/]*)\s*/; function getFunctionName(fn) { var match = "".concat(fn).match(fnNameMatchRegex); var name = match && match[1]; return name || ''; } function getDisplayName(Component) { if (!Component) return undefined; if (typeof Component === 'string') return Component; return Component.displayName || Component.name || getFunctionName(Component) || 'Component'; } var multiKeyStore = { set: function set(cache, key1, key2, value) { var subCache = cache.get(key1); if (!subCache) { subCache = new Map(); cache.set(key1, subCache); } subCache.set(key2, value); }, get: function get(cache, key1, key2) { var subCache = cache.get(key1); return subCache ? subCache.get(key2) : undefined; }, delete: function _delete(cache, key1, key2) { var subCache = cache.get(key1); subCache.delete(key2); } }; var _typeof_1 = createCommonjsModule(function (module) { function _typeof2(obj) { if (typeof Symbol === "function" && typeof Symbol.iterator === "symbol") { _typeof2 = function _typeof2(obj) { return typeof obj; }; } else { _typeof2 = function _typeof2(obj) { return obj && typeof Symbol === "function" && obj.constructor === Symbol && obj !== Symbol.prototype ? "symbol" : typeof obj; }; } return _typeof2(obj); } function _typeof(obj) { if (typeof Symbol === "function" && _typeof2(Symbol.iterator) === "symbol") { module.exports = _typeof = function _typeof(obj) { return _typeof2(obj); }; } else { module.exports = _typeof = function _typeof(obj) { return obj && typeof Symbol === "function" && obj.constructor === Symbol && obj !== Symbol.prototype ? "symbol" : _typeof2(obj); }; } return _typeof(obj); } module.exports = _typeof; }); function mergeClasses() { for (var _len = arguments.length, classes = new Array(_len), _key = 0; _key < _len; _key++) { classes[_key] = arguments[_key]; } return classes.filter(function (c) { return _typeof_1(c) === 'object'; }).reduce(function (acc, curr) { Object.keys(curr).forEach(function (key) { acc[key] = [acc[key] || '', curr[key]].join(' ').trim(); }); return acc; }, {}); } var noopTheme = {}; var withStyles = function withStyles(stylesOrCreator) { var options = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {}; return function (WrappedComponent) { var optionsName = options.name, optionsMeta = options.meta, optionsMedia = options.media, optionsClassNamePrefix = options.classNamePrefix, _options$theme = options.theme, optionsDefaultTheme = _options$theme === void 0 ? noopTheme : _options$theme, optionsDisableGeneration = options.disableGeneration; var listenToTheme = typeof stylesOrCreator === 'function' || typeof optionsName === 'string' || !!Object.keys(optionsDefaultTheme).length; var meta = optionsMeta || optionsName || getDisplayName(WrappedComponent); var defaultJssOptions = { index: increment(), name: optionsName, meta: meta, media: optionsMedia, classNamePrefix: optionsClassNamePrefix || meta }; function withStylesHOC(props) { var context = React.useContext(StylesContext); var classes = props.classes || {}; if (!(context.disableGeneration && optionsDisableGeneration)) { var dynamicSheet; var theme = listenToTheme ? _extends_1({}, optionsDefaultTheme, context.theme) : noopTheme; var jssOptions = _extends_1({}, defaultJssOptions, { theme: theme, media: optionsMedia || context.media || '', generateClassName: context.generateClassName }); var styles = typeof stylesOrCreator === 'function' ? stylesOrCreator(theme) : stylesOrCreator; var manager = multiKeyStore.get(context.manager, styles, theme); if (!manager) { manager = { refs: 0, static: null, dynamic: null }; multiKeyStore.set(context.manager, styles, theme, manager); } var registry = context.registry; classes = React.useMemo(function () { var localClasses; if (manager.refs === 0) { var staticSheet; if (context.cache) { staticSheet = multiKeyStore.get(context.cache, styles, theme); } if (!staticSheet) { staticSheet = context.jss.createStyleSheet(styles, _extends_1({ link: false }, jssOptions)); staticSheet.attach(); if (context.cache) { multiKeyStore.set(context.cache, styles, theme, staticSheet); } } if (registry) { registry.add(staticSheet); } manager.static = staticSheet; manager.dynamic = jss$1.getDynamicStyles(styles); } if (manager.dynamic) { var dynamic = context.jss.createStyleSheet(manager.dynamic, _extends_1({ link: true }, jssOptions)); dynamic.update(props).attach(); dynamicSheet = dynamic; if (registry) { registry.add(dynamic); } localClasses = mergeClasses(manager.static.classes, dynamic.classes); } else { localClasses = manager.static.classes; } manager.refs += 1; return mergeClasses(localClasses, classes); }, [props, context]); React.useEffect(function () { if (dynamicSheet.update) { dynamicSheet.update(props); } return function () { manager.refs -= 1; if (manager.refs === 0) { multiKeyStore.delete(context.manager, styles, theme); context.jss.removeStyleSheet(manager.static); if (registry) { registry.remove(manager.static); } } if (dynamicSheet.update) { context.jss.removeStyleSheet(dynamicSheet); if (registry) { registry.remove(dynamicSheet); } } }; }); } return React.createElement(WrappedComponent, _extends_1({}, props, { classes: classes })); } hoistNonReactStatic(withStylesHOC, WrappedComponent); { withStylesHOC.displayName = "WithStyles(".concat(getDisplayName(WrappedComponent), ")"); withStylesHOC.Wrapped = WrappedComponent; } return withStylesHOC; }; }; exports.createDefaultTheme = createDefaultTheme; exports.createDefaultJssPreset = createDefaultJssPreset; exports.createGenerateClassName = createGenerateClassName; exports.StylesProvider = StylesProvider; exports.withStyles = withStyles; Object.defineProperty(exports, '__esModule', { value: true }); }));