@mui/internal-docs-infra 0.2.1-canary.3 → 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/esm/pipeline/parseSource/addLineGutters.js +5 -5
- package/esm/useCode/useCode.d.ts +5 -0
- package/esm/useCode/useCode.js +4 -1
- package/esm/useCode/useFileNavigation.d.ts +11 -2
- package/esm/useCode/useFileNavigation.js +184 -40
- package/esm/useCode/useVariantSelection.d.ts +2 -1
- package/esm/useCode/useVariantSelection.js +48 -26
- package/esm/useDemo/useDemo.d.ts +5 -0
- package/package.json +1 -2
|
@@ -129,9 +129,9 @@ export function starryNightGutter(tree, sourceLines) {
|
|
|
129
129
|
for (_iterator.s(); !(_step = _iterator.n()).done;) {
|
|
130
130
|
var _frame$properties;
|
|
131
131
|
var frame = _step.value;
|
|
132
|
-
if (frame.type === 'element' && frame.tagName === 'span' && ((_frame$properties = frame.properties) == null ? void 0 : _frame$properties.className) === 'frame' && typeof frame.properties.
|
|
133
|
-
var startLine = frame.properties.
|
|
134
|
-
var endLine = frame.properties.
|
|
132
|
+
if (frame.type === 'element' && frame.tagName === 'span' && ((_frame$properties = frame.properties) == null ? void 0 : _frame$properties.className) === 'frame' && typeof frame.properties.dataFrameStartLine === 'number' && typeof frame.properties.dataFrameEndLine === 'number') {
|
|
133
|
+
var startLine = frame.properties.dataFrameStartLine - 1; // Convert to 0-based index
|
|
134
|
+
var endLine = frame.properties.dataFrameEndLine; // This is already inclusive
|
|
135
135
|
frame.properties.dataAsString = sourceLines.slice(startLine, endLine).join('\n');
|
|
136
136
|
}
|
|
137
137
|
}
|
|
@@ -169,8 +169,8 @@ function createFrame(frameChildren, sourceLines, startLine, endLine) {
|
|
|
169
169
|
|
|
170
170
|
// Store line range information if provided (for dataAsString generation)
|
|
171
171
|
if (sourceLines && startLine !== undefined && endLine !== undefined) {
|
|
172
|
-
properties.
|
|
173
|
-
properties.
|
|
172
|
+
properties.dataFrameStartLine = startLine;
|
|
173
|
+
properties.dataFrameEndLine = endLine;
|
|
174
174
|
}
|
|
175
175
|
return {
|
|
176
176
|
type: 'element',
|
package/esm/useCode/useCode.d.ts
CHANGED
|
@@ -27,6 +27,11 @@ export interface UseCodeResult<T extends {} = {}> {
|
|
|
27
27
|
selectedFileLines: number;
|
|
28
28
|
selectedFileName: string | undefined;
|
|
29
29
|
selectFileName: (fileName: string) => void;
|
|
30
|
+
allFilesSlugs: Array<{
|
|
31
|
+
fileName: string;
|
|
32
|
+
slug: string;
|
|
33
|
+
variantName: string;
|
|
34
|
+
}>;
|
|
30
35
|
expanded: boolean;
|
|
31
36
|
expand: () => void;
|
|
32
37
|
setExpanded: React.Dispatch<React.SetStateAction<boolean>>;
|
package/esm/useCode/useCode.js
CHANGED
|
@@ -86,11 +86,13 @@ export function useCode(contentProps, opts) {
|
|
|
86
86
|
transformedFiles: transformManagement.transformedFiles,
|
|
87
87
|
mainSlug: userProps.slug,
|
|
88
88
|
selectedVariantKey: variantSelection.selectedVariantKey,
|
|
89
|
+
selectVariant: variantSelection.selectVariantProgrammatic,
|
|
89
90
|
variantKeys: variantSelection.variantKeys,
|
|
90
91
|
initialVariant: initialVariant,
|
|
91
92
|
shouldHighlight: shouldHighlight,
|
|
92
93
|
preClassName: preClassName,
|
|
93
|
-
preRef: preRef
|
|
94
|
+
preRef: preRef,
|
|
95
|
+
effectiveCode: effectiveCode
|
|
94
96
|
});
|
|
95
97
|
|
|
96
98
|
// Sub-hook: Copy Functionality
|
|
@@ -115,6 +117,7 @@ export function useCode(contentProps, opts) {
|
|
|
115
117
|
selectedFileLines: fileNavigation.selectedFileLines,
|
|
116
118
|
selectedFileName: fileNavigation.selectedFileName,
|
|
117
119
|
selectFileName: fileNavigation.selectFileName,
|
|
120
|
+
allFilesSlugs: fileNavigation.allFilesSlugs,
|
|
118
121
|
expanded: uiState.expanded,
|
|
119
122
|
expand: uiState.expand,
|
|
120
123
|
setExpanded: uiState.setExpanded,
|
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import * as React from 'react';
|
|
2
|
-
import type { VariantCode, VariantSource } from "../CodeHighlighter/types.js";
|
|
2
|
+
import type { VariantCode, VariantSource, Code } from "../CodeHighlighter/types.js";
|
|
3
3
|
import type { TransformedFiles } from "./useCodeUtils.js";
|
|
4
4
|
interface UseFileNavigationProps {
|
|
5
5
|
selectedVariant: VariantCode | null;
|
|
@@ -11,6 +11,8 @@ interface UseFileNavigationProps {
|
|
|
11
11
|
initialVariant?: string;
|
|
12
12
|
preClassName?: string;
|
|
13
13
|
preRef?: React.Ref<HTMLPreElement>;
|
|
14
|
+
effectiveCode?: Code;
|
|
15
|
+
selectVariant?: React.Dispatch<React.SetStateAction<string>>;
|
|
14
16
|
}
|
|
15
17
|
export interface UseFileNavigationResult {
|
|
16
18
|
selectedFileName: string | undefined;
|
|
@@ -23,6 +25,11 @@ export interface UseFileNavigationResult {
|
|
|
23
25
|
component: React.ReactNode;
|
|
24
26
|
}>;
|
|
25
27
|
selectFileName: (fileName: string) => void;
|
|
28
|
+
allFilesSlugs: Array<{
|
|
29
|
+
fileName: string;
|
|
30
|
+
slug: string;
|
|
31
|
+
variantName: string;
|
|
32
|
+
}>;
|
|
26
33
|
}
|
|
27
34
|
/**
|
|
28
35
|
* Hook for managing file selection and navigation within a code variant
|
|
@@ -36,6 +43,8 @@ export declare function useFileNavigation({
|
|
|
36
43
|
initialVariant,
|
|
37
44
|
shouldHighlight,
|
|
38
45
|
preClassName,
|
|
39
|
-
preRef
|
|
46
|
+
preRef,
|
|
47
|
+
effectiveCode,
|
|
48
|
+
selectVariant
|
|
40
49
|
}: UseFileNavigationProps): UseFileNavigationResult;
|
|
41
50
|
export {};
|
|
@@ -68,7 +68,9 @@ export function useFileNavigation(_ref) {
|
|
|
68
68
|
initialVariant = _ref.initialVariant,
|
|
69
69
|
shouldHighlight = _ref.shouldHighlight,
|
|
70
70
|
preClassName = _ref.preClassName,
|
|
71
|
-
preRef = _ref.preRef
|
|
71
|
+
preRef = _ref.preRef,
|
|
72
|
+
effectiveCode = _ref.effectiveCode,
|
|
73
|
+
selectVariant = _ref.selectVariant;
|
|
72
74
|
// Keep selectedFileName as untransformed filename for internal tracking
|
|
73
75
|
var _React$useState = React.useState(selectedVariant == null ? void 0 : selectedVariant.fileName),
|
|
74
76
|
_React$useState2 = _slicedToArray(_React$useState, 2),
|
|
@@ -92,70 +94,151 @@ export function useFileNavigation(_ref) {
|
|
|
92
94
|
hash = _useUrlHashState2[0],
|
|
93
95
|
setHash = _useUrlHashState2[1];
|
|
94
96
|
|
|
97
|
+
// Track if we're waiting for a variant switch to complete, and which file to select after
|
|
98
|
+
var pendingFileSelection = React.useRef(null);
|
|
99
|
+
var justCompletedPendingSelection = React.useRef(false);
|
|
100
|
+
|
|
95
101
|
// Helper function to check URL hash and switch to matching file
|
|
96
102
|
var checkUrlHashAndSelectFile = React.useCallback(function () {
|
|
97
|
-
if (!
|
|
103
|
+
if (!hash) {
|
|
98
104
|
return;
|
|
99
105
|
}
|
|
100
106
|
|
|
101
|
-
//
|
|
107
|
+
// Try to find matching file - check current variant first
|
|
102
108
|
var matchingFileName;
|
|
109
|
+
var matchingVariantKey;
|
|
110
|
+
|
|
111
|
+
// Step 1: Check current variant (if we have one)
|
|
112
|
+
if (selectedVariant) {
|
|
113
|
+
var isInitialVariant = initialVariant ? selectedVariantKey === initialVariant : variantKeys.length === 0 || selectedVariantKey === variantKeys[0];
|
|
114
|
+
|
|
115
|
+
// Check main file
|
|
116
|
+
if (selectedVariant.fileName) {
|
|
117
|
+
var mainFileSlug = generateFileSlug(mainSlug, selectedVariant.fileName, selectedVariantKey, isInitialVariant);
|
|
118
|
+
if (hash === mainFileSlug) {
|
|
119
|
+
matchingFileName = selectedVariant.fileName;
|
|
120
|
+
matchingVariantKey = selectedVariantKey;
|
|
121
|
+
}
|
|
122
|
+
}
|
|
103
123
|
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
124
|
+
// Check extra files
|
|
125
|
+
if (!matchingFileName && selectedVariant.extraFiles) {
|
|
126
|
+
for (var _i = 0, _Object$keys = Object.keys(selectedVariant.extraFiles); _i < _Object$keys.length; _i++) {
|
|
127
|
+
var fileName = _Object$keys[_i];
|
|
128
|
+
var fileSlug = generateFileSlug(mainSlug, fileName, selectedVariantKey, isInitialVariant);
|
|
129
|
+
if (hash === fileSlug) {
|
|
130
|
+
matchingFileName = fileName;
|
|
131
|
+
matchingVariantKey = selectedVariantKey;
|
|
132
|
+
break;
|
|
133
|
+
}
|
|
134
|
+
}
|
|
112
135
|
}
|
|
113
|
-
}
|
|
114
136
|
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
137
|
+
// Check transformed files
|
|
138
|
+
if (!matchingFileName && transformedFiles) {
|
|
139
|
+
var _iterator = _createForOfIteratorHelper(transformedFiles.files),
|
|
140
|
+
_step;
|
|
141
|
+
try {
|
|
142
|
+
for (_iterator.s(); !(_step = _iterator.n()).done;) {
|
|
143
|
+
var file = _step.value;
|
|
144
|
+
var _fileSlug = generateFileSlug(mainSlug, file.originalName, selectedVariantKey, isInitialVariant);
|
|
145
|
+
if (hash === _fileSlug) {
|
|
146
|
+
matchingFileName = file.originalName;
|
|
147
|
+
matchingVariantKey = selectedVariantKey;
|
|
148
|
+
break;
|
|
149
|
+
}
|
|
150
|
+
}
|
|
151
|
+
} catch (err) {
|
|
152
|
+
_iterator.e(err);
|
|
153
|
+
} finally {
|
|
154
|
+
_iterator.f();
|
|
123
155
|
}
|
|
124
156
|
}
|
|
125
157
|
}
|
|
126
158
|
|
|
127
|
-
//
|
|
128
|
-
if (!matchingFileName &&
|
|
129
|
-
var
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
159
|
+
// Step 2: If no match and we can switch variants, search other variants
|
|
160
|
+
if (!matchingFileName && effectiveCode && selectVariant) {
|
|
161
|
+
for (var _i2 = 0, _Object$entries = Object.entries(effectiveCode); _i2 < _Object$entries.length; _i2++) {
|
|
162
|
+
var _Object$entries$_i = _slicedToArray(_Object$entries[_i2], 2),
|
|
163
|
+
variantKey = _Object$entries$_i[0],
|
|
164
|
+
variant = _Object$entries$_i[1];
|
|
165
|
+
// Skip current variant (already checked) and invalid variants
|
|
166
|
+
if (variantKey === selectedVariantKey || !variant || typeof variant === 'string') {
|
|
167
|
+
continue;
|
|
168
|
+
}
|
|
169
|
+
var _isInitialVariant = initialVariant ? variantKey === initialVariant : variantKeys.length === 0 || variantKey === variantKeys[0];
|
|
170
|
+
|
|
171
|
+
// Check main file
|
|
172
|
+
if (variant.fileName) {
|
|
173
|
+
var _mainFileSlug = generateFileSlug(mainSlug, variant.fileName, variantKey, _isInitialVariant);
|
|
174
|
+
if (hash === _mainFileSlug) {
|
|
175
|
+
matchingFileName = variant.fileName;
|
|
176
|
+
matchingVariantKey = variantKey;
|
|
137
177
|
break;
|
|
138
178
|
}
|
|
139
179
|
}
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
180
|
+
|
|
181
|
+
// Check extra files
|
|
182
|
+
if (!matchingFileName && variant.extraFiles) {
|
|
183
|
+
for (var _i3 = 0, _Object$keys2 = Object.keys(variant.extraFiles); _i3 < _Object$keys2.length; _i3++) {
|
|
184
|
+
var _fileName = _Object$keys2[_i3];
|
|
185
|
+
var _fileSlug2 = generateFileSlug(mainSlug, _fileName, variantKey, _isInitialVariant);
|
|
186
|
+
if (hash === _fileSlug2) {
|
|
187
|
+
matchingFileName = _fileName;
|
|
188
|
+
matchingVariantKey = variantKey;
|
|
189
|
+
break;
|
|
190
|
+
}
|
|
191
|
+
}
|
|
192
|
+
}
|
|
193
|
+
if (matchingFileName) {
|
|
194
|
+
break;
|
|
195
|
+
}
|
|
144
196
|
}
|
|
145
197
|
}
|
|
146
|
-
if (matchingFileName) {
|
|
198
|
+
if (matchingFileName && matchingVariantKey) {
|
|
199
|
+
// If the matching file is in a different variant, switch to that variant first
|
|
200
|
+
if (matchingVariantKey !== selectedVariantKey && selectVariant) {
|
|
201
|
+
// Remember which file to select after variant switch
|
|
202
|
+
pendingFileSelection.current = matchingFileName;
|
|
203
|
+
selectVariant(matchingVariantKey);
|
|
204
|
+
// Don't set the file here - it will be set after variant changes
|
|
205
|
+
return;
|
|
206
|
+
}
|
|
207
|
+
|
|
208
|
+
// Set the file if we're in the correct variant
|
|
209
|
+
pendingFileSelection.current = null;
|
|
147
210
|
setSelectedFileNameInternal(matchingFileName);
|
|
148
|
-
markUserInteraction();
|
|
211
|
+
markUserInteraction();
|
|
149
212
|
}
|
|
150
|
-
}, [selectedVariant,
|
|
213
|
+
}, [hash, selectedVariant, selectedVariantKey, variantKeys, initialVariant, mainSlug, transformedFiles, effectiveCode, selectVariant, markUserInteraction]);
|
|
151
214
|
|
|
152
|
-
//
|
|
215
|
+
// Run hash check when URL hash changes to select the matching file
|
|
216
|
+
// Only depends on hash to avoid re-running when the callback recreates due to variant state changes
|
|
153
217
|
React.useEffect(function () {
|
|
154
218
|
checkUrlHashAndSelectFile();
|
|
155
|
-
|
|
219
|
+
// eslint-disable-next-line react-hooks/exhaustive-deps
|
|
220
|
+
}, [hash]);
|
|
221
|
+
|
|
222
|
+
// When variant switches with a pending file selection, complete the file selection
|
|
223
|
+
React.useEffect(function () {
|
|
224
|
+
if (pendingFileSelection.current && selectedVariant) {
|
|
225
|
+
var fileToSelect = pendingFileSelection.current;
|
|
226
|
+
pendingFileSelection.current = null;
|
|
227
|
+
justCompletedPendingSelection.current = true;
|
|
228
|
+
setSelectedFileNameInternal(fileToSelect);
|
|
229
|
+
markUserInteraction();
|
|
230
|
+
} else {
|
|
231
|
+
justCompletedPendingSelection.current = false;
|
|
232
|
+
}
|
|
233
|
+
}, [selectedVariantKey, selectedVariant, markUserInteraction]);
|
|
156
234
|
|
|
157
235
|
// Reset selectedFileName when variant changes
|
|
158
236
|
React.useEffect(function () {
|
|
237
|
+
// Skip reset if we have a pending file selection from hash navigation
|
|
238
|
+
// OR if we just completed a pending file selection
|
|
239
|
+
if (pendingFileSelection.current || justCompletedPendingSelection.current) {
|
|
240
|
+
return;
|
|
241
|
+
}
|
|
159
242
|
if (selectedVariant && selectedFileNameInternal !== selectedVariant.fileName) {
|
|
160
243
|
// Only reset if current selectedFileName doesn't exist in the new variant
|
|
161
244
|
var hasFile = selectedVariant.fileName === selectedFileNameInternal || selectedFileNameInternal && selectedVariant.extraFiles && selectedVariant.extraFiles[selectedFileNameInternal];
|
|
@@ -189,7 +272,13 @@ export function useFileNavigation(_ref) {
|
|
|
189
272
|
|
|
190
273
|
// Only update the URL hash if it's different from current hash
|
|
191
274
|
if (fileSlug && hash !== fileSlug) {
|
|
192
|
-
|
|
275
|
+
// Only update if current hash is for the same demo (starts with mainSlug)
|
|
276
|
+
// Don't set hash if there's no existing hash - variant changes shouldn't add hashes
|
|
277
|
+
var expectedBaseSlug = toKebabCase(mainSlug);
|
|
278
|
+
if (hash && hash.startsWith("".concat(expectedBaseSlug, ":"))) {
|
|
279
|
+
setHash(fileSlug);
|
|
280
|
+
}
|
|
281
|
+
// Otherwise, don't update - either no hash exists or hash is for a different demo
|
|
193
282
|
}
|
|
194
283
|
}, [selectedVariant, selectedFileNameInternal, transformedFiles, mainSlug, selectedVariantKey, variantKeys, initialVariant, hasUserInteraction, setHash, hash]);
|
|
195
284
|
|
|
@@ -446,12 +535,67 @@ export function useFileNavigation(_ref) {
|
|
|
446
535
|
markUserInteraction(); // Mark that user has made an explicit selection
|
|
447
536
|
setSelectedFileNameInternal(targetFileName);
|
|
448
537
|
}, [selectedVariant, transformedFiles, mainSlug, selectedVariantKey, variantKeys, initialVariant, setHash, markUserInteraction, hash]);
|
|
538
|
+
|
|
539
|
+
// Memoized array of all file slugs for all variants
|
|
540
|
+
var allFilesSlugs = React.useMemo(function () {
|
|
541
|
+
var result = [];
|
|
542
|
+
if (!effectiveCode || !variantKeys.length) {
|
|
543
|
+
return result;
|
|
544
|
+
}
|
|
545
|
+
|
|
546
|
+
// Iterate through all variants
|
|
547
|
+
var _iterator2 = _createForOfIteratorHelper(variantKeys),
|
|
548
|
+
_step2;
|
|
549
|
+
try {
|
|
550
|
+
var _loop = function _loop() {
|
|
551
|
+
var variantKey = _step2.value;
|
|
552
|
+
var variant = effectiveCode[variantKey];
|
|
553
|
+
|
|
554
|
+
// Skip invalid variants
|
|
555
|
+
if (!variant || typeof variant === 'string') {
|
|
556
|
+
return 1; // continue
|
|
557
|
+
}
|
|
558
|
+
|
|
559
|
+
// Determine if this is the initial variant
|
|
560
|
+
var isInitialVariant = initialVariant ? variantKey === initialVariant : variantKeys.length === 0 || variantKey === variantKeys[0];
|
|
561
|
+
|
|
562
|
+
// Add main file if it exists
|
|
563
|
+
if (variant.fileName) {
|
|
564
|
+
result.push({
|
|
565
|
+
fileName: variant.fileName,
|
|
566
|
+
slug: generateFileSlug(mainSlug, variant.fileName, variantKey, isInitialVariant),
|
|
567
|
+
variantName: variantKey
|
|
568
|
+
});
|
|
569
|
+
}
|
|
570
|
+
|
|
571
|
+
// Add extra files
|
|
572
|
+
if (variant.extraFiles) {
|
|
573
|
+
Object.keys(variant.extraFiles).forEach(function (fileName) {
|
|
574
|
+
result.push({
|
|
575
|
+
fileName: fileName,
|
|
576
|
+
slug: generateFileSlug(mainSlug, fileName, variantKey, isInitialVariant),
|
|
577
|
+
variantName: variantKey
|
|
578
|
+
});
|
|
579
|
+
});
|
|
580
|
+
}
|
|
581
|
+
};
|
|
582
|
+
for (_iterator2.s(); !(_step2 = _iterator2.n()).done;) {
|
|
583
|
+
if (_loop()) continue;
|
|
584
|
+
}
|
|
585
|
+
} catch (err) {
|
|
586
|
+
_iterator2.e(err);
|
|
587
|
+
} finally {
|
|
588
|
+
_iterator2.f();
|
|
589
|
+
}
|
|
590
|
+
return result;
|
|
591
|
+
}, [effectiveCode, variantKeys, initialVariant, mainSlug]);
|
|
449
592
|
return {
|
|
450
593
|
selectedFileName: selectedFileName,
|
|
451
594
|
selectedFile: selectedFile,
|
|
452
595
|
selectedFileComponent: selectedFileComponent,
|
|
453
596
|
selectedFileLines: selectedFileLines,
|
|
454
597
|
files: files,
|
|
598
|
+
allFilesSlugs: allFilesSlugs,
|
|
455
599
|
selectFileName: selectFileName
|
|
456
600
|
};
|
|
457
601
|
}
|
|
@@ -10,10 +10,11 @@ export interface UseVariantSelectionResult {
|
|
|
10
10
|
selectedVariantKey: string;
|
|
11
11
|
selectedVariant: VariantCode | null;
|
|
12
12
|
selectVariant: React.Dispatch<React.SetStateAction<string>>;
|
|
13
|
+
selectVariantProgrammatic: React.Dispatch<React.SetStateAction<string>>;
|
|
13
14
|
}
|
|
14
15
|
/**
|
|
15
16
|
* Hook for managing variant selection and providing variant-related data
|
|
16
|
-
* Uses
|
|
17
|
+
* Uses React state as source of truth, with localStorage for persistence
|
|
17
18
|
*/
|
|
18
19
|
export declare function useVariantSelection({
|
|
19
20
|
effectiveCode,
|
|
@@ -4,7 +4,7 @@ import * as React from 'react';
|
|
|
4
4
|
import { usePreference } from "../usePreference/index.js";
|
|
5
5
|
/**
|
|
6
6
|
* Hook for managing variant selection and providing variant-related data
|
|
7
|
-
* Uses
|
|
7
|
+
* Uses React state as source of truth, with localStorage for persistence
|
|
8
8
|
*/
|
|
9
9
|
export function useVariantSelection(_ref) {
|
|
10
10
|
var effectiveCode = _ref.effectiveCode,
|
|
@@ -18,38 +18,59 @@ export function useVariantSelection(_ref) {
|
|
|
18
18
|
});
|
|
19
19
|
}, [effectiveCode]);
|
|
20
20
|
|
|
21
|
-
// Use localStorage hook for variant persistence
|
|
21
|
+
// Use localStorage hook for variant persistence
|
|
22
22
|
var _usePreference = usePreference('variant', variantType || variantKeys, function () {
|
|
23
|
-
// Don't use initialVariant as the fallback - localStorage should take precedence
|
|
24
|
-
// We'll handle the initial variant separately in the selectedVariantKey logic
|
|
25
23
|
return null;
|
|
26
24
|
}),
|
|
27
25
|
_usePreference2 = _slicedToArray(_usePreference, 2),
|
|
28
26
|
storedValue = _usePreference2[0],
|
|
29
27
|
setStoredValue = _usePreference2[1];
|
|
30
28
|
|
|
31
|
-
//
|
|
32
|
-
var
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
29
|
+
// Initialize state from localStorage or initialVariant
|
|
30
|
+
var _React$useState = React.useState(function () {
|
|
31
|
+
// First priority: use stored value if it exists and is valid
|
|
32
|
+
if (storedValue && variantKeys.includes(storedValue)) {
|
|
33
|
+
return storedValue;
|
|
34
|
+
}
|
|
37
35
|
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
36
|
+
// Second priority: use initial variant if provided and valid
|
|
37
|
+
if (initialVariant && variantKeys.includes(initialVariant)) {
|
|
38
|
+
return initialVariant;
|
|
39
|
+
}
|
|
42
40
|
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
41
|
+
// Final fallback: use first available variant
|
|
42
|
+
return variantKeys[0] || '';
|
|
43
|
+
}),
|
|
44
|
+
_React$useState2 = _slicedToArray(_React$useState, 2),
|
|
45
|
+
selectedVariantKey = _React$useState2[0],
|
|
46
|
+
setSelectedVariantKeyState = _React$useState2[1];
|
|
47
|
+
|
|
48
|
+
// Sync with localStorage changes (but don't override programmatic changes)
|
|
49
|
+
// Only sync when storedValue changes, not when selectedVariantKey changes
|
|
50
|
+
var prevStoredValue = React.useRef(storedValue);
|
|
51
|
+
React.useEffect(function () {
|
|
52
|
+
if (storedValue !== prevStoredValue.current) {
|
|
53
|
+
prevStoredValue.current = storedValue;
|
|
54
|
+
if (storedValue && variantKeys.includes(storedValue) && storedValue !== selectedVariantKey) {
|
|
55
|
+
setSelectedVariantKeyState(storedValue);
|
|
56
|
+
}
|
|
57
|
+
}
|
|
58
|
+
}, [storedValue, variantKeys, selectedVariantKey]);
|
|
59
|
+
var setSelectedVariantKeyProgrammatic = React.useCallback(function (value) {
|
|
60
|
+
var resolvedValue = typeof value === 'function' ? value(selectedVariantKey) : value;
|
|
61
|
+
if (variantKeys.includes(resolvedValue)) {
|
|
62
|
+
// Only update React state, not localStorage
|
|
63
|
+
// This prevents conflicts with hash-driven navigation
|
|
64
|
+
setSelectedVariantKeyState(resolvedValue);
|
|
65
|
+
}
|
|
66
|
+
}, [selectedVariantKey, variantKeys]);
|
|
49
67
|
var setSelectedVariantKeyAsUser = React.useCallback(function (value) {
|
|
50
68
|
var resolvedValue = typeof value === 'function' ? value(selectedVariantKey) : value;
|
|
51
|
-
|
|
52
|
-
|
|
69
|
+
if (variantKeys.includes(resolvedValue)) {
|
|
70
|
+
setSelectedVariantKeyState(resolvedValue);
|
|
71
|
+
setStoredValue(resolvedValue);
|
|
72
|
+
}
|
|
73
|
+
}, [setStoredValue, selectedVariantKey, variantKeys]);
|
|
53
74
|
var selectedVariant = React.useMemo(function () {
|
|
54
75
|
var variant = effectiveCode[selectedVariantKey];
|
|
55
76
|
if (variant && _typeof(variant) === 'object' && 'source' in variant) {
|
|
@@ -62,14 +83,15 @@ export function useVariantSelection(_ref) {
|
|
|
62
83
|
React.useEffect(function () {
|
|
63
84
|
if (!selectedVariant && variantKeys.length > 0) {
|
|
64
85
|
// Don't mark this as a user selection - it's just a fallback
|
|
65
|
-
// Use
|
|
66
|
-
|
|
86
|
+
// Use programmatic setter to avoid localStorage save
|
|
87
|
+
setSelectedVariantKeyProgrammatic(variantKeys[0]);
|
|
67
88
|
}
|
|
68
|
-
}, [selectedVariant, variantKeys,
|
|
89
|
+
}, [selectedVariant, variantKeys, setSelectedVariantKeyProgrammatic]);
|
|
69
90
|
return {
|
|
70
91
|
variantKeys: variantKeys,
|
|
71
92
|
selectedVariantKey: selectedVariantKey,
|
|
72
93
|
selectedVariant: selectedVariant,
|
|
73
|
-
selectVariant: setSelectedVariantKeyAsUser
|
|
94
|
+
selectVariant: setSelectedVariantKeyAsUser,
|
|
95
|
+
selectVariantProgrammatic: setSelectedVariantKeyProgrammatic
|
|
74
96
|
};
|
|
75
97
|
}
|
package/esm/useDemo/useDemo.d.ts
CHANGED
|
@@ -65,6 +65,11 @@ export declare function useDemo<T extends {} = {}>(contentProps: ContentProps<T>
|
|
|
65
65
|
selectedFileLines: number;
|
|
66
66
|
selectedFileName: string | undefined;
|
|
67
67
|
selectFileName: (fileName: string) => void;
|
|
68
|
+
allFilesSlugs: Array<{
|
|
69
|
+
fileName: string;
|
|
70
|
+
slug: string;
|
|
71
|
+
variantName: string;
|
|
72
|
+
}>;
|
|
68
73
|
expanded: boolean;
|
|
69
74
|
expand: () => void;
|
|
70
75
|
setExpanded: React.Dispatch<React.SetStateAction<boolean>>;
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@mui/internal-docs-infra",
|
|
3
|
-
"version": "0.2.1
|
|
3
|
+
"version": "0.2.1",
|
|
4
4
|
"author": "MUI Team",
|
|
5
5
|
"description": "MUI Infra - internal documentation creation tools.",
|
|
6
6
|
"keywords": [
|
|
@@ -53,7 +53,6 @@
|
|
|
53
53
|
"engines": {
|
|
54
54
|
"node": ">=22.12.0"
|
|
55
55
|
},
|
|
56
|
-
"gitSha": "f5f45d690b092f240dc070cb49571fa2283e6508",
|
|
57
56
|
"type": "commonjs",
|
|
58
57
|
"exports": {
|
|
59
58
|
"./package.json": "./package.json",
|