-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathLayoutHeader.tsx
More file actions
220 lines (197 loc) · 5.05 KB
/
LayoutHeader.tsx
File metadata and controls
220 lines (197 loc) · 5.05 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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
import { IconArrowLeft } from '@tabler/icons-react';
import {
ForwardedRef,
forwardRef,
Fragment,
HTMLAttributes,
ReactNode,
Ref,
RefCallback,
} from 'react';
import { SlashIcon } from '../../../icons/SlashIcon';
import { tasty } from '../../../tasty';
import { Button } from '../../actions/Button/Button';
import { Link } from '../../actions/Link/Link';
import { useAutoTooltip } from '../use-auto-tooltip';
import { CubeLayoutContentProps, LayoutContent } from './LayoutContent';
const HeaderElement = tasty(LayoutContent, {
as: 'header',
qa: 'LayoutHeader',
styles: {
container: 'none',
// Header always has bottom border (inherent style)
border: 'bottom',
flexShrink: 0,
flexGrow: 0,
Inner: {
$: '>',
display: 'grid',
gridTemplate: `
"breadcrumbs breadcrumbs breadcrumbs breadcrumbs" auto
"back title suffix extra" 1fr
".. subtitle subtitle extra" auto
/ auto max-content 1fr minmax(0, auto)
`,
gap: 0,
placeContent: 'stretch',
placeItems: 'center stretch',
},
Back: {
$: '> Inner >',
gridArea: 'back',
display: 'flex',
placeItems: 'center',
margin: '.5x right',
},
Breadcrumbs: {
$: '> Inner >',
gridArea: 'breadcrumbs',
display: 'flex',
flow: 'row nowrap',
placeItems: 'center start',
gap: '1bw',
preset: 't3 strong',
color: '#dark-02',
},
Title: {
$: '> Inner >',
gridArea: 'title',
preset: {
'': 'h3',
'level=1': 'h1',
'level=2': 'h2',
'level=3': 'h3',
'level=4': 'h4',
'level=5': 'h5',
'level=6': 'h6',
},
color: '#dark',
margin: 0,
overflow: 'hidden',
textOverflow: 'ellipsis',
whiteSpace: 'nowrap',
},
Suffix: {
$: '> Inner >',
gridArea: 'suffix',
display: 'flex',
placeItems: 'center',
},
Extra: {
$: '> Inner >',
gridArea: 'extra',
display: 'flex',
placeItems: 'center',
placeSelf: 'end',
gap: '1x',
width: 'max 100%',
},
Subtitle: {
$: '> Inner >',
gridArea: 'subtitle',
preset: 't3',
color: '#dark-02',
},
},
});
export interface CubeLayoutHeaderProps extends CubeLayoutContentProps {
/** Page/section title */
title?: ReactNode;
/** Title heading level (1-6) */
level?: 1 | 2 | 3 | 4 | 5 | 6;
/** Content next to the title */
suffix?: ReactNode;
/** Content on the right side */
extra?: ReactNode;
/** Text below the title */
subtitle?: ReactNode;
/**
* Navigation breadcrumbs (max 3 items recommended).
* Uses Link component which integrates with the navigation provider.
*/
breadcrumbs?: Array<[label: string, href: string]>;
/** Callback for the back button. When provided, a back arrow button is rendered to the left of the title. */
onBack?: () => void;
}
function LayoutHeader(
props: CubeLayoutHeaderProps,
ref: ForwardedRef<HTMLDivElement>,
) {
const {
title,
level = 3,
suffix,
extra,
subtitle,
breadcrumbs,
onBack,
scrollbar = 'tiny',
children,
mods,
...otherProps
} = props;
// Use auto tooltip for title overflow detection
const { labelRef, renderWithTooltip } = useAutoTooltip({
tooltip: true,
children: typeof title === 'string' ? title : undefined,
});
const hasBreadcrumbs = breadcrumbs && breadcrumbs.length > 0;
const renderBreadcrumbs = () => {
if (!hasBreadcrumbs) return null;
return (
<div data-element="Breadcrumbs">
{breadcrumbs.map(([label, href], index) => (
<Fragment key={href}>
<Link to={href}>{label}</Link>
<SlashIcon />
</Fragment>
))}
</div>
);
};
const renderTitle = (
tooltipProps?: HTMLAttributes<HTMLElement>,
tooltipRef?: Ref<HTMLElement>,
) => {
if (!title) return null;
const TitleTag = `h${level}` as 'h1' | 'h2' | 'h3' | 'h4' | 'h5' | 'h6';
return (
<TitleTag
ref={tooltipRef as Ref<HTMLHeadingElement>}
data-element="Title"
data-level={level}
{...tooltipProps}
>
<span ref={labelRef as RefCallback<HTMLSpanElement>}>{title}</span>
</TitleTag>
);
};
return (
<HeaderElement
{...otherProps}
ref={ref}
mods={{ ...mods, level }}
scrollbar={scrollbar}
>
{renderBreadcrumbs()}
{onBack && (
<div data-element="Back">
<Button
type="neutral"
icon={<IconArrowLeft />}
aria-label="Go back"
onPress={onBack}
/>
</div>
)}
{renderWithTooltip(renderTitle, 'bottom')}
{suffix && <div data-element="Suffix">{suffix}</div>}
{extra && <div data-element="Extra">{extra}</div>}
{subtitle && <div data-element="Subtitle">{subtitle}</div>}
{children}
</HeaderElement>
);
}
const _LayoutHeader = forwardRef(LayoutHeader);
_LayoutHeader.displayName = 'Layout.Header';
export { _LayoutHeader as LayoutHeader };