Skip to content

Production-ready animations, transitions, backgrounds, and scenes for Remotion

feat: toggle theme by key D pressed

dev_wandry300ed2b0586a
diff --git a/app/layout.tsx b/app/layout.tsx
index 1795988e..8f5ed929 100644
--- a/app/layout.tsx
+++ b/app/layout.tsx
@@ -5,6 +5,7 @@ import { NuqsAdapter } from "nuqs/adapters/next/app";
import "./globals.css";
import { OpenPanelComponent } from "@openpanel/nextjs";
import { cn } from "@/lib/utils";
+import { ThemeShortcut } from "./theme-shortcut";
const inter = Manrope({ subsets: ["latin"], variable: "--font-sans" });
@@ -59,6 +60,7 @@ export default function RootLayout({
enableSystem: true,
}}
>
+ <ThemeShortcut />
{children}
</RootProvider>
</NuqsAdapter>
diff --git a/app/theme-shortcut.tsx b/app/theme-shortcut.tsx
new file mode 100644
index 00000000..3c2dd9a9
--- /dev/null
+++ b/app/theme-shortcut.tsx
@@ -0,0 +1,12 @@
+"use client";
+
+import { useThemeShortcut } from "@/hooks/use-theme-shortcut";
+
+/**
+ * Headless listener that wires up the "D" key theme toggle.
+ * Must render inside the theme provider so `useTheme()` has context.
+ */
+export function ThemeShortcut() {
+ useThemeShortcut();
+ return null;
+}
diff --git a/hooks/use-theme-shortcut.ts b/hooks/use-theme-shortcut.ts
new file mode 100644
index 00000000..eb46fb7a
--- /dev/null
+++ b/hooks/use-theme-shortcut.ts
@@ -0,0 +1,37 @@
+import { useTheme } from "next-themes";
+import { useEffect, useRef } from "react";
+
+/**
+ * Toggles between light and dark theme when the "D" key is pressed.
+ * Ignores key presses while the user is typing in a field or holding a modifier.
+ */
+export function useThemeShortcut() {
+ const { resolvedTheme, setTheme } = useTheme();
+
+ // Mirror the theme in a ref so the listener stays registered across toggles.
+ const resolvedThemeRef = useRef(resolvedTheme);
+ resolvedThemeRef.current = resolvedTheme;
+
+ useEffect(() => {
+ const onKeyDown = (event: KeyboardEvent) => {
+ if (event.key.toLowerCase() !== "d") return;
+ if (event.metaKey || event.ctrlKey || event.altKey) return;
+
+ const target = event.target as HTMLElement | null;
+ if (
+ target?.isContentEditable ||
+ target instanceof HTMLInputElement ||
+ target instanceof HTMLTextAreaElement ||
+ target instanceof HTMLSelectElement
+ ) {
+ return;
+ }
+
+ event.preventDefault();
+ setTheme(resolvedThemeRef.current === "dark" ? "light" : "dark");
+ };
+
+ window.addEventListener("keydown", onKeyDown);
+ return () => window.removeEventListener("keydown", onKeyDown);
+ }, [setTheme]);
+}