Skip to content
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

refactor(v2): replace react-toggle with own implementation #4608

Merged
merged 2 commits into from
Apr 15, 2021

Conversation

lex111
Copy link
Contributor

@lex111 lex111 commented Apr 13, 2021

Motivation

A lightweight and simplified alternative to react-toggle built on functional component/hooks instead on class as in original component. Also I deliberately not using touch events, because I think a mobile user can just tap on toggle to switch mode ( not sure if someone used gestures for this).
As another advantage, we get rid of another external dependency and have more control over this component.

Have you read the Contributing Guidelines on pull requests?

Yes

Test Plan

No breaking changes, CSS class names are same.

Related PRs

(If this PR adds or changes functionality, please take some time to update the docs at https://github.com/facebook/docusaurus, and link to your PR here.)

@lex111 lex111 added the pr: polish This PR adds a very minor behavior improvement that users will enjoy. label Apr 13, 2021
@lex111 lex111 requested a review from slorber as a code owner April 13, 2021 12:19
@facebook-github-bot facebook-github-bot added the CLA Signed Signed Facebook CLA label Apr 13, 2021
@netlify
Copy link

netlify bot commented Apr 13, 2021

[V2]

Built with commit dcd0002

https://deploy-preview-4608--docusaurus-2.netlify.app

@github-actions
Copy link

github-actions bot commented Apr 13, 2021

⚡️ Lighthouse report for the changes in this PR:

Category Score
🟠 Performance 77
🟢 Accessibility 96
🟢 Best practices 100
🟢 SEO 100
🟢 PWA 95

Lighthouse ran on https://deploy-preview-4608--docusaurus-2.netlify.app/

@netlify
Copy link

netlify bot commented Apr 13, 2021

[V1]

Built with commit dcd0002

https://deploy-preview-4608--docusaurus-1.netlify.app

@github-actions
Copy link

github-actions bot commented Apr 13, 2021

Size Change: +490 B (0%)

Total Size: 598 kB

Filename Size Change
website/build/assets/js/main.********.js 422 kB +422 B (0%)
website/build/blog/2017/12/14/introducing-docusaurus/index.html 62.1 kB +34 B (0%)
website/build/index.html 26.9 kB +34 B (0%)
ℹ️ View Unchanged
Filename Size Change
website/build/assets/css/styles.********.css 87.2 kB 0 B
website/build/docs/introduction/index.html 235 B 0 B

compressed-size-action

Copy link
Collaborator

@slorber slorber left a comment

Choose a reason for hiding this comment

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

Thanks, looks like a good idea, as the original component is heavier than we need.
I don't think either we need touch events.

For some reason, the build size bot does not report any decrease in size, curious to understand why?

See inline comment

const {
colorMode: {
switchConfig: {darkIcon, darkIconStyle, lightIcon, lightIconStyle},
},
} = useThemeConfig();
const {isClient} = useDocusaurusContext();
const [checked, setChecked] = useState(defaultChecked);
Copy link
Collaborator

@slorber slorber Apr 14, 2021

Choose a reason for hiding this comment

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

Can you extract all the non-docusaurus code to a separate function and mention in a comment it's inspired from react-toggle? Mixing concerns like reading Docusaurus context and handling low-level dom code is not a good idea and will make the code less reusable once we'll have multiple themes

Also, the original comp used PureComponent so we can also use React.memo() around that newly extracted component too (the onChange is already memorized)

Copy link
Contributor Author

Choose a reason for hiding this comment

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

It seems redundant to me to create another component as like before, I also don't understand the gist of memoizing this new child component, but okay I'll do it.

Copy link
Collaborator

Choose a reason for hiding this comment

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

The concept of a component (or hook) is to encapsulate a concerns.
If multiple concerns end up being mixed, you'd rather split and refactor into multiple components. Creating large components and mixing concerns does not help to make the code readable, maintainable, and is also less performant (in most cases) as you have to render the thing as a whole instead.

I'd favor readability and good encapsulation over perf in most cases.

We'll probably someday put this UI component in a Storybook and test it in isolation. We don't necessarily want to couple it to docusaurus context, as the test/storybook will now have to "mock" that context (ie it will require a provider)

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I get it, it's just that our earlier components didn't follow that pattern, although we'll need to refactor them the same way, I guess.

Copy link
Collaborator

Choose a reason for hiding this comment

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

yes that's the goal but no hurry for now

@slorber
Copy link
Collaborator

slorber commented Apr 15, 2021

LGTM thanks 👍

@slorber slorber merged commit 836f927 into master Apr 15, 2021
@slorber slorber deleted the lex111/react-toggle branch August 17, 2021 18:02
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
CLA Signed Signed Facebook CLA pr: polish This PR adds a very minor behavior improvement that users will enjoy.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants