/
githubmirror
/
material-ui
Обзор
Документация
Войти
/
githubmirror
/
material-ui
Код
Запросы
0
Пакеты
0
Релизы
0
Аналитика
Безопасность
master
docs/data/material/components/app-bar/BackToTop.js
90 строк
2 KB
Marija Najdova
[AppBar] Convert to support CSS extraction (#41247)
01 апр 2024, 10:48
Не верифицирован
01 апр 2024, 10:48
67663dd
Код
Авторство
О чём код?
import * as React from 'react'; import PropTypes from 'prop-types'; import AppBar from '@mui/material/AppBar'; import Toolbar from '@mui/material/Toolbar'; import Typography from '@mui/material/Typography'; import CssBaseline from '@mui/material/CssBaseline'; import useScrollTrigger from '@mui/material/useScrollTrigger'; import Box from '@mui/material/Box'; import Container from '@mui/material/Container'; import Fab from '@mui/material/Fab'; import KeyboardArrowUpIcon from '@mui/icons-material/KeyboardArrowUp'; import Fade from '@mui/material/Fade'; function ScrollTop(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, disableHysteresis: true, threshold: 100, }); const handleClick = (event) => { const anchor = (event.target.ownerDocument || document).querySelector( '#back-to-top-anchor', ); if (anchor) { anchor.scrollIntoView({ block: 'center', }); } }; return ( <Fade in={trigger}> <Box onClick={handleClick} role="presentation" sx={{ position: 'fixed', bottom: 16, right: 16 }} > {children} </Box> </Fade> ); } ScrollTop.propTypes = { children: PropTypes.element, /** * Injected by the documentation to work in an iframe. * You won't need it on your project. */ window: PropTypes.func, }; export default function BackToTop(props) { return ( <React.Fragment> <CssBaseline /> <AppBar> <Toolbar> <Typography variant="h6" component="div"> Scroll to see button </Typography> </Toolbar> </AppBar> <Toolbar id="back-to-top-anchor" /> <Container> <Box sx={{ my: 2 }}> {[...new 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> <ScrollTop {...props}> <Fab size="small" aria-label="scroll back to top"> <KeyboardArrowUpIcon /> </Fab> </ScrollTop> </React.Fragment> ); }