-
Notifications
You must be signed in to change notification settings - Fork 1.8k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix(react): iconButtons have reiterate classNames (#15626)
* fix(react): iconButtons have reiterate classNames * fix(react): wrong alignment with button icon only and danger--ghost kind * fix(react): iconButtons have reiterate className --------- Co-authored-by: TJ Egan <[email protected]> Co-authored-by: Andrea N. Cardona <[email protected]>
- Loading branch information
1 parent
6ee35d3
commit e4626be
Showing
3 changed files
with
147 additions
and
107 deletions.
There are no files selected for viewing
This file contains 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 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 |
---|---|---|
@@ -0,0 +1,130 @@ | ||
/** | ||
* Copyright IBM Corp. 2016, 2023 | ||
* | ||
* This source code is licensed under the Apache-2.0 license found in the | ||
* LICENSE file in the root directory of this source tree. | ||
*/ | ||
|
||
import React from 'react'; | ||
import classNames from 'classnames'; | ||
import { usePrefix } from '../../internal/usePrefix'; | ||
import { useId } from '../../internal/useId'; | ||
import { ButtonBaseProps, ButtonProps } from './Button'; | ||
|
||
const ButtonBase = React.forwardRef(function ButtonBase< | ||
T extends React.ElementType | ||
>( | ||
{ | ||
as, | ||
children, | ||
className, | ||
dangerDescription = 'danger', | ||
disabled = false, | ||
hasIconOnly = false, | ||
href, | ||
iconDescription, | ||
isExpressive = false, | ||
isSelected, | ||
kind = 'primary', | ||
onBlur, | ||
onClick, | ||
onFocus, | ||
onMouseEnter, | ||
onMouseLeave, | ||
renderIcon: ButtonImageElement, | ||
size, | ||
tabIndex, | ||
type = 'button', | ||
...rest | ||
}: ButtonProps<T>, | ||
ref: React.Ref<unknown> | ||
) { | ||
const prefix = usePrefix(); | ||
|
||
const buttonClasses = classNames(className, { | ||
[`${prefix}--btn`]: true, | ||
[`${prefix}--btn--sm`]: size === 'sm' && !isExpressive, // TODO: V12 - Remove this class | ||
[`${prefix}--btn--md`]: size === 'md' && !isExpressive, // TODO: V12 - Remove this class | ||
[`${prefix}--btn--xl`]: size === 'xl', // TODO: V12 - Remove this class | ||
[`${prefix}--btn--2xl`]: size === '2xl', // TODO: V12 - Remove this class | ||
[`${prefix}--layout--size-${size}`]: size, | ||
[`${prefix}--btn--${kind}`]: kind, | ||
[`${prefix}--btn--disabled`]: disabled, | ||
[`${prefix}--btn--expressive`]: isExpressive, | ||
[`${prefix}--btn--icon-only`]: | ||
hasIconOnly && !className?.includes(`${prefix}--btn--icon-only`), | ||
[`${prefix}--btn--selected`]: hasIconOnly && isSelected && kind === 'ghost', | ||
}); | ||
|
||
const commonProps = { | ||
tabIndex, | ||
className: buttonClasses, | ||
ref, | ||
}; | ||
|
||
const buttonImage = !ButtonImageElement ? null : ( | ||
<ButtonImageElement | ||
aria-label={iconDescription} | ||
className={`${prefix}--btn__icon`} | ||
aria-hidden="true" | ||
/> | ||
); | ||
|
||
const dangerButtonVariants = ['danger', 'danger--tertiary', 'danger--ghost']; | ||
|
||
let component: React.ElementType = 'button'; | ||
const assistiveId = useId('danger-description'); | ||
const { 'aria-pressed': ariaPressed, 'aria-describedby': ariaDescribedBy } = | ||
rest; | ||
let otherProps: Partial<ButtonBaseProps> = { | ||
disabled, | ||
type, | ||
'aria-describedby': dangerButtonVariants.includes(kind) | ||
? assistiveId | ||
: ariaDescribedBy || undefined, | ||
'aria-pressed': | ||
ariaPressed ?? (hasIconOnly && kind === 'ghost' ? isSelected : undefined), | ||
}; | ||
const anchorProps = { | ||
href, | ||
}; | ||
|
||
let assistiveText: JSX.Element | null = null; | ||
if (dangerButtonVariants.includes(kind)) { | ||
assistiveText = ( | ||
<span id={assistiveId} className={`${prefix}--visually-hidden`}> | ||
{dangerDescription} | ||
</span> | ||
); | ||
} | ||
|
||
if (as) { | ||
component = as; | ||
otherProps = { | ||
...otherProps, | ||
...anchorProps, | ||
}; | ||
} else if (href && !disabled) { | ||
component = 'a'; | ||
otherProps = anchorProps; | ||
} | ||
|
||
return React.createElement( | ||
component, | ||
{ | ||
onMouseEnter, | ||
onMouseLeave, | ||
onFocus, | ||
onBlur, | ||
onClick, | ||
...rest, | ||
...commonProps, | ||
...otherProps, | ||
}, | ||
assistiveText, | ||
children, | ||
buttonImage | ||
); | ||
}); | ||
|
||
export default ButtonBase; |
This file contains 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