Skip to content

Commit

Permalink
fixed it
Browse files Browse the repository at this point in the history
  • Loading branch information
casperUoS committed Sep 6, 2024
1 parent c7cc7e2 commit 05d0bf6
Showing 1 changed file with 20 additions and 2 deletions.
22 changes: 20 additions & 2 deletions src/app/_components/ThemeImage/index.tsx
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
/* eslint-disable @next/next/no-img-element */
'use client'

import React from 'react'
import React, { useEffect, useState } from 'react'

import { useTheme } from '../../_providers/Theme'
import { themeLocalStorageKey } from '../../_providers/Theme/shared'

export const ThemeImage: React.FC<{
src: string
Expand All @@ -12,8 +13,25 @@ export const ThemeImage: React.FC<{
className: string
}> = ({ src, darksrc, alt, className }) => {
const { theme } = useTheme()
const [logoSrc, setLogoSrc] = useState(src) // Default to light theme image

const logoSrc = theme === 'dark' ? darksrc : src
useEffect(() => {
let newTheme
if (typeof window !== 'undefined') {
// Check if running in the browser
if (!theme) {
newTheme = window.localStorage.getItem(themeLocalStorageKey)
} else {
newTheme = theme
}
} else {
// Default to light theme if running on the server
newTheme = 'light'
}

const updatedLogoSrc = newTheme === 'dark' ? darksrc : src
setLogoSrc(updatedLogoSrc)
}, [theme, src, darksrc])

return <img className={className} src={logoSrc} alt={alt} />
}

0 comments on commit 05d0bf6

Please sign in to comment.