@docusaurus/theme-common 2.0.0-beta.21 → 2.0.0-beta.22

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.
Files changed (66) hide show
  1. package/lib/contexts/blogPost.d.ts +33 -0
  2. package/lib/contexts/blogPost.d.ts.map +1 -0
  3. package/lib/contexts/blogPost.js +46 -0
  4. package/lib/contexts/blogPost.js.map +1 -0
  5. package/lib/contexts/doc.d.ts +30 -0
  6. package/lib/contexts/doc.d.ts.map +1 -0
  7. package/lib/contexts/doc.js +48 -0
  8. package/lib/contexts/doc.js.map +1 -0
  9. package/lib/contexts/docsPreferredVersion.d.ts +2 -3
  10. package/lib/contexts/docsPreferredVersion.d.ts.map +1 -1
  11. package/lib/contexts/docsPreferredVersion.js +1 -1
  12. package/lib/contexts/docsPreferredVersion.js.map +1 -1
  13. package/lib/contexts/navbarSecondaryMenu/content.d.ts.map +1 -1
  14. package/lib/contexts/navbarSecondaryMenu/content.js +3 -9
  15. package/lib/contexts/navbarSecondaryMenu/content.js.map +1 -1
  16. package/lib/hooks/useCodeWordWrap.d.ts.map +1 -1
  17. package/lib/hooks/useCodeWordWrap.js +32 -6
  18. package/lib/hooks/useCodeWordWrap.js.map +1 -1
  19. package/lib/hooks/useMutationObserver.d.ts +4 -0
  20. package/lib/hooks/useMutationObserver.d.ts.map +1 -0
  21. package/lib/hooks/useMutationObserver.js +29 -0
  22. package/lib/hooks/useMutationObserver.js.map +1 -0
  23. package/lib/index.d.ts +9 -34
  24. package/lib/index.d.ts.map +1 -1
  25. package/lib/index.js +16 -34
  26. package/lib/index.js.map +1 -1
  27. package/lib/internal.d.ts +41 -0
  28. package/lib/internal.d.ts.map +1 -0
  29. package/lib/internal.js +52 -0
  30. package/lib/internal.js.map +1 -0
  31. package/lib/utils/ThemeClassNames.d.ts +5 -3
  32. package/lib/utils/ThemeClassNames.d.ts.map +1 -1
  33. package/lib/utils/ThemeClassNames.js +5 -3
  34. package/lib/utils/ThemeClassNames.js.map +1 -1
  35. package/lib/utils/docsUtils.js +1 -1
  36. package/lib/utils/docsUtils.js.map +1 -1
  37. package/lib/utils/historyUtils.js +4 -4
  38. package/lib/utils/historyUtils.js.map +1 -1
  39. package/lib/utils/reactUtils.d.ts +33 -6
  40. package/lib/utils/reactUtils.d.ts.map +1 -1
  41. package/lib/utils/reactUtils.js +39 -5
  42. package/lib/utils/reactUtils.js.map +1 -1
  43. package/lib/utils/scrollUtils.js +2 -2
  44. package/lib/utils/scrollUtils.js.map +1 -1
  45. package/lib/utils/useLocationChange.js +2 -2
  46. package/lib/utils/useLocationChange.js.map +1 -1
  47. package/lib/utils/useThemeConfig.d.ts +8 -11
  48. package/lib/utils/useThemeConfig.d.ts.map +1 -1
  49. package/lib/utils/useThemeConfig.js.map +1 -1
  50. package/package.json +18 -11
  51. package/src/contexts/blogPost.tsx +80 -0
  52. package/src/contexts/doc.tsx +71 -0
  53. package/src/contexts/docsPreferredVersion.tsx +2 -2
  54. package/src/contexts/navbarSecondaryMenu/content.tsx +2 -12
  55. package/src/hooks/useCodeWordWrap.ts +50 -1
  56. package/src/hooks/useMutationObserver.ts +38 -0
  57. package/src/index.ts +28 -114
  58. package/src/internal.ts +122 -0
  59. package/src/utils/ThemeClassNames.ts +7 -4
  60. package/src/utils/docsUtils.tsx +1 -1
  61. package/src/utils/historyUtils.ts +5 -5
  62. package/src/utils/reactUtils.tsx +58 -5
  63. package/src/utils/scrollUtils.tsx +2 -2
  64. package/src/utils/useLocationChange.ts +2 -2
  65. package/src/utils/useThemeConfig.ts +8 -11
  66. package/Details.d.ts +0 -14
@@ -5,7 +5,15 @@
5
5
  * LICENSE file in the root directory of this source tree.
6
6
  */
7
7
 
8
- import {useCallback, useEffect, useLayoutEffect, useRef} from 'react';
8
+ import React, {
9
+ useCallback,
10
+ useEffect,
11
+ useLayoutEffect,
12
+ useMemo,
13
+ useRef,
14
+ type ComponentType,
15
+ type ReactNode,
16
+ } from 'react';
9
17
  import ExecutionEnvironment from '@docusaurus/ExecutionEnvironment';
10
18
 
11
19
  /**
@@ -21,18 +29,18 @@ export const useIsomorphicLayoutEffect = ExecutionEnvironment.canUseDOM
21
29
  : useEffect;
22
30
 
23
31
  /**
32
+ * Temporary userland implementation until an official hook is implemented
33
+ * See RFC: https://github.com/reactjs/rfcs/pull/220
34
+ *
24
35
  * Permits to transform an unstable callback (like an arrow function provided as
25
36
  * props) to a "stable" callback that is safe to use in a `useEffect` dependency
26
37
  * array. Useful to avoid React stale closure problems + avoid useless effect
27
38
  * re-executions.
28
39
  *
29
- * Workaround until the React team recommends a good solution, see
30
- * https://github.com/facebook/react/issues/16956
31
- *
32
40
  * This generally works but has some potential drawbacks, such as
33
41
  * https://github.com/facebook/react/issues/16956#issuecomment-536636418
34
42
  */
35
- export function useDynamicCallback<T extends (...args: never[]) => unknown>(
43
+ export function useEvent<T extends (...args: never[]) => unknown>(
36
44
  callback: T,
37
45
  ): T {
38
46
  const ref = useRef<T>(callback);
@@ -74,3 +82,48 @@ export class ReactContextError extends Error {
74
82
  } is called outside the <${providerName}>. ${additionalInfo ?? ''}`;
75
83
  }
76
84
  }
85
+
86
+ /**
87
+ * Shallow-memoize an object. This means the returned object will be the same as
88
+ * the previous render if the property keys and values did not change. This
89
+ * works for simple cases: when property values are primitives or stable
90
+ * objects.
91
+ *
92
+ * @param obj
93
+ */
94
+ export function useShallowMemoObject<O extends object>(obj: O): O {
95
+ const deps = Object.entries(obj);
96
+ // Sort by keys to make it order-insensitive
97
+ deps.sort((a, b) => a[0].localeCompare(b[0]));
98
+ // eslint-disable-next-line react-hooks/exhaustive-deps
99
+ return useMemo(() => obj, deps.flat());
100
+ }
101
+
102
+ type SimpleProvider = ComponentType<{children: ReactNode}>;
103
+
104
+ /**
105
+ * Creates a single React provider from an array of existing providers
106
+ * assuming providers only take "children" as props.
107
+ *
108
+ * Prevents the annoying React element nesting
109
+ * Example here: https://getfrontend.tips/compose-multiple-react-providers/
110
+ *
111
+ * The order matters:
112
+ * - The first provider is at the top of the tree.
113
+ * - The last provider is the most nested one
114
+ *
115
+ * @param providers array of providers to compose
116
+ */
117
+ export function composeProviders(providers: SimpleProvider[]): SimpleProvider {
118
+ // Creates a single React component: it's cheaper to compose JSX elements
119
+ return ({children}) => (
120
+ <>
121
+ {providers.reduceRight(
122
+ (element, CurrentProvider) => (
123
+ <CurrentProvider>{element}</CurrentProvider>
124
+ ),
125
+ children,
126
+ )}
127
+ </>
128
+ );
129
+ }
@@ -16,7 +16,7 @@ import React, {
16
16
  } from 'react';
17
17
  import ExecutionEnvironment from '@docusaurus/ExecutionEnvironment';
18
18
  import useIsBrowser from '@docusaurus/useIsBrowser';
19
- import {useDynamicCallback, ReactContextError} from './reactUtils';
19
+ import {useEvent, ReactContextError} from './reactUtils';
20
20
 
21
21
  type ScrollController = {
22
22
  /** A boolean ref tracking whether scroll events are enabled. */
@@ -104,7 +104,7 @@ export function useScrollPosition(
104
104
  const {scrollEventsEnabledRef} = useScrollController();
105
105
  const lastPositionRef = useRef<ScrollPosition | null>(getScrollPosition());
106
106
 
107
- const dynamicEffect = useDynamicCallback(effect);
107
+ const dynamicEffect = useEvent(effect);
108
108
 
109
109
  useEffect(() => {
110
110
  const handleScroll = () => {
@@ -7,7 +7,7 @@
7
7
 
8
8
  import {useEffect} from 'react';
9
9
  import {useLocation} from '@docusaurus/router';
10
- import {useDynamicCallback, usePrevious} from './reactUtils';
10
+ import {useEvent, usePrevious} from './reactUtils';
11
11
  import type {Location} from 'history';
12
12
 
13
13
  /**
@@ -24,7 +24,7 @@ export function useLocationChange(
24
24
  const location = useLocation();
25
25
  const previousLocation = usePrevious(location);
26
26
 
27
- const onLocationChangeDynamic = useDynamicCallback(onLocationChange);
27
+ const onLocationChangeDynamic = useEvent(onLocationChange);
28
28
 
29
29
  useEffect(() => {
30
30
  if (!previousLocation) {
@@ -20,16 +20,20 @@ export type NavbarItem = {
20
20
  position?: 'left' | 'right';
21
21
  } & {[key: string]: unknown};
22
22
 
23
- export type NavbarLogo = {
23
+ type BaseLogo = {
24
+ alt?: string;
24
25
  src: string;
25
26
  srcDark?: string;
27
+ href?: string;
26
28
  width?: string | number;
27
29
  height?: string | number;
28
- href?: string;
29
30
  target?: string;
30
- alt?: string;
31
+ style?: object;
32
+ className?: string;
31
33
  };
32
34
 
35
+ export type NavbarLogo = BaseLogo;
36
+
33
37
  // TODO improve
34
38
  export type Navbar = {
35
39
  style?: 'dark' | 'primary';
@@ -69,14 +73,7 @@ export type FooterLinkItem = {
69
73
  prependBaseUrlToHref?: string;
70
74
  } & {[key: string]: unknown};
71
75
 
72
- export type FooterLogo = {
73
- alt?: string;
74
- src: string;
75
- srcDark?: string;
76
- width?: string | number;
77
- height?: string | number;
78
- href?: string;
79
- };
76
+ export type FooterLogo = BaseLogo;
80
77
 
81
78
  export type FooterBase = {
82
79
  style: 'light' | 'dark';
package/Details.d.ts DELETED
@@ -1,14 +0,0 @@
1
- /**
2
- * Copyright (c) Facebook, Inc. and its affiliates.
3
- *
4
- * This source code is licensed under the MIT license found in the
5
- * LICENSE file in the root directory of this source tree.
6
- */
7
-
8
- // `Details` is a separate export entry because of side-effects messing with CSS
9
- // insertion order. See https://github.com/facebook/docusaurus/pull/7085.
10
- // However, because TS doesn't recognize `exports` (also a problem in
11
- // `content-docs`), we have to manually create a stub.
12
-
13
- // eslint-disable-next-line import/named
14
- export {Details, type DetailsProps} from './lib/components/Details';