Skip to content

Commit ea20dbe

Browse files
authored
Add variant prop to AppBar with relative, fixed, and hide values. The… (#94)
* Add variant prop to AppBar with relative, fixed, and hide values. The fixed prop is now deprecated. * some cleanup
1 parent 8f4cc9e commit ea20dbe

2 files changed

Lines changed: 33 additions & 7 deletions

File tree

src/AppBar.js

Lines changed: 26 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import { makeStyles } from '@material-ui/core/styles'
33
import { AppBar as MUIAppBar, Toolbar, useScrollTrigger, Slide } from '@material-ui/core'
44
import PropTypes from 'prop-types'
55
import PWAContext from './PWAContext'
6+
import clsx from 'clsx'
67

78
const useStyles = makeStyles(theme => ({
89
/**
@@ -19,6 +20,9 @@ const useStyles = makeStyles(theme => ({
1920
flexDirection: 'column',
2021
alignItems: 'stretch',
2122
},
23+
relative: {
24+
position: 'relative',
25+
},
2226
/**
2327
* Styles applied to the spacer that fills the height behind the floating toolbar.
2428
*/
@@ -46,15 +50,22 @@ const useStyles = makeStyles(theme => ({
4650
},
4751
}))
4852

49-
export default function AppBar({ children, style, fixed, offlineWarning, classes }) {
53+
export default function AppBar({ children, style, variant, fixed, offlineWarning, classes }) {
54+
if (fixed) {
55+
variant = 'fixed'
56+
}
57+
5058
const trigger = useScrollTrigger()
5159
classes = useStyles({ classes })
5260

5361
const { offline } = useContext(PWAContext)
5462

5563
let appBar = (
5664
<MUIAppBar
57-
className={classes.root}
65+
className={clsx({
66+
[classes.root]: true,
67+
[classes.relative]: variant === 'relative',
68+
})}
5869
style={{
5970
...style,
6071
}}
@@ -65,7 +76,7 @@ export default function AppBar({ children, style, fixed, offlineWarning, classes
6576
</MUIAppBar>
6677
)
6778

68-
if (!fixed) {
79+
if (variant === 'hide') {
6980
appBar = (
7081
<Slide appear={false} in={!trigger}>
7182
{appBar}
@@ -75,7 +86,7 @@ export default function AppBar({ children, style, fixed, offlineWarning, classes
7586

7687
return (
7788
<>
78-
<div className={classes.spacer} />
89+
{(variant === 'hide' || variant === 'fixed') && <div className={classes.spacer} />}
7990
{offline && <div className={classes.offline}>{offlineWarning}</div>}
8091
{appBar}
8192
</>
@@ -89,17 +100,27 @@ AppBar.propTypes = {
89100
classes: PropTypes.object,
90101

91102
/**
92-
* Set as `true` if the AppBar should be fixed position.
103+
* Affixes the AppBar to the top of the viewport. This prop is deprecated.
104+
* Use `variant="fixed"` instead.
105+
* @deprecated
93106
*/
94107
fixed: PropTypes.bool,
95108

96109
/**
97110
* String or Element to render within the offline warning container at the top of the app.
98111
*/
99112
offlineWarning: PropTypes.oneOfType([PropTypes.string, PropTypes.element]),
113+
114+
/**
115+
* * relative - The AppBar stays at the top of the page.
116+
* * fixed - The AppBar stays at the top of the viewport.
117+
* * hide - The same as fixed, but the app bar automatically hides when the user scrolls down.
118+
*/
119+
variant: PropTypes.oneOf(['relative', 'fixed', 'hide']),
100120
}
101121

102122
AppBar.defaultProps = {
103123
offlineWarning: 'Your device lost its internet connection.',
124+
variant: 'hide',
104125
fixed: false,
105126
}

test/AppBar.test.js

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,11 +14,11 @@ jest.useFakeTimers()
1414
describe('AppBar', () => {
1515
let wrapper
1616

17-
const Test = ({ offline = false, offlineMessage, fixed = false }) => {
17+
const Test = ({ offline = false, offlineMessage, ...others }) => {
1818
return (
1919
<PWAContext.Provider value={{ offline }}>
2020
<MuiThemeProvider theme={theme}>
21-
<AppBar offlineWarning={offlineMessage} fixed={fixed} />
21+
<AppBar offlineWarning={offlineMessage} {...others} />
2222
</MuiThemeProvider>
2323
</PWAContext.Provider>
2424
)
@@ -46,6 +46,11 @@ describe('AppBar', () => {
4646
expect(wrapper.find(Slide)).toHaveLength(0)
4747
})
4848

49+
it('should accept variant="relative"', () => {
50+
wrapper = mount(<Test variant="relative" />)
51+
expect(wrapper.find(Slide)).toHaveLength(0)
52+
})
53+
4954
// it('should not have any classes except wrap when not scrolled', async () => {
5055
// window.scrollY = 0
5156

0 commit comments

Comments
 (0)