From 8f852604c104bf2ebfd113b11247f2c1975e6f29 Mon Sep 17 00:00:00 2001 From: Luis Rodrigues Date: Mon, 7 Aug 2023 17:39:34 +0200 Subject: [PATCH] refactor: removed delegated-events dependency --- package.json | 3 +-- src/dom/events.ts | 22 +++++++++++++++++----- yarn.lock | 2 +- 3 files changed, 19 insertions(+), 8 deletions(-) diff --git a/package.json b/package.json index 5b5657acd..ecc6750bc 100644 --- a/package.json +++ b/package.json @@ -100,8 +100,7 @@ "dependencies": { "@pacote/get-style": "2.0.1", "@pacote/pixels": "2.0.1", - "@pacote/throttle": "2.0.1", - "delegated-events": "^1.0.10" + "@pacote/throttle": "2.0.1" }, "resolutions": { "cypress": "12.17.3" diff --git a/src/dom/events.ts b/src/dom/events.ts index 00c3e7f8d..252186f0b 100644 --- a/src/dom/events.ts +++ b/src/dom/events.ts @@ -1,5 +1,4 @@ import { throttle } from '@pacote/throttle' -import { off, on } from 'delegated-events' import { UseCases, FootnoteAction } from '../use-cases' const FRAME = 16 @@ -42,6 +41,21 @@ const escapeHandler = (fn: () => void) => (event: KeyboardEvent) => { const onDocument = document.addEventListener const onWindow = window.addEventListener +const delegate = ( + type: string, + selector: string, + listener: EventListener, + options?: AddEventListenerOptions, +) => { + const handler = (event: Event) => { + const target = event.target as Element | null + if (target?.closest(selector)) { + listener.call(event.target, event) + } + } + document.addEventListener(type, handler, options) +} + export function addListeners(useCases: UseCases): () => void { const controller = new AbortController() const { signal } = controller @@ -58,12 +72,10 @@ export function addListeners(useCases: UseCases): () => void { onDocument('gestureend', throttledReposition, { signal }) onWindow('scroll', throttledReposition, { signal }) onWindow('resize', throttledResize, { signal }) - on('mouseover', SELECTOR_FOOTNOTE, showOnHover) - on('mouseout', SELECTOR_FOOTNOTE, hideOnHover) + delegate('mouseover', SELECTOR_FOOTNOTE, showOnHover, { signal }) + delegate('mouseout', SELECTOR_FOOTNOTE, hideOnHover, { signal }) return () => { controller.abort() - off('mouseover', SELECTOR_FOOTNOTE, showOnHover) - off('mouseout', SELECTOR_FOOTNOTE, hideOnHover) } } diff --git a/yarn.lock b/yarn.lock index 5d0640aa4..4413de8dd 100644 --- a/yarn.lock +++ b/yarn.lock @@ -2903,7 +2903,7 @@ delayed-stream@~1.0.0: resolved "https://registry.yarnpkg.com/delayed-stream/-/delayed-stream-1.0.0.tgz#df3ae199acadfb7d440aaae0b29e2272b24ec619" integrity sha512-ZySD7Nf91aLB0RxL4KGrKHBXl7Eds1DAmEdcoVawXnLD7SDhpNgtuII2aAkg7a7QS41jxPSZ17p4VdGnMHk3MQ== -delegated-events@*, delegated-events@^1.0.10: +delegated-events@*: version "1.1.2" resolved "https://registry.yarnpkg.com/delegated-events/-/delegated-events-1.1.2.tgz#741a4ba320718096fb8f7db4ddfaadcb0daa5986" integrity sha512-PjEw4Z5mrvXimyf60HGqMJ3BMcqqWZpBqSDGnEMBdkAtF4P7ltoa6N+UJXhbMMUxPLYFtykCmRs6Ra8I8pwzPQ==