@mui/internal-docs-infra 0.3.1-canary.3 → 0.3.1-canary.5
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/esm/CodeControllerContext/CodeControllerContext.js +2 -2
- package/esm/CodeExternalsContext/CodeExternalsContext.js +1 -1
- package/esm/CodeHighlighter/CodeHighlighter.js +247 -329
- package/esm/CodeHighlighter/CodeHighlighterClient.js +447 -653
- package/esm/CodeHighlighter/CodeHighlighterContext.js +2 -2
- package/esm/CodeHighlighter/CodeHighlighterFallbackContext.js +2 -2
- package/esm/CodeHighlighter/codeToFallbackProps.js +21 -37
- package/esm/CodeHighlighter/errors.js +248 -400
- package/esm/CodeHighlighter/parseControlledCode.js +12 -20
- package/esm/CodeProvider/CodeContext.js +3 -3
- package/esm/CodeProvider/CodeProvider.js +31 -40
- package/esm/abstractCreateDemo/abstractCreateDemo.js +13 -17
- package/esm/abstractCreateDemoClient/abstractCreateDemoClient.js +12 -12
- package/esm/cli/index.js +1 -1
- package/esm/cli/runValidate.js +160 -264
- package/esm/createDemoData/createDemoData.js +11 -12
- package/esm/createSitemap/createSitemap.js +2 -2
- package/esm/pipeline/getFileConventions/fileConventions.js +1 -1
- package/esm/pipeline/getFileConventions/getFileConventions.js +2 -15
- package/esm/pipeline/hastUtils/hastUtils.js +16 -17
- package/esm/pipeline/loadCodeVariant/addCodeVariantPaths.js +24 -24
- package/esm/pipeline/loadCodeVariant/applyCodeTransform.js +12 -22
- package/esm/pipeline/loadCodeVariant/calculateMainFilePath.js +30 -37
- package/esm/pipeline/loadCodeVariant/computeHastDeltas.js +107 -185
- package/esm/pipeline/loadCodeVariant/diffHast.js +18 -53
- package/esm/pipeline/loadCodeVariant/examineCodeVariant.js +24 -27
- package/esm/pipeline/loadCodeVariant/flattenCodeVariant.js +9 -10
- package/esm/pipeline/loadCodeVariant/hasAllCodeVariants.js +5 -5
- package/esm/pipeline/loadCodeVariant/loadCodeFallback.js +516 -731
- package/esm/pipeline/loadCodeVariant/loadCodeVariant.js +679 -1079
- package/esm/pipeline/loadCodeVariant/maybeCodeInitialData.js +14 -20
- package/esm/pipeline/loadCodeVariant/mergeCodeMetadata.js +53 -63
- package/esm/pipeline/loadCodeVariant/parseCode.js +40 -48
- package/esm/pipeline/loadCodeVariant/pathUtils.js +43 -64
- package/esm/pipeline/loadCodeVariant/transformSource.js +55 -125
- package/esm/pipeline/loadPrecomputedCodeHighlighter/loadPrecomputedCodeHighlighter.js +160 -221
- package/esm/pipeline/loadPrecomputedCodeHighlighter/parseCreateFactoryCall.js +377 -479
- package/esm/pipeline/loadPrecomputedCodeHighlighter/parseFunctionArguments.js +171 -173
- package/esm/pipeline/loadPrecomputedCodeHighlighter/performanceLogger.js +14 -30
- package/esm/pipeline/loadPrecomputedCodeHighlighter/replacePrecomputeValue.js +19 -21
- package/esm/pipeline/loadPrecomputedCodeHighlighter/serializeFunctionArguments.js +37 -71
- package/esm/pipeline/loadPrecomputedCodeHighlighterClient/filterRuntimeExternals.js +3 -9
- package/esm/pipeline/loadPrecomputedCodeHighlighterClient/generateImportStatements.js +54 -80
- package/esm/pipeline/loadPrecomputedCodeHighlighterClient/generateResolvedExternals.js +71 -98
- package/esm/pipeline/loadPrecomputedCodeHighlighterClient/injectImportsIntoSource.js +5 -5
- package/esm/pipeline/loadPrecomputedCodeHighlighterClient/loadPrecomputedCodeHighlighterClient.js +161 -211
- package/esm/pipeline/loadPrecomputedSitemap/loadPrecomputedSitemap.js +159 -207
- package/esm/pipeline/loadServerCodeMeta/loadServerCodeMeta.js +42 -64
- package/esm/pipeline/loadServerCodeMeta/resolveModulePathWithFs.js +20 -96
- package/esm/pipeline/loadServerPageIndex/loadServerPageIndex.js +66 -85
- package/esm/pipeline/loadServerSitemap/loadServerSitemap.js +71 -118
- package/esm/pipeline/loadServerSource/loadServerSource.js +121 -148
- package/esm/pipeline/loaderUtils/externalsToPackages.js +7 -7
- package/esm/pipeline/loaderUtils/extractNameAndSlugFromUrl.js +8 -12
- package/esm/pipeline/loaderUtils/fileUrlToPortablePath.js +5 -5
- package/esm/pipeline/loaderUtils/getFileNameFromUrl.js +19 -29
- package/esm/pipeline/loaderUtils/getLanguageFromExtension.js +3 -4
- package/esm/pipeline/loaderUtils/mergeExternals.js +15 -35
- package/esm/pipeline/loaderUtils/parseImportsAndComments.js +413 -433
- package/esm/pipeline/loaderUtils/processRelativeImports.js +153 -239
- package/esm/pipeline/loaderUtils/resolveModulePath.js +544 -1303
- package/esm/pipeline/loaderUtils/rewriteImports.js +73 -111
- package/esm/pipeline/parseSource/addLineGutters.js +33 -45
- package/esm/pipeline/parseSource/grammars.js +3 -3
- package/esm/pipeline/parseSource/parseSource.js +13 -31
- package/esm/pipeline/syncPageIndex/createMarkdownNodes.js +32 -55
- package/esm/pipeline/syncPageIndex/mergeMetadataMarkdown.js +107 -160
- package/esm/pipeline/syncPageIndex/metadataToMarkdown.js +846 -1033
- package/esm/pipeline/syncPageIndex/syncPageIndex.js +291 -438
- package/esm/pipeline/transformHtmlCodePrecomputed/transformHtmlCodePrecomputed.js +213 -311
- package/esm/pipeline/transformMarkdownBlockquoteCallouts/transformMarkdownBlockquoteCallouts.js +10 -10
- package/esm/pipeline/transformMarkdownCode/transformMarkdownCode.js +133 -193
- package/esm/pipeline/transformMarkdownDemoLinks/transformMarkdownDemoLinks.js +25 -27
- package/esm/pipeline/transformMarkdownMetadata/transformMarkdownMetadata.js +572 -717
- package/esm/pipeline/transformMarkdownRelativePaths/transformMarkdownRelativePaths.js +8 -8
- package/esm/pipeline/transformTypescriptToJavascript/removeTypes.js +84 -113
- package/esm/pipeline/transformTypescriptToJavascript/transformTypescriptToJavascript.js +10 -26
- package/esm/useCode/Pre.js +58 -62
- package/esm/useCode/useCode.js +59 -61
- package/esm/useCode/useCodeUtils.js +54 -63
- package/esm/useCode/useCopyFunctionality.js +10 -9
- package/esm/useCode/useFileNavigation.js +150 -212
- package/esm/useCode/useSourceEditing.js +17 -14
- package/esm/useCode/useTransformManagement.js +23 -26
- package/esm/useCode/useUIState.js +12 -20
- package/esm/useCode/useVariantSelection.js +62 -79
- package/esm/useCopier/index.js +29 -56
- package/esm/useDemo/createCodeSandbox.js +12 -15
- package/esm/useDemo/createStackBlitz.js +14 -20
- package/esm/useDemo/exportVariant.js +200 -180
- package/esm/useDemo/exportVariantAsCra.js +22 -25
- package/esm/useDemo/useDemo.js +80 -84
- package/esm/useErrors/ErrorsContext.js +1 -1
- package/esm/useErrors/useErrors.js +3 -3
- package/esm/useLocalStorageState/useLocalStorageState.js +23 -39
- package/esm/usePreference/PreferencesProvider.js +1 -1
- package/esm/usePreference/usePreference.js +9 -11
- package/esm/useSearch/useSearch.js +290 -387
- package/esm/useUrlHashState/useUrlHashState.js +11 -14
- package/esm/withDocsInfra/withDeploymentConfig.js +26 -21
- package/esm/withDocsInfra/withDocsInfra.js +99 -101
- package/package.json +7 -4
package/esm/useDemo/useDemo.js
CHANGED
|
@@ -1,5 +1,3 @@
|
|
|
1
|
-
import _extends from "@babel/runtime/helpers/esm/extends";
|
|
2
|
-
import _slicedToArray from "@babel/runtime/helpers/esm/slicedToArray";
|
|
3
1
|
import * as React from 'react';
|
|
4
2
|
import kebabCase from 'kebab-case';
|
|
5
3
|
import { useCode } from "../useCode/index.js";
|
|
@@ -29,7 +27,7 @@ import { flattenCodeVariant } from "../pipeline/loadCodeVariant/flattenCodeVaria
|
|
|
29
27
|
* Helper to create HTML form element with hidden inputs
|
|
30
28
|
*/
|
|
31
29
|
export function addHiddenInput(form, name, value) {
|
|
32
|
-
|
|
30
|
+
const input = document.createElement('input');
|
|
33
31
|
input.type = 'hidden';
|
|
34
32
|
input.name = name;
|
|
35
33
|
input.value = value;
|
|
@@ -40,21 +38,17 @@ export function addHiddenInput(form, name, value) {
|
|
|
40
38
|
* Generic function to create and submit a form for opening online demo platforms
|
|
41
39
|
* This function creates HTML elements and should be used in browser contexts
|
|
42
40
|
*/
|
|
43
|
-
export function openWithForm(
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
var form = document.createElement('form');
|
|
41
|
+
export function openWithForm({
|
|
42
|
+
url,
|
|
43
|
+
formData,
|
|
44
|
+
method = 'POST',
|
|
45
|
+
target = '_blank'
|
|
46
|
+
}) {
|
|
47
|
+
const form = document.createElement('form');
|
|
51
48
|
form.method = method;
|
|
52
49
|
form.target = target;
|
|
53
50
|
form.action = url;
|
|
54
|
-
Object.entries(formData).forEach(
|
|
55
|
-
var _ref3 = _slicedToArray(_ref2, 2),
|
|
56
|
-
name = _ref3[0],
|
|
57
|
-
value = _ref3[1];
|
|
51
|
+
Object.entries(formData).forEach(([name, value]) => {
|
|
58
52
|
addHiddenInput(form, name, value);
|
|
59
53
|
});
|
|
60
54
|
document.body.appendChild(form);
|
|
@@ -64,130 +58,132 @@ export function openWithForm(_ref) {
|
|
|
64
58
|
|
|
65
59
|
// TODO: take initialVariant and initialTransforms as parameters
|
|
66
60
|
export function useDemo(contentProps, opts) {
|
|
67
|
-
|
|
61
|
+
const code = useCode(contentProps, opts);
|
|
68
62
|
|
|
69
63
|
// Extract export configuration options
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
_ref4$exportCodeSandb = _ref4.exportCodeSandbox,
|
|
76
|
-
codeSandboxExportConfig = _ref4$exportCodeSandb === void 0 ? {} : _ref4$exportCodeSandb;
|
|
64
|
+
const {
|
|
65
|
+
export: commonExportConfig = {},
|
|
66
|
+
exportStackBlitz: stackBlitzExportConfig = {},
|
|
67
|
+
exportCodeSandbox: codeSandboxExportConfig = {}
|
|
68
|
+
} = opts || {};
|
|
77
69
|
|
|
78
70
|
// Get context to access components if available (using React.useContext to avoid import conflicts)
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
return contentProps.slug || (contentProps.name ? kebabCase(contentProps.name, false) : undefined);
|
|
82
|
-
}, [contentProps.slug, contentProps.name]);
|
|
71
|
+
const context = React.useContext(CodeHighlighterContext);
|
|
72
|
+
const slug = React.useMemo(() => contentProps.slug || (contentProps.name ? kebabCase(contentProps.name, false) : undefined), [contentProps.slug, contentProps.name]);
|
|
83
73
|
|
|
84
74
|
// Get the effective components object - context overrides contentProps
|
|
85
|
-
|
|
86
|
-
return
|
|
87
|
-
}, [context
|
|
75
|
+
const effectiveComponents = React.useMemo(() => {
|
|
76
|
+
return context?.components || contentProps.components || {};
|
|
77
|
+
}, [context?.components, contentProps.components]);
|
|
88
78
|
|
|
89
79
|
// Get the component for the current variant
|
|
90
|
-
|
|
80
|
+
const component = React.useMemo(() => {
|
|
91
81
|
return effectiveComponents[code.selectedVariant] || null;
|
|
92
82
|
}, [effectiveComponents, code.selectedVariant]);
|
|
93
83
|
|
|
94
84
|
// Demo-specific ref and focus management
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
(_ref$current = ref.current) == null || _ref$current.focus();
|
|
85
|
+
const ref = React.useRef(null);
|
|
86
|
+
const resetFocus = React.useCallback(() => {
|
|
87
|
+
ref.current?.focus();
|
|
99
88
|
}, []);
|
|
100
89
|
|
|
101
90
|
// Get the effective code - context overrides contentProps if available
|
|
102
|
-
|
|
103
|
-
return
|
|
104
|
-
}, [context
|
|
91
|
+
const effectiveCode = React.useMemo(() => {
|
|
92
|
+
return context?.code || contentProps.code || {};
|
|
93
|
+
}, [context?.code, contentProps.code]);
|
|
105
94
|
|
|
106
95
|
// Create StackBlitz demo callback
|
|
107
|
-
|
|
96
|
+
const openStackBlitz = React.useCallback(() => {
|
|
108
97
|
// Get the current variant code
|
|
109
|
-
|
|
98
|
+
const variantCode = effectiveCode[code.selectedVariant];
|
|
110
99
|
if (!variantCode || typeof variantCode === 'string') {
|
|
111
100
|
console.warn('No valid variant code available for StackBlitz demo');
|
|
112
101
|
return;
|
|
113
102
|
}
|
|
114
|
-
|
|
115
|
-
|
|
103
|
+
const title = contentProps.name || 'Demo';
|
|
104
|
+
const description = `${title} demo`;
|
|
116
105
|
|
|
117
106
|
// Determine if we should use TypeScript based on whether 'js' transform is NOT applied
|
|
118
107
|
// If 'js' transform is applied, it means we're showing the JS version of TS code
|
|
119
|
-
|
|
108
|
+
const useTypescript = code.selectedTransform !== 'js';
|
|
120
109
|
|
|
121
110
|
// Merge common export config with StackBlitz-specific config
|
|
122
|
-
|
|
111
|
+
const mergedConfig = {
|
|
112
|
+
...commonExportConfig,
|
|
113
|
+
...stackBlitzExportConfig,
|
|
123
114
|
variantName: code.selectedVariant,
|
|
124
|
-
title
|
|
125
|
-
description
|
|
126
|
-
useTypescript
|
|
127
|
-
}
|
|
115
|
+
title,
|
|
116
|
+
description,
|
|
117
|
+
useTypescript
|
|
118
|
+
};
|
|
128
119
|
|
|
129
120
|
// Use custom export function if provided, otherwise use default exportVariant
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
exported
|
|
133
|
-
rootFile
|
|
121
|
+
const exportFunction = mergedConfig.exportFunction || exportVariant;
|
|
122
|
+
const {
|
|
123
|
+
exported,
|
|
124
|
+
rootFile
|
|
125
|
+
} = exportFunction(variantCode, mergedConfig);
|
|
134
126
|
|
|
135
127
|
// Flatten the variant to get a flat file structure
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
title
|
|
139
|
-
description
|
|
140
|
-
flattenedFiles
|
|
141
|
-
rootFile
|
|
128
|
+
const flattenedFiles = flattenCodeVariant(exported);
|
|
129
|
+
const stackBlitzDemo = createStackBlitz({
|
|
130
|
+
title,
|
|
131
|
+
description,
|
|
132
|
+
flattenedFiles,
|
|
133
|
+
rootFile
|
|
142
134
|
});
|
|
143
135
|
openWithForm(stackBlitzDemo);
|
|
144
136
|
}, [effectiveCode, code.selectedVariant, code.selectedTransform, contentProps.name, commonExportConfig, stackBlitzExportConfig]);
|
|
145
137
|
|
|
146
138
|
// Create CodeSandbox demo callback
|
|
147
|
-
|
|
139
|
+
const openCodeSandbox = React.useCallback(() => {
|
|
148
140
|
// Get the current variant code
|
|
149
|
-
|
|
141
|
+
const variantCode = effectiveCode[code.selectedVariant];
|
|
150
142
|
if (!variantCode || typeof variantCode === 'string') {
|
|
151
143
|
console.warn('No valid variant code available for CodeSandbox demo');
|
|
152
144
|
return;
|
|
153
145
|
}
|
|
154
|
-
|
|
155
|
-
|
|
146
|
+
const title = contentProps.name || 'Demo';
|
|
147
|
+
const description = `${title} demo`;
|
|
156
148
|
|
|
157
149
|
// Determine if we should use TypeScript based on whether 'js' transform is NOT applied
|
|
158
150
|
// If 'js' transform is applied, it means we're showing the JS version of TS code
|
|
159
|
-
|
|
151
|
+
const useTypescript = code.selectedTransform !== 'js';
|
|
160
152
|
|
|
161
153
|
// Merge common export config with CodeSandbox-specific config
|
|
162
|
-
|
|
154
|
+
const mergedConfig = {
|
|
155
|
+
...commonExportConfig,
|
|
156
|
+
...codeSandboxExportConfig,
|
|
163
157
|
variantName: code.selectedVariant,
|
|
164
|
-
title
|
|
165
|
-
description
|
|
166
|
-
useTypescript
|
|
167
|
-
}
|
|
158
|
+
title,
|
|
159
|
+
description,
|
|
160
|
+
useTypescript
|
|
161
|
+
};
|
|
168
162
|
|
|
169
163
|
// Use custom export function if provided, otherwise use default exportVariantAsCra
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
craExport
|
|
173
|
-
rootFile
|
|
164
|
+
const exportFunction = mergedConfig.exportFunction || exportVariantAsCra;
|
|
165
|
+
const {
|
|
166
|
+
exported: craExport,
|
|
167
|
+
rootFile
|
|
168
|
+
} = exportFunction(variantCode, mergedConfig);
|
|
174
169
|
|
|
175
170
|
// Flatten the variant to get a flat file structure
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
flattenedFiles
|
|
179
|
-
rootFile
|
|
171
|
+
const flattenedFiles = flattenCodeVariant(craExport);
|
|
172
|
+
const codeSandboxDemo = createCodeSandbox({
|
|
173
|
+
flattenedFiles,
|
|
174
|
+
rootFile
|
|
180
175
|
});
|
|
181
176
|
openWithForm(codeSandboxDemo);
|
|
182
177
|
}, [effectiveCode, code.selectedVariant, code.selectedTransform, contentProps.name, commonExportConfig, codeSandboxExportConfig]);
|
|
183
|
-
return
|
|
178
|
+
return {
|
|
179
|
+
...code,
|
|
184
180
|
// Demo-specific additions
|
|
185
|
-
component
|
|
186
|
-
ref
|
|
187
|
-
resetFocus
|
|
188
|
-
openStackBlitz
|
|
189
|
-
openCodeSandbox
|
|
181
|
+
component,
|
|
182
|
+
ref,
|
|
183
|
+
resetFocus,
|
|
184
|
+
openStackBlitz,
|
|
185
|
+
openCodeSandbox,
|
|
190
186
|
name: contentProps.name,
|
|
191
|
-
slug
|
|
192
|
-
}
|
|
187
|
+
slug
|
|
188
|
+
};
|
|
193
189
|
}
|
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
'use client';
|
|
2
2
|
|
|
3
3
|
import * as React from 'react';
|
|
4
|
-
export
|
|
4
|
+
export const CodeErrorsContext = /*#__PURE__*/React.createContext(undefined);
|
|
5
5
|
if (process.env.NODE_ENV !== "production") CodeErrorsContext.displayName = "CodeErrorsContext";
|
|
6
6
|
export function useErrorsContext() {
|
|
7
7
|
return React.useContext(CodeErrorsContext);
|
|
@@ -1,11 +1,11 @@
|
|
|
1
1
|
import { useErrorsContext } from "./ErrorsContext.js";
|
|
2
2
|
export function useErrors(props) {
|
|
3
|
-
|
|
3
|
+
const context = useErrorsContext();
|
|
4
4
|
|
|
5
5
|
// Context errors take precedence over prop errors
|
|
6
6
|
// This ensures client-side errors override server-side errors
|
|
7
|
-
|
|
7
|
+
const errors = context?.errors || props?.errors;
|
|
8
8
|
return {
|
|
9
|
-
errors
|
|
9
|
+
errors
|
|
10
10
|
};
|
|
11
11
|
}
|
|
@@ -1,6 +1,5 @@
|
|
|
1
1
|
'use client';
|
|
2
2
|
|
|
3
|
-
import _slicedToArray from "@babel/runtime/helpers/esm/slicedToArray";
|
|
4
3
|
import * as React from 'react';
|
|
5
4
|
|
|
6
5
|
/**
|
|
@@ -8,9 +7,9 @@ import * as React from 'react';
|
|
|
8
7
|
*/
|
|
9
8
|
|
|
10
9
|
// storage events only work across tabs, we'll use an event emitter to announce within the current tab
|
|
11
|
-
|
|
10
|
+
const currentTabChangeListeners = new Map();
|
|
12
11
|
function onCurrentTabStorageChange(key, handler) {
|
|
13
|
-
|
|
12
|
+
let listeners = currentTabChangeListeners.get(key);
|
|
14
13
|
if (!listeners) {
|
|
15
14
|
listeners = new Set();
|
|
16
15
|
currentTabChangeListeners.set(key, listeners);
|
|
@@ -18,35 +17,33 @@ function onCurrentTabStorageChange(key, handler) {
|
|
|
18
17
|
listeners.add(handler);
|
|
19
18
|
}
|
|
20
19
|
function offCurrentTabStorageChange(key, handler) {
|
|
21
|
-
|
|
20
|
+
const listeners = currentTabChangeListeners.get(key);
|
|
22
21
|
if (!listeners) {
|
|
23
22
|
return;
|
|
24
23
|
}
|
|
25
|
-
listeners
|
|
24
|
+
listeners.delete(handler);
|
|
26
25
|
if (listeners.size === 0) {
|
|
27
|
-
currentTabChangeListeners
|
|
26
|
+
currentTabChangeListeners.delete(key);
|
|
28
27
|
}
|
|
29
28
|
}
|
|
30
29
|
function emitCurrentTabStorageChange(key) {
|
|
31
|
-
|
|
30
|
+
const listeners = currentTabChangeListeners.get(key);
|
|
32
31
|
if (listeners) {
|
|
33
|
-
listeners.forEach(
|
|
34
|
-
return listener();
|
|
35
|
-
});
|
|
32
|
+
listeners.forEach(listener => listener());
|
|
36
33
|
}
|
|
37
34
|
}
|
|
38
35
|
function subscribe(area, key, callback) {
|
|
39
36
|
if (!key) {
|
|
40
|
-
return
|
|
37
|
+
return () => {};
|
|
41
38
|
}
|
|
42
|
-
|
|
39
|
+
const storageHandler = event => {
|
|
43
40
|
if (event.storageArea === area && event.key === key) {
|
|
44
41
|
callback();
|
|
45
42
|
}
|
|
46
43
|
};
|
|
47
44
|
window.addEventListener('storage', storageHandler);
|
|
48
45
|
onCurrentTabStorageChange(key, callback);
|
|
49
|
-
return
|
|
46
|
+
return () => {
|
|
50
47
|
window.removeEventListener('storage', storageHandler);
|
|
51
48
|
offCurrentTabStorageChange(key, callback);
|
|
52
49
|
};
|
|
@@ -57,7 +54,7 @@ function getSnapshot(area, key) {
|
|
|
57
54
|
}
|
|
58
55
|
try {
|
|
59
56
|
return area.getItem(key);
|
|
60
|
-
} catch
|
|
57
|
+
} catch {
|
|
61
58
|
// ignore
|
|
62
59
|
// See https://developer.mozilla.org/en-US/docs/Web/API/Web_Storage_API/Using_the_Web_Storage_API#feature-detecting_localstorage
|
|
63
60
|
return null;
|
|
@@ -73,14 +70,14 @@ function setValue(area, key, value) {
|
|
|
73
70
|
} else {
|
|
74
71
|
area.setItem(key, String(value));
|
|
75
72
|
}
|
|
76
|
-
} catch
|
|
73
|
+
} catch {
|
|
77
74
|
// ignore
|
|
78
75
|
// See https://developer.mozilla.org/en-US/docs/Web/API/Web_Storage_API/Using_the_Web_Storage_API#feature-detecting_localstorage
|
|
79
76
|
return;
|
|
80
77
|
}
|
|
81
78
|
emitCurrentTabStorageChange(key);
|
|
82
79
|
}
|
|
83
|
-
|
|
80
|
+
const serverValue = [null, () => {}];
|
|
84
81
|
function useLocalStorageStateServer() {
|
|
85
82
|
return serverValue;
|
|
86
83
|
}
|
|
@@ -93,33 +90,20 @@ function useLocalStorageStateServer() {
|
|
|
93
90
|
* Since the storage API isn't available in server-rendering environments, we
|
|
94
91
|
* return null during SSR and hydration.
|
|
95
92
|
*/
|
|
96
|
-
function useLocalStorageStateBrowser(key) {
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
var area = window.localStorage;
|
|
102
|
-
var subscribeKey = React.useCallback(function (callback) {
|
|
103
|
-
return subscribe(area, key, callback);
|
|
104
|
-
}, [area, key]);
|
|
105
|
-
var getKeySnapshot = React.useCallback(function () {
|
|
106
|
-
var _getSnapshot;
|
|
107
|
-
return (_getSnapshot = getSnapshot(area, key)) != null ? _getSnapshot : initialValue;
|
|
108
|
-
}, [area, initialValue, key]);
|
|
93
|
+
function useLocalStorageStateBrowser(key, initializer = null) {
|
|
94
|
+
const [initialValue] = React.useState(initializer);
|
|
95
|
+
const area = window.localStorage;
|
|
96
|
+
const subscribeKey = React.useCallback(callback => subscribe(area, key, callback), [area, key]);
|
|
97
|
+
const getKeySnapshot = React.useCallback(() => getSnapshot(area, key) ?? initialValue, [area, initialValue, key]);
|
|
109
98
|
|
|
110
99
|
// Start with null for the hydration, and then switch to the actual value.
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
var setStoredValue = React.useCallback(function (value) {
|
|
116
|
-
var valueToStore = value instanceof Function ? value(storedValue) : value;
|
|
100
|
+
const getKeyServerSnapshot = () => null;
|
|
101
|
+
const storedValue = React.useSyncExternalStore(subscribeKey, getKeySnapshot, getKeyServerSnapshot);
|
|
102
|
+
const setStoredValue = React.useCallback(value => {
|
|
103
|
+
const valueToStore = value instanceof Function ? value(storedValue) : value;
|
|
117
104
|
setValue(area, key, valueToStore);
|
|
118
105
|
}, [area, key, storedValue]);
|
|
119
|
-
|
|
120
|
-
_React$useState4 = _slicedToArray(_React$useState3, 2),
|
|
121
|
-
nonStoredValue = _React$useState4[0],
|
|
122
|
-
setNonStoredValue = _React$useState4[1];
|
|
106
|
+
const [nonStoredValue, setNonStoredValue] = React.useState(initialValue);
|
|
123
107
|
if (!key) {
|
|
124
108
|
return [nonStoredValue, setNonStoredValue];
|
|
125
109
|
}
|
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
'use client';
|
|
2
2
|
|
|
3
3
|
import * as React from 'react';
|
|
4
|
-
export
|
|
4
|
+
export const PreferencesContext = /*#__PURE__*/React.createContext(undefined);
|
|
5
5
|
if (process.env.NODE_ENV !== "production") PreferencesContext.displayName = "PreferencesContext";
|
|
6
6
|
export function usePreferences() {
|
|
7
7
|
return React.useContext(PreferencesContext);
|
|
@@ -1,25 +1,23 @@
|
|
|
1
1
|
'use client';
|
|
2
2
|
|
|
3
|
-
import _toConsumableArray from "@babel/runtime/helpers/esm/toConsumableArray";
|
|
4
3
|
import * as React from 'react';
|
|
5
4
|
import useLocalStorageState from "../useLocalStorageState/index.js";
|
|
6
5
|
import { usePreferences } from "./PreferencesProvider.js";
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
export function usePreference(type, name) {
|
|
10
|
-
|
|
11
|
-
var key = React.useMemo(function () {
|
|
6
|
+
const variantPrefPrefix = '_docs_variant_pref';
|
|
7
|
+
const transformPref = '_docs_transform_pref';
|
|
8
|
+
export function usePreference(type, name, initializer = null) {
|
|
9
|
+
const key = React.useMemo(() => {
|
|
12
10
|
if (!Array.isArray(name)) {
|
|
13
11
|
return name;
|
|
14
12
|
}
|
|
15
13
|
if (name.length <= 1) {
|
|
16
14
|
return null; // Don't use localStorage for single variants - no choice to remember
|
|
17
15
|
}
|
|
18
|
-
return
|
|
16
|
+
return [...name].sort().join(':');
|
|
19
17
|
}, [name]);
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
18
|
+
const preferences = usePreferences();
|
|
19
|
+
const defaultPrefix = type === 'variant' ? variantPrefPrefix : transformPref;
|
|
20
|
+
const prefix = preferences?.prefix ? `${preferences?.prefix}_${type}` : defaultPrefix;
|
|
21
|
+
const storageKey = key ? `${prefix}:${key}` : null;
|
|
24
22
|
return useLocalStorageState(storageKey, initializer);
|
|
25
23
|
}
|