import React, { Children, cloneElement, JSX } from 'react'; import classNames from 'classnames'; import { Constrain } from '../Constrain'; import './PromoBanner.scss'; export type PromoBannerProps = React.HTMLAttributes & { children: JSX.Element | [JSX.Element, JSX.Element] | [JSX.Element, JSX.Element, JSX.Element]; }; export const PromoBanner: React.FC = ({ className, children, ...rest }) => { const length = Children.count(children); return (
{Children.map(children, (child: JSX.Element, index) => { const isCenter = length % 2 !== 0 && Math.floor(length / 2) === index; return cloneElement(child, { className: classNames(child.props.className, 'PromoBanner__child', { 'PromoBanner__child--left': !isCenter && index === 0, 'PromoBanner__child--center': isCenter, 'PromoBanner__child--right': !isCenter && index === length - 1 }) }); })}
); };