forked from Mirrors/gomuks
web/contentvisibility: work around another chrome bug
This commit is contained in:
parent
2a89f10589
commit
249dffaa2d
1 changed files with 7 additions and 3 deletions
|
@ -15,7 +15,7 @@
|
||||||
// along with this program. If not, see <https://www.gnu.org/licenses/>.
|
// along with this program. If not, see <https://www.gnu.org/licenses/>.
|
||||||
import { RefObject, useLayoutEffect, useRef, useState } from "react"
|
import { RefObject, useLayoutEffect, useRef, useState } from "react"
|
||||||
|
|
||||||
export default function useContentVisibilit<T extends HTMLElement>(
|
export default function useContentVisibility<T extends HTMLElement>(
|
||||||
allowRevert = false,
|
allowRevert = false,
|
||||||
): [boolean, RefObject<T | null>] {
|
): [boolean, RefObject<T | null>] {
|
||||||
const ref = useRef<T>(null)
|
const ref = useRef<T>(null)
|
||||||
|
@ -25,13 +25,17 @@ export default function useContentVisibilit<T extends HTMLElement>(
|
||||||
if (!element) {
|
if (!element) {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
const listener = (evt: unknown) => {
|
const listener = ((evt: ContentVisibilityAutoStateChangeEvent) => {
|
||||||
|
if (evt.target !== evt.currentTarget) {
|
||||||
|
// Workaround for chrome bug https://issues.chromium.org/issues/365168180
|
||||||
|
return
|
||||||
|
}
|
||||||
if (!(evt as ContentVisibilityAutoStateChangeEvent).skipped) {
|
if (!(evt as ContentVisibilityAutoStateChangeEvent).skipped) {
|
||||||
setVisible(true)
|
setVisible(true)
|
||||||
} else if (allowRevert) {
|
} else if (allowRevert) {
|
||||||
setVisible(false)
|
setVisible(false)
|
||||||
}
|
}
|
||||||
}
|
}) as (evt: unknown) => void
|
||||||
element.addEventListener("contentvisibilityautostatechange", listener)
|
element.addEventListener("contentvisibilityautostatechange", listener)
|
||||||
return () => element.removeEventListener("contentvisibilityautostatechange", listener)
|
return () => element.removeEventListener("contentvisibilityautostatechange", listener)
|
||||||
}, [allowRevert])
|
}, [allowRevert])
|
||||||
|
|
Loading…
Add table
Reference in a new issue