import type { ReactNode } from 'react'; import React, { useMemo } from 'react'; /** * `href` only makes sense once there's an image to link — with no `src`, `href` would be ignored at runtime. * Model this in the props: `href` is only allowed when `src` is provided (otherwise it must be `undefined`). */ type PerexBoxLinkedImageProps = { href: string; src: string; }; type PerexBoxUnlinkedProps = { href?: undefined; src?: string; }; export type PerexBoxProps = { children: ReactNode; /** * Optional alt text for the image. */ alt?: string; } & (PerexBoxLinkedImageProps | PerexBoxUnlinkedProps); export const PerexBox: React.FC = ({ alt, children, href, src, ...rest }) => { const imgElement = useMemo(() => {alt, [alt, src]); return (
{children}
{src &&
{href ? {imgElement} : imgElement}
}
); };