-
Notifications
You must be signed in to change notification settings - Fork 13
Expand file tree
/
Copy pathModalTabLayout.tsx
More file actions
162 lines (151 loc) · 5.3 KB
/
Copy pathModalTabLayout.tsx
File metadata and controls
162 lines (151 loc) · 5.3 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
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
import React, { useCallback, useEffect, useRef, useState } from "react";
import classNames from "classnames";
import { Button } from ":/components/Button";
import { useCunningham } from ":/components/Provider";
import { CloseIcon } from "./CloseIcon";
import { ChevronRightIcon } from "./ChevronRightIcon";
import { ChevronLeftIcon } from "./ChevronLeftIcon";
import { ModalFooter } from "./ModalFooter";
import { ModalTabVariantProps } from "./index";
export const ModalTabLayout = ({
showCloseButton,
titleId,
...props
}: ModalTabVariantProps & { showCloseButton: boolean; titleId: string }) => {
const { t } = useCunningham();
const [internalActiveTab, setInternalActiveTab] = useState(
props.defaultActiveTab ?? props.tabs[0]?.id,
);
const [mobileView, setMobileView] = useState<"sidebar" | "content">(
"sidebar",
);
const contentRef = useRef<HTMLDivElement>(null);
const [isContentScrollable, setIsContentScrollable] = useState(false);
const checkScrollable = useCallback(() => {
const el = contentRef.current;
if (el) {
setIsContentScrollable(el.scrollHeight > el.clientHeight);
}
}, []);
useEffect(() => {
const el = contentRef.current;
if (!el) return;
checkScrollable();
if (typeof ResizeObserver === "undefined") return;
// eslint-disable-next-line compat/compat -- guarded by typeof check above
const observer = new ResizeObserver(checkScrollable);
observer.observe(el);
return () => observer.disconnect();
}, [checkScrollable]);
const activeTabId = props.activeTab ?? internalActiveTab;
const currentTab =
props.tabs.find((tab) => tab.id === activeTabId) ?? props.tabs[0];
const handleTabChange = (tabId: string) => {
if (props.activeTab === undefined) {
setInternalActiveTab(tabId);
}
props.onTabChange?.(tabId);
setMobileView("content");
};
const handleBackToSidebar = () => {
setMobileView("sidebar");
};
return (
<div
className={classNames("c__modal__tab-layout", {
"c__modal__tab-layout--show-content": mobileView === "content",
})}
>
<div className="c__modal__tab-sidebar">
<div className="c__modal__tab-sidebar__header">
{props.sidebarTitle && (
<h2 id={titleId} className="c__modal__tab-sidebar__title">
{props.sidebarTitle}
</h2>
)}
{showCloseButton && (
<div className="c__modal__tab-sidebar__close">
<Button
icon={<CloseIcon />}
variant="tertiary"
color="neutral"
size="small"
aria-label={t("components.modals.close_button_aria_label")}
onClick={props.onClose}
/>
</div>
)}
</div>
<div className="c__modal__tab-sidebar__nav" role="tablist">
{props.tabs.map((tab) => (
<button
key={tab.id}
role="tab"
type="button"
aria-selected={tab.id === activeTabId}
className={classNames("c__modal__tab-sidebar__item", {
"c__modal__tab-sidebar__item--active": tab.id === activeTabId,
})}
onClick={() => handleTabChange(tab.id)}
>
<span className="c__modal__tab-sidebar__item-label">
{tab.icon && (
<span className="c__modal__tab-sidebar__item-icon">
{tab.icon}
</span>
)}
{tab.label}
</span>
<span className="c__modal__tab-sidebar__item-chevron">
<ChevronRightIcon />
</span>
</button>
))}
</div>
</div>
<div
className={classNames("c__modal__tab-content", {
"c__modal__tab-content--scrollable": isContentScrollable,
})}
>
<div className="c__modal__tab-content__head">
<div className="c__modal__tab-content__header">
<button
type="button"
className="c__modal__tab-content__back"
onClick={handleBackToSidebar}
aria-label={t("components.modals.back_to_tabs_aria_label")}
>
<ChevronLeftIcon />
</button>
<div className="c__modal__tab-content__title-group">
{currentTab?.title && (
<h2 className="c__modal__title">{currentTab.title}</h2>
)}
</div>
{showCloseButton && (
<div className="c__modal__close">
<Button
icon={<CloseIcon />}
variant="tertiary"
color="neutral"
size="small"
aria-label={t("components.modals.close_button_aria_label")}
onClick={props.onClose}
/>
</div>
)}
</div>
{currentTab?.subtitle && (
<div className="c__modal__subtitle">{currentTab.subtitle}</div>
)}
</div>
<div className="c__modal__content" role="tabpanel" ref={contentRef}>
{currentTab?.content}
</div>
{!props.stickyFooter && <ModalFooter {...props} />}
{props.stickyFooter && <ModalFooter {...props} sticky />}
</div>
</div>
);
};