uikit-react-public 0.40.5 → 0.41.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/components/ProgressBar/ProgressBar.d.ts +24 -0
- package/dist/components/ProgressBar/ProgressBar.stories.d.ts +77 -0
- package/dist/components/ProgressBar/__tests__/ProgressBar.test.d.ts +1 -0
- package/dist/components/ProgressBar/index.d.ts +2 -0
- package/dist/components/index.d.ts +2 -0
- package/dist/index.js +4269 -4128
- package/lib/components/ProgressBar/ProgressBar.stories.tsx +131 -0
- package/lib/components/ProgressBar/ProgressBar.tsx +220 -0
- package/lib/components/ProgressBar/__tests__/ProgressBar.test.tsx +120 -0
- package/lib/components/ProgressBar/index.ts +6 -0
- package/lib/components/index.ts +7 -0
- package/package.json +1 -1
|
@@ -0,0 +1,131 @@
|
|
|
1
|
+
import type { Meta, StoryObj } from '@storybook/react-vite';
|
|
2
|
+
import { css } from '@emotion/css';
|
|
3
|
+
import ProgressBar, {
|
|
4
|
+
type ProgressBarSize,
|
|
5
|
+
type ProgressBarVariant,
|
|
6
|
+
} from './ProgressBar';
|
|
7
|
+
|
|
8
|
+
const VARIANTS: ProgressBarVariant[] = [
|
|
9
|
+
'info',
|
|
10
|
+
'success',
|
|
11
|
+
'critical',
|
|
12
|
+
'warning',
|
|
13
|
+
];
|
|
14
|
+
|
|
15
|
+
const SIZES: ProgressBarSize[] = ['small', 'large'];
|
|
16
|
+
|
|
17
|
+
const meta = {
|
|
18
|
+
title: 'Components/ProgressBar',
|
|
19
|
+
component: ProgressBar,
|
|
20
|
+
parameters: {
|
|
21
|
+
layout: 'centered',
|
|
22
|
+
},
|
|
23
|
+
argTypes: {
|
|
24
|
+
value: { control: { type: 'number', min: 0, max: 100 } },
|
|
25
|
+
max: { control: { type: 'number', min: 1 } },
|
|
26
|
+
variant: {
|
|
27
|
+
options: VARIANTS,
|
|
28
|
+
control: { type: 'radio' },
|
|
29
|
+
},
|
|
30
|
+
size: {
|
|
31
|
+
options: SIZES,
|
|
32
|
+
control: { type: 'radio' },
|
|
33
|
+
},
|
|
34
|
+
title: { control: { type: 'text' } },
|
|
35
|
+
label: { control: { type: 'text' } },
|
|
36
|
+
valueLabel: { control: { type: 'text' } },
|
|
37
|
+
testId: { control: { type: 'text' } },
|
|
38
|
+
className: { control: false },
|
|
39
|
+
},
|
|
40
|
+
args: {
|
|
41
|
+
value: 50,
|
|
42
|
+
max: 100,
|
|
43
|
+
variant: 'info',
|
|
44
|
+
size: 'small',
|
|
45
|
+
title: 'Uploading file',
|
|
46
|
+
valueLabel: '50%',
|
|
47
|
+
label: 'Uploading... 50%',
|
|
48
|
+
'aria-label': 'Uploading file',
|
|
49
|
+
},
|
|
50
|
+
tags: ['autodocs'],
|
|
51
|
+
} satisfies Meta<typeof ProgressBar>;
|
|
52
|
+
|
|
53
|
+
export default meta;
|
|
54
|
+
type Story = StoryObj<typeof meta>;
|
|
55
|
+
|
|
56
|
+
export const Default: Story = {};
|
|
57
|
+
|
|
58
|
+
export const Large: Story = {
|
|
59
|
+
args: {
|
|
60
|
+
size: 'large',
|
|
61
|
+
},
|
|
62
|
+
};
|
|
63
|
+
|
|
64
|
+
export const Indeterminate: Story = {
|
|
65
|
+
args: {
|
|
66
|
+
value: undefined,
|
|
67
|
+
valueLabel: undefined,
|
|
68
|
+
label: 'Loading...',
|
|
69
|
+
'aria-label': 'Loading',
|
|
70
|
+
},
|
|
71
|
+
};
|
|
72
|
+
|
|
73
|
+
const stackStyle = css`
|
|
74
|
+
display: flex;
|
|
75
|
+
flex-direction: column;
|
|
76
|
+
gap: 20px;
|
|
77
|
+
width: 360px;
|
|
78
|
+
`;
|
|
79
|
+
|
|
80
|
+
const rowStyle = css`
|
|
81
|
+
display: flex;
|
|
82
|
+
flex-direction: column;
|
|
83
|
+
gap: 8px;
|
|
84
|
+
`;
|
|
85
|
+
|
|
86
|
+
const labelStyle = css`
|
|
87
|
+
font-family: sans-serif;
|
|
88
|
+
font-size: 12px;
|
|
89
|
+
`;
|
|
90
|
+
|
|
91
|
+
export const Variants: Story = {
|
|
92
|
+
parameters: { layout: 'padded' },
|
|
93
|
+
render: () => (
|
|
94
|
+
<div className={stackStyle}>
|
|
95
|
+
{VARIANTS.map((variant) => (
|
|
96
|
+
<div
|
|
97
|
+
key={variant}
|
|
98
|
+
className={rowStyle}
|
|
99
|
+
>
|
|
100
|
+
<span className={labelStyle}>{variant}</span>
|
|
101
|
+
<ProgressBar
|
|
102
|
+
value={50}
|
|
103
|
+
variant={variant}
|
|
104
|
+
aria-label={`${variant} progress`}
|
|
105
|
+
/>
|
|
106
|
+
</div>
|
|
107
|
+
))}
|
|
108
|
+
</div>
|
|
109
|
+
),
|
|
110
|
+
};
|
|
111
|
+
|
|
112
|
+
export const Sizes: Story = {
|
|
113
|
+
parameters: { layout: 'padded' },
|
|
114
|
+
render: () => (
|
|
115
|
+
<div className={stackStyle}>
|
|
116
|
+
{SIZES.map((size) => (
|
|
117
|
+
<div
|
|
118
|
+
key={size}
|
|
119
|
+
className={rowStyle}
|
|
120
|
+
>
|
|
121
|
+
<span className={labelStyle}>{size}</span>
|
|
122
|
+
<ProgressBar
|
|
123
|
+
value={70}
|
|
124
|
+
size={size}
|
|
125
|
+
aria-label={`${size} progress`}
|
|
126
|
+
/>
|
|
127
|
+
</div>
|
|
128
|
+
))}
|
|
129
|
+
</div>
|
|
130
|
+
),
|
|
131
|
+
};
|
|
@@ -0,0 +1,220 @@
|
|
|
1
|
+
import { HTMLAttributes, ReactNode, Ref, memo, useId } from 'react';
|
|
2
|
+
import { css, cx } from '@emotion/css';
|
|
3
|
+
import useTheme from '../../theme/useTheme';
|
|
4
|
+
import type { ThemeType } from '../../theme';
|
|
5
|
+
import marginsStyle, { MarginProps } from '../common/marginsStyle';
|
|
6
|
+
import Text from '../Text';
|
|
7
|
+
|
|
8
|
+
export const NAME = 'ucl-uikit-progress-bar';
|
|
9
|
+
|
|
10
|
+
export type ProgressBarVariant = 'info' | 'success' | 'critical' | 'warning';
|
|
11
|
+
|
|
12
|
+
export type ProgressBarSize = 'small' | 'large';
|
|
13
|
+
|
|
14
|
+
export interface ProgressBarBaseProps extends Omit<
|
|
15
|
+
HTMLAttributes<HTMLDivElement>,
|
|
16
|
+
| 'children'
|
|
17
|
+
| 'title'
|
|
18
|
+
| 'aria-label'
|
|
19
|
+
| 'aria-labelledby'
|
|
20
|
+
| 'aria-describedby'
|
|
21
|
+
| 'role'
|
|
22
|
+
> {
|
|
23
|
+
value?: number | null;
|
|
24
|
+
max?: number;
|
|
25
|
+
variant?: ProgressBarVariant;
|
|
26
|
+
size?: ProgressBarSize;
|
|
27
|
+
title?: ReactNode;
|
|
28
|
+
label?: ReactNode;
|
|
29
|
+
valueLabel?: ReactNode;
|
|
30
|
+
testId?: string;
|
|
31
|
+
ref?: Ref<HTMLDivElement>;
|
|
32
|
+
'aria-label'?: string;
|
|
33
|
+
'aria-labelledby'?: string;
|
|
34
|
+
'aria-describedby'?: string;
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
export type ProgressBarProps = ProgressBarBaseProps & MarginProps;
|
|
38
|
+
|
|
39
|
+
export const getProgressBarVariantColour = (
|
|
40
|
+
theme: ThemeType,
|
|
41
|
+
variant: ProgressBarVariant
|
|
42
|
+
) =>
|
|
43
|
+
({
|
|
44
|
+
info: theme.colour.fill.brand,
|
|
45
|
+
success: theme.colour.fill.success,
|
|
46
|
+
critical: theme.colour.fill.critical,
|
|
47
|
+
warning: theme.colour.fill.warning,
|
|
48
|
+
})[variant];
|
|
49
|
+
|
|
50
|
+
const clamp = (value: number, min: number, max: number) =>
|
|
51
|
+
Math.min(Math.max(value, min), max);
|
|
52
|
+
|
|
53
|
+
const ProgressBar = ({
|
|
54
|
+
value,
|
|
55
|
+
max = 100,
|
|
56
|
+
variant = 'info',
|
|
57
|
+
size = 'small',
|
|
58
|
+
title,
|
|
59
|
+
label,
|
|
60
|
+
valueLabel,
|
|
61
|
+
testId = NAME,
|
|
62
|
+
className,
|
|
63
|
+
ref,
|
|
64
|
+
'aria-label': ariaLabel,
|
|
65
|
+
'aria-labelledby': ariaLabelledBy,
|
|
66
|
+
'aria-describedby': ariaDescribedBy,
|
|
67
|
+
...props
|
|
68
|
+
}: ProgressBarProps) => {
|
|
69
|
+
const [theme] = useTheme();
|
|
70
|
+
const generatedId = useId();
|
|
71
|
+
|
|
72
|
+
const isIndeterminate = value === undefined || value === null;
|
|
73
|
+
const normalizedMax = max > 0 ? max : 100;
|
|
74
|
+
const normalizedValue = isIndeterminate
|
|
75
|
+
? undefined
|
|
76
|
+
: clamp(value, 0, normalizedMax);
|
|
77
|
+
const percentage =
|
|
78
|
+
normalizedValue === undefined ? 0 : (normalizedValue / normalizedMax) * 100;
|
|
79
|
+
const barHeight = size === 'large' ? 8 : 4;
|
|
80
|
+
const fillColour = getProgressBarVariantColour(theme, variant);
|
|
81
|
+
const titleId = `${generatedId}-title`;
|
|
82
|
+
const labelId = `${generatedId}-label`;
|
|
83
|
+
const progressBarLabelledBy =
|
|
84
|
+
ariaLabelledBy ??
|
|
85
|
+
(ariaLabel ? undefined : label ? labelId : title ? titleId : undefined);
|
|
86
|
+
|
|
87
|
+
const rootStyle = css`
|
|
88
|
+
display: flex;
|
|
89
|
+
flex-direction: column;
|
|
90
|
+
gap: ${theme.padding.p8};
|
|
91
|
+
width: 100%;
|
|
92
|
+
`;
|
|
93
|
+
|
|
94
|
+
const labelRowStyle = css`
|
|
95
|
+
display: flex;
|
|
96
|
+
align-items: baseline;
|
|
97
|
+
justify-content: space-between;
|
|
98
|
+
gap: ${theme.padding.p16};
|
|
99
|
+
`;
|
|
100
|
+
|
|
101
|
+
const titleStyle = css`
|
|
102
|
+
color: ${theme.colour.text.default};
|
|
103
|
+
`;
|
|
104
|
+
|
|
105
|
+
const valueLabelStyle = css`
|
|
106
|
+
color: ${theme.colour.text.secondary};
|
|
107
|
+
white-space: nowrap;
|
|
108
|
+
`;
|
|
109
|
+
|
|
110
|
+
const trackStyle = css`
|
|
111
|
+
position: relative;
|
|
112
|
+
width: 100%;
|
|
113
|
+
height: ${barHeight}px;
|
|
114
|
+
overflow: hidden;
|
|
115
|
+
background-color: ${theme.colour.fill.subtle};
|
|
116
|
+
border-radius: 100px;
|
|
117
|
+
`;
|
|
118
|
+
|
|
119
|
+
const fillStyle = cx(
|
|
120
|
+
css`
|
|
121
|
+
display: block;
|
|
122
|
+
height: 100%;
|
|
123
|
+
background-color: ${fillColour};
|
|
124
|
+
border-radius: 100px;
|
|
125
|
+
`,
|
|
126
|
+
isIndeterminate
|
|
127
|
+
? css`
|
|
128
|
+
position: absolute;
|
|
129
|
+
inset-block: 0;
|
|
130
|
+
inline-size: 40%;
|
|
131
|
+
animation: progress-bar-indeterminate 1.6s ease-in-out infinite;
|
|
132
|
+
|
|
133
|
+
@keyframes progress-bar-indeterminate {
|
|
134
|
+
0% {
|
|
135
|
+
transform: translateX(-100%);
|
|
136
|
+
}
|
|
137
|
+
|
|
138
|
+
100% {
|
|
139
|
+
transform: translateX(250%);
|
|
140
|
+
}
|
|
141
|
+
}
|
|
142
|
+
|
|
143
|
+
@media (prefers-reduced-motion: reduce) {
|
|
144
|
+
animation: none;
|
|
145
|
+
transform: translateX(75%);
|
|
146
|
+
}
|
|
147
|
+
`
|
|
148
|
+
: css`
|
|
149
|
+
width: ${percentage}%;
|
|
150
|
+
transition: width 160ms ease-out;
|
|
151
|
+
|
|
152
|
+
@media (prefers-reduced-motion: reduce) {
|
|
153
|
+
transition: none;
|
|
154
|
+
}
|
|
155
|
+
`
|
|
156
|
+
);
|
|
157
|
+
|
|
158
|
+
const labelStyle = css`
|
|
159
|
+
color: ${fillColour};
|
|
160
|
+
`;
|
|
161
|
+
|
|
162
|
+
const style = cx(NAME, rootStyle, marginsStyle(props, theme), className);
|
|
163
|
+
|
|
164
|
+
return (
|
|
165
|
+
<div
|
|
166
|
+
className={style}
|
|
167
|
+
data-testid={testId}
|
|
168
|
+
ref={ref}
|
|
169
|
+
{...props}
|
|
170
|
+
>
|
|
171
|
+
{(title || valueLabel) && (
|
|
172
|
+
<div className={labelRowStyle}>
|
|
173
|
+
{title && (
|
|
174
|
+
<Text
|
|
175
|
+
id={titleId}
|
|
176
|
+
level='sm'
|
|
177
|
+
className={titleStyle}
|
|
178
|
+
>
|
|
179
|
+
{title}
|
|
180
|
+
</Text>
|
|
181
|
+
)}
|
|
182
|
+
{valueLabel && (
|
|
183
|
+
<Text
|
|
184
|
+
level='xs'
|
|
185
|
+
className={valueLabelStyle}
|
|
186
|
+
>
|
|
187
|
+
{valueLabel}
|
|
188
|
+
</Text>
|
|
189
|
+
)}
|
|
190
|
+
</div>
|
|
191
|
+
)}
|
|
192
|
+
<div
|
|
193
|
+
className={trackStyle}
|
|
194
|
+
role='progressbar'
|
|
195
|
+
aria-label={ariaLabel}
|
|
196
|
+
aria-labelledby={progressBarLabelledBy}
|
|
197
|
+
aria-describedby={ariaDescribedBy}
|
|
198
|
+
aria-valuemin={isIndeterminate ? undefined : 0}
|
|
199
|
+
aria-valuemax={isIndeterminate ? undefined : normalizedMax}
|
|
200
|
+
aria-valuenow={normalizedValue}
|
|
201
|
+
>
|
|
202
|
+
<span
|
|
203
|
+
className={fillStyle}
|
|
204
|
+
aria-hidden='true'
|
|
205
|
+
/>
|
|
206
|
+
</div>
|
|
207
|
+
{label && (
|
|
208
|
+
<Text
|
|
209
|
+
id={labelId}
|
|
210
|
+
level='xs'
|
|
211
|
+
className={labelStyle}
|
|
212
|
+
>
|
|
213
|
+
{label}
|
|
214
|
+
</Text>
|
|
215
|
+
)}
|
|
216
|
+
</div>
|
|
217
|
+
);
|
|
218
|
+
};
|
|
219
|
+
|
|
220
|
+
export default memo(ProgressBar);
|
|
@@ -0,0 +1,120 @@
|
|
|
1
|
+
import { describe, expect, test } from 'vitest';
|
|
2
|
+
import { render, screen } from '@testing-library/react';
|
|
3
|
+
import { ThemeContextProvider } from '../../../theme/useTheme';
|
|
4
|
+
import ProgressBar, { getProgressBarVariantColour } from '../ProgressBar';
|
|
5
|
+
import lightTheme from '../../../theme/light/lightTheme';
|
|
6
|
+
import darkTheme from '../../../theme/dark/darkTheme';
|
|
7
|
+
|
|
8
|
+
const wrap = (ui: React.ReactElement) =>
|
|
9
|
+
render(<ThemeContextProvider>{ui}</ThemeContextProvider>);
|
|
10
|
+
|
|
11
|
+
describe('ProgressBar', () => {
|
|
12
|
+
test('renders with default test id', () => {
|
|
13
|
+
const { getByTestId } = wrap(<ProgressBar value={50} />);
|
|
14
|
+
|
|
15
|
+
expect(getByTestId('ucl-uikit-progress-bar')).toBeInTheDocument();
|
|
16
|
+
});
|
|
17
|
+
|
|
18
|
+
test('renders custom title, value label, and label', () => {
|
|
19
|
+
wrap(
|
|
20
|
+
<ProgressBar
|
|
21
|
+
value={50}
|
|
22
|
+
title='coverletter.pdf'
|
|
23
|
+
valueLabel='2.4 MB'
|
|
24
|
+
label='Uploading... 50%'
|
|
25
|
+
/>
|
|
26
|
+
);
|
|
27
|
+
|
|
28
|
+
expect(screen.getByText('coverletter.pdf')).toBeInTheDocument();
|
|
29
|
+
expect(screen.getByText('2.4 MB')).toBeInTheDocument();
|
|
30
|
+
expect(screen.getByText('Uploading... 50%')).toBeInTheDocument();
|
|
31
|
+
});
|
|
32
|
+
|
|
33
|
+
test('sets determinate progressbar aria attributes', () => {
|
|
34
|
+
wrap(
|
|
35
|
+
<ProgressBar
|
|
36
|
+
value={25}
|
|
37
|
+
max={50}
|
|
38
|
+
aria-label='Upload progress'
|
|
39
|
+
/>
|
|
40
|
+
);
|
|
41
|
+
|
|
42
|
+
const progressbar = screen.getByRole('progressbar', {
|
|
43
|
+
name: 'Upload progress',
|
|
44
|
+
});
|
|
45
|
+
|
|
46
|
+
expect(progressbar).toHaveAttribute('aria-valuemin', '0');
|
|
47
|
+
expect(progressbar).toHaveAttribute('aria-valuemax', '50');
|
|
48
|
+
expect(progressbar).toHaveAttribute('aria-valuenow', '25');
|
|
49
|
+
});
|
|
50
|
+
|
|
51
|
+
test('omits value aria attributes for indeterminate progress', () => {
|
|
52
|
+
wrap(<ProgressBar aria-label='Loading' />);
|
|
53
|
+
|
|
54
|
+
const progressbar = screen.getByRole('progressbar', {
|
|
55
|
+
name: 'Loading',
|
|
56
|
+
});
|
|
57
|
+
|
|
58
|
+
expect(progressbar).not.toHaveAttribute('aria-valuemin');
|
|
59
|
+
expect(progressbar).not.toHaveAttribute('aria-valuemax');
|
|
60
|
+
expect(progressbar).not.toHaveAttribute('aria-valuenow');
|
|
61
|
+
});
|
|
62
|
+
|
|
63
|
+
test('clamps aria-valuenow to the allowed range', () => {
|
|
64
|
+
wrap(
|
|
65
|
+
<ProgressBar
|
|
66
|
+
value={120}
|
|
67
|
+
max={100}
|
|
68
|
+
aria-label='Clamped progress'
|
|
69
|
+
/>
|
|
70
|
+
);
|
|
71
|
+
|
|
72
|
+
expect(screen.getByRole('progressbar')).toHaveAttribute(
|
|
73
|
+
'aria-valuenow',
|
|
74
|
+
'100'
|
|
75
|
+
);
|
|
76
|
+
});
|
|
77
|
+
|
|
78
|
+
test('applies custom className', () => {
|
|
79
|
+
const { getByTestId } = wrap(
|
|
80
|
+
<ProgressBar
|
|
81
|
+
value={50}
|
|
82
|
+
className='custom-class'
|
|
83
|
+
/>
|
|
84
|
+
);
|
|
85
|
+
|
|
86
|
+
expect(getByTestId('ucl-uikit-progress-bar').classList).toContain(
|
|
87
|
+
'custom-class'
|
|
88
|
+
);
|
|
89
|
+
});
|
|
90
|
+
|
|
91
|
+
test('uses semantic variant colours in light theme', () => {
|
|
92
|
+
expect(getProgressBarVariantColour(lightTheme, 'info')).toBe(
|
|
93
|
+
lightTheme.colour.fill.brand
|
|
94
|
+
);
|
|
95
|
+
expect(getProgressBarVariantColour(lightTheme, 'success')).toBe(
|
|
96
|
+
lightTheme.colour.fill.success
|
|
97
|
+
);
|
|
98
|
+
expect(getProgressBarVariantColour(lightTheme, 'critical')).toBe(
|
|
99
|
+
lightTheme.colour.fill.critical
|
|
100
|
+
);
|
|
101
|
+
expect(getProgressBarVariantColour(lightTheme, 'warning')).toBe(
|
|
102
|
+
lightTheme.colour.fill.warning
|
|
103
|
+
);
|
|
104
|
+
});
|
|
105
|
+
|
|
106
|
+
test('uses semantic variant colours in dark theme', () => {
|
|
107
|
+
expect(getProgressBarVariantColour(darkTheme, 'info')).toBe(
|
|
108
|
+
darkTheme.colour.fill.brand
|
|
109
|
+
);
|
|
110
|
+
expect(getProgressBarVariantColour(darkTheme, 'success')).toBe(
|
|
111
|
+
darkTheme.colour.fill.success
|
|
112
|
+
);
|
|
113
|
+
expect(getProgressBarVariantColour(darkTheme, 'critical')).toBe(
|
|
114
|
+
darkTheme.colour.fill.critical
|
|
115
|
+
);
|
|
116
|
+
expect(getProgressBarVariantColour(darkTheme, 'warning')).toBe(
|
|
117
|
+
darkTheme.colour.fill.warning
|
|
118
|
+
);
|
|
119
|
+
});
|
|
120
|
+
});
|
package/lib/components/index.ts
CHANGED
|
@@ -46,6 +46,13 @@ export type { BaseCheckboxProps, BaseCheckboxState } from './BaseCheckbox';
|
|
|
46
46
|
export { default as Spinner } from './Spinner';
|
|
47
47
|
export type { SpinnerProps } from './Spinner';
|
|
48
48
|
|
|
49
|
+
export { default as ProgressBar } from './ProgressBar';
|
|
50
|
+
export type {
|
|
51
|
+
ProgressBarProps,
|
|
52
|
+
ProgressBarSize,
|
|
53
|
+
ProgressBarVariant,
|
|
54
|
+
} from './ProgressBar';
|
|
55
|
+
|
|
49
56
|
export { default as Textarea } from './Textarea';
|
|
50
57
|
export type { TextareaProps } from './Textarea';
|
|
51
58
|
|