Skip to content

Commit b88c07a

Browse files
authored
fix(Button): avoid repetitive warnings (#758)
1 parent 1b7fb47 commit b88c07a

File tree

6 files changed

+34
-32
lines changed

6 files changed

+34
-32
lines changed

.changeset/light-suits-hammer.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"@cube-dev/ui-kit": patch
3+
---
4+
5+
Avoid repetitive warnings in Buttons.

.size-limit.cjs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,14 +20,14 @@ module.exports = [
2020
}),
2121
);
2222
},
23-
limit: '285kB',
23+
limit: '286kB',
2424
},
2525
{
2626
name: 'Tree shaking (just a Button)',
2727
path: './dist/es/index.js',
2828
webpack: true,
2929
import: '{ Button }',
30-
limit: '24 kB',
30+
limit: '25 kB',
3131
},
3232
{
3333
name: 'Tree shaking (just an Icon)',

src/components/actions/Button/Button.tsx

Lines changed: 23 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import { FocusableRef } from '@react-types/shared';
22
import { cloneElement, forwardRef, ReactElement, useMemo } from 'react';
33

4+
import { useWarn } from '../../../_internal/hooks/use-warn';
45
import {
56
DANGER_CLEAR_STYLES,
67
DANGER_LINK_STYLES,
@@ -31,11 +32,9 @@ import { LoadingIcon } from '../../../icons';
3132
import {
3233
CONTAINER_STYLES,
3334
extractStyles,
34-
Styles,
3535
tasty,
3636
TEXT_STYLES,
3737
} from '../../../tasty';
38-
import { accessibilityWarning } from '../../../utils/warnings';
3938
import { Text } from '../../content/Text';
4039
import { CubeActionProps } from '../Action/Action';
4140
import { useAction } from '../use-action';
@@ -225,24 +224,28 @@ export const Button = forwardRef(function Button(
225224

226225
children = children || icon || rightIcon ? children : label;
227226

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
246249
}
247250

248251
if (icon) {

src/data/item-themes.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -52,9 +52,9 @@ export const DEFAULT_SECONDARY_STYLES: Styles = {
5252

5353
export const DEFAULT_OUTLINE_STYLES: Styles = {
5454
border: {
55-
'': '#dark.12',
55+
'': true,
5656
focused: '#purple-text',
57-
'[disabled] | disabled': '#border',
57+
'[disabled] | disabled': true,
5858
...(VALIDATION_STYLES.border as Record<string, string>),
5959
},
6060
fill: {

src/stories/BaseProperties.docs.mdx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -65,8 +65,8 @@ All components support React Aria's labeling properties:
6565
### Testing Properties
6666
```jsx
6767
<Button
68-
qa="submit-button"
69-
qaVal="primary-action"
68+
qa="SubmitButton"
69+
qaVal="primary"
7070
>
7171
Submit
7272
</Button>

src/utils/warnings.ts

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -21,12 +21,6 @@ export function propDeprecationWarning(name, props, propList) {
2121
});
2222
}
2323

24-
export function accessibilityWarning(...args) {
25-
if (devMode) {
26-
console.warn(`${PREFIX} accessibility issue:`, ...args);
27-
}
28-
}
29-
3024
export function warn(...args) {
3125
if (devMode) {
3226
console.warn(`${PREFIX}:`, ...args);

0 commit comments

Comments
 (0)