uikit-react-public 0.40.5 → 0.42.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.
@@ -0,0 +1,56 @@
1
+ import type { Meta, StoryObj } from '@storybook/react-vite';
2
+ import { css } from '@emotion/css';
3
+ import Callout from './Callout';
4
+ import Icon from '../Icon';
5
+
6
+ const meta = {
7
+ title: 'Components/Callout',
8
+ component: Callout,
9
+ parameters: {
10
+ layout: 'centered',
11
+ },
12
+ argTypes: {
13
+ heading: { control: { type: 'text' } },
14
+ children: { control: { type: 'text' } },
15
+ showIcon: { control: { type: 'boolean' } },
16
+ icon: { control: false },
17
+ testId: { control: { type: 'text' } },
18
+ className: { control: false },
19
+ },
20
+ args: {
21
+ heading: 'Nobody at UCL will ever ask you for your password',
22
+ children:
23
+ "If you're unsure a request is genuine, contact OneDesk IT before entering anything.",
24
+ showIcon: true,
25
+ },
26
+ tags: ['autodocs'],
27
+ } satisfies Meta<typeof Callout>;
28
+
29
+ export default meta;
30
+ type Story = StoryObj<typeof meta>;
31
+
32
+ export const Default: Story = {};
33
+
34
+ export const WithoutIcon: Story = {
35
+ args: {
36
+ showIcon: false,
37
+ },
38
+ };
39
+
40
+ export const CustomIcon: Story = {
41
+ args: {
42
+ icon: <Icon.Info size={24} />,
43
+ },
44
+ };
45
+
46
+ export const ConstrainedWidth: Story = {
47
+ render: (args) => (
48
+ <div
49
+ className={css`
50
+ width: 420px;
51
+ `}
52
+ >
53
+ <Callout {...args} />
54
+ </div>
55
+ ),
56
+ };
@@ -0,0 +1,109 @@
1
+ import { HTMLAttributes, ReactNode, Ref, memo } from 'react';
2
+ import { css, cx } from '@emotion/css';
3
+ import useTheme from '../../theme/useTheme';
4
+ import marginsStyle, { MarginProps } from '../common/marginsStyle';
5
+ import Heading from '../Heading';
6
+ import Icon from '../Icon';
7
+ import Paragraph from '../Paragraph';
8
+
9
+ export const NAME = 'ucl-uikit-callout';
10
+
11
+ export interface CalloutBaseProps extends HTMLAttributes<HTMLDivElement> {
12
+ heading?: ReactNode;
13
+ icon?: ReactNode;
14
+ showIcon?: boolean;
15
+ testId?: string;
16
+ ref?: Ref<HTMLDivElement>;
17
+ }
18
+
19
+ export type CalloutProps = CalloutBaseProps & MarginProps;
20
+
21
+ const Callout = ({
22
+ heading,
23
+ icon,
24
+ showIcon = true,
25
+ testId = NAME,
26
+ className,
27
+ children,
28
+ ref,
29
+ ...props
30
+ }: CalloutProps) => {
31
+ const [theme] = useTheme();
32
+
33
+ const baseStyle = css`
34
+ box-sizing: border-box;
35
+ display: flex;
36
+ flex-direction: column;
37
+ align-items: flex-start;
38
+ gap: 30px;
39
+ width: 100%;
40
+ padding: ${theme.padding.p24};
41
+ background-color: ${theme.colour.surface.brand};
42
+ border: 1px solid ${theme.colour.border.brand};
43
+ `;
44
+
45
+ const rowStyle = css`
46
+ display: flex;
47
+ align-items: flex-start;
48
+ gap: ${theme.padding.p16};
49
+ width: 100%;
50
+ `;
51
+
52
+ const iconStyle = css`
53
+ flex: 0 0 auto;
54
+ color: ${theme.colour.icon.brand};
55
+ `;
56
+
57
+ const contentStyle = css`
58
+ display: flex;
59
+ flex: 1 1 auto;
60
+ flex-direction: column;
61
+ justify-content: center;
62
+ align-items: flex-start;
63
+ gap: ${theme.padding.p8};
64
+ min-width: 0;
65
+ `;
66
+
67
+ const style = cx(NAME, baseStyle, marginsStyle(props, theme), className);
68
+
69
+ return (
70
+ <div
71
+ className={style}
72
+ data-testid={testId}
73
+ ref={ref}
74
+ {...props}
75
+ >
76
+ <div className={rowStyle}>
77
+ {showIcon && (
78
+ <span
79
+ className={iconStyle}
80
+ aria-hidden='true'
81
+ >
82
+ {icon ?? <Icon.Info size={24} />}
83
+ </span>
84
+ )}
85
+ <div className={contentStyle}>
86
+ {heading && (
87
+ <Heading
88
+ level='xs'
89
+ as='h3'
90
+ noMargins
91
+ >
92
+ {heading}
93
+ </Heading>
94
+ )}
95
+ {children && (
96
+ <Paragraph
97
+ level='md'
98
+ noMargins
99
+ >
100
+ {children}
101
+ </Paragraph>
102
+ )}
103
+ </div>
104
+ </div>
105
+ </div>
106
+ );
107
+ };
108
+
109
+ export default memo(Callout);
@@ -0,0 +1,59 @@
1
+ import { describe, expect, test } from 'vitest';
2
+ import { render, screen } from '@testing-library/react';
3
+ import { ThemeContextProvider } from '../../../theme/useTheme';
4
+ import Callout from '../Callout';
5
+
6
+ const wrap = (ui: React.ReactElement) =>
7
+ render(<ThemeContextProvider>{ui}</ThemeContextProvider>);
8
+
9
+ describe('Callout', () => {
10
+ test('renders with default test id', () => {
11
+ const { getByTestId } = wrap(<Callout />);
12
+
13
+ expect(getByTestId('ucl-uikit-callout')).toBeInTheDocument();
14
+ });
15
+
16
+ test('renders heading and children', () => {
17
+ wrap(
18
+ <Callout heading='Nobody at UCL will ever ask you for your password'>
19
+ If you're unsure a request is genuine, contact OneDesk IT before
20
+ entering anything.
21
+ </Callout>
22
+ );
23
+
24
+ expect(
25
+ screen.getByRole('heading', {
26
+ name: 'Nobody at UCL will ever ask you for your password',
27
+ })
28
+ ).toBeInTheDocument();
29
+ expect(
30
+ screen.getByText(
31
+ /If you're unsure a request is genuine, contact OneDesk IT/
32
+ )
33
+ ).toBeInTheDocument();
34
+ });
35
+
36
+ test('applies custom test id and className', () => {
37
+ const { getByTestId } = wrap(
38
+ <Callout
39
+ testId='custom-callout'
40
+ className='custom-class'
41
+ />
42
+ );
43
+
44
+ expect(getByTestId('custom-callout')).toBeInTheDocument();
45
+ expect(getByTestId('custom-callout').classList).toContain('custom-class');
46
+ });
47
+
48
+ test('renders an icon by default', () => {
49
+ const { container } = wrap(<Callout />);
50
+
51
+ expect(container.querySelector('svg')).toBeInTheDocument();
52
+ });
53
+
54
+ test('can hide the icon', () => {
55
+ const { container } = wrap(<Callout showIcon={false} />);
56
+
57
+ expect(container.querySelector('svg')).not.toBeInTheDocument();
58
+ });
59
+ });
@@ -0,0 +1,2 @@
1
+ export { default } from './Callout';
2
+ export type { CalloutProps } from './Callout';
@@ -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
+ });
@@ -0,0 +1,6 @@
1
+ export { default } from './ProgressBar';
2
+ export type {
3
+ ProgressBarProps,
4
+ ProgressBarSize,
5
+ ProgressBarVariant,
6
+ } from './ProgressBar';