Skip to content

Commit

Permalink
[AppBar] Hide and Elevate on Scroll (#15522)
Browse files Browse the repository at this point in the history
* [AppBar] Hide an Elevate on Scroll

* [useSCrollTrigger] Fix test

* [useScrollTrigger] Fix test

* [useScrollTrigger] Add tests and set default values

* [useScrollTrigger] Add test error message info

* [useScrollTrigger] Add clock to tests

* Update docs/src/pages/demos/app-bar/app-bar.md

Co-Authored-By: cvanem <chris@greenlinkservices.com>

* Update docs/src/pages/demos/app-bar/app-bar.md

Co-Authored-By: cvanem <chris@greenlinkservices.com>

* [Docuementation] Remove duplicate App Bar elevate demo

* [Documentiation] Remove unused props

* [Documentation] Polish comments

* [useScrollTrigger,Documentation] Update demos and prop names

* Remove typescript demos

* [AppBar] Revert PaperProps property addition

* [AppBar] Revert typescript PaperProp changes

* [Documentation] Revert PaperProps changes

* [useScrollTrigger] Fixing tests

* Tests

* [useScrollTrigger] Filter Chrome OS X browser tests

* [useScrollTrigger] Seperate onTrigger  event

* Update prop types and typings

* Remove setTarget useEffect dependency

* Move trigger options to state, update typings

* Update typings

* Simplify demo

* Update scrollTrigger state logic and typings

* Update typings

* Simplify getScrollY

* Fix typing

* Fix getScrollY

* change the API

* Typos
  • Loading branch information
cvanem authored and oliviertassinari committed May 4, 2019
1 parent 4516caf commit 9b1bb8a
Show file tree
Hide file tree
Showing 38 changed files with 639 additions and 26 deletions.
2 changes: 1 addition & 1 deletion docs/src/modules/components/AppFrame.js
Original file line number Diff line number Diff line change
Expand Up @@ -252,7 +252,7 @@ class AppFrame extends React.Component {
<MenuIcon />
</IconButton>
{title !== null && (
<Typography className={classes.title} variant="h6" color="inherit" noWrap>
<Typography className={classes.title} variant="h6" noWrap>
{title}
</Typography>
)}
Expand Down
1 change: 0 additions & 1 deletion docs/src/modules/components/AppTableOfContents.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@ const styles = theme => ({
flexShrink: 0,
order: 2,
position: 'sticky',
wordBreak: 'break-all',
height: 'calc(100vh - 70px - 29px)',
overflowY: 'auto',
padding: theme.spacing(2, 2, 2, 0),
Expand Down
2 changes: 2 additions & 0 deletions docs/src/modules/components/DemoFrame.js
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ class DemoFrame extends React.Component {
}),
sheetsManager: new Map(),
container: this.contentDocument.body,
window: () => this.contentWindow,
});
};

Expand All @@ -60,6 +61,7 @@ class DemoFrame extends React.Component {
<StylesProvider jss={this.state.jss} sheetsManager={this.state.sheetsManager}>
{React.cloneElement(children, {
container: this.state.container,
window: this.state.window,
})}
</StylesProvider>
) : null}
Expand Down
2 changes: 1 addition & 1 deletion docs/src/pages/demos/app-bar/ButtonAppBar.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ function ButtonAppBar() {
<IconButton edge="start" className={classes.menuButton} color="inherit" aria-label="Menu">
<MenuIcon />
</IconButton>
<Typography variant="h6" color="inherit" className={classes.title}>
<Typography variant="h6" className={classes.title}>
News
</Typography>
<Button color="inherit">Login</Button>
Expand Down
2 changes: 1 addition & 1 deletion docs/src/pages/demos/app-bar/ButtonAppBar.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ function ButtonAppBar() {
<IconButton edge="start" className={classes.menuButton} color="inherit" aria-label="Menu">
<MenuIcon />
</IconButton>
<Typography variant="h6" color="inherit" className={classes.title}>
<Typography variant="h6" className={classes.title}>
News
</Typography>
<Button color="inherit">Login</Button>
Expand Down
62 changes: 62 additions & 0 deletions docs/src/pages/demos/app-bar/ElevateAppBar.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
/* eslint-disable prefer-spread */

import React from 'react';
import PropTypes from 'prop-types';
import AppBar from '@material-ui/core/AppBar';
import Toolbar from '@material-ui/core/Toolbar';
import Typography from '@material-ui/core/Typography';
import CssBaseline from '@material-ui/core/CssBaseline';
import useScrollTrigger from '@material-ui/core/useScrollTrigger';
import Box from '@material-ui/core/Box';
import Container from '@material-ui/core/Container';

function ElevationScroll(props) {
const { children, window } = props;
// Note that you normally won't need to set the window ref as useScrollTrigger
// will default to window.
// This is only being set here because the demo is in an iframe.
const trigger = useScrollTrigger({
disableHysteresis: true,
threshold: 0,
target: window ? window() : undefined,
});

return React.cloneElement(children, {
elevation: trigger ? 4 : 0,
});
}

ElevationScroll.propTypes = {
children: PropTypes.node.isRequired,
// Injected by the documentation to work in an iframe.
// You won't need it on your project.
window: PropTypes.func,
};

export default function ElevateAppBar(props) {
return (
<React.Fragment>
<CssBaseline />
<ElevationScroll {...props}>
<AppBar>
<Toolbar>
<Typography variant="h6">Scroll to Elevate App Bar</Typography>
</Toolbar>
</AppBar>
</ElevationScroll>
<Toolbar />
<Container>
<Box my={2}>
{Array.apply(null, Array(12))
.map(
() => `Cras mattis consectetur purus sit amet fermentum.
Cras justo odio, dapibus ac facilisis in, egestas eget quam.
Morbi leo risus, porta ac consectetur ac, vestibulum at eros.
Praesent commodo cursus magna, vel scelerisque nisl consectetur et.`,
)
.join('\n')}
</Box>
</Container>
</React.Fragment>
);
}
67 changes: 67 additions & 0 deletions docs/src/pages/demos/app-bar/ElevateAppBar.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
/* eslint-disable prefer-spread */

import React from 'react';
import PropTypes from 'prop-types';
import AppBar from '@material-ui/core/AppBar';
import Toolbar from '@material-ui/core/Toolbar';
import Typography from '@material-ui/core/Typography';
import CssBaseline from '@material-ui/core/CssBaseline';
import useScrollTrigger from '@material-ui/core/useScrollTrigger';
import Box from '@material-ui/core/Box';
import Container from '@material-ui/core/Container';

interface Props {
window?: () => Window;
children: React.ReactElement;
}

function ElevationScroll(props: Props) {
const { children, window } = props;
// Note that you normally won't need to set the window ref as useScrollTrigger
// will default to window.
// This is only being set here because the demo is in an iframe.
const trigger = useScrollTrigger({
disableHysteresis: true,
threshold: 0,
target: window ? window() : undefined,
});

return React.cloneElement(children, {
elevation: trigger ? 4 : 0,
});
}

ElevationScroll.propTypes = {
children: PropTypes.node.isRequired,
// Injected by the documentation to work in an iframe.
// You won't need it on your project.
window: PropTypes.func,
};

export default function ElevateAppBar(props: Props) {
return (
<React.Fragment>
<CssBaseline />
<ElevationScroll {...props}>
<AppBar>
<Toolbar>
<Typography variant="h6">Scroll to Elevate App Bar</Typography>
</Toolbar>
</AppBar>
</ElevationScroll>
<Toolbar />
<Container>
<Box my={2}>
{Array.apply(null, Array(12))
.map(
() => `Cras mattis consectetur purus sit amet fermentum.
Cras justo odio, dapibus ac facilisis in, egestas eget quam.
Morbi leo risus, porta ac consectetur ac, vestibulum at eros.
Praesent commodo cursus magna, vel scelerisque nisl consectetur et.`,
)
.join('\n')}
</Box>
</Container>
</React.Fragment>
);
}
61 changes: 61 additions & 0 deletions docs/src/pages/demos/app-bar/HideAppBar.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
/* eslint-disable prefer-spread */

import React from 'react';
import PropTypes from 'prop-types';
import AppBar from '@material-ui/core/AppBar';
import Toolbar from '@material-ui/core/Toolbar';
import Typography from '@material-ui/core/Typography';
import CssBaseline from '@material-ui/core/CssBaseline';
import useScrollTrigger from '@material-ui/core/useScrollTrigger';
import Box from '@material-ui/core/Box';
import Container from '@material-ui/core/Container';
import Slide from '@material-ui/core/Slide';

function HideOnScroll(props) {
const { children, window } = props;
// Note that you normally won't need to set the window ref as useScrollTrigger
// will default to window.
// This is only being set here because the demo is in an iframe.
const trigger = useScrollTrigger({ target: window ? window() : undefined });

return (
<Slide appear={false} direction="down" in={!trigger}>
{children}
</Slide>
);
}

HideOnScroll.propTypes = {
children: PropTypes.node.isRequired,
// Injected by the documentation to work in an iframe.
// You won't need it on your project.
window: PropTypes.func,
};

export default function HideAppBar(props) {
return (
<React.Fragment>
<CssBaseline />
<HideOnScroll {...props}>
<AppBar>
<Toolbar>
<Typography variant="h6">Scroll to Hide App Bar</Typography>
</Toolbar>
</AppBar>
</HideOnScroll>
<Toolbar />
<Container>
<Box my={2}>
{Array.apply(null, Array(12))
.map(
() => `Cras mattis consectetur purus sit amet fermentum.
Cras justo odio, dapibus ac facilisis in, egestas eget quam.
Morbi leo risus, porta ac consectetur ac, vestibulum at eros.
Praesent commodo cursus magna, vel scelerisque nisl consectetur et.`,
)
.join('\n')}
</Box>
</Container>
</React.Fragment>
);
}
66 changes: 66 additions & 0 deletions docs/src/pages/demos/app-bar/HideAppBar.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
/* eslint-disable prefer-spread */

import React from 'react';
import PropTypes from 'prop-types';
import AppBar from '@material-ui/core/AppBar';
import Toolbar from '@material-ui/core/Toolbar';
import Typography from '@material-ui/core/Typography';
import CssBaseline from '@material-ui/core/CssBaseline';
import useScrollTrigger from '@material-ui/core/useScrollTrigger';
import Box from '@material-ui/core/Box';
import Container from '@material-ui/core/Container';
import Slide from '@material-ui/core/Slide';

interface Props {
window?: () => Window;
children: React.ReactElement;
}

function HideOnScroll(props: Props) {
const { children, window } = props;
// Note that you normally won't need to set the window ref as useScrollTrigger
// will default to window.
// This is only being set here because the demo is in an iframe.
const trigger = useScrollTrigger({ target: window ? window() : undefined });

return (
<Slide appear={false} direction="down" in={!trigger}>
{children}
</Slide>
);
}

HideOnScroll.propTypes = {
children: PropTypes.node.isRequired,
// Injected by the documentation to work in an iframe.
// You won't need it on your project.
window: PropTypes.func,
};

export default function HideAppBar(props: Props) {
return (
<React.Fragment>
<CssBaseline />
<HideOnScroll {...props}>
<AppBar>
<Toolbar>
<Typography variant="h6">Scroll to Hide App Bar</Typography>
</Toolbar>
</AppBar>
</HideOnScroll>
<Toolbar />
<Container>
<Box my={2}>
{Array.apply(null, Array(12))
.map(
() => `Cras mattis consectetur purus sit amet fermentum.
Cras justo odio, dapibus ac facilisis in, egestas eget quam.
Morbi leo risus, porta ac consectetur ac, vestibulum at eros.
Praesent commodo cursus magna, vel scelerisque nisl consectetur et.`,
)
.join('\n')}
</Box>
</Container>
</React.Fragment>
);
}
2 changes: 1 addition & 1 deletion docs/src/pages/demos/app-bar/MenuAppBar.js
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ function MenuAppBar() {
<IconButton edge="start" className={classes.menuButton} color="inherit" aria-label="Menu">
<MenuIcon />
</IconButton>
<Typography variant="h6" color="inherit" className={classes.title}>
<Typography variant="h6" className={classes.title}>
Photos
</Typography>
{auth && (
Expand Down
2 changes: 1 addition & 1 deletion docs/src/pages/demos/app-bar/MenuAppBar.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ function MenuAppBar() {
<IconButton edge="start" className={classes.menuButton} color="inherit" aria-label="Menu">
<MenuIcon />
</IconButton>
<Typography variant="h6" color="inherit" className={classes.title}>
<Typography variant="h6" className={classes.title}>
Photos
</Typography>
{auth && (
Expand Down
2 changes: 1 addition & 1 deletion docs/src/pages/demos/app-bar/PrimarySearchAppBar.js
Original file line number Diff line number Diff line change
Expand Up @@ -161,7 +161,7 @@ function PrimarySearchAppBar() {
>
<MenuIcon />
</IconButton>
<Typography className={classes.title} variant="h6" color="inherit" noWrap>
<Typography className={classes.title} variant="h6" noWrap>
Material-UI
</Typography>
<div className={classes.search}>
Expand Down
2 changes: 1 addition & 1 deletion docs/src/pages/demos/app-bar/PrimarySearchAppBar.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -163,7 +163,7 @@ function PrimarySearchAppBar() {
>
<MenuIcon />
</IconButton>
<Typography className={classes.title} variant="h6" color="inherit" noWrap>
<Typography className={classes.title} variant="h6" noWrap>
Material-UI
</Typography>
<div className={classes.search}>
Expand Down
2 changes: 1 addition & 1 deletion docs/src/pages/demos/app-bar/SearchAppBar.js
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ function SearchAppBar() {
>
<MenuIcon />
</IconButton>
<Typography className={classes.title} variant="h6" color="inherit" noWrap>
<Typography className={classes.title} variant="h6" noWrap>
Material-UI
</Typography>
<div className={classes.search}>
Expand Down
2 changes: 1 addition & 1 deletion docs/src/pages/demos/app-bar/SearchAppBar.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ function SearchAppBar() {
>
<MenuIcon />
</IconButton>
<Typography className={classes.title} variant="h6" color="inherit" noWrap>
<Typography className={classes.title} variant="h6" noWrap>
Material-UI
</Typography>
<div className={classes.search}>
Expand Down
Loading

0 comments on commit 9b1bb8a

Please sign in to comment.