Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 5 additions & 2 deletions next-themes/src/index.tsx
Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@
'use client'

import * as React from 'react'
import { script } from './script'
import type { Attribute, ThemeProviderProps, UseThemeProps } from './types'

// @ts-ignore
import scriptTemplate from './script.template.js';
const scriptText = scriptTemplate as string;

const colorSchemes = ['light', 'dark']
const MEDIA = '(prefers-color-scheme: dark)'
const isServer = typeof window === 'undefined'
Expand Down Expand Up @@ -213,7 +216,7 @@ const ThemeScript = React.memo(
{...scriptProps}
suppressHydrationWarning
nonce={typeof window === 'undefined' ? nonce : ''}
dangerouslySetInnerHTML={{ __html: `(${script.toString()})(${scriptArgs})` }}
dangerouslySetInnerHTML={{ __html: `(${scriptText})(${scriptArgs})` }}
/>
)
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
export const script = (
(
attribute,
storageKey,
defaultTheme,
Expand All @@ -11,7 +11,7 @@ export const script = (
const el = document.documentElement
const systemThemes = ['light', 'dark']

function updateDOM(theme: string) {
function updateDOM(theme) {

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Note: if there is a concern about loosing typesafety here this solution can be improved by either using jsdoc type comments here or adding a custom esbuild loader to the tsup config,

const attributes = Array.isArray(attribute) ? attribute : [attribute]

attributes.forEach(attr => {
Expand All @@ -28,7 +28,7 @@ export const script = (
setColorScheme(theme)
}

function setColorScheme(theme: string) {
function setColorScheme(theme) {
if (enableColorScheme && systemThemes.includes(theme)) {
el.style.colorScheme = theme
}
Expand Down
5 changes: 4 additions & 1 deletion next-themes/tsup.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,5 +9,8 @@ export default defineConfig({
external: ['react'],
format: ['esm', 'cjs'],
splitting: false,
bundle: true
bundle: true,
loader: {
'.template.js': 'text'
},
})