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(theme-{classic,common}): split navbar into smaller components + cleanup + swizzle config #6895

Merged
merged 18 commits into from
Mar 18, 2022

Conversation

slorber
Copy link
Collaborator

@slorber slorber commented Mar 10, 2022

Breaking change

The theme Navbar components have been significantly updated so if you have swizzled them, you may need to re-eject them.

Motivation

  • Split Navbar into multiple smaller components, easier to swizzle
  • Move more technical code to theme-common

Have you read the Contributing Guidelines on pull requests?

yes

Test Plan

preview as before

@slorber slorber added the pr: polish This PR adds a very minor behavior improvement that users will enjoy. label Mar 10, 2022
@facebook-github-bot facebook-github-bot added the CLA Signed Signed Facebook CLA label Mar 10, 2022
@slorber slorber marked this pull request as draft March 10, 2022 18:33
@netlify
Copy link

netlify bot commented Mar 10, 2022

✔️ [V2]

🔨 Explore the source changes: 44d447a

🔍 Inspect the deploy log: https://app.netlify.com/sites/docusaurus-2/deploys/62349f65599869000892d320

😎 Browse the preview: https://deploy-preview-6895--docusaurus-2.netlify.app

@github-actions
Copy link

github-actions bot commented Mar 10, 2022

⚡️ Lighthouse report for the changes in this PR:

Category Score
🟠 Performance 57
🟢 Accessibility 100
🟢 Best practices 92
🟢 SEO 100
🟢 PWA 90

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

@github-actions
Copy link

github-actions bot commented Mar 10, 2022

Size Change: +2.58 kB (0%)

Total Size: 804 kB

Filename Size Change
website/build/assets/css/styles.********.css 104 kB -35 B (0%)
website/build/assets/js/main.********.js 611 kB +2.61 kB (0%)
ℹ️ View Unchanged
Filename Size Change
website/.docusaurus/globalData.json 49.9 kB 0 B
website/build/index.html 38.8 kB +9 B (0%)

compressed-size-action

@slorber slorber marked this pull request as ready for review March 11, 2022 18:58
@slorber slorber requested a review from Josh-Cena March 12, 2022 15:51
@slorber
Copy link
Collaborator Author

slorber commented Mar 12, 2022

ready for a review

we can keep components as unsafe for now, and eventually mark some as safe later after getting some user feedback?

Copy link
Collaborator

@Josh-Cena Josh-Cena left a comment

Choose a reason for hiding this comment

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

The diff looks good to me! Wondering if this and its sisters should be marked as breaking changes since we changed the theme-common APIs?

Also, I think we can improve the swizzle CLI and ask the user to choose a subset of components (instead of always ejecting everything) in case of swizzling nested components?

Comment on lines +13 to +15
export default function NavbarColorModeToggle({
className,
}: Props): JSX.Element | null {
Copy link
Collaborator

Choose a reason for hiding this comment

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

I wonder if this layer of abstraction is useful? Supposing the entire purpose of ColorModeToggle is to toggle color modes, should we just move the hook calls to ColorModeToggle instead and remove this component?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

There is value to have design system components (ColorModeToggle) to be very simple and have limited dependencies (ie not using any context). This makes it much easier to test then in isolation, add them to Storybook and other things that I plan to do.

This abstraction is likely to be temporary anyway if we provide a NavbarColorModeItem. For now I assume it's good enough, as this component is not marked as safe anyway

The general idea is that each call site should also be responsible to integrate the toggle on a case-by-case basis. For example, rendering null in case toggle is disabled might not be the best solution in all cases: it really depends on the parent component layout.

If we have later an option to have a sidebar color mode toggle, we'd create a similar SidebarColorModeToggle component, allowing the user to easily have distinct toggle designs for navbar/sidebar

image

Copy link
Collaborator

Choose a reason for hiding this comment

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

I see, makes sense

return (
<NavbarContentLayout
left={
// TODO stop hardcoding items?
Copy link
Collaborator

Choose a reason for hiding this comment

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

I think we should do that here, or at least in an immediate follow-up PR. I was asked about how we can change the color mode toggle position and the mobile toggle position. Also in https://docusaurus.canny.io/feature-requests/p/mobile-navbar-toggle-on-righttrailing-edge.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

yes we should definitely give more flexibility here

It is a potential breaking change and probably requires some work so I'd rather have another follow-up PR to do so, eventually, find a way to be retro compatible (like normalizing the config and auto adding color mode toggle + search)?

About the mobile sidebar toggle, it probably doesn't make much sense to place it in other places than top right/left, but we can also give similar flexibility 🤷‍♂️

Comment on lines 27 to 28
// TODO BAD, temporary type hack
T extends {position?: 'left' | 'right'},
Copy link
Collaborator

Choose a reason for hiding this comment

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

Why is this bad? It looks pretty sufficient to me

Copy link
Collaborator Author

@slorber slorber Mar 16, 2022

Choose a reason for hiding this comment

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

If we didn't have the NavbarItem duplicate/incompatible types this wouldn't be necessary.

I'll keep the todo for now as IMHO we should only accept a NavbarItem type (unless we have another reason to use structural typing to split other kinds of items? In which case this util should be moved/renamed and made generic?)

packages/docusaurus-theme-common/src/utils/navbarUtils.tsx Outdated Show resolved Hide resolved
@slorber
Copy link
Collaborator Author

slorber commented Mar 16, 2022

The diff looks good to me! Wondering if this and its sisters should be marked as breaking changes since we changed the theme-common APIs?

I was also thinking about this.

In theory theme-common APIs are not documented and considered internal but at the same time it is a significant refactor that can break existing user code so not sure what to do 🤷‍♂️

Also, I think we can improve the swizzle CLI and ask the user to choose a subset of components (instead of always ejecting everything) in case of swizzling nested components?

yarn workspace website swizzle @docusaurus/theme-classic Navbar --eject --danger

image

Subfolders are not copied anymore by default, and I was thinking that an option --include-subfolders (+ prompt?) or something might be useful?

@slorber slorber mentioned this pull request Mar 16, 2022
@slorber
Copy link
Collaborator Author

slorber commented Mar 16, 2022

Note: I didn't include the NavbarItems in this refactor.

Should we nest everything related to the Navbar inside the navbar folder?

Should we create a "root" folder for all the major scopes of the theme?

  • Navbar
  • Footer
  • Docs
  • Blog
  • Pages
  • Components
  • DesignSystem
  • ?

I think it can be useful but also quite disruptive for existing users, it may not have to happen immediately, we can keep this refactor for a later major version?

(I guess it's quite unlikely that we disagree on this FS structure anyway)

slorber added 2 commits March 18, 2022 15:33
# Conflicts:
#	packages/docusaurus-theme-classic/src/theme/LayoutProviders/index.tsx
#	packages/docusaurus-theme-classic/src/theme/Navbar/index.tsx
#	packages/docusaurus-theme-common/src/utils/colorModeUtils.tsx
@Josh-Cena
Copy link
Collaborator

Should we create a "root" folder for all the major scopes of the theme?

I agree. "design system" would be for reusable components like paginators and breadcrumbs, right

@slorber
Copy link
Collaborator Author

slorber commented Mar 18, 2022

"design system" would be for reusable components like paginators and breadcrumbs

yes, and we should try to encapsulate most of the Infima code there (apart things for spacing / layout etc)

@slorber
Copy link
Collaborator Author

slorber commented Mar 18, 2022

going to merge this

it's not perfect and can't be marked as safe for swizzle, but still better than before

@slorber slorber changed the title refactor(theme-common): split navbar into smaller components + cleanup + swizzle config refactor(theme-{classic,common}): split navbar into smaller components + cleanup + swizzle config Mar 18, 2022
@slorber slorber added the pr: breaking change Existing sites may not build successfully in the new version. Description contains more details. label Mar 18, 2022
@slorber slorber merged commit a1d333e into main Mar 18, 2022
@slorber slorber deleted the slorber/split-navbar branch March 18, 2022 15:21
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: breaking change Existing sites may not build successfully in the new version. Description contains more details. 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