Skip to content

Commit

Permalink
Fix #2042 - Add scroll-snap like behaviour on landing page
Browse files Browse the repository at this point in the history
  • Loading branch information
DukeManh committed Apr 5, 2021
1 parent 8949d34 commit 25ce40b
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 18 deletions.
2 changes: 2 additions & 0 deletions src/web/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
"@material-ui/icons": "^4.11.2",
"@mdx-js/loader": "^1.6.22",
"@next/mdx": "^10.1.3",
"@types/smoothscroll-polyfill": "^0.3.1",
"dotenv": "^8.2.0",
"highlight.js": "10.7.1",
"jwt-decode": "^3.1.2",
Expand All @@ -24,6 +25,7 @@
"react-dom": "^17.0.2",
"react-material-ui-form-validator": "^2.1.4",
"react-use": "^17.2.1",
"smoothscroll-polyfill": "^0.4.4",
"swr": "^0.5.5",
"valid-url": "^1.0.9"
},
Expand Down
62 changes: 44 additions & 18 deletions src/web/src/components/Banner.tsx
Original file line number Diff line number Diff line change
@@ -1,9 +1,15 @@
import { useEffect, useState } from 'react';
import { makeStyles, Theme, createStyles, Fab, Grid, Typography } from '@material-ui/core';
import KeyboardArrowDownIcon from '@material-ui/icons/KeyboardArrowDown';
import { useEffect, useState, useRef } from 'react';
import {
makeStyles,
Theme,
createStyles,
Grid,
Typography,
useScrollTrigger,
} from '@material-ui/core';
import smoothscroll from 'smoothscroll-polyfill';
import { telescopeUrl } from '../config';
import BannerDynamicItems from './BannerDynamicItems';
import ScrollAction from './ScrollAction';

const useStyles = makeStyles((theme: Theme) =>
createStyles({
Expand Down Expand Up @@ -91,6 +97,36 @@ export default function Banner() {
version: '',
});

const timelineAnchor = useRef<HTMLDivElement>(null);
const bannerAnchor = useRef<HTMLDivElement>(null);

const toTimelineTrigger = useScrollTrigger({
disableHysteresis: true,
});
const toBannerTrigger = !useScrollTrigger({
threshold: (timelineAnchor.current?.offsetTop || 0) - 50,
disableHysteresis: true,
});

useEffect(() => {
if (window) {
// Apply smooth scroll polyfill on mobile
smoothscroll.polyfill();
}
}, []);

useEffect(() => {
if (toTimelineTrigger && timelineAnchor?.current) {
timelineAnchor.current.scrollIntoView({ behavior: 'smooth', block: 'start' });
}
}, [toTimelineTrigger]);

useEffect(() => {
if (toBannerTrigger && bannerAnchor?.current) {
bannerAnchor.current.scrollIntoView({ behavior: 'smooth', block: 'start' });
}
}, [toBannerTrigger]);

useEffect(() => {
async function getGitData() {
try {
Expand All @@ -108,11 +144,11 @@ export default function Banner() {
}
}
getGitData();
}, [telescopeUrl]);
}, []);

return (
<>
<div className={classes.heroBanner}>
<div className={classes.heroBanner} ref={bannerAnchor}>
<BannerDynamicItems />

<Typography variant="h1" className={classes.h1}>
Expand All @@ -134,18 +170,8 @@ export default function Banner() {
alignItems="center"
justify="center"
className={classes.container}
>
<Grid id="back-to-top-anchor-mobile" item xs={8}>
<div className={classes.icon}>
<ScrollAction>
<Fab color="secondary" aria-label="scroll-down">
<KeyboardArrowDownIcon className={classes.arrowDownIcon} />
</Fab>
</ScrollAction>
</div>
</Grid>
</Grid>
<div className={classes.anchor} id="back-to-top-anchor" />
/>
<div className={classes.anchor} id="timeline-anchor" ref={timelineAnchor} />
</>
);
}

0 comments on commit 25ce40b

Please sign in to comment.