-
Notifications
You must be signed in to change notification settings - Fork 353
Expand file tree
/
Copy pathhero-banner.component.tsx
More file actions
86 lines (80 loc) · 2.34 KB
/
hero-banner.component.tsx
File metadata and controls
86 lines (80 loc) · 2.34 KB
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
import React, { useRef, useState } from "react";
import styles from "./hero-banner-modal.module.scss";
import { HeroModalStateValues } from "@/features/home/values/hero-modal-state.values";
import Cookies from "js-cookie";
import { HeroModalTypeValues } from "@/features/home/values/hero-modal-type.values";
import { useButton } from "@react-aria/button";
interface HeroBannerComponentProps {
initialModalState: HeroModalStateValues;
modalType: HeroModalTypeValues;
modalCookieKey: string;
modalSummary: string;
modalContent: React.ReactNode;
modalCta: React.ReactNode | null;
}
export const HeroBannerComponent: React.FC<HeroBannerComponentProps> = ({
initialModalState,
modalType,
modalCookieKey,
modalSummary,
modalContent,
modalCta,
}) => {
const buttonRef = useRef<HTMLButtonElement | null>(null);
const [modalState, setModalState] =
useState<HeroModalStateValues>(initialModalState);
const { buttonProps } = useButton(
{
elementType: "span",
preventFocusOnPress: true,
},
buttonRef,
);
const toggleVisibility = () => {
setModalState((prevState) => {
Cookies.set(
modalCookieKey,
prevState === HeroModalStateValues.OPEN
? HeroModalStateValues.CLOSED
: HeroModalStateValues.OPEN,
{
secure: true,
},
);
return prevState === HeroModalStateValues.OPEN
? HeroModalStateValues.CLOSED
: HeroModalStateValues.OPEN;
});
};
return (
<p
className={styles.modal}
aria-expanded={modalState === HeroModalStateValues.OPEN}
data-state={modalState}
data-type={modalType}
>
{modalState === HeroModalStateValues.CLOSED && (
<span>{modalSummary}</span>
)}
<button
{...buttonProps}
aria-label={
modalState === HeroModalStateValues.CLOSED ? "Expand" : "Collapse"
}
ref={buttonRef}
className={styles.modal__control}
onClick={toggleVisibility}
data-state={modalState}
data-type={modalType}
>
<span className={styles.modal__controlIcon}>+</span>
</button>
{modalState === HeroModalStateValues.OPEN && (
<>
<span className={styles.modal__text}>{modalContent}</span>
<span className={styles.modal__cta}>{modalCta}</span>
</>
)}
</p>
);
};