fix(ui-components): guard ThemeToggle against invalid stored theme#8730
fix(ui-components): guard ThemeToggle against invalid stored theme#8730shivxmsharma wants to merge 1 commit intonodejs:mainfrom
Conversation
|
The latest updates on your projects. Learn more about Vercel for GitHub.
|
👋 Codeowner Review RequestThe following codeowners have been identified for the changed files: Team reviewers: @nodejs/nodejs-website Please review the changes when you have a chance. Thank you! 🙏 |
There was a problem hiding this comment.
Pull request overview
Fixes a runtime crash in ThemeToggle by normalizing an unexpected/invalid currentTheme value (e.g., from stale localStorage) to a safe fallback before rendering the trigger icon and determining the active dropdown item.
Changes:
- Normalize
currentThemeto'system'when it’s not a known theme key. - Use the normalized theme for icon lookup and active item highlighting.
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
You can also share your feedback on Copilot code review. Take the survey.
| }) => { | ||
| const TriggerIcon = themeIcons[currentTheme]; | ||
| const normalizedTheme: Theme = | ||
| currentTheme in themeIcons ? currentTheme : 'system'; |
| const normalizedTheme: Theme = | ||
| currentTheme in themeIcons ? currentTheme : 'system'; | ||
| const TriggerIcon = themeIcons[normalizedTheme]; |
|
I don't think we need a type guard here, we know that it's set by us |
|
Thanks for the review, that makes sense. If invalid localStorage theme values are considered out of scope and the project prefers relying on the controlled write path, I’m happy to close this out. |
Description
This PR fixes a runtime crash in
ThemeTogglewhencurrentThemecontains an unexpected value.Previously, the component assumed
currentThemewas always one ofsystem | light | dark. If a stale or invalid value was persisted inlocalStorage(for example,invalid-theme),themeIcons[currentTheme]resolved toundefined, causing React to throw:Element type is invalid: expected a string (for built-in components) or a class/function (for composite components) but got: undefined.The fix normalizes
currentThemetosystemwhen the value is not a known key, and uses the normalized value for both icon lookup and active item highlighting.Validation
I validated the fix with the following steps:
ThemeTogglewith "Element type is invalid".systembehavior.Related Issues
Fixes #8729
Check List
pnpm formatto ensure the code follows the style guide.pnpm testto check if all tests are passing.pnpm buildto check if the website builds without errors.