-
Notifications
You must be signed in to change notification settings - Fork 47
feat(website): add light mode toggle to Docusaurus site #2504
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
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,41 @@ | ||
| import React, { useRef, useEffect } from 'react'; | ||
| import { useColorMode } from '@docusaurus/theme-common'; | ||
| import ColorModeToggle from '@theme-original/ColorModeToggle'; | ||
| import type { Props } from '@theme/ColorModeToggle'; | ||
|
|
||
| function getTitle(colorMode: string): string { | ||
| if (colorMode === 'dark') { | ||
| return 'Let there be light (careful, bugs may become visible)'; | ||
| } | ||
| return 'Return to the dark side'; | ||
| } | ||
|
|
||
| export default function ColorModeToggleWrapper(props: Props): React.ReactNode { | ||
| const ref = useRef<HTMLDivElement>(null); | ||
| const { colorMode } = useColorMode(); | ||
| const title = getTitle(colorMode); | ||
|
|
||
| // Use a MutationObserver to strip the inner button's native title whenever | ||
| // React re-applies it, so the browser falls through to our wrapper's title. | ||
| useEffect(() => { | ||
| const button = ref.current?.querySelector('button'); | ||
| if (!button) { | ||
| return; | ||
| } | ||
| button.removeAttribute('title'); | ||
|
|
||
| const observer = new MutationObserver(() => { | ||
| if (button.hasAttribute('title')) { | ||
| button.removeAttribute('title'); | ||
| } | ||
| }); | ||
| observer.observe(button, { attributes: true, attributeFilter: ['title'] }); | ||
| return () => observer.disconnect(); | ||
| }, [colorMode]); | ||
|
Comment on lines
+18
to
+34
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. If we want this level of customisation I think it would be best to swizzle the ColorModeToggle with eject and customize it rather than rely on useEffect with MutationObserver.
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. FWIW Claude got very angry when Codex suggested this, I had to separate the two before nuclear war erupted
But I'm happy to switch to the eject approach
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. |
||
|
|
||
| return ( | ||
| <div ref={ref} title={title}> | ||
| <ColorModeToggle {...props} /> | ||
| </div> | ||
| ); | ||
| } | ||

Uh oh!
There was an error while loading. Please reload this page.