-
Notifications
You must be signed in to change notification settings - Fork 32
/
Masthead.tsx
158 lines (144 loc) · 4.53 KB
/
Masthead.tsx
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
import React, { useRef, useState } from 'react'
import ReactCSSTransitionGroup from 'react-addons-css-transition-group'
import classNames from 'classnames'
import { Link, Searchbar, ScrollableNav } from '../../components'
import { Logo as DefaultLogo, Search as SearchIcon } from '../../icons'
import { UserLink } from './UserLink'
import {
NotificationPanel,
NotificationsProps,
NOTIFICATION_PLACEMENT,
} from '../NotificationPanel'
export interface MastheadProps {
homeLink?: LinkTypes
LinkComponent?: any
Logo?: React.ComponentType
navItems?: NavItemTypes[]
notifications?: React.ReactElement<NotificationsProps>
onSearch?: (term: string) => void
searchPlaceholder?: string
title: string
hasUnreadNotification?: boolean
user?: UserType
}
export const Masthead: React.FC<MastheadProps> = ({
homeLink,
LinkComponent = Link,
Logo = DefaultLogo,
navItems,
notifications,
onSearch,
searchPlaceholder = '',
title,
hasUnreadNotification,
user,
}) => {
const [showSearch, setShowSearch] = useState(false)
const [showNotifications, setShowNotifications] = useState(false)
const searchButtonRef = useRef<HTMLButtonElement>(null)
const mastheadContainerRef = useRef<HTMLDivElement>(null)
const [containerWidth, setContainerWidth] = useState(0)
const toggleSearch = (
event: React.MouseEvent<HTMLButtonElement, MouseEvent>
) => {
event.currentTarget.blur()
const newShowSearch = !showSearch
// if opening the searchbar then get the container width and set that
// as the width of the searchbar so that it does not spill
// over to other parts of the page, such as the sidebar.
if (newShowSearch === true) {
setContainerWidth(mastheadContainerRef.current.offsetWidth)
}
setShowSearch(!showSearch)
}
const submitSearch = (term: string) => {
onSearch(term)
setShowSearch(false)
}
return (
<div
className={classNames('rn-masthead', {
'rn-masthead--show-search': showSearch,
'rn-masthead--show-notifications': showNotifications,
})}
data-testid="masthead"
ref={mastheadContainerRef}
>
<div className="rn-masthead__main">
<LinkComponent {...homeLink} className="rn-masthead__service-name">
<Logo />
<span
className="rn-masthead__title"
data-testid="masthead-servicename"
>
{title}
</span>
</LinkComponent>
<div className="rn-masthead__options">
{onSearch && (
<>
<button
className={`rn-masthead__option rn-searchbar__btn ${
showSearch ? 'is-active' : 'is-inactive'
}`}
onClick={toggleSearch}
ref={searchButtonRef}
data-testid="masthead-search-button"
>
<SearchIcon />
</button>
<svg className="rn-masthead__vertical-separator">
<line x1="0" y1="14" x2="0" y2="44" />
</svg>
</>
)}
{notifications && (
<NotificationPanel
buttonClassName="rn-masthead__option"
className="rn-masthead__notification"
data-testid="masthead-notifications"
notificationPlacement={NOTIFICATION_PLACEMENT.BELOW}
onHide={() => setShowNotifications(false)}
onShow={() => setShowNotifications(true)}
hasUnreadNotification={hasUnreadNotification}
>
{notifications}
</NotificationPanel>
)}
{user && (
<UserLink
className="rn-masthead__option"
LinkComponent={LinkComponent}
user={user}
/>
)}
</div>
</div>
<ReactCSSTransitionGroup
className="rn-searchbar__overlay"
transitionName="rn-searchbar"
transitionEnterTimeout={300}
transitionLeaveTimeout={300}
>
{onSearch && showSearch && (
<Searchbar
onSearch={submitSearch}
searchButton={searchButtonRef}
searchPlaceholder={searchPlaceholder}
setShowSearch={setShowSearch}
style={{ width: containerWidth }}
/>
)}
</ReactCSSTransitionGroup>
{navItems && (
<ScrollableNav
className="rn-masthead__nav"
navItems={navItems}
LinkComponent={LinkComponent}
data-testid="masthead-nav"
/>
)}
</div>
)
}
Masthead.displayName = 'Masthead'