@docusaurus/theme-common 2.0.0-beta.8e9b829d9 → 2.0.0-beta.9

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 (60) hide show
  1. package/lib/.tsbuildinfo +1 -1
  2. package/lib/components/Collapsible/index.js +5 -13
  3. package/lib/components/Details/index.js +3 -3
  4. package/lib/components/Details/styles.module.css +8 -9
  5. package/lib/index.d.ts +11 -1
  6. package/lib/index.js +8 -0
  7. package/lib/utils/ThemeClassNames.d.ts +36 -12
  8. package/lib/utils/ThemeClassNames.js +36 -3
  9. package/lib/utils/announcementBarUtils.d.ts +1 -1
  10. package/lib/utils/announcementBarUtils.js +6 -6
  11. package/lib/utils/docsPreferredVersion/DocsPreferredVersionProvider.js +1 -1
  12. package/lib/utils/docsPreferredVersion/useDocsPreferredVersion.d.ts +5 -3
  13. package/lib/utils/docsPreferredVersion/useDocsPreferredVersion.js +1 -2
  14. package/lib/utils/generalUtils.js +2 -2
  15. package/lib/utils/historyUtils.d.ts +11 -0
  16. package/lib/utils/historyUtils.js +42 -0
  17. package/lib/utils/jsUtils.d.ts +13 -0
  18. package/lib/utils/jsUtils.js +16 -0
  19. package/lib/utils/mobileSecondaryMenu.js +1 -0
  20. package/lib/utils/reactUtils.d.ts +9 -0
  21. package/lib/utils/reactUtils.js +26 -0
  22. package/lib/utils/regexpUtils.d.ts +10 -0
  23. package/lib/utils/regexpUtils.js +16 -0
  24. package/lib/utils/scrollUtils.d.ts +52 -0
  25. package/lib/utils/scrollUtils.js +137 -0
  26. package/lib/utils/tagsUtils.d.ts +18 -0
  27. package/lib/utils/tagsUtils.js +33 -0
  28. package/lib/utils/tocUtils.d.ts +15 -0
  29. package/lib/utils/tocUtils.js +36 -0
  30. package/lib/utils/useLocationChange.js +5 -9
  31. package/lib/utils/usePrevious.js +3 -2
  32. package/lib/utils/useTOCHighlight.d.ts +14 -0
  33. package/lib/utils/useTOCHighlight.js +124 -0
  34. package/lib/utils/useThemeConfig.d.ts +13 -2
  35. package/package.json +12 -10
  36. package/src/components/Collapsible/index.tsx +6 -18
  37. package/src/components/Details/index.tsx +3 -3
  38. package/src/components/Details/styles.module.css +8 -9
  39. package/src/index.ts +27 -0
  40. package/src/types.d.ts +0 -2
  41. package/src/utils/ThemeClassNames.ts +42 -4
  42. package/src/utils/__tests__/tagUtils.test.ts +66 -0
  43. package/src/utils/__tests__/tocUtils.test.ts +197 -0
  44. package/src/utils/announcementBarUtils.tsx +7 -7
  45. package/src/utils/docsPreferredVersion/DocsPreferredVersionProvider.tsx +4 -4
  46. package/src/utils/docsPreferredVersion/useDocsPreferredVersion.ts +13 -14
  47. package/src/utils/generalUtils.ts +2 -2
  48. package/src/utils/historyUtils.ts +50 -0
  49. package/src/utils/jsUtils.ts +23 -0
  50. package/src/utils/mobileSecondaryMenu.tsx +2 -1
  51. package/src/utils/reactUtils.tsx +34 -0
  52. package/src/utils/regexpUtils.ts +23 -0
  53. package/src/utils/scrollUtils.tsx +238 -0
  54. package/src/utils/storageUtils.ts +1 -1
  55. package/src/utils/tagsUtils.ts +48 -0
  56. package/src/utils/tocUtils.ts +54 -0
  57. package/src/utils/useLocationChange.ts +6 -10
  58. package/src/utils/usePrevious.ts +3 -2
  59. package/src/utils/useTOCHighlight.ts +179 -0
  60. package/src/utils/useThemeConfig.ts +17 -2
@@ -5,19 +5,52 @@
5
5
  * LICENSE file in the root directory of this source tree.
6
6
  */
7
7
  // These class names are used to style page layouts in Docusaurus
8
+ // Those are meant to be targeted by user-provided custom CSS selectors
9
+ // /!\ Please do not modify the classnames! This is a breaking change, and annoying for users!
8
10
  export const ThemeClassNames = {
9
11
  page: {
10
12
  blogListPage: 'blog-list-page',
11
13
  blogPostPage: 'blog-post-page',
12
14
  blogTagsListPage: 'blog-tags-list-page',
13
- blogTagsPostPage: 'blog-tags-post-page',
14
- docPage: 'doc-page',
15
+ blogTagPostListPage: 'blog-tags-post-list-page',
16
+ docsDocPage: 'docs-doc-page',
17
+ docsTagsListPage: 'docs-tags-list-page',
18
+ docsTagDocListPage: 'docs-tags-doc-list-page',
15
19
  mdxPage: 'mdx-page',
16
20
  },
17
21
  wrapper: {
18
22
  main: 'main-wrapper',
19
23
  blogPages: 'blog-wrapper',
20
- docPages: 'docs-wrapper',
24
+ docsPages: 'docs-wrapper',
21
25
  mdxPages: 'mdx-wrapper',
22
26
  },
27
+ // /!\ Please keep the naming convention consistent!
28
+ // Something like: "theme-{blog,doc,version,page}?-<suffix>"
29
+ common: {
30
+ editThisPage: 'theme-edit-this-page',
31
+ lastUpdated: 'theme-last-updated',
32
+ backToTopButton: 'theme-back-to-top-button',
33
+ },
34
+ layout: {
35
+ // TODO add other stable classNames here
36
+ },
37
+ docs: {
38
+ docVersionBanner: 'theme-doc-version-banner',
39
+ docVersionBadge: 'theme-doc-version-badge',
40
+ docMarkdown: 'theme-doc-markdown',
41
+ docTocMobile: 'theme-doc-toc-mobile',
42
+ docTocDesktop: 'theme-doc-toc-desktop',
43
+ docFooter: 'theme-doc-footer',
44
+ docFooterTagsRow: 'theme-doc-footer-tags-row',
45
+ docFooterEditMetaRow: 'theme-doc-footer-edit-meta-row',
46
+ docSidebarMenu: 'theme-doc-sidebar-menu',
47
+ docSidebarItemCategory: 'theme-doc-sidebar-item-category',
48
+ docSidebarItemLink: 'theme-doc-sidebar-item-link',
49
+ docSidebarItemCategoryLevel: (level) => `theme-doc-sidebar-item-category-level-${level}`,
50
+ docSidebarItemLinkLevel: (level) => `theme-doc-sidebar-item-link-level-${level}`,
51
+ // TODO add other stable classNames here
52
+ },
53
+ blog: {
54
+ // TODO add other stable classNames here
55
+ },
23
56
  };
@@ -7,7 +7,7 @@
7
7
  import { ReactNode } from 'react';
8
8
  export declare const AnnouncementBarDismissStorageKey = "docusaurus.announcement.dismiss";
9
9
  declare type AnnouncementBarAPI = {
10
- readonly isClosed: boolean;
10
+ readonly isActive: boolean;
11
11
  readonly close: () => void;
12
12
  };
13
13
  export declare const AnnouncementBarProvider: ({ children, }: {
@@ -5,7 +5,7 @@
5
5
  * LICENSE file in the root directory of this source tree.
6
6
  */
7
7
  import React, { useState, useEffect, useCallback, useMemo, useContext, createContext, } from 'react';
8
- import useDocusaurusContext from '@docusaurus/useDocusaurusContext';
8
+ import useIsBrowser from '@docusaurus/useIsBrowser';
9
9
  import { createStorageSlot } from './storageUtils';
10
10
  import { useThemeConfig } from './useThemeConfig';
11
11
  export const AnnouncementBarDismissStorageKey = 'docusaurus.announcement.dismiss';
@@ -16,9 +16,9 @@ const isDismissedInStorage = () => AnnouncementBarDismissStorage.get() === 'true
16
16
  const setDismissedInStorage = (bool) => AnnouncementBarDismissStorage.set(String(bool));
17
17
  const useAnnouncementBarContextValue = () => {
18
18
  const { announcementBar } = useThemeConfig();
19
- const { isClient } = useDocusaurusContext();
19
+ const isBrowser = useIsBrowser();
20
20
  const [isClosed, setClosed] = useState(() => {
21
- return isClient
21
+ return isBrowser
22
22
  ? // On client navigation: init with localstorage value
23
23
  isDismissedInStorage()
24
24
  : // On server/hydration: always visible to prevent layout shifts (will be hidden with css if needed)
@@ -51,13 +51,13 @@ const useAnnouncementBarContextValue = () => {
51
51
  if (isNewAnnouncement || !isDismissedInStorage()) {
52
52
  setClosed(false);
53
53
  }
54
- }, []);
54
+ }, [announcementBar]);
55
55
  return useMemo(() => {
56
56
  return {
57
- isClosed,
57
+ isActive: !!announcementBar && !isClosed,
58
58
  close: handleClose,
59
59
  };
60
- }, [isClosed]);
60
+ }, [announcementBar, isClosed, handleClose]);
61
61
  };
62
62
  const AnnouncementBarContext = createContext(null);
63
63
  export const AnnouncementBarProvider = ({ children, }) => {
@@ -69,7 +69,7 @@ function useContextValue() {
69
69
  return {
70
70
  savePreferredVersion,
71
71
  };
72
- }, [setState]);
72
+ }, [versionPersistence]);
73
73
  return [state, api];
74
74
  }
75
75
  const Context = createContext(null);
@@ -1,5 +1,7 @@
1
+ /// <reference types="@docusaurus/plugin-content-docs" />
2
+ import { GlobalVersion } from '@theme/hooks/useDocs';
1
3
  export declare function useDocsPreferredVersion(pluginId?: string | undefined): {
2
- readonly preferredVersion: any;
3
- readonly savePreferredVersionName: (versionName: string) => void;
4
+ preferredVersion: GlobalVersion | null | undefined;
5
+ savePreferredVersionName: (versionName: string) => void;
4
6
  };
5
- export declare function useDocsPreferredVersionByPluginId(): Record<string, any>;
7
+ export declare function useDocsPreferredVersionByPluginId(): Record<string, GlobalVersion | null | undefined>;
@@ -8,7 +8,6 @@ import { useCallback } from 'react';
8
8
  import { useDocsPreferredVersionContext } from './DocsPreferredVersionProvider';
9
9
  import { useAllDocsData, useDocsData } from '@theme/hooks/useDocs';
10
10
  import { DEFAULT_PLUGIN_ID } from '@docusaurus/constants';
11
- // TODO improve typing
12
11
  // Note, the preferredVersion attribute will always be null before mount
13
12
  export function useDocsPreferredVersion(pluginId = DEFAULT_PLUGIN_ID) {
14
13
  const docsData = useDocsData(pluginId);
@@ -19,7 +18,7 @@ export function useDocsPreferredVersion(pluginId = DEFAULT_PLUGIN_ID) {
19
18
  : null;
20
19
  const savePreferredVersionName = useCallback((versionName) => {
21
20
  api.savePreferredVersion(pluginId, versionName);
22
- }, [api]);
21
+ }, [api, pluginId]);
23
22
  return { preferredVersion, savePreferredVersionName };
24
23
  }
25
24
  export function useDocsPreferredVersionByPluginId() {
@@ -6,8 +6,8 @@
6
6
  */
7
7
  import useDocusaurusContext from '@docusaurus/useDocusaurusContext';
8
8
  export const useTitleFormatter = (title) => {
9
- const { siteConfig = {} } = useDocusaurusContext();
10
- const { title: siteTitle, titleDelimiter = '|' } = siteConfig;
9
+ const { siteConfig } = useDocusaurusContext();
10
+ const { title: siteTitle, titleDelimiter } = siteConfig;
11
11
  return title && title.trim().length
12
12
  ? `${title.trim()} ${titleDelimiter} ${siteTitle}`
13
13
  : siteTitle;
@@ -0,0 +1,11 @@
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
+ import type { Location, Action } from '@docusaurus/history';
8
+ declare type HistoryBlockHandler = (location: Location, action: Action) => void | false;
9
+ export declare function useHistoryActionHandler(handler: HistoryBlockHandler): void;
10
+ export declare function useHistoryPopHandler(handler: HistoryBlockHandler): void;
11
+ export {};
@@ -0,0 +1,42 @@
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
+ import { useEffect, useRef } from 'react';
8
+ import { useHistory } from '@docusaurus/router';
9
+ /*
10
+ Permits to register a handler that will be called on history actions (pop,push,replace)
11
+ If the handler returns false, the navigation transition will be blocked/cancelled
12
+ */
13
+ export function useHistoryActionHandler(handler) {
14
+ const { block } = useHistory();
15
+ // Avoid stale closure issues without triggering useless re-renders
16
+ const lastHandlerRef = useRef(handler);
17
+ useEffect(() => {
18
+ lastHandlerRef.current = handler;
19
+ }, [handler]);
20
+ useEffect(() => {
21
+ // See https://github.com/remix-run/history/blob/main/docs/blocking-transitions.md
22
+ return block((location, action) => {
23
+ return lastHandlerRef.current(location, action);
24
+ });
25
+ }, [block, lastHandlerRef]);
26
+ }
27
+ /*
28
+ Permits to register a handler that will be called on history pop navigation (backward/forward)
29
+ If the handler returns false, the backward/forward transition will be blocked
30
+
31
+ Unfortunately there's no good way to detect the "direction" (backward/forward) of the POP event.
32
+ */
33
+ export function useHistoryPopHandler(handler) {
34
+ useHistoryActionHandler((location, action) => {
35
+ if (action === 'POP') {
36
+ // Eventually block navigation if handler returns false
37
+ return handler(location, action);
38
+ }
39
+ // Don't block other navigation actions
40
+ return undefined;
41
+ });
42
+ }
@@ -0,0 +1,13 @@
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
+ * Gets the duplicate values in an array.
9
+ * @param arr The array.
10
+ * @param comparator Compares two values and returns `true` if they are equal (duplicated).
11
+ * @returns Value of the elements `v` that have a preceding element `u` where `comparator(u, v) === true`. Values within the returned array are not guaranteed to be unique.
12
+ */
13
+ export declare function duplicates<T>(arr: readonly T[], comparator?: (a: T, b: T) => boolean): T[];
@@ -0,0 +1,16 @@
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
+ // A replacement of lodash in client code
8
+ /**
9
+ * Gets the duplicate values in an array.
10
+ * @param arr The array.
11
+ * @param comparator Compares two values and returns `true` if they are equal (duplicated).
12
+ * @returns Value of the elements `v` that have a preceding element `u` where `comparator(u, v) === true`. Values within the returned array are not guaranteed to be unique.
13
+ */
14
+ export function duplicates(arr, comparator = (a, b) => a === b) {
15
+ return arr.filter((v, vIndex) => arr.findIndex((u) => comparator(u, v)) !== vIndex);
16
+ }
@@ -32,6 +32,7 @@ export function useMobileSecondaryMenuRenderer() {
32
32
  function useShallowMemoizedObject(obj) {
33
33
  return useMemo(() => obj,
34
34
  // Is this safe?
35
+ // eslint-disable-next-line react-hooks/exhaustive-deps
35
36
  [...Object.keys(obj), ...Object.values(obj)]);
36
37
  }
37
38
  // Fill the secondary menu placeholder with some real content
@@ -0,0 +1,9 @@
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
+ import { useLayoutEffect } from 'react';
8
+ export declare const useIsomorphicLayoutEffect: typeof useLayoutEffect;
9
+ export declare function useDynamicCallback<T extends (...args: never[]) => unknown>(callback: T): T;
@@ -0,0 +1,26 @@
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
+ import { useCallback, useEffect, useLayoutEffect, useRef } from 'react';
8
+ // This hook is like useLayoutEffect, but without the SSR warning
9
+ // It seems hacky but it's used in many React libs (Redux, Formik...)
10
+ // Also mentioned here: https://github.com/facebook/react/issues/16956
11
+ // It is useful when you need to update a ref as soon as possible after a React render (before useEffect)
12
+ export const useIsomorphicLayoutEffect = typeof window !== 'undefined' ? useLayoutEffect : useEffect;
13
+ // Permits to transform an unstable callback (like an arrow function provided as props)
14
+ // to a "stable" callback that is safe to use in a useEffect dependency array
15
+ // Useful to avoid React stale closure problems + avoid useless effect re-executions
16
+ //
17
+ // Workaround until the React team recommends a good solution, see https://github.com/facebook/react/issues/16956
18
+ // This generally works has some potential drawbacks, such as https://github.com/facebook/react/issues/16956#issuecomment-536636418
19
+ export function useDynamicCallback(callback) {
20
+ const ref = useRef(callback);
21
+ useIsomorphicLayoutEffect(() => {
22
+ ref.current = callback;
23
+ }, [callback]);
24
+ // @ts-expect-error: TODO, not sure how to fix this TS error
25
+ return useCallback((...args) => ref.current(...args), []);
26
+ }
@@ -0,0 +1,10 @@
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
+ * Utility to convert an optional string into a Regex case sensitive and global
9
+ */
10
+ export declare function isRegexpStringMatch(regexAsString?: string, valueToTest?: string): boolean;
@@ -0,0 +1,16 @@
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
+ * Utility to convert an optional string into a Regex case sensitive and global
9
+ */
10
+ export function isRegexpStringMatch(regexAsString, valueToTest) {
11
+ if (typeof regexAsString === 'undefined' ||
12
+ typeof valueToTest === 'undefined') {
13
+ return false;
14
+ }
15
+ return new RegExp(regexAsString, 'gi').test(valueToTest);
16
+ }
@@ -0,0 +1,52 @@
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
+ import React, { ReactNode } from 'react';
8
+ /**
9
+ * We need a way to update the scroll position while ignoring scroll events
10
+ * without affecting Navbar/BackToTop visibility
11
+ *
12
+ * This API permits to temporarily disable/ignore scroll events
13
+ * Motivated by https://github.com/facebook/docusaurus/pull/5618
14
+ */
15
+ declare type ScrollController = {
16
+ /**
17
+ * A boolean ref tracking whether scroll events are enabled
18
+ */
19
+ scrollEventsEnabledRef: React.MutableRefObject<boolean>;
20
+ /**
21
+ * Enables scroll events in `useScrollPosition`
22
+ */
23
+ enableScrollEvents: () => void;
24
+ /**
25
+ * Disables scroll events in `useScrollPosition`
26
+ */
27
+ disableScrollEvents: () => void;
28
+ };
29
+ export declare function ScrollControllerProvider({ children, }: {
30
+ children: ReactNode;
31
+ }): JSX.Element;
32
+ export declare function useScrollController(): ScrollController;
33
+ declare type ScrollPosition = {
34
+ scrollX: number;
35
+ scrollY: number;
36
+ };
37
+ export declare function useScrollPosition(effect: (position: ScrollPosition, lastPosition: ScrollPosition | null) => void, deps?: unknown[]): void;
38
+ declare type UseScrollPositionBlockerReturn = {
39
+ blockElementScrollPositionUntilNextRender: (el: HTMLElement) => void;
40
+ };
41
+ /**
42
+ * This hook permits to "block" the scroll position of a dom element
43
+ * The idea is that we should be able to update DOM content above this element
44
+ * but the screen position of this element should not change
45
+ *
46
+ * Feature motivated by the Tabs groups:
47
+ * clicking on a tab may affect tabs of the same group upper in the tree
48
+ * Yet to avoid a bad UX, the clicked tab must remain under the user mouse!
49
+ * See GIF here: https://github.com/facebook/docusaurus/pull/5618
50
+ */
51
+ export declare function useScrollPositionBlocker(): UseScrollPositionBlockerReturn;
52
+ export {};
@@ -0,0 +1,137 @@
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
+ import React, { createContext, useCallback, useContext, useEffect, useLayoutEffect, useMemo, useRef, } from 'react';
8
+ import { useDynamicCallback } from './reactUtils';
9
+ import ExecutionEnvironment from '@docusaurus/ExecutionEnvironment';
10
+ function useScrollControllerContextValue() {
11
+ const scrollEventsEnabledRef = useRef(true);
12
+ return useMemo(() => ({
13
+ scrollEventsEnabledRef,
14
+ enableScrollEvents: () => {
15
+ scrollEventsEnabledRef.current = true;
16
+ },
17
+ disableScrollEvents: () => {
18
+ scrollEventsEnabledRef.current = false;
19
+ },
20
+ }), []);
21
+ }
22
+ const ScrollMonitorContext = createContext(undefined);
23
+ export function ScrollControllerProvider({ children, }) {
24
+ return (React.createElement(ScrollMonitorContext.Provider, { value: useScrollControllerContextValue() }, children));
25
+ }
26
+ export function useScrollController() {
27
+ const context = useContext(ScrollMonitorContext);
28
+ if (context == null) {
29
+ throw new Error('"useScrollController" is used but no context provider was found in the React tree.');
30
+ }
31
+ return context;
32
+ }
33
+ const getScrollPosition = () => {
34
+ return ExecutionEnvironment.canUseDOM
35
+ ? {
36
+ scrollX: window.pageXOffset,
37
+ scrollY: window.pageYOffset,
38
+ }
39
+ : null;
40
+ };
41
+ export function useScrollPosition(effect, deps = []) {
42
+ const { scrollEventsEnabledRef } = useScrollController();
43
+ const lastPositionRef = useRef(getScrollPosition());
44
+ const dynamicEffect = useDynamicCallback(effect);
45
+ useEffect(() => {
46
+ const handleScroll = () => {
47
+ if (!scrollEventsEnabledRef.current) {
48
+ return;
49
+ }
50
+ const currentPosition = getScrollPosition();
51
+ if (dynamicEffect) {
52
+ dynamicEffect(currentPosition, lastPositionRef.current);
53
+ }
54
+ lastPositionRef.current = currentPosition;
55
+ };
56
+ const opts = {
57
+ passive: true,
58
+ };
59
+ handleScroll();
60
+ window.addEventListener('scroll', handleScroll, opts);
61
+ return () => window.removeEventListener('scroll', handleScroll, opts);
62
+ }, [
63
+ dynamicEffect,
64
+ scrollEventsEnabledRef,
65
+ // eslint-disable-next-line react-hooks/exhaustive-deps
66
+ ...deps,
67
+ ]);
68
+ }
69
+ function useScrollPositionSaver() {
70
+ const lastElementRef = useRef({
71
+ elem: null,
72
+ top: 0,
73
+ });
74
+ const save = useCallback((elem) => {
75
+ lastElementRef.current = {
76
+ elem,
77
+ top: elem.getBoundingClientRect().top,
78
+ };
79
+ }, []);
80
+ const restore = useCallback(() => {
81
+ const { current: { elem, top }, } = lastElementRef;
82
+ if (!elem) {
83
+ return { restored: false };
84
+ }
85
+ const newTop = elem.getBoundingClientRect().top;
86
+ const heightDiff = newTop - top;
87
+ if (heightDiff) {
88
+ window.scrollBy({ left: 0, top: heightDiff });
89
+ }
90
+ lastElementRef.current = { elem: null, top: 0 };
91
+ return { restored: heightDiff !== 0 };
92
+ }, []);
93
+ return useMemo(() => ({ save, restore }), [restore, save]);
94
+ }
95
+ /**
96
+ * This hook permits to "block" the scroll position of a dom element
97
+ * The idea is that we should be able to update DOM content above this element
98
+ * but the screen position of this element should not change
99
+ *
100
+ * Feature motivated by the Tabs groups:
101
+ * clicking on a tab may affect tabs of the same group upper in the tree
102
+ * Yet to avoid a bad UX, the clicked tab must remain under the user mouse!
103
+ * See GIF here: https://github.com/facebook/docusaurus/pull/5618
104
+ */
105
+ export function useScrollPositionBlocker() {
106
+ const scrollController = useScrollController();
107
+ const scrollPositionSaver = useScrollPositionSaver();
108
+ const nextLayoutEffectCallbackRef = useRef(undefined);
109
+ const blockElementScrollPositionUntilNextRender = useCallback((el) => {
110
+ scrollPositionSaver.save(el);
111
+ scrollController.disableScrollEvents();
112
+ nextLayoutEffectCallbackRef.current = () => {
113
+ const { restored } = scrollPositionSaver.restore();
114
+ nextLayoutEffectCallbackRef.current = undefined;
115
+ // Restoring the former scroll position will trigger a scroll event
116
+ // We need to wait for next scroll event to happen
117
+ // before enabling again the scrollController events
118
+ if (restored) {
119
+ const handleScrollRestoreEvent = () => {
120
+ scrollController.enableScrollEvents();
121
+ window.removeEventListener('scroll', handleScrollRestoreEvent);
122
+ };
123
+ window.addEventListener('scroll', handleScrollRestoreEvent);
124
+ }
125
+ else {
126
+ scrollController.enableScrollEvents();
127
+ }
128
+ };
129
+ }, [scrollController, scrollPositionSaver]);
130
+ useLayoutEffect(() => {
131
+ var _a;
132
+ (_a = nextLayoutEffectCallbackRef.current) === null || _a === void 0 ? void 0 : _a.call(nextLayoutEffectCallbackRef);
133
+ });
134
+ return {
135
+ blockElementScrollPositionUntilNextRender,
136
+ };
137
+ }
@@ -0,0 +1,18 @@
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
+ export declare const translateTagsPageTitle: () => string;
8
+ declare type TagsListItem = Readonly<{
9
+ name: string;
10
+ permalink: string;
11
+ count: number;
12
+ }>;
13
+ export declare type TagLetterEntry = Readonly<{
14
+ letter: string;
15
+ tags: TagsListItem[];
16
+ }>;
17
+ export declare function listTagsByLetters(tags: readonly TagsListItem[]): TagLetterEntry[];
18
+ export {};
@@ -0,0 +1,33 @@
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
+ import { translate } from '@docusaurus/Translate';
8
+ export const translateTagsPageTitle = () => translate({
9
+ id: 'theme.tags.tagsPageTitle',
10
+ message: 'Tags',
11
+ description: 'The title of the tag list page',
12
+ });
13
+ function getTagLetter(tag) {
14
+ return tag[0].toUpperCase();
15
+ }
16
+ export function listTagsByLetters(tags) {
17
+ // Group by letters
18
+ const groups = {};
19
+ Object.values(tags).forEach((tag) => {
20
+ var _a;
21
+ const letter = getTagLetter(tag.name);
22
+ groups[letter] = (_a = groups[letter]) !== null && _a !== void 0 ? _a : [];
23
+ groups[letter].push(tag);
24
+ });
25
+ return (Object.entries(groups)
26
+ // Sort letters
27
+ .sort(([letter1], [letter2]) => letter1.localeCompare(letter2))
28
+ .map(([letter, letterTags]) => {
29
+ // Sort tags inside a letter
30
+ const sortedTags = letterTags.sort((tag1, tag2) => tag1.name.localeCompare(tag2.name));
31
+ return { letter, tags: sortedTags };
32
+ }));
33
+ }
@@ -0,0 +1,15 @@
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
+ import { TOCItem } from '@docusaurus/types';
8
+ declare type FilterTOCParam = {
9
+ toc: readonly TOCItem[];
10
+ minHeadingLevel: number;
11
+ maxHeadingLevel: number;
12
+ };
13
+ export declare function filterTOC({ toc, minHeadingLevel, maxHeadingLevel, }: FilterTOCParam): TOCItem[];
14
+ export declare function useTOCFilter({ toc, minHeadingLevel, maxHeadingLevel, }: FilterTOCParam): readonly TOCItem[];
15
+ export {};
@@ -0,0 +1,36 @@
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
+ import { useMemo } from 'react';
8
+ export function filterTOC({ toc, minHeadingLevel, maxHeadingLevel, }) {
9
+ function isValid(item) {
10
+ return item.level >= minHeadingLevel && item.level <= maxHeadingLevel;
11
+ }
12
+ return toc.flatMap((item) => {
13
+ const filteredChildren = filterTOC({
14
+ toc: item.children,
15
+ minHeadingLevel,
16
+ maxHeadingLevel,
17
+ });
18
+ if (isValid(item)) {
19
+ return [
20
+ {
21
+ ...item,
22
+ children: filteredChildren,
23
+ },
24
+ ];
25
+ }
26
+ else {
27
+ return filteredChildren;
28
+ }
29
+ });
30
+ }
31
+ // Memoize potentially expensive filtering logic
32
+ export function useTOCFilter({ toc, minHeadingLevel, maxHeadingLevel, }) {
33
+ return useMemo(() => {
34
+ return filterTOC({ toc, minHeadingLevel, maxHeadingLevel });
35
+ }, [toc, minHeadingLevel, maxHeadingLevel]);
36
+ }
@@ -4,22 +4,18 @@
4
4
  * This source code is licensed under the MIT license found in the
5
5
  * LICENSE file in the root directory of this source tree.
6
6
  */
7
- import { useEffect, useRef } from 'react';
7
+ import { useEffect } from 'react';
8
8
  import { useLocation } from '@docusaurus/router';
9
9
  import { usePrevious } from './usePrevious';
10
+ import { useDynamicCallback } from './reactUtils';
10
11
  export function useLocationChange(onLocationChange) {
11
12
  const location = useLocation();
12
13
  const previousLocation = usePrevious(location);
13
- const isFirst = useRef(true);
14
+ const onLocationChangeDynamic = useDynamicCallback(onLocationChange);
14
15
  useEffect(() => {
15
- // Prevent first effect to trigger the listener on mount
16
- if (isFirst.current) {
17
- isFirst.current = false;
18
- return;
19
- }
20
- onLocationChange({
16
+ onLocationChangeDynamic({
21
17
  location,
22
18
  previousLocation,
23
19
  });
24
- }, [location]);
20
+ }, [onLocationChangeDynamic, location, previousLocation]);
25
21
  }
@@ -4,10 +4,11 @@
4
4
  * This source code is licensed under the MIT license found in the
5
5
  * LICENSE file in the root directory of this source tree.
6
6
  */
7
- import { useRef, useEffect } from 'react';
7
+ import { useRef } from 'react';
8
+ import { useIsomorphicLayoutEffect } from './reactUtils';
8
9
  export function usePrevious(value) {
9
10
  const ref = useRef();
10
- useEffect(() => {
11
+ useIsomorphicLayoutEffect(() => {
11
12
  ref.current = value;
12
13
  });
13
14
  return ref.current;