-
Notifications
You must be signed in to change notification settings - Fork 17
[TAPS-0000] Migrate Button and Switch to BASE UI #4906
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
Open
OleksandrNechai
wants to merge
4
commits into
master
Choose a base branch
from
migrate-button-switch-to-base-ui
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -9,8 +9,7 @@ import type { | |
| TextLabelProps, | ||
| } from '@toptal/picasso-shared' | ||
| import { useTitleCase } from '@toptal/picasso-shared' | ||
| import { Button as MUIButtonBase } from '@mui/base/Button' | ||
| import type { ButtonRootSlotProps } from '@mui/base/Button' | ||
| import { Button as BaseUIButton } from '@base-ui/react/button' | ||
| import { Loader } from '@toptal/picasso-loader' | ||
| import { Container } from '@toptal/picasso-container' | ||
| import { noop, toTitleCase } from '@toptal/picasso-utils' | ||
|
|
@@ -60,14 +59,15 @@ const getIcon = ({ icon }: { icon?: ReactElement }) => { | |
| }) | ||
| } | ||
|
|
||
| const RootElement = forwardRef( | ||
| (props: ButtonRootSlotProps & { as: ElementType }, ref) => { | ||
| // eslint-disable-next-line @typescript-eslint/no-unused-vars | ||
| const { ownerState, as: Root, ...rest } = props | ||
| const isValidAs = (value: Props['as']) => { | ||
| const valueType = typeof value | ||
|
|
||
| return <Root {...rest} ref={ref} /> | ||
| } | ||
| ) | ||
| return ( | ||
| valueType === 'string' || | ||
| valueType === 'function' || | ||
| (valueType === 'object' && value !== null) | ||
| ) | ||
| } | ||
|
|
||
| export const ButtonBase: OverridableComponent<Props> = forwardRef< | ||
| HTMLButtonElement, | ||
|
|
@@ -98,7 +98,7 @@ export const ButtonBase: OverridableComponent<Props> = forwardRef< | |
|
|
||
| const titleCase = useTitleCase(propsTitleCase) | ||
| const finalChildren = [titleCase ? toTitleCase(children) : children] | ||
| const finalRootElementName = typeof as === 'string' ? as : 'a' | ||
| const finalAs: ElementType = isValidAs(as) ? as : 'a' | ||
|
|
||
| if (icon) { | ||
| const iconComponent = getIcon({ icon }) | ||
|
|
@@ -110,27 +110,32 @@ export const ButtonBase: OverridableComponent<Props> = forwardRef< | |
| } | ||
| } | ||
|
|
||
| const finalClassName = twMerge(createCoreClassNames({ disabled }), className) | ||
| const finalClassName = twMerge( | ||
| 'base-Button-root', | ||
| createCoreClassNames({ disabled }), | ||
| className | ||
| ) | ||
| const isNativeButton = finalAs === 'button' | ||
|
|
||
| return ( | ||
| <MUIButtonBase | ||
| <BaseUIButton | ||
| {...rest} | ||
| ref={ref} | ||
| onClick={getClickHandler(loading, onClick)} | ||
| ref={ref as React.Ref<HTMLElement>} | ||
| onClick={ | ||
| getClickHandler(loading, onClick) as BaseUIButton.Props['onClick'] | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. type casing, most likely unnecessary
Collaborator
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. |
||
| } | ||
| className={finalClassName} | ||
| style={style} | ||
| nativeButton={isNativeButton} | ||
| render={isNativeButton ? undefined : React.createElement(finalAs)} | ||
| aria-disabled={disabled} | ||
| disabled={disabled} | ||
| title={title} | ||
| value={value} | ||
| type={type} | ||
| data-component-type='button' | ||
| tabIndex={rest.tabIndex ?? disabled ? -1 : 0} | ||
| tabIndex={rest.tabIndex ?? (disabled ? -1 : 0)} | ||
| role={rest.role ?? 'button'} | ||
| rootElementName={finalRootElementName as keyof HTMLElementTagNameMap} | ||
| slots={{ root: RootElement }} | ||
| // @ts-ignore | ||
| slotProps={{ root: { as } }} | ||
| > | ||
| <Container | ||
| as='span' | ||
|
|
@@ -151,7 +156,7 @@ export const ButtonBase: OverridableComponent<Props> = forwardRef< | |
| size='small' | ||
| /> | ||
| )} | ||
| </MUIButtonBase> | ||
| </BaseUIButton> | ||
| ) | ||
| }) | ||
|
|
||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.

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.
looks like a bad pattern to me 🤔 and we would need to do that in other components too most likely, so I would think it through carefully
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.
wdym? it does same thing as before but more thoroughly. We can keep the code as it was though.