aboutsummaryrefslogtreecommitdiffstats
path: root/src/components/organisms/toolbar/main-nav.tsx
blob: a5a105efb84b2c4e93d5a1387e0a865601c457d4 (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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
import {
  forwardRef,
  type ReactNode,
  type ForwardRefRenderFunction,
} from 'react';
import { useIntl } from 'react-intl';
import {
  BooleanField,
  type BooleanFieldProps,
  Icon,
  Label,
  Nav,
} from '../../atoms';
import { NavList, NavItem, NavLink } from '../../molecules';
import mainNavStyles from './main-nav.module.scss';
import sharedStyles from './toolbar-items.module.scss';

export type MainNavItem = {
  id: string;
  href: string;
  label: string;
  logo?: ReactNode;
};

export type MainNavProps = {
  /**
   * Set additional classnames to the nav element.
   */
  className?: string;
  /**
   * The button state.
   */
  isActive: BooleanFieldProps['isChecked'];
  /**
   * The main nav items.
   */
  items: MainNavItem[];
  /**
   * A callback function to handle button state.
   */
  setIsActive: BooleanFieldProps['onChange'];
};

const MainNavWithRef: ForwardRefRenderFunction<HTMLDivElement, MainNavProps> = (
  { className = '', isActive = false, items, setIsActive },
  ref
) => {
  const intl = useIntl();
  const label = isActive
    ? intl.formatMessage({
        defaultMessage: 'Close menu',
        description: 'MainNav: Close label',
        id: 'aJC7D2',
      })
    : intl.formatMessage({
        defaultMessage: 'Open menu',
        description: 'MainNav: Open label',
        id: 'GTbGMy',
      });

  return (
    <div className={`${sharedStyles.item} ${mainNavStyles.item}`} ref={ref}>
      <BooleanField
        className={`${sharedStyles.checkbox} ${mainNavStyles.checkbox}`}
        id="main-nav-button"
        isChecked={isActive}
        name="main-nav-button"
        onChange={setIsActive}
        type="checkbox"
        value="open"
      />
      <Label
        aria-label={label}
        className={`${sharedStyles.label} ${mainNavStyles.label}`}
        htmlFor="main-nav-button"
      >
        <Icon shape="hamburger" />
      </Label>
      <Nav
        className={`${sharedStyles.modal} ${mainNavStyles.modal} ${className}`}
      >
        <NavList isInline spacing="2xs">
          {items.map(({ id, ...link }) => (
            <NavItem key={id}>
              <NavLink {...link} isStack variant="main" />
            </NavItem>
          ))}
        </NavList>
      </Nav>
    </div>
  );
};

/**
 * MainNav component
 *
 * Render the main navigation.
 */
export const MainNav = forwardRef(MainNavWithRef);