-
Notifications
You must be signed in to change notification settings - Fork 67
Expand file tree
/
Copy pathCTABanner.tsx
More file actions
379 lines (345 loc) · 12.6 KB
/
Copy pathCTABanner.tsx
File metadata and controls
379 lines (345 loc) · 12.6 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
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
import React, {forwardRef, useCallback, useMemo, type Ref} from 'react'
import {clsx} from 'clsx'
import type {BaseProps} from '../component-helpers'
import {Grid} from '../Grid'
import {Heading, HeadingProps} from '../Heading'
import {Text, TextProps} from '../Text'
import {ButtonGroup} from '../ButtonGroup'
/**
* Design tokens
*/
import '@primer/brand-primitives/lib/design-tokens/css/tokens/functional/components/cta-banner/colors-with-modes.css'
/** * Main Stylesheet (as a CSS Module) */
import styles from './CTABanner.module.css'
import {Image} from '../Image'
import {Link, type LinkProps} from '../Link'
export const CTABannerBackgroundColors = ['default', 'subtle'] as const
type ResponsiveMap<T> = {
narrow?: T
regular?: T
wide?: T
}
type ResponsiveBackgroundImageSrcMap = ResponsiveMap<string | string[]>
type BackgroundColors = (typeof CTABannerBackgroundColors)[number] | AnyString
type ResponsiveBackgroundColorMap = ResponsiveMap<BackgroundColors>
type ResponsiveBackgroundImagePositionMap = ResponsiveMap<string | string[]>
type ResponsiveBackgroundImageSizeMap = ResponsiveMap<string | string[]>
export type CTABannerProps = BaseProps<HTMLElement> &
React.HTMLAttributes<HTMLElement> & {
/**
* Alternative presentations
*/
variant?: 'default' | 'balanced' | 'minimal'
/**
* The alignment of the content within the banner.
*/
align?: 'start' | 'center'
/**
* A flag to add a border to the banner.
*/
hasBorder?: boolean
/**
* Enable optional grid lines
*/
hasGridLines?: boolean
/**
* A flag to remove the shadow from the banner.
* @deprecated - hasShadow will be removed in a future release.
*/
hasShadow?: boolean
/**
* A flag to remove the background from the banner.
*/
hasBackground?: boolean
/**
* Optional, custom background color.
*/
backgroundColor?: BackgroundColors | ResponsiveBackgroundColorMap
/**
* Optional, custom background image.
*/
backgroundImageSrc?: string | ResponsiveBackgroundImageSrcMap
/**
* Optional, custom background position.
*/
backgroundImagePosition?: string | ResponsiveBackgroundImagePositionMap
/**
* Optional, custom background size.
*/
backgroundImageSize?: string | ResponsiveBackgroundImageSizeMap
/**
* Escape-hatch for inserting custom React components.
* Warning:
* This prop isn't advertised in our docs but remains part of the public API for edge-cases.
* Need to use this prop? Please check in with #primer-brand first to confirm correct usage.
*/
leadingComponent?: React.FunctionComponent
/**
* Escape-hatch for inserting custom React components.
* Warning:
* This prop isn't advertised in our docs but remains part of the public API for edge-cases.
* Need to use this prop? Please check in with #primer-brand first to confirm correct usage.
*/
trailingComponent?: React.FunctionComponent
}
const Root = forwardRef(
(
{
align = 'center',
hasBorder = false,
hasGridLines = false,
hasShadow = false,
hasBackground = true,
backgroundColor = 'var(--brand-CTABanner-bgColor)',
backgroundImageSrc,
backgroundImagePosition = 'center',
backgroundImageSize = 'cover',
className,
children,
leadingComponent: LeadingComponent,
trailingComponent: TrailingComponent,
style,
variant = 'default',
...props
}: CTABannerProps,
ref: Ref<HTMLElement>,
) => {
const processBackgroundValue = useCallback((value: string | string[], property) => {
if (property === 'background-image-src') {
return `url(${value})`
}
if (property === 'background-color' && typeof value === 'string') {
return CTABannerBackgroundColors.includes(value as (typeof CTABannerBackgroundColors)[number])
? `var(--brand-color-canvas-${value})`
: value
}
return value
}, [])
const createStyles = useCallback(
(property, value) =>
typeof value === 'string' || Array.isArray(value)
? {[`--brand-CTABanner-${property}`]: processBackgroundValue(value, property)}
: {
[`--brand-CTABanner-narrow-${property}`]: processBackgroundValue(value?.narrow, property),
[`--brand-CTABanner-regular-${property}`]: processBackgroundValue(value?.regular, property),
[`--brand-CTABanner-wide-${property}`]: processBackgroundValue(value?.wide, property),
},
[processBackgroundValue],
)
const backgroundStyles = useMemo(() => {
const allStyles = {}
const addStyle = (property, value) => {
if (value) {
Object.assign(allStyles, createStyles(property, value))
}
}
addStyle('background-color', backgroundColor)
addStyle('background-image-src', backgroundImageSrc)
addStyle('background-image-position', backgroundImagePosition)
addStyle('background-image-size', backgroundImageSize)
return allStyles
}, [backgroundColor, backgroundImageSrc, backgroundImagePosition, backgroundImageSize, createStyles])
const {ImageChild, ButtonGroupChild, restChildren, hasImageChild} = useMemo(() => {
const result = React.Children.toArray(children).reduce<{
ImageChild?: React.ReactElement
ButtonGroupChild?: React.ReactElement
restChildren: React.ReactElement[]
hasImageChild: boolean
}>(
(acc, child) => {
if (React.isValidElement(child)) {
if (child.type === _Image) {
acc.hasImageChild = true
if (variant === 'balanced') {
acc.ImageChild = child
} else if (variant !== 'minimal') {
acc.restChildren.push(child)
}
// minimal variant: image is intentionally excluded from rendering
} else if (variant === 'minimal' && child.type === _ButtonGroup) {
acc.ButtonGroupChild = child
} else if (child.type === _Heading) {
const headingChild = child as React.ReactElement<CTABannerHeadingProps>
acc.restChildren.push(
headingChild.props.size === undefined
? React.cloneElement(headingChild, {
size: variant === 'minimal' ? '6' : defaultHeadingSize,
})
: headingChild,
)
} else {
acc.restChildren.push(child)
}
}
return acc
},
{ImageChild: undefined, ButtonGroupChild: undefined, restChildren: [], hasImageChild: false},
)
return result
}, [children, variant])
const hasSideColumn = (variant === 'balanced' && ImageChild) || (variant === 'minimal' && ButtonGroupChild)
const Tag: React.FC<React.PropsWithChildren> = hasGridLines
? ({children: inner}) => <div className={styles['CTABanner-outer-container--border']}>{inner}</div>
: React.Fragment
const defaultAlign = variant === 'balanced' || variant === 'minimal' ? 'start' : align
const hasSystemBackgroundColor =
typeof backgroundColor === 'string' &&
CTABannerBackgroundColors.includes(backgroundColor as (typeof CTABannerBackgroundColors)[number])
if (process.env.NODE_ENV === 'development' || process.env.NODE_ENV === 'test') {
if (variant === 'balanced' && !ImageChild) {
// eslint-disable-next-line no-console
console.warn(
'CTABanner: The "balanced" variant requires a CTABanner.Image child to display the two-column layout correctly.',
)
}
if (variant === 'minimal' && hasImageChild) {
// eslint-disable-next-line no-console
console.warn('CTABanner: Image child is not supported in minimal variant and will not be rendered.')
}
}
return (
<Tag>
<section
ref={ref}
className={clsx(
styles.CTABanner,
hasShadow && styles['CTABanner--shadow'],
styles[`CTABanner--variant-${variant}`],
hasSystemBackgroundColor && styles[`CTABanner--bgColor-${backgroundColor}`],
className,
)}
style={{...backgroundStyles, ...style}}
{...props}
>
<div
className={clsx(
styles['CTABanner-container'],
hasBorder && styles['CTABanner-container--border'],
hasGridLines && styles['CTABanner-container--border-gridlines'],
!hasGridLines && styles['CTABanner-container--rounded'],
hasBackground && styles['CTABanner-container--background'],
)}
>
{hasSideColumn ? (
<Grid className={styles['CTABanner-grid']}>
<Grid.Column span={{xsmall: 12, large: 6}} className={styles['CTABanner-grid-column--primary']}>
<div
className={clsx(
styles['CTABanner-content'],
defaultAlign === 'center' && styles['CTABanner-content--center'],
)}
>
{LeadingComponent && <LeadingComponent />}
{restChildren}
{TrailingComponent && <TrailingComponent />}
</div>
</Grid.Column>
<Grid.Column span={{xsmall: 12, large: 6}} className={styles['CTABanner-grid-column--secondary']}>
{ImageChild || ButtonGroupChild}
</Grid.Column>
</Grid>
) : (
<div
className={clsx(
styles['CTABanner-content'],
defaultAlign === 'center' && styles['CTABanner-content--center'],
)}
>
{LeadingComponent && <LeadingComponent />}
{restChildren}
{TrailingComponent && <TrailingComponent />}
</div>
)}
</div>
</section>
</Tag>
)
},
)
const defaultHeadingLevel = 'h3'
type CTABannerHeadingProps = BaseProps<HTMLHeadingElement> & {
children: React.ReactNode | React.ReactNode[]
} & HeadingProps
const defaultHeadingSize: HeadingProps['size'] = '3'
const _Heading = forwardRef(
(
{as = defaultHeadingLevel, size = defaultHeadingSize, className, children, ...props}: CTABannerHeadingProps,
ref: Ref<HTMLHeadingElement>,
) => {
return (
<Heading ref={ref} className={clsx(styles['CTABanner-heading'], className)} size={size} as={as} {...props}>
{children}
</Heading>
)
},
)
type CTABannerDescriptionProps = React.HtmlHTMLAttributes<HTMLParagraphElement> &
BaseProps<HTMLParagraphElement> & {
variant?: TextProps['variant']
}
const Description = forwardRef(
({className, children, variant = 'muted', ...props}: CTABannerDescriptionProps, ref: Ref<HTMLParagraphElement>) => {
return (
<Text ref={ref} className={clsx(styles['CTABanner-description'], className)} as="p" variant={variant} {...props}>
{children}
</Text>
)
},
)
const _ButtonGroup = forwardRef(
(
{className, buttonSize = 'medium', buttonsAs, children, ...props}: React.ComponentProps<typeof ButtonGroup>,
ref: Ref<HTMLDivElement>,
) => {
return (
<ButtonGroup
buttonSize={buttonSize}
buttonsAs={buttonsAs}
className={clsx(styles['CTABanner-buttonGroup'], className)}
ref={ref}
{...props}
>
{children}
</ButtonGroup>
)
},
)
type CTABannerImageProps = BaseProps<HTMLImageElement> &
Omit<React.ImgHTMLAttributes<HTMLImageElement>, 'src' | 'alt'> & {
src: string
alt: string
}
const _Image = forwardRef(({className, src, alt, ...props}: CTABannerImageProps, ref: Ref<HTMLImageElement>) => {
return (
<Image
ref={ref}
className={clsx(styles['CTABanner-image'], className)}
src={src}
alt={alt}
borderRadius="medium"
{...props}
/>
)
})
type CTABannerLogoProps = BaseProps<HTMLDivElement> &
React.HTMLAttributes<HTMLDivElement> & {
children: React.ReactNode
}
const _Logo = forwardRef(({className, children, ...props}: CTABannerLogoProps, ref: Ref<HTMLDivElement>) => (
<div ref={ref} className={clsx(styles['CTABanner-logo'], className)} {...props}>
{children}
</div>
))
const _Link = ({className, children, variant = 'accent', ...props}: LinkProps) => (
<Link variant={variant} className={clsx(styles['CTABanner-link'], className)} {...props}>
{children}
</Link>
)
export const CTABanner = Object.assign(Root, {
Heading: _Heading,
Description,
ButtonGroup: _ButtonGroup,
Image: _Image,
Logo: _Logo,
Link: _Link,
})