Skip to content

Commit 81fc117

Browse files
fix(button): dont render double icons on icon-only-button when loading (#4023)
Co-authored-by: Tobias Barsnes <tobias.barsnes@digdir.no>
1 parent 9827d77 commit 81fc117

5 files changed

Lines changed: 26 additions & 2 deletions

File tree

.changeset/big-parts-refuse.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"@digdir/designsystemet-react": patch
3+
---
4+
5+
**Button**: For `icon`-buttons, dont render children if loading is true

packages/react/src/components/button/button.mdx

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,8 @@ Vurder først om det er betre å bruke [`Link`](/docs/komponenter-link--docs)-ko
5454

5555
### Knappar som lastar
5656

57-
Når vi skal vise brukaren at knappen held på å laste noko, kan vi kombinere knappen med ein spinner. I eksempelet under bruker vi proppen `loading` for å vise at knappen lastar.
57+
Når vi skal vise brukaren at knappen held på å laste noko, kan vi kombinere knappen med ein spinner. I eksempelet under bruker vi proppen `loading` for å vise at knappen lastar.
58+
Viss `icon` er sett saman med `loading`, vil lasteindikatoren visast i staden for ikonet.
5859

5960
<Canvas of={ButtonStories.Loading} />
6061

packages/react/src/components/button/button.stories.tsx

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -120,5 +120,9 @@ export const Loading: StoryFn<typeof Button> = () => (
120120
<Button variant='tertiary' loading>
121121
Laster...
122122
</Button>
123+
<Button icon loading aria-label='Rediger'>
124+
{/* When loading is true and icon is set, loading will take precedence */}
125+
<PencilWritingIcon aria-hidden />
126+
</Button>
123127
</>
124128
);

packages/react/src/components/button/button.test.tsx

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,13 @@ import { Button } from './button';
88
const user = userEvent.setup();
99

1010
describe('Button', () => {
11+
beforeAll(() => {
12+
// Spinner for loading state uses animations, which we need to mock
13+
if (!document.getAnimations) {
14+
document.getAnimations = () => [];
15+
}
16+
});
17+
1118
it('should render as aria-disabled when aria-disabled is true regardless of variant', () => {
1219
render({
1320
'aria-disabled': true,
@@ -59,6 +66,12 @@ describe('Button', () => {
5966
expect(screen.getByRole('link')).not.toHaveAttribute('type');
6067
expect(screen.queryByRole('button')).toBeNull();
6168
});
69+
70+
it('should not render children when icon-only button is loading', () => {
71+
render({ loading: true, icon: true, children: 'Button text' });
72+
expect(screen.queryByText('Button text')).toBeNull();
73+
expect(screen.getByRole('button')).toHaveAttribute('aria-busy');
74+
});
6275
});
6376

6477
const render = (props?: ButtonProps) => renderRtl(<Button {...props} />);

packages/react/src/components/button/button.tsx

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ export type ButtonProps = MergeRight<
2121
'data-color'?: Color | Extract<SeverityColors, 'danger'>;
2222
/**
2323
* Toggle icon only styling, pass icon as children
24+
* When combined with loading, the loading-icon will be shown instead of the icon.
2425
* @default false
2526
*/
2627
icon?: boolean;
@@ -84,7 +85,7 @@ export const Button = forwardRef<HTMLButtonElement, ButtonProps>(
8485
) : (
8586
loading // Allow custom loading spinner
8687
)}
87-
<Slottable>{children}</Slottable>
88+
<Slottable>{icon && loading ? null : children}</Slottable>
8889
</Component>
8990
);
9091
},

0 commit comments

Comments
 (0)