UNPKG

@dnb/eufemia

Version:

DNB Eufemia Design System UI Library

105 lines (104 loc) 5.61 kB
"use strict"; "use client"; Object.defineProperty(exports, "__esModule", { value: true }); exports.IsolatedStyleScopeContext = void 0; exports.default = IsolatedStyleScope; exports.getCurrentStyleScopeElement = getCurrentStyleScopeElement; Object.defineProperty(exports, "getStyleScopeHash", { enumerable: true, get: function () { return _pluginScopeHash.getStyleScopeHash; } }); exports.useIsolatedStyleScope = useIsolatedStyleScope; var _react = _interopRequireWildcard(require("react")); var _pluginScopeHash = require("../plugins/postcss-isolated-style-scope/plugin-scope-hash.js"); var _BuildInfo = require("./build-info/BuildInfo.js"); function _interopRequireWildcard(e, t) { if ("function" == typeof WeakMap) var r = new WeakMap(), n = new WeakMap(); return (_interopRequireWildcard = function (e, t) { if (!t && e && e.__esModule) return e; var o, i, f = { __proto__: null, default: e }; if (null === e || "object" != typeof e && "function" != typeof e) return f; if (o = t ? n : r) { if (o.has(e)) return o.get(e); o.set(e, f); } for (const t in e) "default" !== t && {}.hasOwnProperty.call(e, t) && ((i = (o = Object.defineProperty) && Object.getOwnPropertyDescriptor(e, t)) && (i.get || i.set) ? o(f, t, i) : f[t] = e[t]); return f; })(e, t); } const IsolatedStyleScopeContext = exports.IsolatedStyleScopeContext = _react.default.createContext(undefined); const parentScopeContextMap = new Map(); function IsolatedStyleScope(props) { const styleScopeContext = (0, _react.useContext)(IsolatedStyleScopeContext); const { scopeHash = 'auto', disableCoreStyleWrapper = false, uniqueKey = 'default', innerRef, children, style } = props; const localRef = (0, _react.useRef)(); const scopeElementRef = innerRef || localRef; const generatedScopeHash = scopeHash === 'auto' ? (styleScopeContext === null || styleScopeContext === void 0 ? void 0 : styleScopeContext.generatedScopeHash) || (0, _pluginScopeHash.getStyleScopeHash)() : scopeHash; const isNestedWithNewScope = styleScopeContext && styleScopeContext.generatedScopeHash && styleScopeContext.generatedScopeHash !== generatedScopeHash; const parentContextMap = (styleScopeContext === null || styleScopeContext === void 0 ? void 0 : styleScopeContext.parentContextMap) || parentScopeContextMap; if (styleScopeContext !== null && styleScopeContext !== void 0 && styleScopeContext.generatedScopeHash) { parentContextMap.set(styleScopeContext.generatedScopeHash, styleScopeContext); } if (styleScopeContext !== null && styleScopeContext !== void 0 && styleScopeContext.generatedScopeHash ? props.scopeHash : true) { var _styleScopeContext$in; if (uniqueKey === false || !(styleScopeContext !== null && styleScopeContext !== void 0 && (_styleScopeContext$in = styleScopeContext.internalKeys) !== null && _styleScopeContext$in !== void 0 && _styleScopeContext$in.has(uniqueKey)) || isNestedWithNewScope) { var _styleScopeContext$sc; const internalKeys = new Set((styleScopeContext === null || styleScopeContext === void 0 ? void 0 : styleScopeContext.internalKeys) || []); if (typeof uniqueKey === 'string') { internalKeys.add(uniqueKey); } return _react.default.createElement(IsolatedStyleScopeContext.Provider, { value: { scopeHash, generatedScopeHash, disableCoreStyleWrapper, style, scopeElementRef, internalKeys: internalKeys, parentContextMap } }, _react.default.createElement("div", { "data-scope-hash": scopeHash === 'auto' ? (_styleScopeContext$sc = styleScopeContext === null || styleScopeContext === void 0 ? void 0 : styleScopeContext.scopeHash) !== null && _styleScopeContext$sc !== void 0 ? _styleScopeContext$sc : scopeHash : scopeHash, "data-scope-hash-id": uniqueKey || undefined, "data-scope-sha": uniqueKey ? (0, _BuildInfo.getSha)() : undefined, className: generatedScopeHash, style: style || (styleScopeContext === null || styleScopeContext === void 0 ? void 0 : styleScopeContext.style), ref: scopeElementRef }, disableCoreStyleWrapper ? children : _react.default.createElement("div", { className: "dnb-core-style" }, children))); } } return children; } function getCurrentStyleScopeElement(currentElement, scopeHash = 'auto', fallback = null) { if (scopeHash === 'auto') { scopeHash = (0, _pluginScopeHash.getStyleScopeHash)(); } if (typeof window === 'undefined') { return undefined; } if (scopeHash) { return currentElement.closest(`.${scopeHash}`); } return fallback || document.body; } function useIsolatedStyleScope(scopeHash) { const styleScopeContext = (0, _react.useContext)(IsolatedStyleScopeContext); const { scopeElementRef, generatedScopeHash, parentContextMap } = styleScopeContext || {}; const getScopeElement = (0, _react.useCallback)(() => { var _parentCtx$scopeEleme; if (!scopeHash || scopeHash === generatedScopeHash) { return scopeElementRef === null || scopeElementRef === void 0 ? void 0 : scopeElementRef.current; } const map = parentContextMap || parentScopeContextMap; const parentCtx = map.get(scopeHash); return parentCtx === null || parentCtx === void 0 || (_parentCtx$scopeEleme = parentCtx.scopeElementRef) === null || _parentCtx$scopeEleme === void 0 ? void 0 : _parentCtx$scopeEleme.current; }, [scopeElementRef, generatedScopeHash, parentContextMap, scopeHash]); return { getScopeElement }; } //# sourceMappingURL=IsolatedStyleScope.js.map