Skip to content

Commit

Permalink
add dark mode
Browse files Browse the repository at this point in the history
  • Loading branch information
d-ivashchuk committed Mar 28, 2024
1 parent 2f689c5 commit 5b41bbf
Show file tree
Hide file tree
Showing 6 changed files with 104 additions and 31 deletions.
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@
"lucide-react": "^0.363.0",
"next": "^14.1.3",
"next-auth": "^4.24.6",
"next-themes": "^0.3.0",
"posthog-js": "^1.116.6",
"react": "18.2.0",
"react-dom": "18.2.0",
Expand Down
61 changes: 37 additions & 24 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

40 changes: 40 additions & 0 deletions src/components/patterns/color-mode-switch.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
"use client";

import * as React from "react";
import { Moon, Sun } from "lucide-react";
import { useTheme } from "next-themes";

import { Button } from "~/components/ui/button";
import {
DropdownMenu,
DropdownMenuContent,
DropdownMenuItem,
DropdownMenuTrigger,
} from "~/components/ui/dropdown-menu";

export function ColorModeSwitch() {
const { setTheme } = useTheme();

return (
<DropdownMenu>
<DropdownMenuTrigger asChild>
<Button variant="outline" size="icon">
<Sun className="h-[1.2rem] w-[1.2rem] rotate-0 scale-100 transition-all dark:-rotate-90 dark:scale-0" />
<Moon className="absolute h-[1.2rem] w-[1.2rem] rotate-90 scale-0 transition-all dark:rotate-0 dark:scale-100" />
<span className="sr-only">Toggle theme</span>
</Button>
</DropdownMenuTrigger>
<DropdownMenuContent align="end">
<DropdownMenuItem onClick={() => setTheme("light")}>
Light
</DropdownMenuItem>
<DropdownMenuItem onClick={() => setTheme("dark")}>
Dark
</DropdownMenuItem>
<DropdownMenuItem onClick={() => setTheme("system")}>
System
</DropdownMenuItem>
</DropdownMenuContent>
</DropdownMenu>
);
}
2 changes: 2 additions & 0 deletions src/components/patterns/layout.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import { Input } from "~/components/ui/input";
import { Sheet, SheetContent, SheetTrigger } from "~/components/ui/sheet";
import LoginLogoutButton from "./login-logout-button";
import UserButton from "./user-button";
import { ColorModeSwitch } from "./color-mode-switch";

export async function Layout({ children }: { children: React.ReactNode }) {
return (
Expand Down Expand Up @@ -120,6 +121,7 @@ export async function Layout({ children }: { children: React.ReactNode }) {
/>
</div>
</form>
<ColorModeSwitch />
<DropdownMenu>
<DropdownMenuTrigger asChild>
<Button variant="secondary" size="icon" className="rounded-full">
Expand Down
22 changes: 15 additions & 7 deletions src/components/providers/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import { SessionProvider, useSession } from "next-auth/react";
import { TRPCReactProvider } from "~/trpc/react";
import { env } from "~/env.mjs";
import { useSearchParams } from "next/navigation";
import { ThemeProvider } from "./theme-provider";

if (typeof window !== "undefined" && process.env.NODE_ENV === "production") {
posthog.init(env.NEXT_PUBLIC_POSTHOG_API_KEY!, {
Expand All @@ -31,13 +32,20 @@ const PostHogIdentification = ({ children }: { children: React.ReactNode }) => {

const Providers = ({ children }: { children: React.ReactNode }) => {
return (
<TRPCReactProvider>
<SessionProvider>
<PostHogProvider client={posthog}>
<PostHogIdentification>{children}</PostHogIdentification>
</PostHogProvider>
</SessionProvider>
</TRPCReactProvider>
<ThemeProvider
attribute="class"
defaultTheme="system"
enableSystem
disableTransitionOnChange
>
<TRPCReactProvider>
<SessionProvider>
<PostHogProvider client={posthog}>
<PostHogIdentification>{children}</PostHogIdentification>
</PostHogProvider>
</SessionProvider>
</TRPCReactProvider>
</ThemeProvider>
);
};

Expand Down
9 changes: 9 additions & 0 deletions src/components/providers/theme-provider.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
"use client";

import * as React from "react";
import { ThemeProvider as NextThemesProvider } from "next-themes";
import { type ThemeProviderProps } from "next-themes/dist/types";

export function ThemeProvider({ children, ...props }: ThemeProviderProps) {
return <NextThemesProvider {...props}>{children}</NextThemesProvider>;
}

0 comments on commit 5b41bbf

Please sign in to comment.