Skip to content

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

Merged

Conversation

TomRytt
Copy link
Collaborator

@TomRytt TomRytt commented Dec 27, 2024

Description

*Not sure if this should be filed under refactor or fix.

Moved logic from LanguageToggle to themeContext for four main advantages:

  1. Update the direction when the language changes and ensure theme.direction is synced with i18n.dir for one source of truth.
  2. Language toggle logic felt more appropriate in context instead of directly in component
  3. Now LanguageToggle is just LanguageToggleButton, which feels more appropriate since the toggle logic moved to the context and the button just triggers it.
  4. Now instead of calling i18n.dir() in functions, can use theme.direction directly.

screenshots

image
image

@TomRytt TomRytt added the frontend frontend developers issue label Dec 27, 2024
@TomRytt TomRytt self-assigned this Dec 27, 2024
@TomRytt TomRytt requested a review from NoamGaash as a code owner December 27, 2024 15:08
@TomRytt TomRytt changed the title Fix: Moved language toggle to themeContext and update theme.direction on language change fix: Moved language toggle to themeContext and update theme.direction on language change Dec 27, 2024
Copy link
Contributor

github-actions bot commented Dec 27, 2024

Copy link
Member

@NoamGaash NoamGaash left a comment

Choose a reason for hiding this comment

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

Thank you @TomRytt ! 🥇

const { defaultAlgorithm, darkAlgorithm } = theme

export const ThemeProvider: FC<PropsWithChildren> = ({ children }) => {
const [isDarkTheme, setIsDarkTheme] = useLocalStorage<boolean>('isDarkTheme')

const [direction, setDirection] = useState<'ltr' | 'rtl'>('rtl')
Copy link
Member

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

Copy link
Collaborator Author

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.

Copy link
Member

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 )

Copy link
Collaborator Author

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?

Copy link
Collaborator Author

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?

Copy link
Member

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 :)

@TomRytt TomRytt merged commit 8da17e1 into hasadna:main Jan 4, 2025
13 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
frontend frontend developers issue
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants