Button: Allow disabled button to still be focused (#87516)

This commit is contained in:
Joao Silva 2024-05-20 10:39:52 +01:00 committed by GitHub
parent 8f6dcfee8b
commit cf407fe8de
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 33 additions and 3 deletions

View File

@ -1,3 +1,4 @@
import { action } from '@storybook/addon-actions';
import { StoryFn } from '@storybook/react'; import { StoryFn } from '@storybook/react';
import React from 'react'; import React from 'react';
@ -26,6 +27,9 @@ export default {
tooltip: { tooltip: {
control: 'text', control: 'text',
}, },
disabled: {
control: 'boolean',
},
className: { className: {
table: { table: {
disable: true, disable: true,
@ -96,7 +100,9 @@ export const Examples: StoryFn<typeof Button> = () => {
); );
}; };
export const Basic: StoryFn<typeof Button> = (args: ButtonProps) => <Button {...args} />; export const Basic: StoryFn<typeof Button> = (args: ButtonProps) => (
<Button onClick={action('clicked button')} {...args} />
);
Basic.args = { Basic.args = {
children: 'Example button', children: 'Example button',

View File

@ -45,7 +45,9 @@ export const Button = React.forwardRef<HTMLButtonElement, ButtonProps>(
className, className,
type = 'button', type = 'button',
tooltip, tooltip,
disabled,
tooltipPlacement, tooltipPlacement,
onClick,
...otherProps ...otherProps
}, },
ref ref
@ -60,10 +62,30 @@ export const Button = React.forwardRef<HTMLButtonElement, ButtonProps>(
iconOnly: !children, iconOnly: !children,
}); });
const buttonStyles = cx(
styles.button,
{
[styles.disabled]: disabled,
},
className
);
const hasTooltip = Boolean(tooltip);
// In order to standardise Button please always consider using IconButton when you need a button with an icon only // In order to standardise Button please always consider using IconButton when you need a button with an icon only
// When using tooltip, ref is forwarded to Tooltip component instead for https://github.com/grafana/grafana/issues/65632 // When using tooltip, ref is forwarded to Tooltip component instead for https://github.com/grafana/grafana/issues/65632
const button = ( const button = (
<button className={cx(styles.button, className)} type={type} {...otherProps} ref={tooltip ? undefined : ref}> <button
className={buttonStyles}
type={type}
onClick={disabled ? undefined : onClick}
{...otherProps}
// In order for the tooltip to be accessible when disabled,
// we need to set aria-disabled instead of the native disabled attribute
aria-disabled={hasTooltip && disabled}
disabled={!hasTooltip && disabled}
ref={tooltip ? undefined : ref}
>
<IconRenderer icon={icon} size={size} className={styles.icon} /> <IconRenderer icon={icon} size={size} className={styles.icon} />
{children && <span className={styles.content}>{children}</span>} {children && <span className={styles.content}>{children}</span>}
</button> </button>
@ -217,7 +239,9 @@ export const getButtonStyles = (props: StyleProps) => {
':disabled': disabledStyles, ':disabled': disabledStyles,
'&[disabled]': disabledStyles, '&[disabled]': disabledStyles,
}), }),
disabled: css(disabledStyles), disabled: css(disabledStyles, {
'&:hover': css(disabledStyles),
}),
img: css({ img: css({
width: '16px', width: '16px',
height: '16px', height: '16px',