Skip to content

Commit

Permalink
refactor: removed delegated-events dependency
Browse files Browse the repository at this point in the history
  • Loading branch information
lrodrigues-newstore committed Aug 7, 2023
1 parent 4c3fb34 commit 8f85260
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 8 deletions.
3 changes: 1 addition & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
22 changes: 17 additions & 5 deletions src/dom/events.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import { throttle } from '@pacote/throttle'
import { off, on } from 'delegated-events'
import { UseCases, FootnoteAction } from '../use-cases'

const FRAME = 16
Expand Down Expand Up @@ -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
Expand All @@ -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)
}
}
2 changes: 1 addition & 1 deletion yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -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==
Expand Down

0 comments on commit 8f85260

Please sign in to comment.