summaryrefslogtreecommitdiffstats
path: root/src/components/Sidebar/Sidebar.tsx
blob: 9e2079d86ac3864c4b89a71260f29c1551c656e8 (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
import { Children, cloneElement, isValidElement, ReactNode } from 'react';
import styles from './Sidebar.module.scss';

type SidebarPosition = 'left' | 'right';

const Sidebar = ({
  children,
  position,
  ariaLabel,
  title,
}: {
  children: ReactNode;
  position: SidebarPosition;
  ariaLabel?: string;
  title?: string;
}) => {
  const childrenWithProps = Children.map(children, (child) => {
    if (isValidElement(child)) {
      return cloneElement(child, { titleLevel: title ? 3 : 2 });
    }
    return child;
  });

  const positionClass = `wrapper--${position}`;

  return (
    <aside
      className={`${styles.wrapper} ${styles[positionClass]}`}
      aria-label={ariaLabel}
      aria-labelledby={title ? `${position}-sidebar-title` : undefined}
    >
      <div className={styles.body}>
        {title && <h2 id={`${position}-sidebar-title`}>{title}</h2>}
        {childrenWithProps}
      </div>
    </aside>
  );
};

export default Sidebar;