read progress update
- update progress on page height change - make use of useCallback
This commit is contained in:
parent
9c42cb73a6
commit
7baef539fd
1 changed files with 9 additions and 3 deletions
|
@ -1,4 +1,4 @@
|
||||||
import { useEffect, useState } from "react"
|
import { useCallback, useEffect, useState } from "react"
|
||||||
import styled from "styled-components"
|
import styled from "styled-components"
|
||||||
import { Link, useLocation } from "react-router-dom"
|
import { Link, useLocation } from "react-router-dom"
|
||||||
import ReactTooltip from "react-tooltip"
|
import ReactTooltip from "react-tooltip"
|
||||||
|
@ -86,14 +86,20 @@ const ReadProgress = () => {
|
||||||
const location = useLocation()
|
const location = useLocation()
|
||||||
|
|
||||||
// https://stackoverflow.com/a/8028584/12979111
|
// https://stackoverflow.com/a/8028584/12979111
|
||||||
function scrollHandler() {
|
const scrollHandler = useCallback(() => {
|
||||||
setScroll(((h[st] || b[st]) / ((h[sh] || b[sh]) - h.clientHeight)) * 100)
|
setScroll(((h[st] || b[st]) / ((h[sh] || b[sh]) - h.clientHeight)) * 100)
|
||||||
}
|
}, [])
|
||||||
|
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
|
const resizeObserver = new ResizeObserver(() => {
|
||||||
|
scrollHandler()
|
||||||
|
})
|
||||||
|
|
||||||
|
resizeObserver.observe(document.body)
|
||||||
window.addEventListener("scroll", scrollHandler)
|
window.addEventListener("scroll", scrollHandler)
|
||||||
|
|
||||||
return () => {
|
return () => {
|
||||||
|
resizeObserver.disconnect()
|
||||||
window.removeEventListener("scroll", scrollHandler)
|
window.removeEventListener("scroll", scrollHandler)
|
||||||
}
|
}
|
||||||
}, [])
|
}, [])
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue