-
Notifications
You must be signed in to change notification settings - Fork 108
fix: Moved language toggle to themeContext and update theme.direction on language change #991
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
fix: Moved language toggle to themeContext and update theme.direction on language change #991
Conversation
…e theme when darkMode or direction changes
…ce actual toggle logic moved to context
…since it is now globally updated
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thank you @TomRytt ! 🥇
src/layout/ThemeContext.tsx
Outdated
const { defaultAlgorithm, darkAlgorithm } = theme | ||
|
||
export const ThemeProvider: FC<PropsWithChildren> = ({ children }) => { | ||
const [isDarkTheme, setIsDarkTheme] = useLocalStorage<boolean>('isDarkTheme') | ||
|
||
const [direction, setDirection] = useState<'ltr' | 'rtl'>('rtl') |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
do we need a state variable here? sounds like we can use derived state here
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
When I checked previously with a useEffect on the i18n.language to see where it is changing and re-create the theme.direction based on it, it didn't catch those changes so I created this state to update when changing language. I'll test it again and remove it if i can successfully derive the direction from the state.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think it can be calculated without hooks at all - it's a primitive string so there's no harm in recalculating it without memoization (unlike object references that could trigger unnecessary renders )
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You were right. I fixed this in both branches by mistake. Is that still ok to merge them now?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@NoamGaash Are the new changes better? Can I merge now?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@TomRytt sure, thanks! once everything is approved and passing merging is great :)
Description
*Not sure if this should be filed under refactor or fix.
Moved logic from LanguageToggle to themeContext for four main advantages:
screenshots