-
Notifications
You must be signed in to change notification settings - Fork 67
Expand file tree
/
Copy pathSectionIntroStacked.tsx
More file actions
284 lines (246 loc) · 8.12 KB
/
Copy pathSectionIntroStacked.tsx
File metadata and controls
284 lines (246 loc) · 8.12 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
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
import React, {forwardRef, PropsWithChildren, useMemo, type Ref, useCallback} from 'react'
import {clsx} from 'clsx'
import {Grid} from '../Grid'
import {Link, LinkProps} from '../Link'
import {Heading, HeadingProps, defaultHeadingTag} from '../Heading'
import {Text, TextProps} from '../Text'
import {Icon, IconProps} from '../Icon'
import {useAnimation} from '../animation'
import styles from './SectionIntroStacked.module.css'
import gridlineStyles from '../component-helpers/shared.module.css'
import type {BaseProps} from '../component-helpers'
export const SectionIntroStackedVariants = ['default', 'gridline'] as const
export type SectionIntroStackedVariant = (typeof SectionIntroStackedVariants)[number]
export type SectionIntroStackedProps = React.HTMLAttributes<HTMLElement> &
BaseProps<HTMLElement> &
PropsWithChildren & {
variant?: SectionIntroStackedVariant
}
const Root = forwardRef<HTMLElement, PropsWithChildren<SectionIntroStackedProps>>(
({animate, className, children, style, variant = 'default', ...props}, ref) => {
const {classes: animationClasses, styles: animationInlineStyles} = useAnimation(animate)
const childrenArray = React.Children.toArray(children)
// extract Items and everything else into two separate arrays
const items = childrenArray.filter(child => React.isValidElement(child) && child.type === ItemsBase)
const otherChildren = childrenArray.filter(child => !items.includes(child))
return (
<header
ref={ref}
className={clsx(
styles.SectionIntroStacked,
variant === 'gridline' && styles['SectionIntroStacked--variant-gridline'],
variant === 'gridline' && gridlineStyles.gridline,
animationClasses,
className,
)}
{...props}
style={{...animationInlineStyles, ...style}}
>
<Grid fullWidth enableGutters={variant !== 'gridline'} columnGap="none">
<Grid.Column span={{large: 6}} className={styles['SectionIntroStacked-content']}>
{otherChildren}
</Grid.Column>
<Grid.Column span={{large: 6}} start={{large: 7}}>
{items}
</Grid.Column>
</Grid>
</header>
)
},
)
type SectionIntroStackedHeadingProps = BaseProps<HTMLHeadingElement> & HeadingProps
const defaultHeadingSize = '3'
const _Heading = forwardRef(
(
{
as = defaultHeadingTag,
size = defaultHeadingSize,
className,
children,
...props
}: PropsWithChildren<SectionIntroStackedHeadingProps>,
ref: Ref<HTMLHeadingElement>,
) => {
const childrenArray = useMemo(() => React.Children.toArray(children), [children])
const getConditionalVariant = useCallback(() => {
if (childrenArray.some(child => React.isValidElement(child) && (child.type === 'b' || child.type === 'em'))) {
return 'muted'
}
return 'default'
}, [childrenArray])
const defaultColor = childrenArray.length === 1 ? 'default' : getConditionalVariant()
return (
<Heading
ref={ref}
className={clsx(
styles[`SectionIntroStacked-heading`],
defaultColor === 'muted' && styles[`SectionIntroStacked-heading--muted`],
className,
)}
size={size}
as={as}
{...props}
>
{children}
</Heading>
)
},
)
type SectionIntroStackedDescriptionProps = Omit<TextProps, 'as' | 'variant'> &
BaseProps<HTMLParagraphElement> & {
as?: 'p'
children: React.ReactNode | React.ReactNode[]
}
const _Description = forwardRef(
({className, children, ...props}: SectionIntroStackedDescriptionProps, ref: Ref<HTMLParagraphElement>) => {
return (
<Text
as="p"
className={clsx(styles['SectionIntroStacked-description'], className)}
ref={ref}
variant="muted"
{...props}
>
{children}
</Text>
)
},
)
type SectionIntroStackedLinkProps = LinkProps & BaseProps<HTMLAnchorElement>
const _Link = forwardRef(
(
{className, children, variant = 'accent', size = 'medium', ...props}: SectionIntroStackedLinkProps,
ref: Ref<HTMLAnchorElement>,
) => {
return (
<Link
ref={ref}
className={clsx(styles['SectionIntroStacked-link'], className)}
size={size}
variant={variant}
{...props}
>
{children}
</Link>
)
},
)
type SectionIntroStackedItemsProps = BaseProps<HTMLUListElement>
const ItemsBase = ({animate, className, children, ...rest}: PropsWithChildren<SectionIntroStackedItemsProps>) => {
const {classes: animationClasses, styles: animationInlineStyles} = useAnimation(animate)
const timelineClassName = clsx(animationClasses, styles['SectionIntroStacked-items'], className)
return (
<ul className={timelineClassName} style={{...animationInlineStyles}} {...rest}>
{children}
</ul>
)
}
type SectionIntroStackedItemIconProps = IconProps
const ItemIcon = ({className, color = 'green', ...props}: SectionIntroStackedItemIconProps) => {
return (
<Icon
className={clsx(styles['SectionIntroStackedItem__icon'], className)}
hasBackground
size="small"
color={color}
{...props}
/>
)
}
type SectionIntroStackedItemHeadingProps = HeadingProps
const ItemHeading = forwardRef(
(
{
as = 'h3',
size = 'subhead-large',
className,
children,
...props
}: PropsWithChildren<SectionIntroStackedItemHeadingProps>,
ref: Ref<HTMLHeadingElement>,
) => {
return (
<Heading
ref={ref}
as={as}
size={size}
className={clsx(styles['SectionIntroStackedItem__heading'], className)}
{...props}
>
{children}
</Heading>
)
},
)
type SectionIntroStackedItemDescriptionProps = Omit<TextProps, 'as' | 'variant' | 'size'> &
BaseProps<HTMLParagraphElement> & {
as?: 'p'
children: React.ReactNode | React.ReactNode[]
}
const ItemDescription = forwardRef(
({className, children, ...props}: SectionIntroStackedItemDescriptionProps, ref: Ref<HTMLParagraphElement>) => {
return (
<Text
as="p"
variant="muted"
size="200"
className={clsx(styles['SectionIntroStackedItem__description'], className)}
ref={ref}
{...props}
>
{children}
</Text>
)
},
)
export type SectionIntroStackedItemProps = BaseProps<HTMLLIElement>
const ItemBase = ({className, children, ...rest}: PropsWithChildren<SectionIntroStackedItemProps>) => {
const itemClassName = clsx(styles['SectionIntroStackedItem-item'], className)
const childrenArray = useMemo(() => React.Children.toArray(children), [children])
const hasSubComponents = childrenArray.some(
child =>
React.isValidElement(child) &&
(child.type === ItemIcon || child.type === ItemHeading || child.type === ItemDescription),
)
if (hasSubComponents) {
const iconChild = childrenArray.find(child => React.isValidElement(child) && child.type === ItemIcon)
const contentChildren = childrenArray.filter(child => !(React.isValidElement(child) && child.type === ItemIcon))
return (
<li className={clsx(itemClassName, !!iconChild && styles['SectionIntroStackedItem-item--with-icon'])} {...rest}>
{iconChild}
<div className={styles['SectionIntroStackedItem__content']}>{contentChildren}</div>
</li>
)
}
const getConditionalVariant = () => {
if (childrenArray.some(child => React.isValidElement(child) && (child.type === 'b' || child.type === 'em'))) {
return 'muted'
}
return 'default'
}
const defaultColor = childrenArray.length === 1 ? 'default' : getConditionalVariant()
return (
<li className={itemClassName} {...rest}>
<Text
as="span"
className={clsx(
styles['SectionIntroStackedItem__item-text'],
defaultColor === 'muted' && styles[`SectionIntroStackedItem__item-text--muted`],
)}
size="350"
>
{children}
</Text>
</li>
)
}
export const SectionIntroStacked = Object.assign(Root, {
Heading: _Heading,
Description: _Description,
Link: _Link,
Items: ItemsBase,
Item: ItemBase,
ItemIcon,
ItemHeading,
ItemDescription,
})