-
Notifications
You must be signed in to change notification settings - Fork 202
Expand file tree
/
Copy pathEmptyState.tsx
More file actions
76 lines (71 loc) · 1.94 KB
/
Copy pathEmptyState.tsx
File metadata and controls
76 lines (71 loc) · 1.94 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
import { useId } from 'react';
import { Link } from 'react-router-dom';
import type { ReactNode, ComponentPropsWithoutRef } from 'react';
type ActionShape = {
label: string;
to?: string;
onClick?: () => void;
};
export type EmptyStateProps = {
illustration: ReactNode;
title: string;
description: string;
eyebrow?: string;
tone?: 'info' | 'success';
primaryAction?: ActionShape;
secondaryAction?: ActionShape;
titleId?: string;
} & ComponentPropsWithoutRef<'div'>;
function renderAction(action: ActionShape, className: string) {
if (action.to) {
return (
<Link key={action.label} to={action.to} className={className}>
{action.label}
</Link>
);
}
return (
<button key={action.label} type="button" className={className} onClick={action.onClick}>
{action.label}
</button>
);
}
export function EmptyState({
illustration,
title,
description,
eyebrow,
tone,
primaryAction,
secondaryAction,
titleId: customTitleId,
className = '',
...props
}: EmptyStateProps) {
const autoId = useId();
const titleId = customTitleId ?? `empty-state-title-${autoId}`;
const toneClass = tone ? `empty-state--${tone}` : '';
return (
<div
className={`empty-state ${toneClass} ${className}`.trim()}
role="status"
aria-live="polite"
aria-labelledby={titleId}
{...props}
>
{illustration}
{tone && <div className="empty-state__accent-rule" />}
{eyebrow && <p className="empty-state__eyebrow">{eyebrow}</p>}
<h2 className="empty-state__title" id={titleId}>
{title}
</h2>
<p className="empty-state__description">{description}</p>
{(primaryAction || secondaryAction) && (
<div className="empty-state__actions">
{primaryAction && renderAction(primaryAction, 'empty-state-btn')}
{secondaryAction && renderAction(secondaryAction, 'empty-state-secondary-btn')}
</div>
)}
</div>
);
}