diff --git a/app/javascript/retrospring/features/answerbox/index.ts b/app/javascript/retrospring/features/answerbox/index.ts index 9f836842..846a1ca8 100644 --- a/app/javascript/retrospring/features/answerbox/index.ts +++ b/app/javascript/retrospring/features/answerbox/index.ts @@ -1,17 +1,10 @@ -import registerEvents from 'utilities/registerEvents'; -import { createShareEvent } from './share'; +import { on } from 'retrospring/utilities/on'; +import { shareEventHandler } from './share'; export default (): void => { if ('share' in navigator) { document.body.classList.add('cap-web-share'); - const entries: NodeList = document.querySelectorAll('.answerbox:not(.js-initialized)'); - entries.forEach((element: HTMLElement) => { - registerEvents([ - { type: 'click', target: element.querySelector('[name=ab-share]'), handler: createShareEvent(element) } - ]); - - element.classList.add('js-initialized'); - }); + on('click', '[name=ab-share]', shareEventHandler); } } diff --git a/app/javascript/retrospring/features/answerbox/share.ts b/app/javascript/retrospring/features/answerbox/share.ts index 34c2ba54..f623ec75 100644 --- a/app/javascript/retrospring/features/answerbox/share.ts +++ b/app/javascript/retrospring/features/answerbox/share.ts @@ -1,11 +1,11 @@ import noop from 'utilities/noop'; -export function createShareEvent(answerbox: HTMLElement): () => void { - return function (): void { - navigator.share({ - url: answerbox.querySelector('.answerbox__answer-date > a, a.answerbox__permalink').href - }) +export function shareEventHandler(event: Event): void { + const answerbox = (event.target as HTMLElement).closest('.answerbox'); + + navigator.share({ + url: answerbox.querySelector('.answerbox__answer-date > a, a.answerbox__permalink').href + }) .then(noop) .catch(noop) - } }