aboutsummaryrefslogtreecommitdiffstats
path: root/src/components/organisms/widgets/image-widget/image-widget.tsx
blob: 23e6775b8e7587b96326d305e05cf3cf71320ab1 (plain)
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
import {
  forwardRef,
  type ForwardRefRenderFunction,
  type ReactNode,
} from 'react';
import { Figure, Link, type FigureProps } from '../../../atoms';
import { Collapsible, type CollapsibleProps } from '../../../molecules';
import styles from './image-widget.module.scss';

export type ImageWidgetProps = Omit<CollapsibleProps, 'children'> & {
  /**
   * Add a caption to the image.
   */
  description?: FigureProps['caption'];
  /**
   * The image.
   */
  img: ReactNode;
  /**
   * Add a link to the image.
   */
  url?: string;
};

const ImageWidgetWithRef: ForwardRefRenderFunction<
  HTMLDivElement,
  ImageWidgetProps
> = ({ description, img, isCollapsed, url, ...props }, ref) => (
  <Collapsible {...props} ref={ref}>
    <Figure caption={description} hasBorders>
      {url ? (
        <Link className={styles.link} href={url}>
          {img}
        </Link>
      ) : (
        img
      )}
    </Figure>
  </Collapsible>
);

/**
 * ImageWidget component
 *
 * Renders a widget that print an image and an optional text.
 */
export const ImageWidget = forwardRef(ImageWidgetWithRef);