|
1 | 1 | import { FocusableRef } from '@react-types/shared'; |
2 | 2 | import { cloneElement, forwardRef, ReactElement, useMemo } from 'react'; |
3 | 3 |
|
| 4 | +import { useWarn } from '../../../_internal/hooks/use-warn'; |
4 | 5 | import { |
5 | 6 | DANGER_CLEAR_STYLES, |
6 | 7 | DANGER_LINK_STYLES, |
@@ -31,11 +32,9 @@ import { LoadingIcon } from '../../../icons'; |
31 | 32 | import { |
32 | 33 | CONTAINER_STYLES, |
33 | 34 | extractStyles, |
34 | | - Styles, |
35 | 35 | tasty, |
36 | 36 | TEXT_STYLES, |
37 | 37 | } from '../../../tasty'; |
38 | | -import { accessibilityWarning } from '../../../utils/warnings'; |
39 | 38 | import { Text } from '../../content/Text'; |
40 | 39 | import { CubeActionProps } from '../Action/Action'; |
41 | 40 | import { useAction } from '../use-action'; |
@@ -225,24 +224,28 @@ export const Button = forwardRef(function Button( |
225 | 224 |
|
226 | 225 | children = children || icon || rightIcon ? children : label; |
227 | 226 |
|
228 | | - if (!children) { |
229 | | - const specifiedLabel = |
230 | | - label ?? props['aria-label'] ?? props['aria-labelledby']; |
231 | | - if (icon) { |
232 | | - if (!specifiedLabel) { |
233 | | - accessibilityWarning( |
234 | | - 'If you provide `icon` property for a Button and do not provide any children then you should specify the `aria-label` property to make sure the Button element stays accessible.', |
235 | | - ); |
236 | | - label = 'Unnamed'; // fix to avoid warning in production |
237 | | - } |
238 | | - } else { |
239 | | - if (!specifiedLabel) { |
240 | | - accessibilityWarning( |
241 | | - 'If you provide no children for a Button then you should specify the `aria-label` property to make sure the Button element stays accessible.', |
242 | | - ); |
243 | | - label = 'Unnamed'; // fix to avoid warning in production |
244 | | - } |
245 | | - } |
| 227 | + const specifiedLabel = |
| 228 | + label ?? props['aria-label'] ?? props['aria-labelledby']; |
| 229 | + |
| 230 | + // Warn about accessibility issues when button has no accessible label |
| 231 | + useWarn(!children && icon && !specifiedLabel, { |
| 232 | + key: ['button-icon-no-label', !!icon], |
| 233 | + args: [ |
| 234 | + 'accessibility issue:', |
| 235 | + 'If you provide `icon` property for a Button and do not provide any children then you should specify the `aria-label` property to make sure the Button element stays accessible.', |
| 236 | + ], |
| 237 | + }); |
| 238 | + |
| 239 | + useWarn(!children && !icon && !specifiedLabel, { |
| 240 | + key: ['button-no-content-no-label', !!icon], |
| 241 | + args: [ |
| 242 | + 'accessibility issue:', |
| 243 | + 'If you provide no children for a Button then you should specify the `aria-label` property to make sure the Button element stays accessible.', |
| 244 | + ], |
| 245 | + }); |
| 246 | + |
| 247 | + if (!children && !specifiedLabel) { |
| 248 | + label = 'Unnamed'; // fix to avoid warning in production |
246 | 249 | } |
247 | 250 |
|
248 | 251 | if (icon) { |
|
0 commit comments