fix: theme button (#1394)

This commit is contained in:
Himself65
2023-03-07 14:45:19 -06:00
committed by GitHub
parent 1362584880
commit d565f71939
2 changed files with 14 additions and 25 deletions

View File

@@ -4,17 +4,13 @@ import { useState } from 'react';
import { MoonIcon, SunIcon } from './Icons'; import { MoonIcon, SunIcon } from './Icons';
import { StyledSwitchItem, StyledThemeModeSwitch } from './style'; import { StyledSwitchItem, StyledThemeModeSwitch } from './style';
export const ThemeModeSwitch = () => { export const ThemeModeSwitch = () => {
const { theme, setTheme } = useTheme(); const { setTheme, resolvedTheme } = useTheme();
const [isHover, setIsHover] = useState(false); const [isHover, setIsHover] = useState(false);
const [firstTrigger, setFirstTrigger] = useState(false);
return ( return (
<StyledThemeModeSwitch <StyledThemeModeSwitch
data-testid="change-theme-container" data-testid="change-theme-container"
onMouseEnter={() => { onMouseEnter={() => {
setIsHover(true); setIsHover(true);
if (!firstTrigger) {
setFirstTrigger(true);
}
}} }}
onMouseLeave={() => { onMouseLeave={() => {
setIsHover(false); setIsHover(false);
@@ -22,9 +18,8 @@ export const ThemeModeSwitch = () => {
> >
<StyledSwitchItem <StyledSwitchItem
data-testid="change-theme-light" data-testid="change-theme-light"
active={theme === 'light'} active={resolvedTheme === 'light'}
isHover={isHover} isHover={isHover}
firstTrigger={firstTrigger}
onClick={() => { onClick={() => {
setTheme('light'); setTheme('light');
}} }}
@@ -33,9 +28,8 @@ export const ThemeModeSwitch = () => {
</StyledSwitchItem> </StyledSwitchItem>
<StyledSwitchItem <StyledSwitchItem
data-testid="change-theme-dark" data-testid="change-theme-dark"
active={theme === 'dark'} active={resolvedTheme === 'dark'}
isHover={isHover} isHover={isHover}
firstTrigger={firstTrigger}
onClick={() => { onClick={() => {
setTheme('dark'); setTheme('dark');
}} }}

View File

@@ -15,8 +15,7 @@ export const StyledThemeModeSwitch = styled('div')({
export const StyledSwitchItem = styled('div')<{ export const StyledSwitchItem = styled('div')<{
active: boolean; active: boolean;
isHover: boolean; isHover: boolean;
firstTrigger: boolean; }>(({ active, isHover, theme }) => {
}>(({ active, isHover, firstTrigger, theme }) => {
const activeRaiseAnimate = toString( const activeRaiseAnimate = toString(
spring({ top: '0' }, { top: '-100%' }, { preset: 'gentle' }) spring({ top: '0' }, { top: '-100%' }, { preset: 'gentle' })
); );
@@ -34,26 +33,22 @@ export const StyledSwitchItem = styled('div')<{
? { ? {
color: theme.colors.iconColor, color: theme.colors.iconColor,
top: '0', top: '0',
animation: firstTrigger animation: css`
? css`
${keyframes`${ ${keyframes`${
isHover ? activeRaiseAnimate : activeDeclineAnimate isHover ? activeRaiseAnimate : activeDeclineAnimate
}`} ${ANIMATE_DURATION}ms forwards }`} ${ANIMATE_DURATION}ms forwards
` `,
: 'unset',
animationDirection: isHover ? 'normal' : 'alternate', animationDirection: isHover ? 'normal' : 'alternate',
} }
: { : {
top: '100%', top: '100%',
color: theme.colors.primaryColor, color: theme.colors.primaryColor,
backgroundColor: theme.colors.hoverBackground, backgroundColor: theme.colors.hoverBackground,
animation: firstTrigger animation: css`
? css`
${keyframes`${ ${keyframes`${
isHover ? raiseAnimate : declineAnimate isHover ? raiseAnimate : declineAnimate
}`} ${ANIMATE_DURATION}ms forwards }`} ${ANIMATE_DURATION}ms forwards
` `,
: 'unset',
animationDirection: isHover ? 'normal' : 'alternate', animationDirection: isHover ? 'normal' : 'alternate',
}; };
return css` return css`