@salt-ds/styles 0.0.0-snapshot-20230504082840 → 0.1.0
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/use-style-injection/InsertionPointProvider.js +27 -0
- package/dist-cjs/use-style-injection/InsertionPointProvider.js.map +1 -0
- package/dist-cjs/use-style-injection/useStyleInjection.js +14 -9
- 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/use-style-injection/InsertionPointProvider.js +22 -0
- package/dist-es/use-style-injection/InsertionPointProvider.js.map +1 -0
- package/dist-es/use-style-injection/useStyleInjection.js +14 -9
- package/dist-es/use-style-injection/useStyleInjection.js.map +1 -1
- package/dist-types/use-style-injection/InsertionPointProvider.d.ts +9 -0
- package/dist-types/use-style-injection/index.d.ts +1 -0
- package/dist-types/use-style-injection/useStyleInjection.d.ts +3 -5
- package/package.json +1 -1
package/dist-cjs/index.js
CHANGED
|
@@ -3,8 +3,11 @@
|
|
|
3
3
|
Object.defineProperty(exports, '__esModule', { value: true });
|
|
4
4
|
|
|
5
5
|
var useStyleInjection = require('./use-style-injection/useStyleInjection.js');
|
|
6
|
+
var InsertionPointProvider = require('./use-style-injection/InsertionPointProvider.js');
|
|
6
7
|
|
|
7
8
|
|
|
8
9
|
|
|
9
10
|
exports.useComponentCssInjection = useStyleInjection.useComponentCssInjection;
|
|
11
|
+
exports.InsertionPointProvider = InsertionPointProvider.InsertionPointProvider;
|
|
12
|
+
exports.useInsertionPoint = InsertionPointProvider.useInsertionPoint;
|
|
10
13
|
//# 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,27 @@
|
|
|
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 InsertionPointContext = React.createContext(null);
|
|
9
|
+
if (process.env.NODE_ENV !== "production") {
|
|
10
|
+
InsertionPointContext.displayName = "InsertionPointContext";
|
|
11
|
+
}
|
|
12
|
+
function InsertionPointProvider(props) {
|
|
13
|
+
const { insertionPoint: insertionPointProp, children } = props;
|
|
14
|
+
const value = React.useMemo(() => insertionPointProp, [insertionPointProp]);
|
|
15
|
+
return /* @__PURE__ */ jsxRuntime.jsx(InsertionPointContext.Provider, {
|
|
16
|
+
value,
|
|
17
|
+
children
|
|
18
|
+
});
|
|
19
|
+
}
|
|
20
|
+
function useInsertionPoint() {
|
|
21
|
+
const value = React.useContext(InsertionPointContext);
|
|
22
|
+
return value;
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
exports.InsertionPointProvider = InsertionPointProvider;
|
|
26
|
+
exports.useInsertionPoint = useInsertionPoint;
|
|
27
|
+
//# sourceMappingURL=InsertionPointProvider.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"InsertionPointProvider.js","sources":["../src/use-style-injection/InsertionPointProvider.tsx"],"sourcesContent":["import { createContext, ReactNode, useContext, useMemo } from \"react\";\n\nexport interface InsertionPointContextType {\n insertionPoint: ChildNode | null;\n}\n\nconst InsertionPointContext = createContext<ChildNode | null>(null);\n\nif (process.env.NODE_ENV !== \"production\") {\n InsertionPointContext.displayName = \"InsertionPointContext\";\n}\n\nexport interface InsertionPointProviderProps extends InsertionPointContextType {\n children: ReactNode;\n}\n\nexport function InsertionPointProvider(props: InsertionPointProviderProps) {\n const { insertionPoint: insertionPointProp, children } = props;\n const value = useMemo(() => insertionPointProp, [insertionPointProp]);\n\n return (\n <InsertionPointContext.Provider value={value}>\n {children}\n </InsertionPointContext.Provider>\n );\n}\n\nexport function useInsertionPoint() {\n const value = useContext(InsertionPointContext);\n return value;\n}\n"],"names":["createContext","useMemo","jsx","useContext"],"mappings":";;;;;;;AAMA,MAAM,qBAAA,GAAwBA,oBAAgC,IAAI,CAAA,CAAA;AAElE,IAAI,OAAA,CAAQ,GAAI,CAAA,QAAA,KAAa,YAAc,EAAA;AACzC,EAAA,qBAAA,CAAsB,WAAc,GAAA,uBAAA,CAAA;AACtC,CAAA;AAMO,SAAS,uBAAuB,KAAoC,EAAA;AACzE,EAAA,MAAM,EAAE,cAAA,EAAgB,kBAAoB,EAAA,QAAA,EAAa,GAAA,KAAA,CAAA;AACzD,EAAA,MAAM,QAAQC,aAAQ,CAAA,MAAM,kBAAoB,EAAA,CAAC,kBAAkB,CAAC,CAAA,CAAA;AAEpE,EACE,uBAAAC,cAAA,CAAC,sBAAsB,QAAtB,EAAA;AAAA,IAA+B,KAAA;AAAA,IAC7B,QAAA;AAAA,GACH,CAAA,CAAA;AAEJ,CAAA;AAEO,SAAS,iBAAoB,GAAA;AAClC,EAAM,MAAA,KAAA,GAAQC,iBAAW,qBAAqB,CAAA,CAAA;AAC9C,EAAO,OAAA,KAAA,CAAA;AACT;;;;;"}
|
|
@@ -3,6 +3,7 @@
|
|
|
3
3
|
Object.defineProperty(exports, '__esModule', { value: true });
|
|
4
4
|
|
|
5
5
|
var React = require('react');
|
|
6
|
+
var InsertionPointProvider = require('./InsertionPointProvider.js');
|
|
6
7
|
|
|
7
8
|
function _interopNamespace(e) {
|
|
8
9
|
if (e && e.__esModule) return e;
|
|
@@ -28,43 +29,47 @@ var _a;
|
|
|
28
29
|
const maybeUseInsertionEffect = (_a = React__namespace[`${"useInsertionEffect"}${""}`]) != null ? _a : React__namespace.useLayoutEffect;
|
|
29
30
|
const windowSheetsMap = /* @__PURE__ */ new WeakMap();
|
|
30
31
|
function useComponentCssInjection({
|
|
31
|
-
|
|
32
|
+
testId,
|
|
32
33
|
css,
|
|
33
34
|
window: targetWindow
|
|
34
35
|
}) {
|
|
36
|
+
const insertionPoint = InsertionPointProvider.useInsertionPoint();
|
|
35
37
|
maybeUseInsertionEffect(() => {
|
|
36
38
|
var _a2, _b;
|
|
37
|
-
|
|
38
|
-
|
|
39
|
+
if (!targetWindow) {
|
|
40
|
+
return;
|
|
41
|
+
}
|
|
42
|
+
const sheetsMap = (_a2 = windowSheetsMap.get(targetWindow)) != null ? _a2 : /* @__PURE__ */ new Map();
|
|
43
|
+
const styleMap = (_b = sheetsMap.get(css)) != null ? _b : { styleElement: null, count: 0 };
|
|
39
44
|
if (styleMap.styleElement == null) {
|
|
40
45
|
styleMap.styleElement = targetWindow.document.createElement("style");
|
|
41
46
|
styleMap.styleElement.setAttribute("type", "text/css");
|
|
42
|
-
styleMap.styleElement.setAttribute("data-salt-style",
|
|
47
|
+
styleMap.styleElement.setAttribute("data-salt-style", testId || "");
|
|
43
48
|
styleMap.styleElement.textContent = css;
|
|
44
49
|
styleMap.count = 1;
|
|
45
50
|
targetWindow.document.head.insertBefore(
|
|
46
51
|
styleMap.styleElement,
|
|
47
|
-
targetWindow.document.head.firstChild
|
|
52
|
+
insertionPoint || targetWindow.document.head.firstChild
|
|
48
53
|
);
|
|
49
54
|
} else {
|
|
50
55
|
styleMap.styleElement.textContent = css;
|
|
51
56
|
styleMap.count++;
|
|
52
57
|
}
|
|
53
|
-
sheetsMap.set(
|
|
58
|
+
sheetsMap.set(css, styleMap);
|
|
54
59
|
windowSheetsMap.set(targetWindow, sheetsMap);
|
|
55
60
|
return () => {
|
|
56
61
|
const sheetsMap2 = windowSheetsMap.get(targetWindow);
|
|
57
|
-
const styleMap2 = sheetsMap2 == null ? void 0 : sheetsMap2.get(
|
|
62
|
+
const styleMap2 = sheetsMap2 == null ? void 0 : sheetsMap2.get(css);
|
|
58
63
|
if (styleMap2 == null ? void 0 : styleMap2.styleElement) {
|
|
59
64
|
styleMap2.count--;
|
|
60
65
|
if (styleMap2.count < 1) {
|
|
61
66
|
targetWindow.document.head.removeChild(styleMap2.styleElement);
|
|
62
67
|
styleMap2.styleElement = null;
|
|
63
|
-
sheetsMap2 == null ? void 0 : sheetsMap2.delete(
|
|
68
|
+
sheetsMap2 == null ? void 0 : sheetsMap2.delete(css);
|
|
64
69
|
}
|
|
65
70
|
}
|
|
66
71
|
};
|
|
67
|
-
}, [
|
|
72
|
+
}, [testId, css, targetWindow]);
|
|
68
73
|
}
|
|
69
74
|
|
|
70
75
|
exports.useComponentCssInjection = useComponentCssInjection;
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"useStyleInjection.js","sources":["../src/use-style-injection/useStyleInjection.ts"],"sourcesContent":["import * as React from \"react\";\n\n
|
|
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 -- Workaround for https://github.com/webpack/webpack/issues/14814 */\nconst maybeUseInsertionEffect: typeof React.useLayoutEffect =\n (React as any)[`${\"useInsertionEffect\"}${\"\"}`] ?? 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":";;;;;;;;;;;;;;;;;;;;;;;;;;;AAAA,IAAA,EAAA,CAAA;AAIA,MAAM,2BACH,EAAc,GAAAA,gBAAA,CAAA,CAAA,EAAG,oBAAuB,CAAA,EAAA,EAAA,CAAA,CAAA,CAAA,KAAxC,YAAiDA,gBAAM,CAAA,eAAA,CAAA;AAgB1D,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,iBAAiBC,wCAAkB,EAAA,CAAA;AAEzC,EAAA,uBAAA,CAAwB,MAAM;AA9BhC,IAAA,IAAAC,GAAA,EAAA,EAAA,CAAA;AA+BI,IAAA,IAAI,CAAC,YAA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GAAA,CAAI,YAAY,CAAA,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
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,22 @@
|
|
|
1
|
+
import { jsx } from 'react/jsx-runtime';
|
|
2
|
+
import { createContext, useMemo, useContext } from 'react';
|
|
3
|
+
|
|
4
|
+
const InsertionPointContext = createContext(null);
|
|
5
|
+
if (process.env.NODE_ENV !== "production") {
|
|
6
|
+
InsertionPointContext.displayName = "InsertionPointContext";
|
|
7
|
+
}
|
|
8
|
+
function InsertionPointProvider(props) {
|
|
9
|
+
const { insertionPoint: insertionPointProp, children } = props;
|
|
10
|
+
const value = useMemo(() => insertionPointProp, [insertionPointProp]);
|
|
11
|
+
return /* @__PURE__ */ jsx(InsertionPointContext.Provider, {
|
|
12
|
+
value,
|
|
13
|
+
children
|
|
14
|
+
});
|
|
15
|
+
}
|
|
16
|
+
function useInsertionPoint() {
|
|
17
|
+
const value = useContext(InsertionPointContext);
|
|
18
|
+
return value;
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
export { InsertionPointProvider, useInsertionPoint };
|
|
22
|
+
//# sourceMappingURL=InsertionPointProvider.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"InsertionPointProvider.js","sources":["../src/use-style-injection/InsertionPointProvider.tsx"],"sourcesContent":["import { createContext, ReactNode, useContext, useMemo } from \"react\";\n\nexport interface InsertionPointContextType {\n insertionPoint: ChildNode | null;\n}\n\nconst InsertionPointContext = createContext<ChildNode | null>(null);\n\nif (process.env.NODE_ENV !== \"production\") {\n InsertionPointContext.displayName = \"InsertionPointContext\";\n}\n\nexport interface InsertionPointProviderProps extends InsertionPointContextType {\n children: ReactNode;\n}\n\nexport function InsertionPointProvider(props: InsertionPointProviderProps) {\n const { insertionPoint: insertionPointProp, children } = props;\n const value = useMemo(() => insertionPointProp, [insertionPointProp]);\n\n return (\n <InsertionPointContext.Provider value={value}>\n {children}\n </InsertionPointContext.Provider>\n );\n}\n\nexport function useInsertionPoint() {\n const value = useContext(InsertionPointContext);\n return value;\n}\n"],"names":[],"mappings":";;;AAMA,MAAM,qBAAA,GAAwB,cAAgC,IAAI,CAAA,CAAA;AAElE,IAAI,OAAA,CAAQ,GAAI,CAAA,QAAA,KAAa,YAAc,EAAA;AACzC,EAAA,qBAAA,CAAsB,WAAc,GAAA,uBAAA,CAAA;AACtC,CAAA;AAMO,SAAS,uBAAuB,KAAoC,EAAA;AACzE,EAAA,MAAM,EAAE,cAAA,EAAgB,kBAAoB,EAAA,QAAA,EAAa,GAAA,KAAA,CAAA;AACzD,EAAA,MAAM,QAAQ,OAAQ,CAAA,MAAM,kBAAoB,EAAA,CAAC,kBAAkB,CAAC,CAAA,CAAA;AAEpE,EACE,uBAAA,GAAA,CAAC,sBAAsB,QAAtB,EAAA;AAAA,IAA+B,KAAA;AAAA,IAC7B,QAAA;AAAA,GACH,CAAA,CAAA;AAEJ,CAAA;AAEO,SAAS,iBAAoB,GAAA;AAClC,EAAM,MAAA,KAAA,GAAQ,WAAW,qBAAqB,CAAA,CAAA;AAC9C,EAAO,OAAA,KAAA,CAAA;AACT;;;;"}
|
|
@@ -1,46 +1,51 @@
|
|
|
1
1
|
import * as React from 'react';
|
|
2
|
+
import { useInsertionPoint } from './InsertionPointProvider.js';
|
|
2
3
|
|
|
3
4
|
var _a;
|
|
4
5
|
const maybeUseInsertionEffect = (_a = React[`${"useInsertionEffect"}${""}`]) != null ? _a : React.useLayoutEffect;
|
|
5
6
|
const windowSheetsMap = /* @__PURE__ */ new WeakMap();
|
|
6
7
|
function useComponentCssInjection({
|
|
7
|
-
|
|
8
|
+
testId,
|
|
8
9
|
css,
|
|
9
10
|
window: targetWindow
|
|
10
11
|
}) {
|
|
12
|
+
const insertionPoint = useInsertionPoint();
|
|
11
13
|
maybeUseInsertionEffect(() => {
|
|
12
14
|
var _a2, _b;
|
|
13
|
-
|
|
14
|
-
|
|
15
|
+
if (!targetWindow) {
|
|
16
|
+
return;
|
|
17
|
+
}
|
|
18
|
+
const sheetsMap = (_a2 = windowSheetsMap.get(targetWindow)) != null ? _a2 : /* @__PURE__ */ new Map();
|
|
19
|
+
const styleMap = (_b = sheetsMap.get(css)) != null ? _b : { styleElement: null, count: 0 };
|
|
15
20
|
if (styleMap.styleElement == null) {
|
|
16
21
|
styleMap.styleElement = targetWindow.document.createElement("style");
|
|
17
22
|
styleMap.styleElement.setAttribute("type", "text/css");
|
|
18
|
-
styleMap.styleElement.setAttribute("data-salt-style",
|
|
23
|
+
styleMap.styleElement.setAttribute("data-salt-style", testId || "");
|
|
19
24
|
styleMap.styleElement.textContent = css;
|
|
20
25
|
styleMap.count = 1;
|
|
21
26
|
targetWindow.document.head.insertBefore(
|
|
22
27
|
styleMap.styleElement,
|
|
23
|
-
targetWindow.document.head.firstChild
|
|
28
|
+
insertionPoint || targetWindow.document.head.firstChild
|
|
24
29
|
);
|
|
25
30
|
} else {
|
|
26
31
|
styleMap.styleElement.textContent = css;
|
|
27
32
|
styleMap.count++;
|
|
28
33
|
}
|
|
29
|
-
sheetsMap.set(
|
|
34
|
+
sheetsMap.set(css, styleMap);
|
|
30
35
|
windowSheetsMap.set(targetWindow, sheetsMap);
|
|
31
36
|
return () => {
|
|
32
37
|
const sheetsMap2 = windowSheetsMap.get(targetWindow);
|
|
33
|
-
const styleMap2 = sheetsMap2 == null ? void 0 : sheetsMap2.get(
|
|
38
|
+
const styleMap2 = sheetsMap2 == null ? void 0 : sheetsMap2.get(css);
|
|
34
39
|
if (styleMap2 == null ? void 0 : styleMap2.styleElement) {
|
|
35
40
|
styleMap2.count--;
|
|
36
41
|
if (styleMap2.count < 1) {
|
|
37
42
|
targetWindow.document.head.removeChild(styleMap2.styleElement);
|
|
38
43
|
styleMap2.styleElement = null;
|
|
39
|
-
sheetsMap2 == null ? void 0 : sheetsMap2.delete(
|
|
44
|
+
sheetsMap2 == null ? void 0 : sheetsMap2.delete(css);
|
|
40
45
|
}
|
|
41
46
|
}
|
|
42
47
|
};
|
|
43
|
-
}, [
|
|
48
|
+
}, [testId, css, targetWindow]);
|
|
44
49
|
}
|
|
45
50
|
|
|
46
51
|
export { useComponentCssInjection };
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"useStyleInjection.js","sources":["../src/use-style-injection/useStyleInjection.ts"],"sourcesContent":["import * as React from \"react\";\n\n
|
|
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 -- Workaround for https://github.com/webpack/webpack/issues/14814 */\nconst maybeUseInsertionEffect: typeof React.useLayoutEffect =\n (React as any)[`${\"useInsertionEffect\"}${\"\"}`] ?? 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":["_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0 +1,9 @@
|
|
|
1
|
+
import { ReactNode } from "react";
|
|
2
|
+
export interface InsertionPointContextType {
|
|
3
|
+
insertionPoint: ChildNode | null;
|
|
4
|
+
}
|
|
5
|
+
export interface InsertionPointProviderProps extends InsertionPointContextType {
|
|
6
|
+
children: ReactNode;
|
|
7
|
+
}
|
|
8
|
+
export declare function InsertionPointProvider(props: InsertionPointProviderProps): JSX.Element;
|
|
9
|
+
export declare function useInsertionPoint(): ChildNode | null;
|
|
@@ -1,8 +1,6 @@
|
|
|
1
|
-
declare type WindowLike = Window & typeof globalThis;
|
|
2
1
|
export interface UseComponentCssInjection {
|
|
3
|
-
|
|
2
|
+
testId?: string;
|
|
4
3
|
css: string;
|
|
5
|
-
window
|
|
4
|
+
window?: Window | null;
|
|
6
5
|
}
|
|
7
|
-
export declare function useComponentCssInjection({
|
|
8
|
-
export {};
|
|
6
|
+
export declare function useComponentCssInjection({ testId, css, window: targetWindow, }: UseComponentCssInjection): void;
|