Skip to content

fix: add null check for event.key in ThemeHotkey to prevent crash on browser autofill (fixes #10378)#10401

Open
armorbreak001 wants to merge 1 commit intoshadcn-ui:mainfrom
armorbreak001:fix/theme-hotkey-autofill-crash
Open

fix: add null check for event.key in ThemeHotkey to prevent crash on browser autofill (fixes #10378)#10401
armorbreak001 wants to merge 1 commit intoshadcn-ui:mainfrom
armorbreak001:fix/theme-hotkey-autofill-crash

Conversation

@armorbreak001
Copy link
Copy Markdown

Background

The default theme-provider.tsx generated by npx shadcn@latest init contains a ThemeHotkey component that listens for the Ctrl+D keyboard shortcut to toggle the theme. The listener calls event.key.toLowerCase() without checking if event.key is defined.

During browser autofill/autocomplete on Chrome and Edge, the browser fires a synthetic keydown event where event.key is undefined. This causes an uncaught TypeError:

Uncaught TypeError: Cannot read properties of undefined (reading toLowerCase)
    at onKeyDown (theme-provider.tsx)

Solution

Add a null/undefined guard before accessing event.key.toLowerCase() in all affected files. This follows the same defensive pattern already used elsewhere in the handler (e.g., event.defaultPrevented check).

Changes

  • templates/next-app/components/theme-provider.tsx: Added !event.key || guard
  • templates/next-monorepo/apps/web/components/theme-provider.tsx: Added !event.key || guard
  • templates/vite-app/src/components/theme-provider.tsx: Added !event.key || guard
  • templates/vite-monorepo/apps/web/src/components/theme-provider.tsx: Added !event.key || guard
  • apps/v4/app/(app)/create/lib/v0.ts: Added !event.key || guard

Verification

  1. Create a new Next.js project with npx shadcn@latest init
  2. Add <input type="email" /> to a page
  3. Save an autofill entry in Chrome
  4. Focus the input and select a suggestion from the browser autocomplete dropdown
  5. Before fix: TypeError in console
  6. After fix: Autofill completes normally, no errors

Closes #10378

@vercel
Copy link
Copy Markdown
Contributor

vercel bot commented Apr 15, 2026

@armorbreak001 is attempting to deploy a commit to the shadcn-pro Team on Vercel.

A member of the Team first needs to authorize it.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[bug]: TypeError: Cannot read properties of undefined (reading 'toLowerCase') in ThemeHotkey on browser autofill keydown

1 participant