forked from Seneca-CDOT/telescope
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fixes: Seneca-CDOT#1229 Implemented an infinite scrolling without Loa…
…dMoreButton
- Loading branch information
Showing
2 changed files
with
60 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
import React from 'react'; | ||
import PropTypes from 'prop-types'; | ||
|
||
import { Container, Button, Grid } from '@material-ui/core'; | ||
import { makeStyles } from '@material-ui/core/styles'; | ||
|
||
const useStyles = makeStyles((theme) => ({ | ||
content: { | ||
'& > *': { | ||
padding: theme.spacing(2), | ||
bottom: theme.spacing(4), | ||
}, | ||
}, | ||
})); | ||
|
||
function LoadMoreButton({ onScroll }) { | ||
const classes = useStyles(); | ||
const $buttonRef = React.useRef(null); | ||
|
||
// This will make the automatic infinite scrolling feature | ||
// Once the "button" is on the viewport(shown on the window), | ||
// The new posts are updated(call onClick() -- setSize(size + 1) in Posts.jsx --) | ||
React.useEffect(() => { | ||
let options = { | ||
root: null, | ||
rootMargin: '0px', | ||
threshold: 1.0, | ||
}; | ||
|
||
let observer = new IntersectionObserver((entries, observer) => { | ||
entries.forEach((entry) => { | ||
if (entry.isIntersecting) { | ||
onScroll(); | ||
} | ||
}); | ||
}, options); | ||
observer.observe($buttonRef.current); | ||
|
||
return () => { | ||
observer.unobserve($buttonRef.current); | ||
}; | ||
}, []); | ||
|
||
return ( | ||
<Container> | ||
<Grid item xs={12} className={classes.content}> | ||
<Button ref={$buttonRef}>Load More Posts</Button> | ||
</Grid> | ||
</Container> | ||
); | ||
} | ||
|
||
LoadMoreButton.propTypes = { | ||
onClick: PropTypes.func, | ||
}; | ||
|
||
export default LoadMoreButton; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters