diff --git a/www/docs/src/components/LargeCard/index.tsx b/www/docs/src/components/LargeCard/index.tsx index 5731dc35af..14a1169d78 100644 --- a/www/docs/src/components/LargeCard/index.tsx +++ b/www/docs/src/components/LargeCard/index.tsx @@ -3,6 +3,7 @@ import BorderedIcon from "../BorderedIcon" import clsx from "clsx" import Badge from "../Badge" import { IconProps } from "@site/src/theme/Icon/index" +import Link from "@docusaurus/Link" type LargeCardProps = { Icon: React.FC @@ -78,13 +79,13 @@ const LargeCard: React.FC = ({ {children} - + > ) } diff --git a/www/docs/src/providers/Sidebar/index.tsx b/www/docs/src/providers/Sidebar/index.tsx new file mode 100644 index 0000000000..c359f038f7 --- /dev/null +++ b/www/docs/src/providers/Sidebar/index.tsx @@ -0,0 +1,103 @@ +import React, { + createContext, + useCallback, + useContext, + useEffect, + useState, +} from "react" +import { prefersReducedMotion } from "@docusaurus/theme-common" +import useIsBrowser from "@docusaurus/useIsBrowser" + +type SidebarContextType = { + hasSidebar: boolean + hiddenSidebar: boolean + setHiddenSidebar: (value: boolean) => void + hiddenSidebarContainer: boolean + setHiddenSidebarContainer: (value: boolean) => void + floatingSidebar: boolean + setFloatingSidebar: (value: boolean) => void + onCollapse: () => void +} + +const SidebarContext = createContext(null) + +type SidebarProviderProps = { + sidebarName?: string + children?: React.ReactNode +} + +const SidebarProvider: React.FC = ({ + sidebarName, + children, +}) => { + const [hiddenSidebar, setHiddenSidebar] = useState(false) + const [hiddenSidebarContainer, setHiddenSidebarContainer] = useState(false) + const [floatingSidebar, setFloatingSidebar] = useState(false) + + const toggleSidebar = useCallback(() => { + if (hiddenSidebar) { + setHiddenSidebar(false) + } + // onTransitionEnd won't fire when sidebar animation is disabled + // fixes https://github.com/facebook/docusaurus/issues/8918 + if (!hiddenSidebar && prefersReducedMotion()) { + setHiddenSidebar(true) + } + setHiddenSidebarContainer((value) => !value) + }, [setHiddenSidebarContainer, hiddenSidebar]) + + useEffect(() => { + function isEditingContent(event: KeyboardEvent) { + const element = event.target as HTMLElement + const tagName = element.tagName + return ( + element.isContentEditable || + tagName === "INPUT" || + tagName === "SELECT" || + tagName === "TEXTAREA" + ) + } + + function sidebarShortcut(e: KeyboardEvent) { + if ( + (e.metaKey || e.ctrlKey) && + e.key.toLowerCase() === "i" && + !isEditingContent(e) + ) { + e.preventDefault() + toggleSidebar() + } + } + + window.addEventListener("keydown", sidebarShortcut) + + return () => { + window.removeEventListener("keydown", sidebarShortcut) + } + }) + + return ( + + {children} + + ) +} + +export default SidebarProvider + +export const useSidebar = () => { + const context = useContext(SidebarContext) + + return context +} diff --git a/www/docs/src/theme/DocItem/Layout/index.tsx b/www/docs/src/theme/DocItem/Layout/index.tsx index b7eccd5c1f..f0184b3bdb 100644 --- a/www/docs/src/theme/DocItem/Layout/index.tsx +++ b/www/docs/src/theme/DocItem/Layout/index.tsx @@ -1,4 +1,4 @@ -import React, { useContext } from "react" +import React from "react" import clsx from "clsx" import { useWindowSize } from "@docusaurus/theme-common" import { useDoc } from "@docusaurus/theme-common/internal" @@ -12,7 +12,7 @@ import DocItemContent from "@theme/DocItem/Content" import DocBreadcrumbs from "@theme/DocBreadcrumbs" import type { Props } from "@theme/DocItem/Layout" import Footer from "@theme/Footer" -import { SidebarContext } from "@site/src/context/sidebar" +import { useSidebar } from "@site/src/providers/Sidebar" /** * Decide if the toc should be rendered, on mobile or desktop viewports @@ -40,7 +40,7 @@ function useDocTOC() { export default function DocItemLayout({ children }: Props): JSX.Element { const docTOC = useDocTOC() - const sidebarContext = useContext(SidebarContext) + const sidebarContext = useSidebar() return (
diff --git a/www/docs/src/theme/DocPage/index.tsx b/www/docs/src/theme/DocPage/index.tsx index b9930cd753..c4c384bc74 100644 --- a/www/docs/src/theme/DocPage/index.tsx +++ b/www/docs/src/theme/DocPage/index.tsx @@ -17,6 +17,7 @@ import NotFound from "@theme/NotFound" import SearchMetadata from "@theme/SearchMetadata" import type { Props } from "@theme/DocPage" import { SidebarContext } from "@site/src/context/sidebar" +import SidebarProvider from "@site/src/providers/Sidebar" function DocPageMetadata(props: Props): JSX.Element { const { versionMetadata } = props @@ -46,53 +47,6 @@ export default function DocPage(props: Props): JSX.Element { } const { docElement, sidebarName, sidebarItems } = currentDocRouteMetadata - const [hiddenSidebar, setHiddenSidebar] = useState(false) - const [hiddenSidebarContainer, setHiddenSidebarContainer] = useState(false) - const [floatingSidebar, setFloatingSidebar] = useState(false) - const toggleSidebar = useCallback(() => { - if (hiddenSidebar) { - setHiddenSidebar(false) - } - // onTransitionEnd won't fire when sidebar animation is disabled - // fixes https://github.com/facebook/docusaurus/issues/8918 - if (!hiddenSidebar && prefersReducedMotion()) { - setHiddenSidebar(true) - } - setHiddenSidebarContainer((value) => !value) - }, [setHiddenSidebarContainer, hiddenSidebar]) - - useEffect(() => { - function isEditingContent(event: KeyboardEvent) { - const element = event.target as HTMLElement - const tagName = element.tagName - return ( - element.isContentEditable || - tagName === "INPUT" || - tagName === "SELECT" || - tagName === "TEXTAREA" - ) - } - - function sidebarShortcut(e: KeyboardEvent) { - if ( - (e.metaKey || e.ctrlKey) && - e.key.toLowerCase() === "i" && - !isEditingContent(e) - ) { - e.preventDefault() - toggleSidebar() - } - } - - window.addEventListener("keydown", sidebarShortcut) - - return () => { - window.removeEventListener("keydown", sidebarShortcut) - } - }) - - console.log(sidebarName) - return ( <> @@ -107,20 +61,9 @@ export default function DocPage(props: Props): JSX.Element { > - + {docElement} - + diff --git a/www/docs/src/theme/Navbar/Content/index.tsx b/www/docs/src/theme/Navbar/Content/index.tsx index 8c74c981a0..9346cff23d 100644 --- a/www/docs/src/theme/Navbar/Content/index.tsx +++ b/www/docs/src/theme/Navbar/Content/index.tsx @@ -1,4 +1,4 @@ -import React, { useContext, type ReactNode } from "react" +import React, { type ReactNode } from "react" import { useThemeConfig } from "@docusaurus/theme-common" import { splitNavbarItems, @@ -13,7 +13,7 @@ import Tooltip from "@site/src/components/Tooltip" import { ThemeConfig } from "@medusajs/docs" import useIsBrowser from "@docusaurus/useIsBrowser" import clsx from "clsx" -import { SidebarContext } from "@site/src/context/sidebar" +import { useSidebar } from "@site/src/providers/Sidebar" function useNavbarItems() { // TODO temporary casting until ThemeConfig type is improved @@ -70,7 +70,7 @@ export default function NavbarContent(): JSX.Element { sidebar: { hideable }, }, } = useThemeConfig() as ThemeConfig - const sidebarContext = useContext(SidebarContext) + const sidebarContext = useSidebar() const isBrowser = useIsBrowser() const isApple = isBrowser