diff --git a/packages/docusaurus-theme-common/src/contexts/colorMode.tsx b/packages/docusaurus-theme-common/src/contexts/colorMode.tsx index a2347f1c01..0544fdb7c9 100644 --- a/packages/docusaurus-theme-common/src/contexts/colorMode.tsx +++ b/packages/docusaurus-theme-common/src/contexts/colorMode.tsx @@ -24,7 +24,7 @@ type ContextValue = { /** Current color mode. */ readonly colorMode: ColorMode; /** Set new color mode. */ - readonly setColorMode: (colorMode: ColorMode) => void; + readonly setColorMode: (colorMode: ColorMode | null) => void; // TODO legacy APIs kept for retro-compatibility: deprecate them readonly isDarkTheme: boolean; diff --git a/website/docs/api/misc/theme-common.md b/website/docs/api/misc/theme-common.md new file mode 100644 index 0000000000..d77a4886a7 --- /dev/null +++ b/website/docs/api/misc/theme-common.md @@ -0,0 +1,67 @@ +--- +sidebar_position: 3 +title: '📦 theme-common' +slug: '/api/misc/@docusaurus/theme-common' +--- + +Common code for Docusaurus themes. + +:::caution Note about API publicity + +Most of `@docusaurus/theme-common` is **internal code** for `@docusaurus/theme-classic` and future official themes, extracted for reusability. External theme authors are not required to use this package to achieve functionality (although are strongly advised to). + +Most APIs may be refactored between minor versions—function parameters can change, APIs can come and go, semantics may vary. However, most APIs are still exposed to the end user through swizzling. For this reason, theme components dependent on fragile theme-common APIs will be unsafe for eject. + +APIs listed below are fully stable and intended for public consumption. However, all theme-common APIs have JSDoc. In a proper editor, hovering over the imported identifier will show the explanation for the API and its semantics, which means even if they don't appear on this page, you shouldn't be lost when using them. + +::: + +## Contexts {#contexts} + +### `useColorMode` {#useColorMode} + +A React hook to access the color context. This hook returns an enum value of the current color mode and a callback to set a new color mode. + +Signature: + +```ts +type ColorMode = 'light' | 'dark'; + +declare function useColorMode(): { + colorMode: ColorMode; + setColorMode: (colorMode: ColorMode | null) => void; +}; +``` + +Besides `light` and `dark`, `setColorMode` also accepts `null`, which will reset the color mode to the system theme (with `respectPrefersColorScheme: true`), or the default mode otherwise. + +Usage example: + +```jsx +import React from 'react'; +// highlight-next-line +import {useColorMode} from '@docusaurus/theme-common'; + +const Example = () => { + // highlight-next-line + const {colorMode} = useColorMode(); + + return