@mui/internal-docs-infra 0.2.1 → 0.2.3-canary.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/CHANGELOG.md CHANGED
@@ -1,5 +1,9 @@
1
1
  # Versions
2
2
 
3
+ ## 2.0.8
4
+
5
+ Test release
6
+
3
7
  ## 2.0.7
4
8
 
5
9
  Test release
@@ -67,7 +67,8 @@ export function useCode(contentProps, opts) {
67
67
  var variantSelection = useVariantSelection({
68
68
  effectiveCode: effectiveCode,
69
69
  initialVariant: initialVariant,
70
- variantType: contentProps.variantType
70
+ variantType: contentProps.variantType,
71
+ mainSlug: userProps.slug
71
72
  });
72
73
 
73
74
  // Sub-hook: Transform Management
@@ -1,6 +1,20 @@
1
1
  import * as React from 'react';
2
2
  import type { VariantCode, VariantSource, Code } from "../CodeHighlighter/types.js";
3
3
  import type { TransformedFiles } from "./useCodeUtils.js";
4
+ /**
5
+ * Converts a string to kebab-case
6
+ * @param str - The string to convert
7
+ * @returns kebab-case string
8
+ */
9
+ export declare function toKebabCase(str: string): string;
10
+ /**
11
+ * Checks if the URL hash is relevant to a specific demo
12
+ * Hash format is: {mainSlug}:{variantName}:{fileName} or {mainSlug}:{fileName}
13
+ * @param urlHash - The URL hash (without '#')
14
+ * @param mainSlug - The main slug for the demo
15
+ * @returns true if the hash starts with the demo's slug
16
+ */
17
+ export declare function isHashRelevantToDemo(urlHash: string | null, mainSlug?: string): boolean;
4
18
  interface UseFileNavigationProps {
5
19
  selectedVariant: VariantCode | null;
6
20
  transformedFiles: TransformedFiles | undefined;
@@ -14,12 +14,27 @@ import { Pre } from "./Pre.js";
14
14
  * @returns kebab-case string
15
15
  */
16
16
  import { jsx as _jsx } from "react/jsx-runtime";
17
- function toKebabCase(str) {
17
+ export function toKebabCase(str) {
18
18
  return str
19
19
  // Insert a dash before any uppercase letter that follows a lowercase letter or digit
20
20
  .replace(/([a-z0-9])([A-Z])/g, '$1-$2').toLowerCase().replace(/[^a-z0-9.]+/g, '-').replace(/^-+|-+$/g, '');
21
21
  }
22
22
 
23
+ /**
24
+ * Checks if the URL hash is relevant to a specific demo
25
+ * Hash format is: {mainSlug}:{variantName}:{fileName} or {mainSlug}:{fileName}
26
+ * @param urlHash - The URL hash (without '#')
27
+ * @param mainSlug - The main slug for the demo
28
+ * @returns true if the hash starts with the demo's slug
29
+ */
30
+ export function isHashRelevantToDemo(urlHash, mainSlug) {
31
+ if (!urlHash || !mainSlug) {
32
+ return false;
33
+ }
34
+ var kebabSlug = toKebabCase(mainSlug);
35
+ return urlHash.startsWith("".concat(kebabSlug, ":"));
36
+ }
37
+
23
38
  /**
24
39
  * Generates a file slug based on main slug, file name, and variant name
25
40
  * @param mainSlug - The main component/demo slug
@@ -274,8 +289,7 @@ export function useFileNavigation(_ref) {
274
289
  if (fileSlug && hash !== fileSlug) {
275
290
  // Only update if current hash is for the same demo (starts with mainSlug)
276
291
  // 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, ":"))) {
292
+ if (isHashRelevantToDemo(hash, mainSlug)) {
279
293
  setHash(fileSlug);
280
294
  }
281
295
  // Otherwise, don't update - either no hash exists or hash is for a different demo
@@ -4,6 +4,7 @@ interface UseVariantSelectionProps {
4
4
  effectiveCode: Code;
5
5
  initialVariant?: string;
6
6
  variantType?: string;
7
+ mainSlug?: string;
7
8
  }
8
9
  export interface UseVariantSelectionResult {
9
10
  variantKeys: string[];
@@ -19,6 +20,7 @@ export interface UseVariantSelectionResult {
19
20
  export declare function useVariantSelection({
20
21
  effectiveCode,
21
22
  initialVariant,
22
- variantType
23
+ variantType,
24
+ mainSlug
23
25
  }: UseVariantSelectionProps): UseVariantSelectionResult;
24
26
  export {};
@@ -2,6 +2,8 @@ import _slicedToArray from "@babel/runtime/helpers/esm/slicedToArray";
2
2
  import _typeof from "@babel/runtime/helpers/esm/typeof";
3
3
  import * as React from 'react';
4
4
  import { usePreference } from "../usePreference/index.js";
5
+ import { useUrlHashState } from "../useUrlHashState/index.js";
6
+ import { isHashRelevantToDemo } from "./useFileNavigation.js";
5
7
  /**
6
8
  * Hook for managing variant selection and providing variant-related data
7
9
  * Uses React state as source of truth, with localStorage for persistence
@@ -9,7 +11,8 @@ import { usePreference } from "../usePreference/index.js";
9
11
  export function useVariantSelection(_ref) {
10
12
  var effectiveCode = _ref.effectiveCode,
11
13
  initialVariant = _ref.initialVariant,
12
- variantType = _ref.variantType;
14
+ variantType = _ref.variantType,
15
+ mainSlug = _ref.mainSlug;
13
16
  // Get variant keys from effective code
14
17
  var variantKeys = React.useMemo(function () {
15
18
  return Object.keys(effectiveCode).filter(function (key) {
@@ -18,6 +21,15 @@ export function useVariantSelection(_ref) {
18
21
  });
19
22
  }, [effectiveCode]);
20
23
 
24
+ // Check if there's a URL hash present that's relevant to this demo
25
+ // Only override localStorage if hash starts with this demo's slug
26
+ var _useUrlHashState = useUrlHashState(),
27
+ _useUrlHashState2 = _slicedToArray(_useUrlHashState, 1),
28
+ urlHash = _useUrlHashState2[0];
29
+ var hasRelevantUrlHash = React.useMemo(function () {
30
+ return isHashRelevantToDemo(urlHash, mainSlug);
31
+ }, [urlHash, mainSlug]);
32
+
21
33
  // Use localStorage hook for variant persistence
22
34
  var _usePreference = usePreference('variant', variantType || variantKeys, function () {
23
35
  return null;
@@ -26,14 +38,9 @@ export function useVariantSelection(_ref) {
26
38
  storedValue = _usePreference2[0],
27
39
  setStoredValue = _usePreference2[1];
28
40
 
29
- // Initialize state from localStorage or initialVariant
41
+ // Initialize state - will be updated by effect if localStorage should be used
30
42
  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
- }
35
-
36
- // Second priority: use initial variant if provided and valid
43
+ // Use initial variant if provided and valid
37
44
  if (initialVariant && variantKeys.includes(initialVariant)) {
38
45
  return initialVariant;
39
46
  }
@@ -45,17 +52,44 @@ export function useVariantSelection(_ref) {
45
52
  selectedVariantKey = _React$useState2[0],
46
53
  setSelectedVariantKeyState = _React$useState2[1];
47
54
 
48
- // Sync with localStorage changes (but don't override programmatic changes)
55
+ // On mount, check if we should restore from localStorage
56
+ // This needs to be in an effect because we need to check hasRelevantUrlHash which depends on urlHash
57
+ var _React$useState3 = React.useState(false),
58
+ _React$useState4 = _slicedToArray(_React$useState3, 2),
59
+ hasInitialized = _React$useState4[0],
60
+ setHasInitialized = _React$useState4[1];
61
+ React.useEffect(function () {
62
+ if (hasInitialized) {
63
+ return;
64
+ }
65
+ setHasInitialized(true);
66
+
67
+ // If there's a relevant URL hash, don't use localStorage - hash takes priority
68
+ if (hasRelevantUrlHash) {
69
+ return;
70
+ }
71
+
72
+ // If we have a stored value, use it (localStorage takes priority over initialVariant)
73
+ if (storedValue && variantKeys.includes(storedValue)) {
74
+ setSelectedVariantKeyState(storedValue);
75
+ }
76
+ }, [hasInitialized, hasRelevantUrlHash, storedValue, variantKeys]);
77
+
78
+ // Sync with localStorage changes (but don't override programmatic changes or when hash is present)
49
79
  // Only sync when storedValue changes, not when selectedVariantKey changes
50
80
  var prevStoredValue = React.useRef(storedValue);
51
81
  React.useEffect(function () {
82
+ // Don't sync from localStorage when a relevant URL hash is present - hash takes absolute priority
83
+ if (hasRelevantUrlHash) {
84
+ return;
85
+ }
52
86
  if (storedValue !== prevStoredValue.current) {
53
87
  prevStoredValue.current = storedValue;
54
88
  if (storedValue && variantKeys.includes(storedValue) && storedValue !== selectedVariantKey) {
55
89
  setSelectedVariantKeyState(storedValue);
56
90
  }
57
91
  }
58
- }, [storedValue, variantKeys, selectedVariantKey]);
92
+ }, [storedValue, variantKeys, selectedVariantKey, hasRelevantUrlHash]);
59
93
  var setSelectedVariantKeyProgrammatic = React.useCallback(function (value) {
60
94
  var resolvedValue = typeof value === 'function' ? value(selectedVariantKey) : value;
61
95
  if (variantKeys.includes(resolvedValue)) {
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.3-canary.0",
4
4
  "author": "MUI Team",
5
5
  "description": "MUI Infra - internal documentation creation tools.",
6
6
  "keywords": [
@@ -53,6 +53,7 @@
53
53
  "engines": {
54
54
  "node": ">=22.12.0"
55
55
  },
56
+ "gitSha": "ac83e0cbcf905f4d7a2023de4c7dc600105f86e8",
56
57
  "type": "commonjs",
57
58
  "exports": {
58
59
  "./package.json": "./package.json",