-
Notifications
You must be signed in to change notification settings - Fork 3k
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
Example: Theme switching migration #27346
Example: Theme switching migration #27346
Conversation
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.
Some thoughts on readability
merged #27324 |
@grgia Should i (ever) remove the "draft" from this PR? Or will this example stay draft forever? haha In this case, i won't need to fill out PR author checklist and do the rest of the obligatory work... |
@grgia further simplified the guidelines/migration process in the PR description 👍 |
we decided to leave this as a draft for now |
Closing this since we're done migrating the app! 🚀 |
This PR needs #27324 (review) to be merged first
@grgia
Details
Goal of this PR/Template
This PR aims to provide outlines and both a class component and functional component example of how to use the new theme switching logic in the Expensify app.
Migration process
In order to update a component so it uses the new theme switching, we have to do a few things:
src/styles/styles.js
with the styles from theThemeStylesProvider
src/styles/theme/default.js
with the theme from theThemeProvider
Implementation
Replacing imports
At the beginning of the file, you should have some imports like this:
These imports could be named slightly different like
theme
instead ofdefaultTheme
, remove them anyway.Remove these imports and replace it with the imports of the new hooks/HOCs. You might not need both hooks/HOCs, import only the ones you need:
Functional components
For functional components we introduced two hooks
useTheme
anduseThemeStyles
, both of which will give you the theme-adapted colors and styles. Add one or both of these lines:Class components
For class components we have to do things slightly different. We've introduced two HOCs
withTheme
andwithThemeStyles
, which will add the theme-adapted colors and styles to the props of your component. Add one or both of these lines:Integrating the dynamic theme and styles
In order to migrate a component so that it uses the dynamic theme and styles provided by the theme switching logic, we'll have to do a few things:
Functional components
Use these two hooks at the beginning of the component:
Class components
Wrap the component with one or both of these HOCs. If the component already uses
compose
to wrap multiple HOCs, make sure to continue using it:By adding these HOCs, you should receive two objects
theme
andthemeStyles
inside the component'sprops
. Make sure to add some convenience variables to the top of yourrender
function and everywhere the theme and styles are used, so the code doesn't get too cluttered:Replace all occurrences of static theme and styles
If the previous static imports were named exactly as the new dynamic theme and styles variables, we don't have to change anything.
If not, simply replace all occurrences of...
staticStyles.someStyle.
withstyles.someStyle.
defaultTheme.someColor.
withtheme.someColor.
This can easily and quickly be done with the "Find and replace" function in the Editor/IDE of your choice. Still make sure, that only the right occurrences of this search pattern get replaced with the new code. (Don't mess up other stuff!🤠)
Wrapping it up
That's it! 🚀 Make sure to not change anything else or mess anything up by using "Find and replace" in your Editor/IDE. Also make sure to always use the provided TS types.
Fixed Issues
$ #27342
Tests
Offline tests
QA Steps
PR Author Checklist
### Fixed Issues
section aboveTests
sectionOffline steps
sectionQA steps
sectiontoggleReport
and notonIconClick
)myBool && <MyComponent />
.src/languages/*
files and using the translation methodWaiting for Copy
label for a copy review on the original GH to get the correct copy.STYLE.md
) were followedAvatar
, I verified the components usingAvatar
are working as expected)/** comment above it */
this
properly so there are no scoping issues (i.e. foronClick={this.submit}
the methodthis.submit
should be bound tothis
in the constructor)this
are necessary to be bound (i.e. avoidthis.submit = this.submit.bind(this);
ifthis.submit
is never passed to a component event handler likeonClick
)StyleUtils.getBackgroundAndBorderStyle(themeColors.componentBG)
)Avatar
is modified, I verified thatAvatar
is working as expected in all cases)ScrollView
component to make it scrollable when more elements are added to the page.main
branch was merged into this PR after a review, I tested again and verified the outcome was still expected according to theTest
steps.Screenshots/Videos
Web
Mobile Web - Chrome
Mobile Web - Safari
Desktop
iOS
Android