| 1 |
import { forwardRef } from 'react'; |
| 2 |
import styles from '@patternfly/react-styles/css/components/Button/button'; |
| 3 |
import { css } from '@patternfly/react-styles'; |
| 4 |
import { Spinner, spinnerSize } from '../Spinner'; |
| 5 |
import { useOUIAProps, OUIAProps } from '../../helpers/OUIA/ouia'; |
| 6 |
import { Badge } from '../Badge'; |
| 7 |
import RhUiStarFillIcon from '@patternfly/react-icons/dist/esm/icons/rh-ui-star-fill-icon'; |
| 8 |
import RhUiStarIcon from '@patternfly/react-icons/dist/esm/icons/rh-ui-star-icon'; |
| 9 |
import RhUiSettingsFillIcon from '@patternfly/react-icons/dist/esm/icons/rh-ui-settings-fill-icon'; |
| 10 |
import { hamburgerIcon } from './hamburgerIcon'; |
| 11 |
|
| 12 |
export enum ButtonVariant { |
| 13 |
primary = 'primary', |
| 14 |
secondary = 'secondary', |
| 15 |
tertiary = 'tertiary', |
| 16 |
danger = 'danger', |
| 17 |
warning = 'warning', |
| 18 |
link = 'link', |
| 19 |
plain = 'plain', |
| 20 |
control = 'control', |
| 21 |
stateful = 'stateful' |
| 22 |
} |
| 23 |
|
| 24 |
export enum ButtonType { |
| 25 |
button = 'button', |
| 26 |
submit = 'submit', |
| 27 |
reset = 'reset' |
| 28 |
} |
| 29 |
|
| 30 |
export enum ButtonSize { |
| 31 |
default = 'default', |
| 32 |
sm = 'sm', |
| 33 |
lg = 'lg' |
| 34 |
} |
| 35 |
|
| 36 |
export enum ButtonState { |
| 37 |
read = 'read', |
| 38 |
unread = 'unread', |
| 39 |
attention = 'attention' |
| 40 |
} |
| 41 |
|
| 42 |
export interface BadgeCountObject { |
| 43 |
/** Adds styling to the badge to indicate it has been read */ |
| 44 |
isRead?: boolean; |
| 45 |
/** Adds count number right of button */ |
| 46 |
count?: number; |
| 47 |
/** Additional classes added to the badge count */ |
| 48 |
className?: string; |
| 49 |
} |
| 50 |
|
| 51 |
export interface ButtonProps extends Omit<React.HTMLProps<HTMLButtonElement>, 'ref' | 'size'>, OUIAProps { |
| 52 |
/** Content rendered inside the button */ |
| 53 |
children?: React.ReactNode; |
| 54 |
/** Additional classes added to the button */ |
| 55 |
className?: string; |
| 56 |
/** Sets the base component to render. defaults to button */ |
| 57 |
component?: React.ElementType<any> | React.ComponentType<any>; |
| 58 |
/** Adds clicked styling to button. */ |
| 59 |
isClicked?: boolean; |
| 60 |
/** Adds block styling to button */ |
| 61 |
isBlock?: boolean; |
| 62 |
/** Adds disabled styling and disables the button using the disabled html attribute */ |
| 63 |
isDisabled?: boolean; |
| 64 |
/** Adds disabled styling and communicates that the button is disabled using the aria-disabled html attribute */ |
| 65 |
isAriaDisabled?: boolean; |
| 66 |
/** Adds progress styling to button */ |
| 67 |
isLoading?: boolean; |
| 68 |
/** Text describing that current loading status or progress */ |
| 69 |
spinnerAriaValueText?: string; |
| 70 |
/** Accessible label for the spinner to describe what is loading */ |
| 71 |
spinnerAriaLabel?: string; |
| 72 |
/** Id of element which describes what is being loaded */ |
| 73 |
spinnerAriaLabelledBy?: string; |
| 74 |
/** Events to prevent when the button is in an aria-disabled state */ |
| 75 |
inoperableEvents?: string[]; |
| 76 |
/** Adds inline styling to a link button */ |
| 77 |
isInline?: boolean; |
| 78 |
/** Adds favorite styling to a button */ |
| 79 |
isFavorite?: boolean; |
| 80 |
/** Flag indicating whether the button is favorited or not, only when isFavorite is true. */ |
| 81 |
isFavorited?: boolean; |
| 82 |
/** Adds styling which affects the size of the button */ |
| 83 |
size?: 'default' | 'sm' | 'lg'; |
| 84 |
/** Sets button type */ |
| 85 |
type?: 'button' | 'submit' | 'reset'; |
| 86 |
/** Adds button variant styles */ |
| 87 |
variant?: 'primary' | 'secondary' | 'tertiary' | 'danger' | 'warning' | 'link' | 'plain' | 'control' | 'stateful'; |
| 88 |
/** Sets state of the stateful button variant. Default is "unread" */ |
| 89 |
state?: 'read' | 'unread' | 'attention'; |
| 90 |
/** Applies no padding on a plain button variant. Use when plain button is placed inline with text */ |
| 91 |
hasNoPadding?: boolean; |
| 92 |
/** Sets position of the icon. Note: "left" and "right" are deprecated. Use "start" and "end" instead */ |
| 93 |
iconPosition?: 'start' | 'end' | 'left' | 'right'; |
| 94 |
/** Adds accessible text to the button. */ |
| 95 |
'aria-label'?: string; |
| 96 |
/** Icon for the button. */ |
| 97 |
icon?: React.ReactNode | null; |
| 98 |
/** Sets the button tabindex. */ |
| 99 |
tabIndex?: number; |
| 100 |
/** Adds danger styling to secondary or link button variants */ |
| 101 |
isDanger?: boolean; |
| 102 |
/** Flag indicating whether content the button controls is expanded or not. Required when isHamburger is true. */ |
| 103 |
isExpanded?: boolean; |
| 104 |
/** Flag indicating the button is a settings button. This will override the icon property. */ |
| 105 |
isSettings?: boolean; |
| 106 |
/** Flag indicating the button is a hamburger button. This will override the icon property. */ |
| 107 |
isHamburger?: boolean; |
| 108 |
/** Adjusts and animates the hamburger icon to indicate what will happen upon clicking the button. */ |
| 109 |
hamburgerVariant?: 'expand' | 'collapse'; |
| 110 |
/** @beta Flag indicating the button is a circle button. Intended for buttons that only contain an icon.. */ |
| 111 |
isCircle?: boolean; |
| 112 |
/** @beta Flag indicating the button is a docked variant button. For use in docked navigation. */ |
| 113 |
isDocked?: boolean; |
| 114 |
/** @beta Flag indicating the docked button should display text. Only applies when isDocked is true. */ |
| 115 |
isTextExpanded?: boolean; |
| 116 |
/** @hide Forwarded ref */ |
| 117 |
innerRef?: React.Ref<any>; |
| 118 |
/** Adds count number to button */ |
| 119 |
countOptions?: BadgeCountObject; |
| 120 |
/** Value to overwrite the randomly generated data-ouia-component-id.*/ |
| 121 |
ouiaId?: number | string; |
| 122 |
/** Set the value of data-ouia-safe. Only set to true when the component is in a static state, i.e. no animations are occurring. At all other times, this value must be false. */ |
| 123 |
ouiaSafe?: boolean; |
| 124 |
} |
| 125 |
|
| 126 |
const ButtonBase: React.FunctionComponent<ButtonProps> = ({ |
| 127 |
children = null, |
| 128 |
className = '', |
| 129 |
component = 'button', |
| 130 |
isClicked = false, |
| 131 |
isBlock = false, |
| 132 |
isDisabled = false, |
| 133 |
isAriaDisabled = false, |
| 134 |
isLoading = null, |
| 135 |
isDanger = false, |
| 136 |
isExpanded, |
| 137 |
isSettings, |
| 138 |
isHamburger, |
| 139 |
hamburgerVariant, |
| 140 |
isCircle, |
| 141 |
isDocked = false, |
| 142 |
isTextExpanded = false, |
| 143 |
spinnerAriaValueText, |
| 144 |
spinnerAriaLabelledBy, |
| 145 |
spinnerAriaLabel, |
| 146 |
size = ButtonSize.default, |
| 147 |
inoperableEvents = ['onClick', 'onKeyPress'], |
| 148 |
isInline = false, |
| 149 |
isFavorite = false, |
| 150 |
isFavorited = false, |
| 151 |
type = ButtonType.button, |
| 152 |
variant = ButtonVariant.primary, |
| 153 |
state = ButtonState.unread, |
| 154 |
hasNoPadding = false, |
| 155 |
iconPosition = 'start', |
| 156 |
'aria-label': ariaLabel = null, |
| 157 |
icon = null, |
| 158 |
role, |
| 159 |
ouiaId, |
| 160 |
ouiaSafe = true, |
| 161 |
tabIndex = null, |
| 162 |
innerRef, |
| 163 |
countOptions, |
| 164 |
...props |
| 165 |
}: ButtonProps) => { |
| 166 |
if (isHamburger && ![true, false].includes(isExpanded)) { |
| 167 |
// eslint-disable-next-line no-console |
| 168 |
console.error( |
| 169 |
'Button: when the isHamburger property is passed in, you must also pass in a boolean value to the isExpanded property. It is expected that a hamburger button controls the expansion of other content.' |
| 170 |
); |
| 171 |
} |
| 172 |
// TODO: Remove isSettings || isHamburger || isFavorite conditional in breaking change to throw this warning for any button that does not have children or aria name |
| 173 |
if ((isSettings || isHamburger || isFavorite) && !ariaLabel && !children && !props['aria-labelledby']) { |
| 174 |
// eslint-disable-next-line no-console |
| 175 |
console.error( |
| 176 |
'Button: you must provide either visible text content or an accessible name via the aria-label or aria-labelledby properties.' |
| 177 |
); |
| 178 |
} |
| 179 |
|
| 180 |
const ouiaProps = useOUIAProps(Button.displayName, ouiaId, ouiaSafe, variant); |
| 181 |
const Component = component as any; |
| 182 |
const isButtonElement = Component === 'button'; |
| 183 |
const isInlineSpan = isInline && Component === 'span'; |
| 184 |
const isIconAlignedAtEnd = iconPosition === 'end' || iconPosition === 'right'; |
| 185 |
const shouldOverrideIcon = isSettings || isHamburger || isFavorite; |
| 186 |
|
| 187 |
const preventedEvents = inoperableEvents.reduce( |
| 188 |
(handlers, eventToPrevent) => ({ |
| 189 |
...handlers, |
| 190 |
[eventToPrevent]: (event: React.SyntheticEvent<HTMLButtonElement>) => { |
| 191 |
event.preventDefault(); |
| 192 |
} |
| 193 |
}), |
| 194 |
{} |
| 195 |
); |
| 196 |
|
| 197 |
const getDefaultTabIdx = () => { |
| 198 |
if (isDisabled) { |
| 199 |
return isButtonElement ? null : -1; |
| 200 |
} else if (isAriaDisabled) { |
| 201 |
return null; |
| 202 |
} else if (isInlineSpan) { |
| 203 |
return 0; |
| 204 |
} |
| 205 |
}; |
| 206 |
|
| 207 |
const renderIcon = () => { |
| 208 |
let iconContent; |
| 209 |
|
| 210 |
if (isFavorite) { |
| 211 |
iconContent = ( |
| 212 |
<> |
| 213 |
<span className={css('pf-v6-c-button__icon-favorite')}> |
| 214 |
<RhUiStarIcon /> |
| 215 |
</span> |
| 216 |
<span className={css('pf-v6-c-button__icon-favorited')}> |
| 217 |
<RhUiStarFillIcon /> |
| 218 |
</span> |
| 219 |
</> |
| 220 |
); |
| 221 |
} |
| 222 |
|
| 223 |
if (isSettings) { |
| 224 |
iconContent = <RhUiSettingsFillIcon />; |
| 225 |
} |
| 226 |
if (isHamburger) { |
| 227 |
iconContent = hamburgerIcon; |
| 228 |
} |
| 229 |
if (icon && !shouldOverrideIcon) { |
| 230 |
iconContent = icon; |
| 231 |
} |
| 232 |
|
| 233 |
return ( |
| 234 |
iconContent && ( |
| 235 |
<span className={css(styles.buttonIcon, children && styles.modifiers[isIconAlignedAtEnd ? 'end' : 'start'])}> |
| 236 |
{iconContent} |
| 237 |
</span> |
| 238 |
) |
| 239 |
); |
| 240 |
}; |
| 241 |
const _icon = renderIcon(); |
| 242 |
const _children = children && <span className={css('pf-v6-c-button__text')}>{children}</span>; |
| 243 |
// We only want to render the aria-disabled attribute when true, similar to the disabled attribute natively. |
| 244 |
const shouldRenderAriaDisabled = isAriaDisabled || (!isButtonElement && isDisabled); |
| 245 |
|
| 246 |
return ( |
| 247 |
<Component |
| 248 |
aria-expanded={isExpanded} // Move this after the spread props in next breaking change |
| 249 |
{...props} |
| 250 |
{...(isAriaDisabled ? preventedEvents : null)} |
| 251 |
{...(shouldRenderAriaDisabled && { 'aria-disabled': true })} |
| 252 |
aria-label={ariaLabel} |
| 253 |
className={css( |
| 254 |
styles.button, |
| 255 |
styles.modifiers[variant], |
| 256 |
isSettings && styles.modifiers.settings, |
| 257 |
isHamburger && styles.modifiers.hamburger, |
| 258 |
isHamburger && hamburgerVariant && styles.modifiers[hamburgerVariant], |
| 259 |
isBlock && styles.modifiers.block, |
| 260 |
isDisabled && !isButtonElement && styles.modifiers.disabled, |
| 261 |
isAriaDisabled && styles.modifiers.ariaDisabled, |
| 262 |
isClicked && styles.modifiers.clicked, |
| 263 |
isInline && variant === ButtonVariant.link && styles.modifiers.inline, |
| 264 |
isFavorite && styles.modifiers.favorite, |
| 265 |
isFavorite && isFavorited && styles.modifiers.favorited, |
| 266 |
isDanger && (variant === ButtonVariant.secondary || variant === ButtonVariant.link) && styles.modifiers.danger, |
| 267 |
isLoading !== null && variant !== ButtonVariant.plain && styles.modifiers.progress, |
| 268 |
isLoading && styles.modifiers.inProgress, |
| 269 |
hasNoPadding && variant === ButtonVariant.plain && styles.modifiers.noPadding, |
| 270 |
variant === ButtonVariant.stateful && styles.modifiers[state], |
| 271 |
size === ButtonSize.sm && styles.modifiers.small, |
| 272 |
size === ButtonSize.lg && styles.modifiers.displayLg, |
| 273 |
isCircle && styles.modifiers.circle, |
| 274 |
isDocked && styles.modifiers.docked, |
| 275 |
isDocked && isTextExpanded && styles.modifiers.textExpanded, |
| 276 |
className |
| 277 |
)} |
| 278 |
disabled={isButtonElement ? isDisabled : null} |
| 279 |
tabIndex={tabIndex !== null ? tabIndex : getDefaultTabIdx()} |
| 280 |
type={isButtonElement || isInlineSpan ? type : null} |
| 281 |
role={isInlineSpan ? 'button' : role} |
| 282 |
ref={innerRef} |
| 283 |
{...ouiaProps} |
| 284 |
> |
| 285 |
{isLoading && ( |
| 286 |
<span className={css(styles.buttonProgress)}> |
| 287 |
<Spinner |
| 288 |
size={spinnerSize.md} |
| 289 |
isInline={isInline} |
| 290 |
aria-valuetext={spinnerAriaValueText} |
| 291 |
aria-label={spinnerAriaLabel} |
| 292 |
aria-labelledby={spinnerAriaLabelledBy} |
| 293 |
/> |
| 294 |
</span> |
| 295 |
)} |
| 296 |
{isIconAlignedAtEnd ? ( |
| 297 |
<> |
| 298 |
{_children} |
| 299 |
{_icon} |
| 300 |
</> |
| 301 |
) : ( |
| 302 |
<> |
| 303 |
{_icon} |
| 304 |
{_children} |
| 305 |
</> |
| 306 |
)} |
| 307 |
{countOptions && ( |
| 308 |
<span className={css(styles.buttonCount, countOptions.className)}> |
| 309 |
<Badge isRead={countOptions.isRead} isDisabled={isDisabled}> |
| 310 |
{countOptions.count} |
| 311 |
</Badge> |
| 312 |
</span> |
| 313 |
)} |
| 314 |
</Component> |
| 315 |
); |
| 316 |
}; |
| 317 |
|
| 318 |
export const Button = forwardRef((props: ButtonProps, ref: React.Ref<any>) => <ButtonBase innerRef={ref} {...props} />); |
| 319 |
|
| 320 |
Button.displayName = 'Button'; |