-
Notifications
You must be signed in to change notification settings - Fork 198
/
Copy pathNavTab.js
187 lines (172 loc) · 5.17 KB
/
NavTab.js
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
import React, { useCallback } from 'react'
import { styled, useTheme } from '@mui/material/styles'
import { Fade, Tab, Paper } from '@mui/material'
import PropTypes from 'prop-types'
import Link from '../link/Link'
import useMediaQuery from '@mui/material/useMediaQuery'
import { bindHover, bindPopover } from 'material-ui-popup-state'
import { usePopupState } from 'material-ui-popup-state/hooks'
import HoverPopover from 'material-ui-popup-state/HoverPopover'
const PREFIX = 'RSFNavTab'
const defaultClasses = {
popover: `${PREFIX}-popover`,
tab: `${PREFIX}-tab`,
link: `${PREFIX}-link`,
ripple: `${PREFIX}-ripple`,
paper: `${PREFIX}-paper`,
innerPaper: `${PREFIX}-innerPaper`,
}
// TODO jss-to-styled codemod: The Fragment root was replaced by div. Change the tag if needed.
const Root = styled('div')(({ theme }) => ({
/**
* Styles applied to the Popover element for desktop users.
*/
[`& .${defaultClasses.popover}`]: {
pointerEvents: 'none',
maxHeight: '80%',
},
/**
* Styles applied to the `Tab` element.
*/
[`& .${defaultClasses.tab}`]: {
paddingLeft: theme.spacing(2),
paddingRight: theme.spacing(2),
},
/**
* Styles applied to the root [`Link`](/apiReference/link/Link) element.
*/
[`& .${defaultClasses.link}`]: {
textDecoration: 'none',
color: 'inherit',
fontWeight: 300,
},
/**
* Styles applied to the root element of the `Tab`'s `TouchRippleProps` classes.
*/
[`& .${defaultClasses.ripple}`]: {
zIndex: 2,
},
/**
* Styles applied to the Popover element's `Paper` element for desktop users.
*/
[`& .${defaultClasses.paper}`]: {
pointerEvents: 'all',
paddingTop: 2, // we add 2 pixels of transparent padding and move the menu up two pixels to cover the tab indicator
marginTop: -2, // so that the user doesn't temporarily mouse over the indicator when moving between the tab and the menu, causing the menu to flicker.
background: 'transparent',
boxShadow: '0px 5px 5px -3px rgba(0,0,0,0.2), 0px 8px 10px 1px rgba(0,0,0,0.14)',
},
/**
* Styles applied to the Popover element's `Paper` element for desktop users.
*/
[`& .${defaultClasses.innerPaper}`]: {
borderTop: `1px solid ${theme.palette.divider}`,
},
}))
/**
* A single navigational tab, which links to another page in the app based on the `as` and `href` props.
* Children are displayed in a menu that is shown on mouse over on desktop devices.
*
* Example:
*
* ```js
* <NavTab href="/c/[categoryId]" as="/c/shirts" label="Shirts">
* <div style={{ padding: 20 }}>
* <Link href="/s/[subcategoryId]" as="/s/long-sleeve-shirts">Long Sleeve Shirts</Link>
* <Link href="/s/[subcategoryId]" as="/s/turtlenecks">Turtlenecks</Link>
* <Link href="/s/[subcategoryId]" as="/s/tees">Tee Shirts</Link>
* </div>
* </NavTab>
* ```
*
* Accessibility:
*
* This component supports keyboard navigation. The user can open the menu by pressing the enter key when the `NavTab` is focused.
*/
const NavTab = function({ href, as, prefetch, children, classes: c = {}, ...props }) {
const classes = { ...defaultClasses, ...c }
const theme = useTheme()
const isSmall = useMediaQuery(theme.breakpoints.down('sm'))
const popupState = usePopupState({
variant: 'popover',
popupId: 'navTabPopup'
})
const handleEnterKeyDown = useCallback(e => {
if(e.key === 'Enter') {
e.preventDefault()
popupState.open(e)
}
}, [])
const hideMenu = useCallback(e => {
e.preventDefault()
popupState.close(e)
}, [])
return (
<Root>
<Link
className={classes.link}
href={href}
as={as}
onClick={hideMenu}
prefetch={prefetch}
{...bindHover(popupState)}
>
<Tab
onKeyDown={handleEnterKeyDown}
classes={{ root: classes.tab }}
aria-haspopup={children != null}
aria-expanded={popupState.isOpen}
{...props}
TouchRippleProps={{
classes: {
root: classes.ripple,
},
}}
/>
</Link>
{(children && !isSmall) ? (
<HoverPopover
{...bindPopover(popupState)}
className={classes.popover}
anchorOrigin={{
vertical: 'bottom',
horizontal: 'center',
}}
TransitionComponent={Fade}
transformOrigin={{
vertical: 'top',
horizontal: 'center',
}}
PaperProps={{
square: true,
className: classes.paper,
}}
classes={classes}
>
<Paper
className={classes.innerPaper}
square
>
{children}
</Paper>
</HoverPopover>
) : null}
</Root>
)
}
NavTab.propTypes = {
/**
* The link path
*/
as: PropTypes.string.isRequired,
/**
* The next.js route pattern
*/
href: PropTypes.string.isRequired,
/**
* Override or extend the styles applied to the component. See [CSS API](#css) below for more details.
*/
classes: PropTypes.object,
prefetch: PropTypes.oneOf(['always', 'visible', false]),
}
export default React.memo(NavTab)