@salt-ds/styles 0.1.2 → 0.2.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist-cjs/index.js +3 -0
- package/dist-cjs/index.js.map +1 -1
- package/dist-cjs/style-injection-provider/index.js +24 -0
- package/dist-cjs/style-injection-provider/index.js.map +1 -0
- package/dist-cjs/use-style-injection/useStyleInjection.js +3 -1
- package/dist-cjs/use-style-injection/useStyleInjection.js.map +1 -1
- package/dist-es/index.js +1 -0
- package/dist-es/index.js.map +1 -1
- package/dist-es/style-injection-provider/index.js +19 -0
- package/dist-es/style-injection-provider/index.js.map +1 -0
- package/dist-es/use-style-injection/useStyleInjection.js +3 -1
- package/dist-es/use-style-injection/useStyleInjection.js.map +1 -1
- package/dist-types/index.d.ts +1 -0
- package/dist-types/style-injection-provider/index.d.ts +9 -0
- package/package.json +1 -1
package/dist-cjs/index.js
CHANGED
|
@@ -4,10 +4,13 @@ Object.defineProperty(exports, '__esModule', { value: true });
|
|
|
4
4
|
|
|
5
5
|
var useStyleInjection = require('./use-style-injection/useStyleInjection.js');
|
|
6
6
|
var InsertionPointProvider = require('./use-style-injection/InsertionPointProvider.js');
|
|
7
|
+
var index = require('./style-injection-provider/index.js');
|
|
7
8
|
|
|
8
9
|
|
|
9
10
|
|
|
10
11
|
exports.useComponentCssInjection = useStyleInjection.useComponentCssInjection;
|
|
11
12
|
exports.InsertionPointProvider = InsertionPointProvider.InsertionPointProvider;
|
|
12
13
|
exports.useInsertionPoint = InsertionPointProvider.useInsertionPoint;
|
|
14
|
+
exports.StyleInjectionProvider = index.StyleInjectionProvider;
|
|
15
|
+
exports.useStyleInjection = index.useStyleInjection;
|
|
13
16
|
//# sourceMappingURL=index.js.map
|
package/dist-cjs/index.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.js","sources":[],"sourcesContent":[],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"index.js","sources":[],"sourcesContent":[],"names":[],"mappings":";;;;;;;;;;;;;;"}
|
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
Object.defineProperty(exports, '__esModule', { value: true });
|
|
4
|
+
|
|
5
|
+
var jsxRuntime = require('react/jsx-runtime');
|
|
6
|
+
var React = require('react');
|
|
7
|
+
|
|
8
|
+
const StyleInjectionContext = React.createContext(true);
|
|
9
|
+
function useStyleInjection(enableStyleInjection) {
|
|
10
|
+
const enableStyleInjectionFromContext = React.useContext(StyleInjectionContext);
|
|
11
|
+
return enableStyleInjection != null ? enableStyleInjection : enableStyleInjectionFromContext;
|
|
12
|
+
}
|
|
13
|
+
function StyleInjectionProvider(props) {
|
|
14
|
+
const { value: enableStyleInjectionProp, children } = props;
|
|
15
|
+
const value = useStyleInjection(enableStyleInjectionProp);
|
|
16
|
+
return /* @__PURE__ */ jsxRuntime.jsx(StyleInjectionContext.Provider, {
|
|
17
|
+
value,
|
|
18
|
+
children
|
|
19
|
+
});
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
exports.StyleInjectionProvider = StyleInjectionProvider;
|
|
23
|
+
exports.useStyleInjection = useStyleInjection;
|
|
24
|
+
//# sourceMappingURL=index.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.js","sources":["../src/style-injection-provider/index.tsx"],"sourcesContent":["import React, { type ReactNode, createContext, useContext } from \"react\";\nexport interface StyleInjectionContextType {\n value?: boolean;\n}\n\nconst StyleInjectionContext = createContext(true);\n\nexport function useStyleInjection(enableStyleInjection?: boolean): boolean {\n const enableStyleInjectionFromContext = useContext(StyleInjectionContext);\n return enableStyleInjection ?? enableStyleInjectionFromContext;\n}\n\nexport interface StyleInjectionProviderProps extends StyleInjectionContextType {\n children: ReactNode;\n}\n\nexport function StyleInjectionProvider(props: StyleInjectionProviderProps) {\n const { value: enableStyleInjectionProp, children } = props;\n const value = useStyleInjection(enableStyleInjectionProp);\n\n return (\n <StyleInjectionContext.Provider value={value}>\n {children}\n </StyleInjectionContext.Provider>\n );\n}\n"],"names":["createContext","useContext","jsx"],"mappings":";;;;;;;AAKA,MAAM,qBAAA,GAAwBA,oBAAc,IAAI,CAAA,CAAA;AAEzC,SAAS,kBAAkB,oBAAyC,EAAA;AACzE,EAAM,MAAA,+BAAA,GAAkCC,iBAAW,qBAAqB,CAAA,CAAA;AACxE,EAAA,OAAO,oBAAwB,IAAA,IAAA,GAAA,oBAAA,GAAA,+BAAA,CAAA;AACjC,CAAA;AAMO,SAAS,uBAAuB,KAAoC,EAAA;AACzE,EAAA,MAAM,EAAE,KAAA,EAAO,wBAA0B,EAAA,QAAA,EAAa,GAAA,KAAA,CAAA;AACtD,EAAM,MAAA,KAAA,GAAQ,kBAAkB,wBAAwB,CAAA,CAAA;AAExD,EACE,uBAAAC,cAAA,CAAC,sBAAsB,QAAtB,EAAA;AAAA,IAA+B,KAAA;AAAA,IAC7B,QAAA;AAAA,GACH,CAAA,CAAA;AAEJ;;;;;"}
|
|
@@ -4,6 +4,7 @@ Object.defineProperty(exports, '__esModule', { value: true });
|
|
|
4
4
|
|
|
5
5
|
var React = require('react');
|
|
6
6
|
var InsertionPointProvider = require('./InsertionPointProvider.js');
|
|
7
|
+
var index = require('../style-injection-provider/index.js');
|
|
7
8
|
|
|
8
9
|
function _interopNamespace(e) {
|
|
9
10
|
if (e && e.__esModule) return e;
|
|
@@ -33,10 +34,11 @@ function useComponentCssInjection({
|
|
|
33
34
|
css,
|
|
34
35
|
window: targetWindow
|
|
35
36
|
}) {
|
|
37
|
+
const styleInjectionEnabled = index.useStyleInjection();
|
|
36
38
|
const insertionPoint = InsertionPointProvider.useInsertionPoint();
|
|
37
39
|
maybeUseInsertionEffect(() => {
|
|
38
40
|
var _a2, _b;
|
|
39
|
-
if (!targetWindow) {
|
|
41
|
+
if (!targetWindow || !styleInjectionEnabled) {
|
|
40
42
|
return;
|
|
41
43
|
}
|
|
42
44
|
const sheetsMap = (_a2 = windowSheetsMap.get(targetWindow)) != null ? _a2 : /* @__PURE__ */ new Map();
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"useStyleInjection.js","sources":["../src/use-style-injection/useStyleInjection.ts"],"sourcesContent":["import * as React from \"react\";\nimport { useInsertionPoint } from \"./InsertionPointProvider\";\n\n/* eslint-disable @typescript-eslint/no-unsafe-member-access, @typescript-eslint/no-unsafe-assignment, @typescript-eslint/no-explicit-any -- Workaround for https://github.com/webpack/webpack/issues/14814#issuecomment-1536757985 */\nconst maybeUseInsertionEffect: typeof React.useLayoutEffect =\n (React as any)[\"useInsertionEffect\".toString()] ?? React.useLayoutEffect;\n/* eslint-enable @typescript-eslint/no-unsafe-member-access, @typescript-eslint/no-unsafe-assignment */\n\nexport interface UseComponentCssInjection {\n testId?: string;\n css: string;\n window?: Window | null;\n}\n\ntype StyleElementMap = Map<\n string,\n { styleElement: HTMLStyleElement | null; count: number }\n>;\n\n// windowSheetsMap maps window objects to StyleElementMaps\n// A StyleElementMap maps css strings to style element tags\nconst windowSheetsMap = new WeakMap<Window, StyleElementMap>();\n\nexport function useComponentCssInjection({\n testId,\n css,\n window: targetWindow,\n}: UseComponentCssInjection): void {\n const insertionPoint = useInsertionPoint();\n\n maybeUseInsertionEffect(() => {\n if (!targetWindow) {\n return;\n }\n\n const sheetsMap =\n windowSheetsMap.get(targetWindow) ??\n new Map<\n string,\n { styleElement: HTMLStyleElement | null; count: number }\n >();\n const styleMap = sheetsMap.get(css) ?? { styleElement: null, count: 0 };\n\n if (styleMap.styleElement == null) {\n styleMap.styleElement = targetWindow.document.createElement(\"style\");\n styleMap.styleElement.setAttribute(\"type\", \"text/css\");\n styleMap.styleElement.setAttribute(\"data-salt-style\", testId || \"\");\n styleMap.styleElement.textContent = css;\n\n styleMap.count = 1;\n\n targetWindow.document.head.insertBefore(\n styleMap.styleElement,\n insertionPoint || targetWindow.document.head.firstChild\n );\n } else {\n styleMap.styleElement.textContent = css;\n styleMap.count++;\n }\n sheetsMap.set(css, styleMap);\n windowSheetsMap.set(targetWindow, sheetsMap);\n\n return () => {\n const sheetsMap = windowSheetsMap.get(targetWindow);\n const styleMap = sheetsMap?.get(css);\n if (styleMap?.styleElement) {\n styleMap.count--;\n if (styleMap.count < 1) {\n targetWindow.document.head.removeChild(styleMap.styleElement);\n styleMap.styleElement = null;\n sheetsMap?.delete(css);\n }\n }\n };\n }, [testId, css, targetWindow]);\n}\n"],"names":["React","useInsertionPoint","_a","sheetsMap","styleMap"],"mappings":"
|
|
1
|
+
{"version":3,"file":"useStyleInjection.js","sources":["../src/use-style-injection/useStyleInjection.ts"],"sourcesContent":["import * as React from \"react\";\nimport { useInsertionPoint } from \"./InsertionPointProvider\";\nimport { useStyleInjection } from \"../style-injection-provider\";\n\n/* eslint-disable @typescript-eslint/no-unsafe-member-access, @typescript-eslint/no-unsafe-assignment, @typescript-eslint/no-explicit-any -- Workaround for https://github.com/webpack/webpack/issues/14814#issuecomment-1536757985 */\nconst maybeUseInsertionEffect: typeof React.useLayoutEffect =\n (React as any)[\"useInsertionEffect\".toString()] ?? React.useLayoutEffect;\n/* eslint-enable @typescript-eslint/no-unsafe-member-access, @typescript-eslint/no-unsafe-assignment */\n\nexport interface UseComponentCssInjection {\n testId?: string;\n css: string;\n window?: Window | null;\n}\n\ntype StyleElementMap = Map<\n string,\n { styleElement: HTMLStyleElement | null; count: number }\n>;\n\n// windowSheetsMap maps window objects to StyleElementMaps\n// A StyleElementMap maps css strings to style element tags\nconst windowSheetsMap = new WeakMap<Window, StyleElementMap>();\n\nexport function useComponentCssInjection({\n testId,\n css,\n window: targetWindow,\n}: UseComponentCssInjection): void {\n const styleInjectionEnabled = useStyleInjection();\n const insertionPoint = useInsertionPoint();\n\n maybeUseInsertionEffect(() => {\n if (!targetWindow || !styleInjectionEnabled) {\n return;\n }\n\n const sheetsMap =\n windowSheetsMap.get(targetWindow) ??\n new Map<\n string,\n { styleElement: HTMLStyleElement | null; count: number }\n >();\n const styleMap = sheetsMap.get(css) ?? { styleElement: null, count: 0 };\n\n if (styleMap.styleElement == null) {\n styleMap.styleElement = targetWindow.document.createElement(\"style\");\n styleMap.styleElement.setAttribute(\"type\", \"text/css\");\n styleMap.styleElement.setAttribute(\"data-salt-style\", testId || \"\");\n styleMap.styleElement.textContent = css;\n\n styleMap.count = 1;\n\n targetWindow.document.head.insertBefore(\n styleMap.styleElement,\n insertionPoint || targetWindow.document.head.firstChild\n );\n } else {\n styleMap.styleElement.textContent = css;\n styleMap.count++;\n }\n sheetsMap.set(css, styleMap);\n windowSheetsMap.set(targetWindow, sheetsMap);\n\n return () => {\n const sheetsMap = windowSheetsMap.get(targetWindow);\n const styleMap = sheetsMap?.get(css);\n if (styleMap?.styleElement) {\n styleMap.count--;\n if (styleMap.count < 1) {\n targetWindow.document.head.removeChild(styleMap.styleElement);\n styleMap.styleElement = null;\n sheetsMap?.delete(css);\n }\n }\n };\n }, [testId, css, targetWindow]);\n}\n"],"names":["React","useStyleInjection","useInsertionPoint","_a","sheetsMap","styleMap"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;AAAA,IAAA,EAAA,CAAA;AAKA,MAAM,2BACH,EAAc,GAAAA,gBAAA,CAAA,oBAAA,CAAqB,QAAS,EAAA,CAAA,KAA5C,YAAkDA,gBAAM,CAAA,eAAA,CAAA;AAgB3D,MAAM,eAAA,uBAAsB,OAAiC,EAAA,CAAA;AAEtD,SAAS,wBAAyB,CAAA;AAAA,EACvC,MAAA;AAAA,EACA,GAAA;AAAA,EACA,MAAQ,EAAA,YAAA;AACV,CAAmC,EAAA;AACjC,EAAA,MAAM,wBAAwBC,uBAAkB,EAAA,CAAA;AAChD,EAAA,MAAM,iBAAiBC,wCAAkB,EAAA,CAAA;AAEzC,EAAA,uBAAA,CAAwB,MAAM;AAhChC,IAAA,IAAAC,GAAA,EAAA,EAAA,CAAA;AAiCI,IAAI,IAAA,CAAC,YAAgB,IAAA,CAAC,qBAAuB,EAAA;AAC3C,MAAA,O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A,CAAA;AAClD,MAAA,MAAMC,SAAWD,GAAAA,UAAAA,IAAA,IAAAA,GAAAA,KAAAA,CAAAA,GAAAA,UAAAA,CAAW,GAAI,CAAA,GAAA,CAAA,CAAA;AAChC,MAAIC,IAAAA,SAAAA,IAAA,IAAAA,GAAAA,KAAAA,CAAAA,GAAAA,SAAAA,CAAU,YAAc,EAAA;AAC1B,QAAAA,SAAS,CAAA,KAAA,EAAA,CAAA;AACT,QAAIA,IAAAA,SAAAA,CAAS,QAAQ,CAAG,EAAA;AACtB,UAAA,YAAA,CAAa,QAAS,CAAA,IAAA,CAAK,WAAYA,CAAAA,SAAAA,CAAS,YAAY,CAAA,CAAA;AAC5D,UAAAA,UAAS,YAAe,GAAA,IAAA,CAAA;AACxB,UAAAD,UAAAA,IAAA,IAAAA,GAAAA,KAAAA,CAAAA,GAAAA,UAAAA,CAAW,MAAO,CAAA,GAAA,CAAA,CAAA;AAAA,SACpB;AAAA,OACF;AAAA,KACF,CAAA;AAAA,GACC,EAAA,CAAC,MAAQ,EAAA,GAAA,EAAK,YAAY,CAAC,CAAA,CAAA;AAChC;;;;"}
|
package/dist-es/index.js
CHANGED
|
@@ -1,3 +1,4 @@
|
|
|
1
1
|
export { useComponentCssInjection } from './use-style-injection/useStyleInjection.js';
|
|
2
2
|
export { InsertionPointProvider, useInsertionPoint } from './use-style-injection/InsertionPointProvider.js';
|
|
3
|
+
export { StyleInjectionProvider, useStyleInjection } from './style-injection-provider/index.js';
|
|
3
4
|
//# sourceMappingURL=index.js.map
|
package/dist-es/index.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.js","sources":[],"sourcesContent":[],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"index.js","sources":[],"sourcesContent":[],"names":[],"mappings":";;"}
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
import { jsx } from 'react/jsx-runtime';
|
|
2
|
+
import { createContext, useContext } from 'react';
|
|
3
|
+
|
|
4
|
+
const StyleInjectionContext = createContext(true);
|
|
5
|
+
function useStyleInjection(enableStyleInjection) {
|
|
6
|
+
const enableStyleInjectionFromContext = useContext(StyleInjectionContext);
|
|
7
|
+
return enableStyleInjection != null ? enableStyleInjection : enableStyleInjectionFromContext;
|
|
8
|
+
}
|
|
9
|
+
function StyleInjectionProvider(props) {
|
|
10
|
+
const { value: enableStyleInjectionProp, children } = props;
|
|
11
|
+
const value = useStyleInjection(enableStyleInjectionProp);
|
|
12
|
+
return /* @__PURE__ */ jsx(StyleInjectionContext.Provider, {
|
|
13
|
+
value,
|
|
14
|
+
children
|
|
15
|
+
});
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
export { StyleInjectionProvider, useStyleInjection };
|
|
19
|
+
//# sourceMappingURL=index.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.js","sources":["../src/style-injection-provider/index.tsx"],"sourcesContent":["import React, { type ReactNode, createContext, useContext } from \"react\";\nexport interface StyleInjectionContextType {\n value?: boolean;\n}\n\nconst StyleInjectionContext = createContext(true);\n\nexport function useStyleInjection(enableStyleInjection?: boolean): boolean {\n const enableStyleInjectionFromContext = useContext(StyleInjectionContext);\n return enableStyleInjection ?? enableStyleInjectionFromContext;\n}\n\nexport interface StyleInjectionProviderProps extends StyleInjectionContextType {\n children: ReactNode;\n}\n\nexport function StyleInjectionProvider(props: StyleInjectionProviderProps) {\n const { value: enableStyleInjectionProp, children } = props;\n const value = useStyleInjection(enableStyleInjectionProp);\n\n return (\n <StyleInjectionContext.Provider value={value}>\n {children}\n </StyleInjectionContext.Provider>\n );\n}\n"],"names":[],"mappings":";;;AAKA,MAAM,qBAAA,GAAwB,cAAc,IAAI,CAAA,CAAA;AAEzC,SAAS,kBAAkB,oBAAyC,EAAA;AACzE,EAAM,MAAA,+BAAA,GAAkC,WAAW,qBAAqB,CAAA,CAAA;AACxE,EAAA,OAAO,oBAAwB,IAAA,IAAA,GAAA,oBAAA,GAAA,+BAAA,CAAA;AACjC,CAAA;AAMO,SAAS,uBAAuB,KAAoC,EAAA;AACzE,EAAA,MAAM,EAAE,KAAA,EAAO,wBAA0B,EAAA,QAAA,EAAa,GAAA,KAAA,CAAA;AACtD,EAAM,MAAA,KAAA,GAAQ,kBAAkB,wBAAwB,CAAA,CAAA;AAExD,EACE,uBAAA,GAAA,CAAC,sBAAsB,QAAtB,EAAA;AAAA,IAA+B,KAAA;AAAA,IAC7B,QAAA;AAAA,GACH,CAAA,CAAA;AAEJ;;;;"}
|
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
import * as React from 'react';
|
|
2
2
|
import { useInsertionPoint } from './InsertionPointProvider.js';
|
|
3
|
+
import { useStyleInjection } from '../style-injection-provider/index.js';
|
|
3
4
|
|
|
4
5
|
var _a;
|
|
5
6
|
const maybeUseInsertionEffect = (_a = React["useInsertionEffect".toString()]) != null ? _a : React.useLayoutEffect;
|
|
@@ -9,10 +10,11 @@ function useComponentCssInjection({
|
|
|
9
10
|
css,
|
|
10
11
|
window: targetWindow
|
|
11
12
|
}) {
|
|
13
|
+
const styleInjectionEnabled = useStyleInjection();
|
|
12
14
|
const insertionPoint = useInsertionPoint();
|
|
13
15
|
maybeUseInsertionEffect(() => {
|
|
14
16
|
var _a2, _b;
|
|
15
|
-
if (!targetWindow) {
|
|
17
|
+
if (!targetWindow || !styleInjectionEnabled) {
|
|
16
18
|
return;
|
|
17
19
|
}
|
|
18
20
|
const sheetsMap = (_a2 = windowSheetsMap.get(targetWindow)) != null ? _a2 : /* @__PURE__ */ new Map();
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"useStyleInjection.js","sources":["../src/use-style-injection/useStyleInjection.ts"],"sourcesContent":["import * as React from \"react\";\nimport { useInsertionPoint } from \"./InsertionPointProvider\";\n\n/* eslint-disable @typescript-eslint/no-unsafe-member-access, @typescript-eslint/no-unsafe-assignment, @typescript-eslint/no-explicit-any -- Workaround for https://github.com/webpack/webpack/issues/14814#issuecomment-1536757985 */\nconst maybeUseInsertionEffect: typeof React.useLayoutEffect =\n (React as any)[\"useInsertionEffect\".toString()] ?? React.useLayoutEffect;\n/* eslint-enable @typescript-eslint/no-unsafe-member-access, @typescript-eslint/no-unsafe-assignment */\n\nexport interface UseComponentCssInjection {\n testId?: string;\n css: string;\n window?: Window | null;\n}\n\ntype StyleElementMap = Map<\n string,\n { styleElement: HTMLStyleElement | null; count: number }\n>;\n\n// windowSheetsMap maps window objects to StyleElementMaps\n// A StyleElementMap maps css strings to style element tags\nconst windowSheetsMap = new WeakMap<Window, StyleElementMap>();\n\nexport function useComponentCssInjection({\n testId,\n css,\n window: targetWindow,\n}: UseComponentCssInjection): void {\n const insertionPoint = useInsertionPoint();\n\n maybeUseInsertionEffect(() => {\n if (!targetWindow) {\n return;\n }\n\n const sheetsMap =\n windowSheetsMap.get(targetWindow) ??\n new Map<\n string,\n { styleElement: HTMLStyleElement | null; count: number }\n >();\n const styleMap = sheetsMap.get(css) ?? { styleElement: null, count: 0 };\n\n if (styleMap.styleElement == null) {\n styleMap.styleElement = targetWindow.document.createElement(\"style\");\n styleMap.styleElement.setAttribute(\"type\", \"text/css\");\n styleMap.styleElement.setAttribute(\"data-salt-style\", testId || \"\");\n styleMap.styleElement.textContent = css;\n\n styleMap.count = 1;\n\n targetWindow.document.head.insertBefore(\n styleMap.styleElement,\n insertionPoint || targetWindow.document.head.firstChild\n );\n } else {\n styleMap.styleElement.textContent = css;\n styleMap.count++;\n }\n sheetsMap.set(css, styleMap);\n windowSheetsMap.set(targetWindow, sheetsMap);\n\n return () => {\n const sheetsMap = windowSheetsMap.get(targetWindow);\n const styleMap = sheetsMap?.get(css);\n if (styleMap?.styleElement) {\n styleMap.count--;\n if (styleMap.count < 1) {\n targetWindow.document.head.removeChild(styleMap.styleElement);\n styleMap.styleElement = null;\n sheetsMap?.delete(css);\n }\n }\n };\n }, [testId, css, targetWindow]);\n}\n"],"names":["_a","sheetsMap","styleMap"],"mappings":"
|
|
1
|
+
{"version":3,"file":"useStyleInjection.js","sources":["../src/use-style-injection/useStyleInjection.ts"],"sourcesContent":["import * as React from \"react\";\nimport { useInsertionPoint } from \"./InsertionPointProvider\";\nimport { useStyleInjection } from \"../style-injection-provider\";\n\n/* eslint-disable @typescript-eslint/no-unsafe-member-access, @typescript-eslint/no-unsafe-assignment, @typescript-eslint/no-explicit-any -- Workaround for https://github.com/webpack/webpack/issues/14814#issuecomment-1536757985 */\nconst maybeUseInsertionEffect: typeof React.useLayoutEffect =\n (React as any)[\"useInsertionEffect\".toString()] ?? React.useLayoutEffect;\n/* eslint-enable @typescript-eslint/no-unsafe-member-access, @typescript-eslint/no-unsafe-assignment */\n\nexport interface UseComponentCssInjection {\n testId?: string;\n css: string;\n window?: Window | null;\n}\n\ntype StyleElementMap = Map<\n string,\n { styleElement: HTMLStyleElement | null; count: number }\n>;\n\n// windowSheetsMap maps window objects to StyleElementMaps\n// A StyleElementMap maps css strings to style element tags\nconst windowSheetsMap = new WeakMap<Window, StyleElementMap>();\n\nexport function useComponentCssInjection({\n testId,\n css,\n window: targetWindow,\n}: UseComponentCssInjection): void {\n const styleInjectionEnabled = useStyleInjection();\n const insertionPoint = useInsertionPoint();\n\n maybeUseInsertionEffect(() => {\n if (!targetWindow || !styleInjectionEnabled) {\n return;\n }\n\n const sheetsMap =\n windowSheetsMap.get(targetWindow) ??\n new Map<\n string,\n { styleElement: HTMLStyleElement | null; count: number }\n >();\n const styleMap = sheetsMap.get(css) ?? { styleElement: null, count: 0 };\n\n if (styleMap.styleElement == null) {\n styleMap.styleElement = targetWindow.document.createElement(\"style\");\n styleMap.styleElement.setAttribute(\"type\", \"text/css\");\n styleMap.styleElement.setAttribute(\"data-salt-style\", testId || \"\");\n styleMap.styleElement.textContent = css;\n\n styleMap.count = 1;\n\n targetWindow.document.head.insertBefore(\n styleMap.styleElement,\n insertionPoint || targetWindow.document.head.firstChild\n );\n } else {\n styleMap.styleElement.textContent = css;\n styleMap.count++;\n }\n sheetsMap.set(css, styleMap);\n windowSheetsMap.set(targetWindow, sheetsMap);\n\n return () => {\n const sheetsMap = windowSheetsMap.get(targetWindow);\n const styleMap = sheetsMap?.get(css);\n if (styleMap?.styleElement) {\n styleMap.count--;\n if (styleMap.count < 1) {\n targetWindow.document.head.removeChild(styleMap.styleElement);\n styleMap.styleElement = null;\n sheetsMap?.delete(css);\n }\n }\n };\n }, [testId, css, targetWindow]);\n}\n"],"names":["_a","sheetsMap","styleMap"],"mappings":";;;;AAAA,IAAA,EAAA,CAAA;AAKA,MAAM,2BACH,EAAc,GAAA,KAAA,CAAA,oBAAA,CAAqB,QAAS,EAAA,CAAA,KAA5C,YAAkD,KAAM,CAAA,eAAA,CAAA;AAgB3D,MAAM,eAAA,uBAAsB,OAAiC,EAAA,CAAA;AAEtD,SAAS,wBAAyB,CAAA;AAAA,EACvC,MAAA;AAAA,EACA,GAAA;AAAA,EACA,MAAQ,EAAA,YAAA;AACV,CAAmC,EAAA;AACjC,EAAA,MAAM,wBAAwB,iBAAkB,EAAA,CAAA;AAChD,EAAA,MAAM,iBAAiB,iBAAkB,EAAA,CAAA;AAEzC,EAAA,uBAAA,CAAwB,MAAM;AAhChC,IAAA,IAAAA,GAAA,EAAA,EAAA,CAAA;AAiCI,IAAI,IAAA,CAAC,YAAgB,IAAA,CAAC,qBAAuB,EAAA;AAC3C,MAAA,OAAA;AAAA,KACF;AAEA,IAAM,MAAA,SAAA,GAAA,CACJA,MAAA,eAAgB,CAAA,GAAA,CAAI,YAAY,CAAhC,KAAA,IAAA,GAAAA,GACA,mBAAA,IAAI,GAGF,EAAA,CAAA;AACJ,IAAM,MAAA,QAAA,GAAA,CAAW,EAAU,GAAA,SAAA,CAAA,GAAA,CAAI,GAAG,CAAA,KAAjB,YAAsB,EAAE,YAAA,EAAc,IAAM,EAAA,KAAA,EAAO,CAAE,EAAA,CAAA;AAEtE,IAAI,IAAA,QAAA,CAAS,gBAAgB,IAAM,EAAA;AACjC,MAAA,QAAA,CAAS,YAAe,GAAA,YAAA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AAY,CAAA,CAAA;AAC5D,UAAAA,UAAS,YAAe,GAAA,IAAA,CAAA;AACxB,UAAAD,UAAAA,IAAA,IAAAA,GAAAA,KAAAA,CAAAA,GAAAA,UAAAA,CAAW,MAAO,CAAA,GAAA,CAAA,CAAA;AAAA,SACpB;AAAA,OACF;AAAA,KACF,CAAA;AAAA,GACC,EAAA,CAAC,MAAQ,EAAA,GAAA,EAAK,YAAY,CAAC,CAAA,CAAA;AAChC;;;;"}
|
package/dist-types/index.d.ts
CHANGED
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
import { type ReactNode } from "react";
|
|
2
|
+
export interface StyleInjectionContextType {
|
|
3
|
+
value?: boolean;
|
|
4
|
+
}
|
|
5
|
+
export declare function useStyleInjection(enableStyleInjection?: boolean): boolean;
|
|
6
|
+
export interface StyleInjectionProviderProps extends StyleInjectionContextType {
|
|
7
|
+
children: ReactNode;
|
|
8
|
+
}
|
|
9
|
+
export declare function StyleInjectionProvider(props: StyleInjectionProviderProps): JSX.Element;
|