Skip to content

Commit

Permalink
fix: add color mode script runtime safeguard (#5639)
Browse files Browse the repository at this point in the history
* fix: add color mode script runtime safeguard

* chore: add changeset
  • Loading branch information
sQVe committed Feb 28, 2022
1 parent 5cd5cff commit 0017511
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 1 deletion.
5 changes: 5 additions & 0 deletions .changeset/poor-llamas-obey.md
@@ -0,0 +1,5 @@
---
"@chakra-ui/color-mode": patch
---

Adds a runtime safeguard for `ColorModeScript`.
9 changes: 8 additions & 1 deletion packages/color-mode/src/color-mode-script.tsx
Expand Up @@ -52,7 +52,14 @@ interface ColorModeScriptProps {
* to help prevent flash of color mode that can happen during page load.
*/
export const ColorModeScript = (props: ColorModeScriptProps) => {
const { initialColorMode = "light" } = props
let { initialColorMode = "light" } = props

// Runtime safeguard against invalid color mode values.
const validColorModeValues = ["dark", "light", "system"] as const
if (!validColorModeValues.includes(initialColorMode)) {
initialColorMode = "light"
}

const html = `(${String(setScript)})('${initialColorMode}')`
return (
<script nonce={props.nonce} dangerouslySetInnerHTML={{ __html: html }} />
Expand Down

0 comments on commit 0017511

Please sign in to comment.