|
| 1 | +import React, { useState, useRef } from 'react'; |
| 2 | +import PropTypes from 'prop-types'; |
| 3 | +import { makeStyles } from '@material-ui/core/styles'; |
| 4 | +import { useRouter } from 'next/router'; |
| 5 | + |
| 6 | +import Button from '@material-ui/core/Button'; |
| 7 | +import ClickAwayListener from '@material-ui/core/ClickAwayListener'; |
| 8 | +import Grow from '@material-ui/core/Grow'; |
| 9 | +import Paper from '@material-ui/core/Paper'; |
| 10 | +import Popper from '@material-ui/core/Popper'; |
| 11 | +import MenuItem from '@material-ui/core/MenuItem'; |
| 12 | +import MenuList from '@material-ui/core/MenuList'; |
| 13 | + |
| 14 | +import Grid from '@material-ui/core/Grid'; |
| 15 | +import Hidden from '@material-ui/core/Hidden'; |
| 16 | + |
| 17 | +import KeyboardArrowDownIcon from '@material-ui/icons/KeyboardArrowDown'; |
| 18 | +import KeyboardArrowRightIcon from '@material-ui/icons/KeyboardArrowRight'; |
| 19 | + |
| 20 | +import { headerToId } from './list-of-contents'; |
| 21 | + |
| 22 | +const reqSource = require.context('!raw-loader!@docs/pages', true, /\.md/); |
| 23 | + |
| 24 | +const itemStyles = makeStyles(() => ({ |
| 25 | + a: { |
| 26 | + textDecoration: 'none', |
| 27 | + color: 'inherit' |
| 28 | + } |
| 29 | +})); |
| 30 | + |
| 31 | +const Item = ({ text, setOpen }) => { |
| 32 | + const router = useRouter(); |
| 33 | + const labelText = text.replace(/#/g, ''); |
| 34 | + const styles = itemStyles(); |
| 35 | + |
| 36 | + return ( |
| 37 | + <MenuItem onClick={() => setOpen(false)} component="a" className={styles.a} href={`${router.pathname}#${headerToId(text)}`} title={labelText}> |
| 38 | + {labelText} |
| 39 | + </MenuItem> |
| 40 | + ); |
| 41 | +}; |
| 42 | + |
| 43 | +Item.propTypes = { |
| 44 | + text: PropTypes.string.isRequired, |
| 45 | + setOpen: PropTypes.func.isRequired |
| 46 | +}; |
| 47 | + |
| 48 | +const contentStyles = makeStyles(() => ({ |
| 49 | + button: { |
| 50 | + paddingRight: 0, |
| 51 | + marginTop: '-24px' |
| 52 | + }, |
| 53 | + wrapper: { |
| 54 | + width: '100%', |
| 55 | + display: 'flex', |
| 56 | + justifyContent: 'flex-end' |
| 57 | + } |
| 58 | +})); |
| 59 | + |
| 60 | +const ListOfContents = ({ file }) => { |
| 61 | + const [open, setOpen] = useState(false); |
| 62 | + const styles = contentStyles(); |
| 63 | + const anchorRef = useRef(null); |
| 64 | + |
| 65 | + const handleListKeyDown = (event) => { |
| 66 | + if (event.key === 'Tab') { |
| 67 | + event.preventDefault(); |
| 68 | + setOpen(false); |
| 69 | + } |
| 70 | + }; |
| 71 | + |
| 72 | + const text = reqSource(`./${file}.md`).default; |
| 73 | + |
| 74 | + const regex = /^#+ .*/gm; |
| 75 | + const found = text.match(regex) || []; |
| 76 | + |
| 77 | + return ( |
| 78 | + <Grid item xs={12} md={false}> |
| 79 | + <Hidden implementation="css" mdUp> |
| 80 | + <div className={styles.wrapper}> |
| 81 | + <ClickAwayListener onClickAway={() => setOpen(false)}> |
| 82 | + <div> |
| 83 | + <Button |
| 84 | + ref={anchorRef} |
| 85 | + className={styles.button} |
| 86 | + aria-controls={open ? 'menu-list-grow' : undefined} |
| 87 | + aria-haspopup="true" |
| 88 | + onClick={() => setOpen((prevOpen) => !prevOpen)} |
| 89 | + endIcon={!open ? <KeyboardArrowRightIcon /> : <KeyboardArrowDownIcon />} |
| 90 | + > |
| 91 | + Show content |
| 92 | + </Button> |
| 93 | + <Popper open={open} role={undefined} transition disablePortal anchorEl={anchorRef.current}> |
| 94 | + {({ TransitionProps }) => ( |
| 95 | + <Grow {...TransitionProps} style={{ transformOrigin: 'center top' }}> |
| 96 | + <Paper> |
| 97 | + <MenuList autoFocusItem={open} id="menu-list-grow" onKeyDown={handleListKeyDown}> |
| 98 | + {found.map((text) => ( |
| 99 | + <Item key={text} text={text} setOpen={setOpen} /> |
| 100 | + ))} |
| 101 | + </MenuList> |
| 102 | + </Paper> |
| 103 | + </Grow> |
| 104 | + )} |
| 105 | + </Popper> |
| 106 | + </div> |
| 107 | + </ClickAwayListener> |
| 108 | + </div> |
| 109 | + </Hidden> |
| 110 | + </Grid> |
| 111 | + ); |
| 112 | +}; |
| 113 | + |
| 114 | +ListOfContents.propTypes = { |
| 115 | + file: PropTypes.string.isRequired |
| 116 | +}; |
| 117 | + |
| 118 | +export default ListOfContents; |
0 commit comments