Skip to content

Commit

Permalink
Handle using system theme when toggling from the library
Browse files Browse the repository at this point in the history
  • Loading branch information
jacksonh committed Feb 22, 2024
1 parent b56610c commit 84fcbce
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 35 deletions.
27 changes: 11 additions & 16 deletions packages/web/components/templates/PrimaryDropdown.tsx
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
import { useRouter } from 'next/router'
import { Moon, Sun } from 'phosphor-react'
import { ReactNode, useCallback, useState } from 'react'
import { ReactNode, useCallback } from 'react'
import { useGetViewerQuery } from '../../lib/networking/queries/useGetViewerQuery'
import { getCurrentLocalTheme, updateTheme } from '../../lib/themeUpdater'
import { Avatar } from '../elements/Avatar'
import { AvatarDropdown } from '../elements/AvatarDropdown'
import {
Expand All @@ -16,7 +15,7 @@ import { Box, HStack, SpanBox, VStack } from '../elements/LayoutPrimitives'
import { StyledText } from '../elements/StyledText'
import { styled, theme, ThemeId } from '../tokens/stitches.config'
import { LayoutType } from './homeFeed/HomeFeedContainer'
import { DropdownMenu } from '@radix-ui/react-dropdown-menu'
import { useCurrentTheme, isDarkTheme } from '../../lib/hooks/useCurrentTheme'

type PrimaryDropdownProps = {
children?: ReactNode
Expand Down Expand Up @@ -267,15 +266,7 @@ export const StyledToggleButton = styled('button', {
})

function ThemeSection(props: PrimaryDropdownProps): JSX.Element {
const [displayTheme, setDisplayTheme] = useState(getCurrentLocalTheme())

const doUpdateTheme = useCallback(
(newTheme: ThemeId) => {
updateTheme(newTheme)
setDisplayTheme(newTheme)
},
[setDisplayTheme]
)
const { currentTheme, setCurrentTheme } = useCurrentTheme()

return (
<>
Expand Down Expand Up @@ -311,18 +302,22 @@ function ThemeSection(props: PrimaryDropdownProps): JSX.Element {
}}
>
<StyledToggleButton
data-state={getCurrentLocalTheme() != ThemeId.Dark ? 'on' : 'off'}
data-state={
!(currentTheme && isDarkTheme(currentTheme)) ? 'on' : 'off'
}
onClick={() => {
doUpdateTheme(ThemeId.Light)
setCurrentTheme(ThemeId.Light)
}}
>
Light
<Sun size={15} color={theme.colors.thTextContrast2.toString()} />
</StyledToggleButton>
<StyledToggleButton
data-state={getCurrentLocalTheme() == ThemeId.Dark ? 'on' : 'off'}
data-state={
currentTheme && isDarkTheme(currentTheme) ? 'on' : 'off'
}
onClick={() => {
doUpdateTheme(ThemeId.Dark)
setCurrentTheme(ThemeId.Dark)
}}
>
Dark
Expand Down
38 changes: 19 additions & 19 deletions packages/web/lib/hooks/useCurrentTheme.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,25 @@ const themeKey = 'currentTheme'
const preferredDarkThemeKey = 'preferredDarkThemeKey'
const preferredLightThemeKey = 'preferredLightThemeKey'

export const isDarkTheme = (themeId: string): boolean => {
if (
themeId === 'Dark' ||
themeId === 'Darker' ||
themeId === 'Apollo' ||
themeId == 'Black'
) {
return true
}
return false
}

export const isLightTheme = (themeId: string): boolean => {
if (themeId === 'Sepia' || themeId == 'Light') {
return true
}
return false
}

export function useCurrentTheme() {
const isDarkMode = useDarkModeListener()
const [currentThemeInternal, setCurrentThemeInternal] = usePersistedState<
Expand All @@ -29,25 +48,6 @@ export function useCurrentTheme() {
}
)

const isDarkTheme = (themeId: string): boolean => {
if (
themeId === 'Dark' ||
themeId === 'Darker' ||
themeId === 'Apollo' ||
themeId == 'Black'
) {
return true
}
return false
}

const isLightTheme = (themeId: string): boolean => {
if (themeId === 'Sepia' || themeId == 'Light') {
return true
}
return false
}

const currentTheme = useMemo(() => {
return currentThemeInternal
}, [currentThemeInternal])
Expand Down

0 comments on commit 84fcbce

Please sign in to comment.