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

Refactored SplitButton (breaking) #2602

Open
wants to merge 2 commits into
base: main
Choose a base branch
from

Conversation

kraenhansen
Copy link
Member

@kraenhansen kraenhansen commented Dec 17, 2024

✍️ Proposed changes

As per my suggestion in #2601, future regressions could be avoided if the types derived from MenuProps intended for pass-through is allow-listed via a Pick instead of deny-listed via Omit.

This change is breaking, since it's removing a lot of props from the SplitButton which were never actually passed through to the Menu.

This also optimize the internals by passing darkMode directly to child components instead of wrapping them in a new LeafyGreenProvider - this is not breaking, just a "drive-by" optimization.

NOTE: If #2601 merge before this PR, it needs to be updated to add renderDarkMenu in the allow-list.

✅ Checklist

For bug fixes, new features & breaking changes

  • I have added tests that prove my fix is effective or that my feature works
  • I have added necessary documentation (if appropriate)
  • I have run yarn changeset and documented my changes

For new components

  • I have added my new package to the global tsconfig
  • I have added my new package to the Table of Contents on the global README
  • I have verified the Live Example looks as intended on the design website.

🧪 How to test changes

  • Add the @leafygreen-ui/split-button to a project
  • Observe how the onEnter or renderDarkMenu props are available on the SplitButton.
  • Apply this patch.
  • Observe how the onEnter or renderDarkMenu props are no longer available on the SplitButton.

@kraenhansen kraenhansen requested a review from a team as a code owner December 17, 2024 12:51
@kraenhansen kraenhansen requested review from tsck and removed request for a team December 17, 2024 12:51
Copy link

changeset-bot bot commented Dec 17, 2024

🦋 Changeset detected

Latest commit: 20a5089

The changes in this PR will be included in the next version bump.

This PR includes changesets to release 1 package
Name Type
@leafygreen-ui/split-button Major

Not sure what this means? Click here to learn what changesets are.

Click here if you're a maintainer who wants to add another changeset to this PR

@@ -56,7 +54,7 @@ export const SplitButton = InferredPolymorphic<SplitButtonProps, 'button'>(
ref: React.Ref<any>,
) => {
const { Component } = useInferredPolymorphic(as, rest, 'button');
const { darkMode, theme } = useDarkMode(darkModeProp);
const { theme } = useDarkMode(darkMode);
Copy link
Collaborator

Choose a reason for hiding this comment

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

We still want to use the darkMode value returned from useDarkMode because it reads and returns the darkMode value from the parent LeafyGreenProvider. If darkMode is passed into the component, it will take precedence over the LeafyGreenProvider darkMode value.

Copy link
Member Author

@kraenhansen kraenhansen Dec 17, 2024

Choose a reason for hiding this comment

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

If darkMode is passed into the component, it will take precedence over the LeafyGreenProvider darkMode value.

That's the exact the behaviour I'd expect though?

I expect the Button and Menu (rendered internally in SplitButton) to call useDarkMode by themselves to apply the right theme from the provider iff a darkMode prop isn't passed to the SplitButton.

Copy link
Collaborator

Choose a reason for hiding this comment

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

Thank you for pointing this out! You're absolutely correct.

However, we’ll stick with the current implementation, as we intentionally pass darkMode directly to the LeafyGreenProvider. This approach is deliberate and aligns with how we typically handle darkMode across other LeafyGreen components.

Copy link
Member Author

Choose a reason for hiding this comment

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

I do see this pattern being used a lot of places and being documented here: https://github.com/mongodb/leafygreen-ui/blob/main/STYLEGUIDE.md#passing-darkmode-to-children

I'll go ahead and revert that change 👍

I do however think this decision should be revisited, wrapping components in multiple layers of these context providers isn't "free" and they all contribute to a messier component graph as well as potential rendering bottlenecks - especially when the context updates.

@kraenhansen
Copy link
Member Author

I've drafted this again, since there are more changes that I'd like to include: It isn't possible to pass menu separators through the menuItems prop.

@@ -53,18 +53,22 @@ type ButtonProps = Omit<
'rightGlyph' | 'href' | 'as' | 'variant'
>;

export type SelectedMenuProps = Omit<
export type SelectedMenuProps = Pick<
Copy link
Collaborator

@shaneeza shaneeza Dec 17, 2024

Choose a reason for hiding this comment

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

I think this makes sense, this way we are explicit with what props menu accepts

Copy link
Collaborator

@shaneeza shaneeza left a comment

Choose a reason for hiding this comment

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

Thanks for the PR! The type updates make sense. However, we wouldn't want to remove the LeafyGreenProvider since passing darkMode to the LeafyGreenProvider is an explicit pattern we've adopted in other LG components.

@kraenhansen
Copy link
Member Author

I've created one more PR with suggestioned breaking changes to the SplitButton API: #2604 - if those are accepted it would be ideal if they get released together, to avoid two major version bumps right after each other.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants