aboutsummaryrefslogtreecommitdiffstats
path: root/src/components/molecules/layout/columns.tsx
blob: 56cd1a10af8f368e52f84c0cfe47f1f1a29903b2 (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 type {
  FC,
  HTMLAttributes,
  ReactComponentElement,
  ReactNode,
} from 'react';
import styles from './columns.module.scss';

export type ColumnProps = HTMLAttributes<HTMLDivElement> & {
  children: ReactNode;
};

/**
 * Column component.
 *
 * Render the body as a column.
 */
export const Column: FC<ColumnProps> = ({ children, ...props }) => (
  <div {...props}>{children}</div>
);

// eslint-disable-next-line @typescript-eslint/no-magic-numbers
type ColumnsNumber = 2 | 3 | 4;

export type ColumnsProps = {
  /**
   * The columns.
   */
  children: ReactComponentElement<typeof Column>[];
  /**
   * Set additional classnames to the columns wrapper.
   */
  className?: string;
  /**
   * The number of columns.
   */
  count: ColumnsNumber;
  /**
   * Should the columns be stacked on small devices? Default: true.
   */
  responsive?: boolean;
};

/**
 * Columns component.
 *
 * Render some Column components as columns.
 */
export const Columns: FC<ColumnsProps> = ({
  children,
  className = '',
  count,
  responsive = true,
}) => {
  const countClass = `wrapper--${count}-columns`;
  const responsiveClass = responsive
    ? `wrapper--responsive`
    : 'wrapper--no-responsive';
  const wrapperClass = `${styles.wrapper} ${styles[countClass]} ${styles[responsiveClass]} ${className}`;

  return <div className={wrapperClass}>{children}</div>;
};