Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor: move @theme/hooks to @docusaurus/theme-common #6289

Merged
merged 6 commits into from
Jan 7, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
147 changes: 12 additions & 135 deletions packages/docusaurus-theme-classic/src/theme-classic.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -215,86 +215,6 @@ declare module '@theme/Heading' {
export default function Heading(props: Props): JSX.Element;
}

declare module '@theme/hooks/useHideableNavbar' {
export type useHideableNavbarReturns = {
readonly navbarRef: (node: HTMLElement | null) => void;
readonly isNavbarVisible: boolean;
};

const useHideableNavbar: (hideOnScroll: boolean) => useHideableNavbarReturns;
export default useHideableNavbar;
}

declare module '@theme/hooks/useLockBodyScroll' {
const useLockBodyScroll: (lock?: boolean) => void;
export default useLockBodyScroll;
}

declare module '@theme/hooks/usePrismTheme' {
import type defaultTheme from 'prism-react-renderer/themes/palenight';

const usePrismTheme: () => typeof defaultTheme;
export default usePrismTheme;
}

declare module '@theme/hooks/useTabGroupChoice' {
export type useTabGroupChoiceReturns = {
readonly tabGroupChoices: {readonly [groupId: string]: string};
readonly setTabGroupChoices: (groupId: string, newChoice: string) => void;
};

const useTabGroupChoice: () => useTabGroupChoiceReturns;
export default useTabGroupChoice;
}

declare module '@theme/hooks/useTheme' {
export type useThemeReturns = {
readonly isDarkTheme: boolean;
readonly setLightTheme: () => void;
readonly setDarkTheme: () => void;
};

const useTheme: () => useThemeReturns;
export default useTheme;
}

declare module '@theme/hooks/useThemeContext' {
export type ThemeContextProps = {
isDarkTheme: boolean;
setLightTheme: () => void;
setDarkTheme: () => void;
};

export default function useThemeContext(): ThemeContextProps;
}

declare module '@theme/hooks/useUserPreferencesContext' {
export type UserPreferencesContextProps = {
tabGroupChoices: {readonly [groupId: string]: string};
setTabGroupChoices: (groupId: string, newChoice: string) => void;
};

export default function useUserPreferencesContext(): UserPreferencesContextProps;
}

declare module '@theme/hooks/useWindowSize' {
export const windowSizes: {
desktop: 'desktop';
mobile: 'mobile';
ssr: 'ssr';
};

export type WindowSize = keyof typeof windowSizes;

export default function useWindowSize(): WindowSize;
}

declare module '@theme/hooks/useKeyboardNavigation' {
const useKeyboardNavigation: () => void;

export default useKeyboardNavigation;
}

declare module '@theme/Layout' {
import type {ReactNode} from 'react';

Expand All @@ -314,17 +234,25 @@ declare module '@theme/Layout' {
};
}

const Layout: (props: Props) => JSX.Element;
export default Layout;
export default function Layout(props: Props): JSX.Element;
}

declare module '@theme/LayoutHead' {
import type {Props as LayoutProps} from '@theme/Layout';

export interface Props extends Omit<LayoutProps, 'children'> {}

const LayoutHead: (props: Props) => JSX.Element;
export default LayoutHead;
export default function LayoutHead(props: Props): JSX.Element;
}

declare module '@theme/LayoutProviders' {
import type {ReactNode} from 'react';

export interface Props {
readonly children: ReactNode;
}

export default function LayoutProviders(props: Props): JSX.Element;
}

declare module '@theme/SearchMetadata' {
Expand Down Expand Up @@ -621,17 +549,6 @@ declare module '@theme/Details' {
export default Details;
}

declare module '@theme/ThemeProvider' {
import type {ReactNode} from 'react';

export interface Props {
readonly children: ReactNode;
}

const ThemeProvider: (props: Props) => JSX.Element;
export default ThemeProvider;
}

declare module '@theme/TOCItems' {
import type {TOCItem} from '@docusaurus/types';

Expand Down Expand Up @@ -712,46 +629,6 @@ declare module '@theme/Toggle' {
export default Toggle;
}

declare module '@theme/UserPreferencesProvider' {
import type {ReactNode} from 'react';

export interface Props {
readonly children: ReactNode;
}

const UserPreferencesProvider: (props: Props) => JSX.Element;
export default UserPreferencesProvider;
}

declare module '@theme/LayoutProviders' {
import type {ReactNode} from 'react';

export interface Props {
readonly children: ReactNode;
}

const LayoutProviders: (props: Props) => JSX.Element;
export default LayoutProviders;
}

declare module '@theme/ThemeContext' {
import type {Context} from 'react';
import type {ThemeContextProps} from '@theme/hooks/useThemeContext';

const ThemeContext: Context<ThemeContextProps | undefined>;
export default ThemeContext;
}

declare module '@theme/UserPreferencesContext' {
import type {Context} from 'react';
import type {UserPreferencesContextProps} from '@theme/hooks/useUserPreferencesContext';

const UserPreferencesContext: Context<
UserPreferencesContextProps | undefined
>;
export default UserPreferencesContext;
}

declare module '@theme/Logo' {
import type {ComponentProps} from 'react';

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,8 @@ import {
parseLanguage,
parseLines,
ThemeClassNames,
usePrismTheme,
} from '@docusaurus/theme-common';
import usePrismTheme from '@theme/hooks/usePrismTheme';
import type {Props} from '@theme/CodeBlock';

import styles from './styles.module.css';
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@

import React from 'react';
import clsx from 'clsx';
import useWindowSize from '@theme/hooks/useWindowSize';
import DocPaginator from '@theme/DocPaginator';
import DocVersionBanner from '@theme/DocVersionBanner';
import DocVersionBadge from '@theme/DocVersionBadge';
Expand All @@ -18,7 +17,7 @@ import TOC from '@theme/TOC';
import TOCCollapsible from '@theme/TOCCollapsible';
import Heading from '@theme/Heading';
import styles from './styles.module.css';
import {ThemeClassNames} from '@docusaurus/theme-common';
import {ThemeClassNames, useWindowSize} from '@docusaurus/theme-common';

export default function DocItem(props: Props): JSX.Element {
const {content: DocContent} = props;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,8 @@ import {
type MobileSecondaryMenuComponent,
ThemeClassNames,
useScrollPosition,
useWindowSize,
} from '@docusaurus/theme-common';
import useWindowSize from '@theme/hooks/useWindowSize';
import Logo from '@theme/Logo';
import IconArrow from '@theme/IconArrow';
import {translate} from '@docusaurus/Translate';
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,7 @@ import Footer from '@theme/Footer';
import LayoutProviders from '@theme/LayoutProviders';
import LayoutHead from '@theme/LayoutHead';
import type {Props} from '@theme/Layout';
import useKeyboardNavigation from '@theme/hooks/useKeyboardNavigation';
import {ThemeClassNames} from '@docusaurus/theme-common';
import {ThemeClassNames, useKeyboardNavigation} from '@docusaurus/theme-common';
import ErrorPageContent from '@theme/ErrorPageContent';
import './styles.css';

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,9 @@
*/

import React from 'react';
import ThemeProvider from '@theme/ThemeProvider';
import UserPreferencesProvider from '@theme/UserPreferencesProvider';
import {
ColorModeProvider,
TabGroupChoiceProvider,
AnnouncementBarProvider,
DocsPreferredVersionContextProvider,
MobileSecondaryMenuProvider,
Expand All @@ -18,18 +18,18 @@ import type {Props} from '@theme/LayoutProviders';

export default function LayoutProviders({children}: Props): JSX.Element {
return (
<ThemeProvider>
<ColorModeProvider>
<AnnouncementBarProvider>
<UserPreferencesProvider>
<TabGroupChoiceProvider>
<ScrollControllerProvider>
<DocsPreferredVersionContextProvider>
<MobileSecondaryMenuProvider>
{children}
</MobileSecondaryMenuProvider>
</DocsPreferredVersionContextProvider>
</ScrollControllerProvider>
</UserPreferencesProvider>
</TabGroupChoiceProvider>
</AnnouncementBarProvider>
</ThemeProvider>
</ColorModeProvider>
);
}
10 changes: 5 additions & 5 deletions packages/docusaurus-theme-classic/src/theme/Navbar/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -10,16 +10,16 @@ import clsx from 'clsx';
import Translate from '@docusaurus/Translate';
import SearchBar from '@theme/SearchBar';
import Toggle from '@theme/Toggle';
import useThemeContext from '@theme/hooks/useThemeContext';
import {
useThemeConfig,
useMobileSecondaryMenuRenderer,
usePrevious,
useHistoryPopHandler,
useHideableNavbar,
useLockBodyScroll,
useWindowSize,
useColorMode,
} from '@docusaurus/theme-common';
import useHideableNavbar from '@theme/hooks/useHideableNavbar';
import useLockBodyScroll from '@theme/hooks/useLockBodyScroll';
import useWindowSize from '@theme/hooks/useWindowSize';
import {useActivePlugin} from '@docusaurus/plugin-content-docs/client';
import NavbarItem, {type Props as NavbarItemConfig} from '@theme/NavbarItem';
import Logo from '@theme/Logo';
Expand Down Expand Up @@ -88,7 +88,7 @@ function useColorModeToggle() {
const {
colorMode: {disableSwitch},
} = useThemeConfig();
const {isDarkTheme, setLightTheme, setDarkTheme} = useThemeContext();
const {isDarkTheme, setLightTheme, setDarkTheme} = useColorMode();
const toggle = useCallback(
(e) => (e.target.checked ? setDarkTheme() : setLightTheme()),
[setLightTheme, setDarkTheme],
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,10 @@ import React from 'react';
import renderer from 'react-test-renderer';
import Tabs from '../index';
import TabItem from '../../TabItem';
import UserPreferencesProvider from '@theme/UserPreferencesProvider';
import {ScrollControllerProvider} from '@docusaurus/theme-common';
import {
TabGroupChoiceProvider,
ScrollControllerProvider,
} from '@docusaurus/theme-common';

describe('Tabs', () => {
test('Should reject bad Tabs child', () => {
Expand Down Expand Up @@ -57,7 +59,7 @@ describe('Tabs', () => {
expect(() => {
renderer.create(
<ScrollControllerProvider>
<UserPreferencesProvider>
<TabGroupChoiceProvider>
<Tabs>
<TabItem value="v1">Tab 1</TabItem>
<TabItem value="v2">Tab 2</TabItem>
Expand Down Expand Up @@ -102,7 +104,7 @@ describe('Tabs', () => {
Tab 2
</TabItem>
</Tabs>
</UserPreferencesProvider>
</TabGroupChoiceProvider>
</ScrollControllerProvider>,
);
}).not.toThrow(); // TODO Better Jest infrastructure to mock the Layout
Expand All @@ -113,15 +115,15 @@ describe('Tabs', () => {
const tabs = ['Apple', 'Banana', 'Carrot'];
renderer.create(
<ScrollControllerProvider>
<UserPreferencesProvider>
<TabGroupChoiceProvider>
<Tabs
values={tabs.map((t, idx) => ({label: t, value: idx}))}
defaultValue={0}>
{tabs.map((t, idx) => (
<TabItem value={idx}>{t}</TabItem>
))}
</Tabs>
</UserPreferencesProvider>
</TabGroupChoiceProvider>
</ScrollControllerProvider>,
);
}).not.toThrow();
Expand Down
9 changes: 6 additions & 3 deletions packages/docusaurus-theme-classic/src/theme/Tabs/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,11 @@ import React, {
type ReactElement,
} from 'react';
import useIsBrowser from '@docusaurus/useIsBrowser';
import useUserPreferencesContext from '@theme/hooks/useUserPreferencesContext';
import {useScrollPositionBlocker, duplicates} from '@docusaurus/theme-common';
import {
useScrollPositionBlocker,
duplicates,
useTabGroupChoice,
} from '@docusaurus/theme-common';
import type {Props} from '@theme/Tabs';
import type {Props as TabItemProps} from '@theme/TabItem';

Expand Down Expand Up @@ -83,7 +86,7 @@ function TabsComponent(props: Props): JSX.Element {
);
}

const {tabGroupChoices, setTabGroupChoices} = useUserPreferencesContext();
const {tabGroupChoices, setTabGroupChoices} = useTabGroupChoice();
const [selectedValue, setSelectedValue] = useState(defaultValue);
const tabRefs: (HTMLLIElement | null)[] = [];
const {blockElementScrollPositionUntilNextRender} =
Expand Down
15 changes: 0 additions & 15 deletions packages/docusaurus-theme-classic/src/theme/ThemeContext.ts

This file was deleted.

Loading