UNPKG

@ant-design/cssinjs

Version:

Component level cssinjs resolution for antd

87 lines (82 loc) 4.5 kB
"use strict"; var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault"); var _typeof = require("@babel/runtime/helpers/typeof"); Object.defineProperty(exports, "__esModule", { value: true }); exports.StyleProvider = exports.CSS_IN_JS_INSTANCE = exports.ATTR_TOKEN = exports.ATTR_MARK = exports.ATTR_CACHE_PATH = void 0; exports.createCache = createCache; exports.default = void 0; var _objectSpread2 = _interopRequireDefault(require("@babel/runtime/helpers/objectSpread2")); var _objectWithoutProperties2 = _interopRequireDefault(require("@babel/runtime/helpers/objectWithoutProperties")); var _useMemo = _interopRequireDefault(require("rc-util/lib/hooks/useMemo")); var _isEqual = _interopRequireDefault(require("rc-util/lib/isEqual")); var React = _interopRequireWildcard(require("react")); var _Cache = _interopRequireDefault(require("./Cache")); var _excluded = ["children"]; function _getRequireWildcardCache(e) { if ("function" != typeof WeakMap) return null; var r = new WeakMap(), t = new WeakMap(); return (_getRequireWildcardCache = function _getRequireWildcardCache(e) { return e ? t : r; })(e); } function _interopRequireWildcard(e, r) { if (!r && e && e.__esModule) return e; if (null === e || "object" != _typeof(e) && "function" != typeof e) return { default: e }; var t = _getRequireWildcardCache(r); if (t && t.has(e)) return t.get(e); var n = { __proto__: null }, a = Object.defineProperty && Object.getOwnPropertyDescriptor; for (var u in e) if ("default" !== u && Object.prototype.hasOwnProperty.call(e, u)) { var i = a ? Object.getOwnPropertyDescriptor(e, u) : null; i && (i.get || i.set) ? Object.defineProperty(n, u, i) : n[u] = e[u]; } return n.default = e, t && t.set(e, n), n; } var ATTR_TOKEN = exports.ATTR_TOKEN = 'data-token-hash'; var ATTR_MARK = exports.ATTR_MARK = 'data-css-hash'; var ATTR_CACHE_PATH = exports.ATTR_CACHE_PATH = 'data-cache-path'; // Mark css-in-js instance in style element var CSS_IN_JS_INSTANCE = exports.CSS_IN_JS_INSTANCE = '__cssinjs_instance__'; function createCache() { var cssinjsInstanceId = Math.random().toString(12).slice(2); // Tricky SSR: Move all inline style to the head. // PS: We do not recommend tricky mode. if (typeof document !== 'undefined' && document.head && document.body) { var styles = document.body.querySelectorAll("style[".concat(ATTR_MARK, "]")) || []; var firstChild = document.head.firstChild; Array.from(styles).forEach(function (style) { style[CSS_IN_JS_INSTANCE] = style[CSS_IN_JS_INSTANCE] || cssinjsInstanceId; // Not force move if no head if (style[CSS_IN_JS_INSTANCE] === cssinjsInstanceId) { document.head.insertBefore(style, firstChild); } }); // Deduplicate of moved styles var styleHash = {}; Array.from(document.querySelectorAll("style[".concat(ATTR_MARK, "]"))).forEach(function (style) { var hash = style.getAttribute(ATTR_MARK); if (styleHash[hash]) { if (style[CSS_IN_JS_INSTANCE] === cssinjsInstanceId) { var _style$parentNode; (_style$parentNode = style.parentNode) === null || _style$parentNode === void 0 || _style$parentNode.removeChild(style); } } else { styleHash[hash] = true; } }); } return new _Cache.default(cssinjsInstanceId); } var StyleContext = /*#__PURE__*/React.createContext({ hashPriority: 'low', cache: createCache(), defaultCache: true }); var StyleProvider = exports.StyleProvider = function StyleProvider(props) { var children = props.children, restProps = (0, _objectWithoutProperties2.default)(props, _excluded); var parentContext = React.useContext(StyleContext); var context = (0, _useMemo.default)(function () { var mergedContext = (0, _objectSpread2.default)({}, parentContext); Object.keys(restProps).forEach(function (key) { var value = restProps[key]; if (restProps[key] !== undefined) { mergedContext[key] = value; } }); var cache = restProps.cache; mergedContext.cache = mergedContext.cache || createCache(); mergedContext.defaultCache = !cache && parentContext.defaultCache; return mergedContext; }, [parentContext, restProps], function (prev, next) { return !(0, _isEqual.default)(prev[0], next[0], true) || !(0, _isEqual.default)(prev[1], next[1], true); }); return /*#__PURE__*/React.createElement(StyleContext.Provider, { value: context }, children); }; var _default = exports.default = StyleContext;