aboutsummaryrefslogtreecommitdiffstats
path: root/src/components/organisms/navbar/navbar-item/navbar-item.tsx
blob: 993b613f43774f4e7857a75ea28784479d77032e (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
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
import {
  type ReactNode,
  useCallback,
  type ForwardRefRenderFunction,
  forwardRef,
  useRef,
} from 'react';
import {
  useBoolean,
  useOnClickOutside,
  useOnRouteChange,
  type useOnClickOutsideHandler,
  useTimeout,
} from '../../../../utils/hooks';
import {
  Checkbox,
  Heading,
  Icon,
  type IconShape,
  Label,
  Overlay,
  Flip,
  FlipSide,
  type ListItemProps,
  ListItem,
} from '../../../atoms';
import { Modal } from '../../../molecules';
import styles from './navbar-item.module.scss';

export type NavbarItemActivationHandler = (isActive: boolean) => void;

export type NavbarItemProps = Omit<
  ListItemProps,
  'children' | 'hideMarker' | 'id'
> & {
  /**
   * Add a delay (in ms) before triggering the `onActivation` handler.
   */
  activationHandlerDelay?: number;
  /**
   * The modal contents.
   */
  children: ReactNode;
  /**
   * An icon to illustrate the nav item.
   */
  icon: IconShape;
  /**
   * The item id.
   */
  id: string;
  /**
   * An accessible name for the nav item.
   */
  label: string;
  /**
   * The modal heading.
   */
  modalHeading?: string;
  /**
   * Make the modal always visible from the given breakpoint.
   */
  modalVisibleFrom?: 'sm' | 'md';
  /**
   * A callback function to handle item activation.
   */
  onActivation?: NavbarItemActivationHandler;
  /**
   * Should we add the icon on the modal?
   *
   * @default false
   */
  showIconOnModal?: boolean;
};

const NavbarItemWithRef: ForwardRefRenderFunction<
  HTMLLIElement,
  NavbarItemProps
> = (
  {
    activationHandlerDelay,
    children,
    className = '',
    icon,
    id,
    label,
    modalHeading,
    modalVisibleFrom,
    onActivation,
    showIconOnModal = false,
    ...props
  },
  ref
) => {
  const itemClass = [
    styles.item,
    modalVisibleFrom
      ? styles[`item--hidden-controller-${modalVisibleFrom}`]
      : '',
    className,
  ].join(' ');
  const { deactivate, state: isActive, toggle } = useBoolean(false);
  const labelRef = useRef<HTMLLabelElement>(null);
  const checkboxRef = useRef<HTMLInputElement>(null);
  const deactivateItem: useOnClickOutsideHandler = useCallback(
    (e) => {
      const isCheckbox =
        e.target && checkboxRef.current?.contains(e.target as Node);
      const isLabel = e.target && labelRef.current?.contains(e.target as Node);

      if (!isCheckbox && !isLabel) deactivate();
    },
    [deactivate]
  );
  const modalRef = useOnClickOutside<HTMLDivElement>(deactivateItem);

  useOnRouteChange(deactivate, 'end');

  const handleActivation = useCallback(() => {
    if (onActivation) onActivation(isActive);
  }, [isActive, onActivation]);

  useTimeout(handleActivation, activationHandlerDelay);

  return (
    <ListItem {...props} className={itemClass} hideMarker ref={ref}>
      <Checkbox
        className={styles.checkbox}
        id={id}
        isChecked={isActive}
        name={id}
        onChange={toggle}
        ref={checkboxRef}
        value={id}
      />
      <Label
        aria-label={label}
        className={styles.label}
        htmlFor={id}
        ref={labelRef}
      >
        {icon === 'hamburger' ? (
          <Icon
            aria-hidden
            className={styles[`icon--${icon}`]}
            shape={icon}
            // eslint-disable-next-line react/jsx-no-literals
            size="lg"
          />
        ) : (
          <Flip aria-hidden className={styles.flip} showBack={isActive}>
            <FlipSide className={styles.flip__side}>
              <Icon
                shape={icon}
                // eslint-disable-next-line react/jsx-no-literals
                size="lg"
              />
            </FlipSide>
            <FlipSide className={styles.flip__side} isBack>
              <Icon
                // eslint-disable-next-line react/jsx-no-literals
                shape="cross"
              />
            </FlipSide>
          </Flip>
        )}
      </Label>
      <Overlay className={styles.overlay} isVisible={isActive}>
        <Modal
          className={styles.modal}
          heading={
            modalHeading ? (
              <Heading isFake level={3}>
                {modalHeading}
              </Heading>
            ) : null
          }
          icon={showIconOnModal ? <Icon shape={icon} /> : null}
          ref={modalRef}
        >
          {children}
        </Modal>
      </Overlay>
    </ListItem>
  );
};

export const NavbarItem = forwardRef(NavbarItemWithRef);