UNPKG

@dnb/eufemia

Version:

DNB Eufemia Design System UI Library

91 lines (90 loc) 4.46 kB
"use client"; import React, { useCallback, useContext, useRef } from 'react'; import { getStyleScopeHash } from '../plugins/postcss-isolated-style-scope/plugin-scope-hash.js'; import { getSha } from './build-info/BuildInfo.js'; export { getStyleScopeHash }; export const IsolatedStyleScopeContext = React.createContext(undefined); const parentScopeContextMap = new Map(); export default function IsolatedStyleScope(props) { const styleScopeContext = useContext(IsolatedStyleScopeContext); const { scopeHash = 'auto', disableCoreStyleWrapper = false, uniqueKey = 'default', innerRef, children, style } = props; const localRef = useRef(); const scopeElementRef = innerRef || localRef; const generatedScopeHash = scopeHash === 'auto' ? (styleScopeContext === null || styleScopeContext === void 0 ? void 0 : styleScopeContext.generatedScopeHash) || 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.createElement(IsolatedStyleScopeContext.Provider, { value: { scopeHash, generatedScopeHash, disableCoreStyleWrapper, style, scopeElementRef, internalKeys: internalKeys, parentContextMap } }, React.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 ? getSha() : undefined, className: generatedScopeHash, style: style || (styleScopeContext === null || styleScopeContext === void 0 ? void 0 : styleScopeContext.style), ref: scopeElementRef }, disableCoreStyleWrapper ? children : React.createElement("div", { className: "dnb-core-style" }, children))); } } return children; } export function getCurrentStyleScopeElement(currentElement, scopeHash = 'auto', fallback = null) { if (scopeHash === 'auto') { scopeHash = getStyleScopeHash(); } if (typeof window === 'undefined') { return undefined; } if (scopeHash) { return currentElement.closest(`.${scopeHash}`); } return fallback || document.body; } export function useIsolatedStyleScope(scopeHash) { const styleScopeContext = useContext(IsolatedStyleScopeContext); const { scopeElementRef, generatedScopeHash, parentContextMap } = styleScopeContext || {}; const getScopeElement = 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