cleanup event listeners with solid-primitives/event-listener (#20619)

This commit is contained in:
Brendan Allan
2026-04-02 17:40:03 +08:00
committed by GitHub
parent 327f62526a
commit 69d047ae7d
22 changed files with 102 additions and 176 deletions

View File

@@ -1,5 +1,6 @@
import { createEffect, on, onCleanup } from "solid-js"
import { createEffect, createSignal, on, onCleanup } from "solid-js"
import { createStore } from "solid-js/store"
import { makeEventListener } from "@solid-primitives/event-listener"
import { createResizeObserver } from "@solid-primitives/resize-observer"
export interface AutoScrollOptions {
@@ -14,7 +15,6 @@ export function createAutoScroll(options: AutoScrollOptions) {
let settling = false
let settleTimer: ReturnType<typeof setTimeout> | undefined
let autoTimer: ReturnType<typeof setTimeout> | undefined
let cleanup: (() => void) | undefined
let auto: { top: number; time: number } | undefined
const threshold = () => options.bottomThreshold ?? 10
@@ -216,26 +216,14 @@ export function createAutoScroll(options: AutoScrollOptions) {
onCleanup(() => {
if (settleTimer) clearTimeout(settleTimer)
if (autoTimer) clearTimeout(autoTimer)
if (cleanup) cleanup()
})
return {
scrollRef: (el: HTMLElement | undefined) => {
if (cleanup) {
cleanup()
cleanup = undefined
}
scroll = el
if (!el) return
updateOverflowAnchor(el)
el.addEventListener("wheel", handleWheel, { passive: true })
cleanup = () => {
el.removeEventListener("wheel", handleWheel)
}
makeEventListener(el, "wheel", handleWheel, { passive: true })
},
contentRef: (el: HTMLElement | undefined) => setStore("contentRef", el),
handleScroll,