Skip to content
This repository was archived by the owner on Jun 29, 2021. It is now read-only.

Commit 1eea26e

Browse files
committed
✨ Add elevation to sticky header
1 parent 651fbc0 commit 1eea26e

File tree

1 file changed

+36
-2
lines changed

1 file changed

+36
-2
lines changed

src/Header.tsx

Lines changed: 36 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import {
1010
ToolbarProps,
1111
IconButtonProps,
1212
} from '@committed/components'
13+
import useScrollTrigger from '@material-ui/core/useScrollTrigger'
1314
import { useLayout } from './Root'
1415
import { Layout, Position } from './types'
1516

@@ -51,6 +52,13 @@ export interface HeaderProps {
5152
* Should the menu icon be shown in the
5253
*/
5354
showMenuIcon?: boolean
55+
/**
56+
* Set to elevate the app bar using shadow.
57+
* Shadow depth, corresponds to dp in the spec.
58+
* It accepts values between 0 and 24 inclusive.
59+
* @default 0
60+
*/
61+
elevation: number
5462
children?: ReactNode
5563
}
5664

@@ -67,8 +75,25 @@ const useStyles = makeStyles(({ transitions }) => ({
6775
},
6876
}))
6977

78+
interface ElevationScrollProps {
79+
base: number
80+
children: React.ReactElement
81+
}
82+
83+
const ElevationScroll = ({ children, base }: ElevationScrollProps) => {
84+
const trigger = useScrollTrigger({
85+
disableHysteresis: true,
86+
threshold: 0,
87+
})
88+
89+
return React.cloneElement(children, {
90+
elevation: trigger ? Math.min(base + 4, 24) : base,
91+
})
92+
}
93+
7094
interface DumbProps extends Pick<Layout, 'open' | 'setOpen'> {
7195
zIndex: number
96+
elevation: number
7297
headerPosition: Position
7398
width: string
7499
marginLeft: number
@@ -79,6 +104,7 @@ interface DumbProps extends Pick<Layout, 'open' | 'setOpen'> {
79104

80105
export const DumbHeader = ({
81106
className,
107+
elevation,
82108
style,
83109
closeMenuIcon,
84110
openMenuIcon,
@@ -112,7 +138,7 @@ export const DumbHeader = ({
112138
return (
113139
<AppBar
114140
color={color}
115-
elevation={0}
141+
elevation={elevation}
116142
className={`${className} ${classes.root}`}
117143
position={headerPosition}
118144
style={{
@@ -142,6 +168,7 @@ export const Header = ({
142168
toolbarProps = {},
143169
menuButtonProps = {},
144170
showMenuIcon = true,
171+
elevation = 0,
145172
}: HeaderProps) => {
146173
const theme = useTheme()
147174
const layout = useLayout()
@@ -182,10 +209,11 @@ export const Header = ({
182209
pushed: '100%',
183210
}[headerResponse]
184211

185-
return (
212+
const header = (
186213
<DumbHeader
187214
className={className}
188215
style={style}
216+
elevation={elevation}
189217
closeMenuIcon={closeMenuIcon}
190218
openMenuIcon={openMenuIcon}
191219
color={color}
@@ -207,4 +235,10 @@ export const Header = ({
207235
showMenuRight={showMenuRight}
208236
/>
209237
)
238+
239+
if (headerPosition === 'sticky') {
240+
return <ElevationScroll base={elevation}>{header}</ElevationScroll>
241+
} else {
242+
return header
243+
}
210244
}

0 commit comments

Comments
 (0)