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

feat(v2): dark mode toggle customization #3127

Merged
Merged
Show file tree
Hide file tree
Changes from 8 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
26 changes: 21 additions & 5 deletions packages/docusaurus-theme-classic/src/theme/Toggle/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -13,17 +13,33 @@ import useDocusaurusContext from '@docusaurus/useDocusaurusContext';
import clsx from 'clsx';
import styles from './styles.module.css';

const Moon = () => <span className={clsx(styles.toggle, styles.moon)} />;
const Sun = () => <span className={clsx(styles.toggle, styles.sun)} />;
const Dark = ({icon, style}) => (
<span className={clsx(styles.toggle, styles.dark)} style={style}>
{icon}
</span>
);
const Light = ({icon, style}) => (
<span className={clsx(styles.toggle, styles.light)} style={style}>
{icon}
</span>
);

export default function (props: ComponentProps<typeof Toggle>): JSX.Element {
const {isClient} = useDocusaurusContext();
const {isClient, siteConfig = {}} = useDocusaurusContext();

const {
themeConfig: {
colorMode: {
switchConfig: {darkIcon, darkIconStyle, lightIcon, lightIconStyle},
},
},
} = siteConfig;
return (
<Toggle
disabled={!isClient}
icons={{
checked: <Moon />,
unchecked: <Sun />,
checked: <Dark icon={darkIcon} style={darkIconStyle} />,
unchecked: <Light icon={lightIcon} style={lightIconStyle} />,
}}
{...props}
/>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,6 @@
.toggle::before {
position: absolute;
}
.moon::before {
content: '\1F31C';
}
.sun::before {
content: '\1F31E';
}

/**
* styles for React Toggle
Expand Down
9 changes: 9 additions & 0 deletions packages/docusaurus-theme-classic/src/themeConfigSchema.js
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,15 @@ const ColorModeSchema = Joi.object({
defaultMode: Joi.string().equal('dark', 'light').default('light'),
disableSwitch: Joi.bool().default(false),
respectPrefersColorScheme: Joi.bool().default(false),
switchConfig: Joi.object({
darkIcon: Joi.string().default('🌜'),
darkIconStyle: Joi.object(),
lightIcon: Joi.string().default('🌞'),
lightIconStyle: Joi.object(),
}).default({
darkIcon: '🌜',
lightIcon: '🌞',
}),
}).default({
defaultMode: 'light',
disableSwitch: false,
Drewbi marked this conversation as resolved.
Show resolved Hide resolved
Expand Down
15 changes: 15 additions & 0 deletions website/docs/docusaurus.config.js.md
Original file line number Diff line number Diff line change
Expand Up @@ -158,6 +158,21 @@ Example:
```js title="docusaurus.config.js"
module.exports = {
themeConfig: {
colorMode: {
defaultMode: 'light',
disableSwitch: false,
respectPrefersColorScheme: true,
switchConfig: {
darkIcon: '🌙',
darkIconStyle: {
marginLeft: '2px',
},
lightIcon: '\u2600',
lightIconStyle: {
marginLeft: '1px',
},
},
},
navbar: {
title: 'Site Title',
logo: {
Expand Down
4 changes: 2 additions & 2 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -17578,7 +17578,7 @@ react-dev-utils@^9.1.0:
strip-ansi "5.2.0"
text-table "0.2.0"

react-dom@^16.10.2, react-dom@^16.8.4:
react-dom@^16.8.4:
version "16.13.1"
resolved "https://registry.yarnpkg.com/react-dom/-/react-dom-16.13.1.tgz#c1bd37331a0486c078ee54c4740720993b2e0e7f"
integrity sha512-81PIMmVLnCNLO/fFOQxdQkvEq/+Hfpv24XNJfpyZhTRfO0QcmQIF/PgCa1zCOj2w1hrn12MFLyaJ/G0+Mxtfag==
Expand Down Expand Up @@ -17739,7 +17739,7 @@ react-waypoint@^9.0.2:
prop-types "^15.0.0"
react-is "^16.6.3"

react@^16.10.2, react@^16.8.4:
react@^16.8.4:
version "16.13.1"
resolved "https://registry.yarnpkg.com/react/-/react-16.13.1.tgz#2e818822f1a9743122c063d6410d85c1e3afe48e"
integrity sha512-YMZQQq32xHLX0bz5Mnibv1/LHb3Sqzngu7xstSM+vrkE5Kzr9xE0yMByK5kMoTK30YVJE61WfbxIFFvfeDKT1w==
Expand Down