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

fix(Button): add danger variant assistive text #8222

Merged
43 changes: 35 additions & 8 deletions packages/react/src/components/Button/Button.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import { ButtonKinds } from '../../prop-types/types';
import deprecate from '../../prop-types/deprecate';
import { composeEventHandlers } from '../../tools/events';
import { keys, matches } from '../../internal/keyboard';
import { useId } from '../../internal/useId';
import toggleClass from '../../tools/toggleClass';

const { prefix } = settings;
Expand All @@ -30,6 +31,7 @@ const Button = React.forwardRef(function Button(
tabIndex,
type,
renderIcon: ButtonImageElement,
dangerDescription,
iconDescription,
hasIconOnly,
tooltipPosition,
Expand Down Expand Up @@ -139,23 +141,42 @@ const Button = React.forwardRef(function Button(
/>
);

const dangerButtonVariants = ['danger', 'danger--tertiary', 'danger--ghost'];

let component = 'button';
const assistiveId = useId('danger-description');
let otherProps = {
disabled,
type,
'aria-describedby': dangerButtonVariants.includes(kind)
? assistiveId
: null,
'aria-pressed': hasIconOnly && kind === 'ghost' ? isSelected : null,
};
const anchorProps = {
href,
};
const assistiveText = hasIconOnly ? (
<div
ref={tooltipRef}
onMouseEnter={handleMouseEnter}
className={`${prefix}--assistive-text`}>
{iconDescription}
</div>
) : null;

let assistiveText;
if (hasIconOnly) {
assistiveText = (
<div
ref={tooltipRef}
onMouseEnter={handleMouseEnter}
className={`${prefix}--assistive-text`}>
{iconDescription}
</div>
);
} else if (dangerButtonVariants.includes(kind)) {
assistiveText = (
<span id={assistiveId} className={`${prefix}--visually-hidden`}>
{dangerDescription}
</span>
);
} else {
assistiveText = null;
}

if (as) {
component = as;
otherProps = {
Expand Down Expand Up @@ -205,6 +226,11 @@ Button.propTypes = {
*/
className: PropTypes.string,

/**
* Specify the message read by screen readers for the danger button variant
*/
dangerDescription: PropTypes.string,

/**
* Specify whether the Button should be disabled, or not
*/
Expand Down Expand Up @@ -322,6 +348,7 @@ Button.defaultProps = {
disabled: false,
kind: 'primary',
size: 'default',
dangerDescription: 'danger',
tooltipAlignment: 'center',
tooltipPosition: 'top',
};
Expand Down