Skip to content

Commit 5725b41

Browse files
committed
feat(spinner): css changes and data attrs (#2390)
part of #2295 Though I'd like to use ```html <svg data-size="*" /> ``` ```css width: attr(data-size px, '40px'); height: attr(data-size px, '40px'); ``` this is not currently supported. https://caniuse.com/?search=attr
1 parent 30c2735 commit 5725b41

File tree

4 files changed

+49
-37
lines changed

4 files changed

+49
-37
lines changed

.changeset/small-queens-breathe.md

+6
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
---
2+
"@digdir/designsystemet-css": patch
3+
"@digdir/designsystemet-react": patch
4+
---
5+
6+
Spinner: Style using data attributes

packages/css/spinner.css

+36-20
Original file line numberDiff line numberDiff line change
@@ -1,42 +1,58 @@
11
.ds-spinner {
22
--dsc-spinner-background: var(--ds-color-neutral-surface-default);
3-
--dsc-spinner-stroke: var(--ds-color-accent-base-default);
3+
--dsc-spinner-stroke: var(--ds-color-neutral-border-default);
4+
--dsc-spinner-size: var(--ds-sizing-11);
5+
--dsc-spinner-animation-duration: 2s;
46

7+
width: var(--dsc-spinner-size);
8+
height: var(--dsc-spinner-size);
59
animation: ds-spinner-rotate-animation linear infinite;
6-
animation-duration: 2s;
10+
animation-duration: var(--dsc-spinner-animation-duration);
11+
12+
&[data-color='accent'] {
13+
--dsc-spinner-stroke: var(--ds-color-accent-base-default);
14+
}
15+
16+
&[data-size='2xs'] {
17+
--dsc-spinner-size: var(--ds-sizing-3);
18+
}
19+
20+
&[data-size='xs'] {
21+
--dsc-spinner-size: var(--ds-sizing-5);
22+
}
23+
24+
&[data-size='sm'] {
25+
--dsc-spinner-size: var(--ds-sizing-8);
26+
}
27+
28+
&[data-size='lg'] {
29+
--dsc-spinner-size: var(--ds-sizing-15);
30+
}
31+
32+
&[data-size='xl'] {
33+
--dsc-spinner-size: var(--ds-sizing-22);
34+
}
35+
}
36+
37+
.ds-spinner__background {
38+
stroke: var(--dsc-spinner-background);
739
}
840

941
.ds-spinner__circle {
1042
stroke: var(--dsc-spinner-stroke);
1143
stroke-dasharray: 1px, 200px;
1244
transform-origin: center;
1345
animation: ds-spinner-stroke-animation ease-in-out infinite;
14-
animation-duration: 2s;
46+
animation-duration: var(--dsc-spinner-animation-duration);
1547
}
1648

1749
/* Prefers reduced motion needs longer animation,
1850
but don't remove it since it is not decorative.
1951
*/
2052
@media (prefers-reduced-motion: reduce) {
2153
.ds-spinner {
22-
animation-duration: 6s;
54+
--dsc-spinner-animation-duration: 6s;
2355
}
24-
25-
.ds-spinner__circle {
26-
animation-duration: 6s;
27-
}
28-
}
29-
30-
.ds-spinner--neutral {
31-
--dsc-spinner-stroke: var(--ds-color-neutral-border-default);
32-
}
33-
34-
.ds-spinner--accent {
35-
--dsc-spinner-stroke: var(--ds-color-accent-base-default);
36-
}
37-
38-
.ds-spinner__background {
39-
stroke: var(--dsc-spinner-background);
4056
}
4157

4258
@keyframes ds-spinner-rotate-animation {

packages/react/src/components/loaders/Spinner/Spinner.test.tsx

+4-3
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,10 @@ beforeAll(() => {
99
describe('spinner', (): void => {
1010
it('should render with default medium size', (): void => {
1111
render(<Spinner title='Loading' />);
12-
expect(screen.getByTitle('Loading').parentElement).toHaveStyle({
13-
width: '40px',
14-
});
12+
expect(screen.getByTitle('Loading').parentElement).toHaveAttribute(
13+
'data-size',
14+
'md',
15+
);
1516
});
1617

1718
it('should render with title "loading', (): void => {

packages/react/src/components/loaders/Spinner/Spinner.tsx

+3-14
Original file line numberDiff line numberDiff line change
@@ -3,17 +3,6 @@ import type * as React from 'react';
33

44
import { useSynchronizedAnimation } from '../../../utilities';
55

6-
const sizeMap: {
7-
[key in NonNullable<SpinnerProps['size']>]: number;
8-
} = {
9-
'2xs': 13,
10-
xs: 20,
11-
sm: 27,
12-
md: 40,
13-
lg: 56,
14-
xl: 79,
15-
};
16-
176
export type SpinnerProps = {
187
/** Spinner title */
198
title: string;
@@ -36,7 +25,6 @@ export const Spinner = ({
3625
color = 'neutral',
3726
size = 'md',
3827
className,
39-
style,
4028
...rest
4129
}: SpinnerProps): JSX.Element => {
4230
const svgRef = useSynchronizedAnimation<SVGSVGElement>(
@@ -49,10 +37,11 @@ export const Spinner = ({
4937

5038
return (
5139
<svg
52-
className={cl('ds-spinner', `ds-spinner--${color}`, className)}
53-
style={{ width: sizeMap[size], height: sizeMap[size], ...style }}
40+
className={cl('ds-spinner', className)}
5441
viewBox='0 0 50 50'
5542
ref={svgRef}
43+
data-color={color}
44+
data-size={size}
5645
{...rest}
5746
>
5847
<title>{title}</title>

0 commit comments

Comments
 (0)