aboutsummaryrefslogtreecommitdiffstats
path: root/src/components/atoms/lists/description-list/group.tsx
blob: 2d1fb4baa71c6cd69173edd16565816367f50917 (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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
import {
  forwardRef,
  type CSSProperties,
  type HTMLAttributes,
  type ReactNode,
  type ForwardRefRenderFunction,
} from 'react';
import type { Spacing } from '../../../../types';
import styles from './description-list.module.scss';

export type GroupProps = Omit<HTMLAttributes<HTMLDivElement>, 'children'> & {
  /**
   * The term(s) and description(s) of a description list.
   */
  children: ReactNode;
  /**
   * Should the term & description in the group be inlined?
   *
   * @default false
   */
  isInline?: boolean;
  /**
   * Define the spacing between list items.
   *
   * @default null
   */
  spacing?: Spacing | null;
};

const GroupWithRef: ForwardRefRenderFunction<HTMLDivElement, GroupProps> = (
  {
    children,
    className = '',
    isInline = false,
    spacing = null,
    style,
    ...props
  },
  ref
) => {
  const itemSpacing = spacing === null ? 0 : `var(--spacing-${spacing})`;
  const layoutClass = styles[isInline ? 'group--inline' : 'group--stack'];
  const groupClass = `${styles.group} ${layoutClass} ${className}`;
  const groupStyles = {
    ...style,
    '--itemSpacing': itemSpacing,
  } as CSSProperties;

  return (
    <div {...props} className={groupClass} ref={ref} style={groupStyles}>
      {children}
    </div>
  );
};

/**
 * Group component.
 *
 * Use it to wrap `Description` and `Term` components in a `DescriptionList`
 * component.
 */
export const Group = forwardRef(GroupWithRef);