import React, { Children, createContext, useContext } from 'react'; import { FeatureHeaderProps, FeatureHeaderPrimaryProps } from './types'; import { ConditionalWrapper, WrapperTag } from '../../common'; import clsx from 'clsx'; type FeatureHeaderSettingsContextProps = { hasCoverImage?: boolean, hasNoImagePadding?: boolean } const FeatureHeaderSettingsContext = createContext({ hasCoverImage: undefined, hasNoImagePadding: undefined }); const Primary = ({ children, title, ...props }: FeatureHeaderPrimaryProps) => { return (

{title}

{children}
) }; const Secondary = ({ children, ...props }: React.AllHTMLAttributes) => { const hasCoverImage = useContext(FeatureHeaderSettingsContext).hasCoverImage; const hasNoImagePadding = useContext(FeatureHeaderSettingsContext).hasNoImagePadding; children = Children.map(children, child => { const thisChild = child as React.ReactElement; if (thisChild && ['img', 'svg', 'picture'].includes(thisChild.type as string)) { return React.cloneElement(thisChild, { className: 'ds_feature-header__image' }); } else { return child; } }); return (
{children}
); }; const FeatureHeader = ({ backgroundColor, children, className, hasCoverImage, hasNoImagePadding, id, isFullWidth, isWideText, isTopAlignImage, tagName = 'div', ...props }: FeatureHeaderProps) => { let backgroundColorClassName; if (backgroundColor === 'secondary') { backgroundColorClassName = 'ds_feature-header--background-secondary'; } else if (backgroundColor === 'tertiary') { backgroundColorClassName = 'ds_feature-header--background-tertiary'; } else if (backgroundColor === 'brand') { backgroundColorClassName = 'ds_feature-header--background'; } return (
{children}
} > {children}
); }; FeatureHeader.displayName = 'FeatureHeader'; Primary.displayName = 'FeatureHeader.Primary'; Secondary.displayName = 'FeatureHeader.Secondary'; FeatureHeader.Primary = Primary; FeatureHeader.Secondary = Secondary; export default FeatureHeader;