-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Extract reusable Caption and MediaWrapper component and use it for St…
…oryImage
- Loading branch information
1 parent
fde6f9e
commit 79af62a
Showing
5 changed files
with
162 additions
and
63 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
import { cnb } from 'cnbuilder'; | ||
import { Container } from '@/components/Container'; | ||
import * as styles from './MediaWrapper.styles'; | ||
|
||
/** | ||
* This is a caption component for images, looping video and embedded video | ||
* that provides a shared set of layout and options. | ||
*/ | ||
|
||
export type CaptionProps = React.HTMLAttributes<HTMLDivElement> & { | ||
caption?: React.ReactNode; | ||
isCaptionInset?: boolean; | ||
captionBgColor?: styles.CaptionBgColorType; | ||
}; | ||
|
||
export const Caption = ({ | ||
caption, | ||
isCaptionInset, | ||
captionBgColor = 'transparent', | ||
className, | ||
...props | ||
}: CaptionProps) => { | ||
return ( | ||
<Container | ||
as="figcaption" | ||
width={isCaptionInset ? 'site' : 'full'} | ||
className={cnb(styles.captionWrapper, styles.captionBgColors[captionBgColor])} | ||
{...props} | ||
> | ||
<div className={styles.caption(captionBgColor)}> | ||
{caption} | ||
</div> | ||
</Container> | ||
); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
import { cnb } from 'cnbuilder'; | ||
|
||
export const captionBgColors = { | ||
black: 'bg-gc-black', | ||
'black-60': 'bg-black-true/60', | ||
white: 'bg-white', | ||
transparent: '', | ||
}; | ||
export type CaptionBgColorType = keyof typeof captionBgColors; | ||
|
||
export const root = (isFullHeight?: boolean) => cnb(isFullHeight ? 'h-full' : ''); | ||
export const animateWrapper = (isFullHeight?: boolean) => cnb(isFullHeight ? 'h-full' : ''); | ||
export const figure = (isFullHeight: boolean) => cnb(isFullHeight ? 'h-full' : ''); | ||
export const mediaWrapper = (isFullHeight: boolean, isParallax: boolean) => cnb('relative', | ||
isFullHeight ? 'h-full' : '', | ||
isParallax ? 'overflow-hidden' : '', | ||
); | ||
export const captionWrapper = 'mt-0'; | ||
export const caption = (captionBgColor: CaptionBgColorType) => cnb( | ||
'*:*:leading-display max-w-prose-wide first:*:*:mt-0', | ||
!!captionBgColor && captionBgColor !== 'transparent' ? 'px-1em py-08em' : 'pt-06em', | ||
); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
import { cnb } from 'cnbuilder'; | ||
import { AnimateInView, type AnimationType } from '@/components/Animate'; | ||
import { Caption } from './Caption'; | ||
import { WidthBox, type WidthType } from '@/components/WidthBox'; | ||
import { type PaddingType } from '@/utilities/datasource'; | ||
import { imageAspectRatios, type ImageAspectRatioType } from '@/utilities/datasource'; | ||
import * as styles from './MediaWrapper.styles'; | ||
|
||
/** | ||
* This is a wrapper component for images and video components. | ||
* that provides a shared set of layout and caption options. | ||
*/ | ||
|
||
export type MediaWrapperProps = React.HTMLAttributes<HTMLDivElement> & { | ||
caption?: React.ReactNode; | ||
isCaptionInset?: boolean; | ||
captionBgColor?: styles.CaptionBgColorType; | ||
aspectRatio?: ImageAspectRatioType; | ||
isFullHeight?: boolean; | ||
isParallax?: boolean; | ||
boundingWidth?: 'site' | 'full'; | ||
width?: WidthType; | ||
pt?: PaddingType; | ||
pb?: PaddingType; | ||
animation?: AnimationType; | ||
delay?: number; | ||
}; | ||
|
||
export const MediaWrapper = ({ | ||
caption, | ||
aspectRatio, | ||
isFullHeight, | ||
isParallax, | ||
boundingWidth = 'full', | ||
width, | ||
pt, | ||
pb, | ||
isCaptionInset, | ||
captionBgColor = 'transparent', | ||
animation = 'none', | ||
delay, | ||
children, | ||
className, | ||
...props | ||
}: MediaWrapperProps) => { | ||
return ( | ||
<WidthBox | ||
{...props} | ||
boundingWidth={boundingWidth} | ||
width={width} | ||
pt={pt} | ||
pb={pb} | ||
className={cnb(styles.root(isFullHeight), className)} | ||
> | ||
<AnimateInView animation={animation} delay={delay} className={styles.animateWrapper(isFullHeight)}> | ||
<figure className={styles.figure(isFullHeight)}> | ||
<div className={cnb(imageAspectRatios[aspectRatio], styles.mediaWrapper(isFullHeight, isParallax))}> | ||
{children} | ||
</div> | ||
{caption && ( | ||
<Caption caption={caption} isCaptionInset={isCaptionInset} captionBgColor={captionBgColor} /> | ||
)} | ||
</figure> | ||
</AnimateInView> | ||
</WidthBox> | ||
); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
export * from './Caption'; | ||
export * from './MediaWrapper'; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters