Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix #2042 - Add scroll-snap like behaviour on landing page #2080

Merged
merged 1 commit into from
Apr 7, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion 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.2",
"jwt-decode": "^3.1.2",
Expand All @@ -22,7 +23,9 @@
"react": "^17.0.2",
"react-dom": "^17.0.2",
"react-use": "^17.2.1",
"swr": "^0.5.5"
"smoothscroll-polyfill": "^0.4.4",
"swr": "^0.5.5",
"valid-url": "^1.0.9"
},
"devDependencies": {
"@types/node": "^14.14.37",
Expand Down
58 changes: 47 additions & 11 deletions src/web/src/components/Banner.tsx
Original file line number Diff line number Diff line change
@@ -1,9 +1,16 @@
import { useEffect, useState } from 'react';
import { makeStyles, Theme, createStyles, Fab, Typography } from '@material-ui/core';
import { useEffect, useState, useRef } from 'react';
import {
makeStyles,
Theme,
createStyles,
Typography,
useScrollTrigger,
Fab,
} from '@material-ui/core';
import smoothscroll from 'smoothscroll-polyfill';
import KeyboardArrowDownIcon from '@material-ui/icons/KeyboardArrowDown';
import { telescopeUrl } from '../config';
import BannerDynamicItems from './BannerDynamicItems';
import ScrollAction from './ScrollAction';
import LandingButtons from './BannerButtons';

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

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

const toTimelineTrigger = useScrollTrigger({
threshold: 50,
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 +146,11 @@ export default function Banner() {
}
}
getGitData();
}, [telescopeUrl]);
}, []);

return (
<>
<div className={classes.heroBanner}>
<div className={classes.heroBanner} ref={bannerAnchor}>
<BannerDynamicItems />
<LandingButtons />
</div>
Expand All @@ -134,13 +172,11 @@ export default function Banner() {
>
v {gitInfo.version}
</a>
<ScrollAction>
<Fab color="primary" aria-label="scroll-down">
<KeyboardArrowDownIcon className={classes.arrowDownIcon} />
</Fab>
</ScrollAction>
<Fab color="primary" aria-label="scroll-down">
<KeyboardArrowDownIcon className={classes.arrowDownIcon} />
</Fab>
</div>
<div className={classes.anchor} id="posts-anchor" />
<div className={classes.anchor} id="posts-anchor" ref={timelineAnchor} />
</>
);
}