Skip to content

Commit

Permalink
UI: Enable system option for theme toggle on NoteBook layout
Browse files Browse the repository at this point in the history
  • Loading branch information
fuma-nama committed Jan 19, 2025
1 parent f2e880f commit 5d41bf1
Show file tree
Hide file tree
Showing 4 changed files with 49 additions and 21 deletions.
5 changes: 5 additions & 0 deletions .changeset/honest-dryers-cry.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'fumadocs-ui': patch
---

Enable system option for theme toggle on NoteBook layout
59 changes: 41 additions & 18 deletions packages/ui/src/components/layout/theme-toggle.tsx
Original file line number Diff line number Diff line change
@@ -1,47 +1,70 @@
'use client';
import { cva } from 'class-variance-authority';
import { Moon, Sun } from 'lucide-react';
import { Moon, Sun, Airplay } from 'lucide-react';
import { useTheme } from 'next-themes';
import { type ButtonHTMLAttributes } from 'react';
import { type HTMLAttributes, useLayoutEffect, useState } from 'react';
import { cn } from '@/utils/cn';

const buttonVariants = cva(
'size-7 rounded-full p-1.5 text-fd-muted-foreground',
{
variants: {
dark: {
true: 'dark:bg-fd-accent dark:text-fd-accent-foreground',
false:
'bg-fd-accent text-fd-accent-foreground dark:bg-transparent dark:text-fd-muted-foreground',
active: {
true: 'bg-fd-accent text-fd-accent-foreground',
false: 'text-fd-muted-foreground',
},
},
},
);

export function ThemeToggle({
className,
mode = 'light-dark',
...props
}: ButtonHTMLAttributes<HTMLButtonElement>): React.ReactElement {
const { setTheme, resolvedTheme } = useTheme();
}: HTMLAttributes<HTMLDivElement> & {
mode?: 'light-dark' | 'light-dark-system';
}) {
const { setTheme, theme, resolvedTheme } = useTheme();
const [mounted, setMounted] = useState(false);
let value = mode === 'light-dark' ? resolvedTheme : theme;
if (!mounted) value = undefined;

const onToggle = () => {
setTheme(resolvedTheme === 'dark' ? 'light' : 'dark');
};
useLayoutEffect(() => {
setMounted(true);
}, []);

return (
<button
type="button"
<div
className={cn(
'inline-flex items-center rounded-full border p-[3px]',
className,
)}
data-theme-toggle=""
aria-label="Toggle Theme"
onClick={onToggle}
{...props}
>
<Sun className={cn(buttonVariants({ dark: false }))} />
<Moon className={cn(buttonVariants({ dark: true }))} />
</button>
<button
className={cn(buttonVariants({ active: value === 'light' }))}
onClick={() => setTheme('light')}
aria-label="Light Theme"
>
<Sun className="size-full" />
</button>
<button
className={cn(buttonVariants({ active: value === 'dark' }))}
onClick={() => setTheme('dark')}
aria-label="Dark Theme"
>
<Moon className="size-full" />
</button>
{mode === 'light-dark-system' ? (
<button
className={cn(buttonVariants({ active: value === 'system' }))}
onClick={() => setTheme('system')}
aria-label="System Theme"
>
<Airplay className="size-full" />
</button>
) : null}
</div>
);
}
2 changes: 1 addition & 1 deletion packages/ui/src/layouts/notebook.client.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ export function Navbar(props: HTMLAttributes<HTMLElement>) {
} as object
}
>
<div className="mx-auto flex size-full flex-row items-center border-b border-fd-foreground/10 px-4 md:gap-1.5 lg:px-8">
<div className="mx-auto flex size-full flex-row items-center border-b border-fd-foreground/10 px-4 md:gap-1.5">
{props.children}
</div>
</header>
Expand Down
4 changes: 2 additions & 2 deletions packages/ui/src/layouts/notebook.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ export function DocsLayout({
);

const pageStyles: PageStyles = {
tocNav: cn('lg:px-4 xl:hidden'),
tocNav: cn('xl:hidden'),
toc: cn('max-xl:hidden'),
page: cn('mt-[var(--fd-nav-height)]'),
};
Expand Down Expand Up @@ -203,7 +203,7 @@ function DocsNavbar({
<Languages className="size-5" />
</LanguageToggle>
) : null}
<ThemeToggle className="p-0 max-md:hidden" />
<ThemeToggle className="max-md:hidden" mode="light-dark-system" />
</Navbar>
);
}
Expand Down

0 comments on commit 5d41bf1

Please sign in to comment.