UNPKG

@dnb/eufemia

Version:

DNB Eufemia Design System UI Library

108 lines (107 loc) 5.08 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 = require("react"); var _pluginScopeHash = require("../plugins/postcss-isolated-style-scope/plugin-scope-hash.js"); var _BuildInfo = require("./build-info/BuildInfo.js"); var _jsxRuntime = require("react/jsx-runtime"); const IsolatedStyleScopeContext = exports.IsolatedStyleScopeContext = (0, _react.createContext)(undefined); const parentScopeContextMap = new Map(); function IsolatedStyleScope(props) { const styleScopeContext = (0, _react.useContext)(IsolatedStyleScopeContext); const { scopeHash = 'auto', disableCoreStyleWrapper = false, uniqueKey = 'default', ref: refProp, children, style } = props; const localRef = (0, _react.useRef)(undefined); const scopeElementRef = refProp || 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 (0, _jsxRuntime.jsx)(IsolatedStyleScopeContext, { value: { scopeHash, generatedScopeHash, disableCoreStyleWrapper, style, scopeElementRef, internalKeys: internalKeys, parentContextMap }, children: (0, _jsxRuntime.jsx)("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, children: disableCoreStyleWrapper ? children : (0, _jsxRuntime.jsx)("div", { className: "dnb-core-style", children: 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