Skip to content

Fix/scroll #395

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

Merged
merged 4 commits into from
Jul 20, 2020
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
detect page bottom
Signed-off-by: shmck <shawn.j.mckay@gmail.com>
  • Loading branch information
ShMcK committed Jul 20, 2020
commit 96ef687f2aa363c6755a6fbdfee344923eb7f399
1 change: 0 additions & 1 deletion web-app/src/containers/Tutorial/components/Level.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@ const styles = {
display: 'flex' as 'flex',
flexDirection: 'column' as 'column',
padding: 0,
paddingBottom: '5rem',
},

text: {
Expand Down
48 changes: 46 additions & 2 deletions web-app/src/containers/Tutorial/components/ScrollContent.tsx
Original file line number Diff line number Diff line change
@@ -1,26 +1,70 @@
import * as React from 'react'

const styles = {
scrollIndicator: {
position: 'fixed' as 'fixed',
bottom: '2rem',
},
}

type Props = {
item: string
children: React.ReactElement
}

const ScrollContent = ({ item, children }: Props) => {
const [showScrollIndicator, setShowScrollIndicator] = React.useState(false)
const pageTopRef: React.RefObject<any> = React.useRef(null)

const pageBottomRef: React.RefObject<any> = React.useRef(null)

const scrollToTop = () => {
pageTopRef.current.scrollIntoView({ behavior: 'smooth' })
let hideTimeout: any

// API to detect if an HTML element is in the viewport
// https://developer.mozilla.org/en-US/docs/Web/API/Intersection_Observer_API
const observer = new IntersectionObserver(
([entry]) => {
// show a scroll indicator to let the user know
// they can scroll down for more
const isVisible = !entry.isIntersecting
setShowScrollIndicator(isVisible)
if (!isVisible) {
hideTimeout = setTimeout(() => {
setShowScrollIndicator(false)
}, 3000)
}
},
{
rootMargin: '0px',
},
)

const showTimeout = setTimeout(() => {
// detect if bottom of page is visible

if (pageBottomRef.current) {
observer.observe(pageBottomRef.current)
}
}, 300)
return () => {
// cleanup timeouts & subs
observer.unobserve(pageBottomRef.current)
clearTimeout(showTimeout)
clearTimeout(hideTimeout)
}
}

React.useEffect(scrollToTop, [item])

console.log(`showScrollIndicator = ${showScrollIndicator}`)

return (
<>
<div ref={pageTopRef} />
{children}
<div ref={pageBottomRef} />
{showScrollIndicator ? <div style={styles.scrollIndicator}>MORE</div> : null}
<div ref={pageBottomRef}>BOTTOM</div>
</>
)
}
Expand Down
5 changes: 4 additions & 1 deletion web-app/src/containers/Tutorial/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,9 @@ import Continue from './components/Continue'
import ScrollContent from './components/ScrollContent'

const styles = {
page: {
paddingBottom: '5rem',
},
header: {
display: 'flex' as 'flex',
alignItems: 'center',
Expand Down Expand Up @@ -113,7 +116,7 @@ const TutorialPage = (props: PageProps) => {

return (
<div>
<div>
<div css={styles.page}>
<div css={styles.header}>
<a onClick={() => setMenuVisible(!menuVisible)}>
<Icon type="toggle-left" size="small" style={{ color: '#333' }} />
Expand Down