Skip to content

Commit

Permalink
Add option for whether to dismiss a footnote when touching outside it
Browse files Browse the repository at this point in the history
This option is especially useful if the footnote is expanded inline
instead of in a popover because then it doesn't make sense to hide it
when clicking another place in the document.
  • Loading branch information
jminer committed Sep 5, 2023
1 parent 2955bcb commit b9fb437
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 2 deletions.
2 changes: 1 addition & 1 deletion src/dom/events.ts
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ const delegate = (
export function addListeners(useCases: UseCases): () => void {
const controller = new AbortController()
const { signal } = controller
const toggleOnTouch = touchHandler(useCases.toggle, useCases.dismissAll)
const toggleOnTouch = touchHandler(useCases.toggle, useCases.documentTouch)
const dismissOnEscape = escapeHandler(useCases.dismissAll)
const throttledReposition = throttle(useCases.repositionAll, FRAME)
const throttledResize = throttle(useCases.resizeAll, FRAME)
Expand Down
1 change: 1 addition & 0 deletions src/settings.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ export const DEFAULT_SETTINGS: Settings = {
anchorPattern: /(fn|footnote|note)[:\-_\d]/gi,
dismissDelay: 100,
dismissOnUnhover: false,
dismissOnDocumentTouch: true,
footnoteSelector: 'li',
hoverDelay: 250,
numberResetSelector: '',
Expand Down
12 changes: 11 additions & 1 deletion src/use-cases.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ export type UseCaseSettings<T> = Readonly<{
dismissCallback?: ActionCallback<T>
dismissDelay: number
dismissOnUnhover: boolean
dismissOnDocumentTouch: boolean
hoverDelay: number
}>

Expand All @@ -36,6 +37,7 @@ export type UseCases = Readonly<{
activate: DelayedFootnoteAction
dismiss: DelayedFootnoteAction
dismissAll: () => void
documentTouch: () => void
hover: FootnoteAction
repositionAll: () => void
resizeAll: () => void
Expand Down Expand Up @@ -82,12 +84,20 @@ export function createUseCases<T>(
}
}

const dismissAll = () => footnotes.forEach(dismiss(settings.dismissDelay))

return {
activate: (id, delay) => ifFound(activate(delay))(id),

dismiss: (id, delay) => ifFound(dismiss(delay))(id),

dismissAll: () => footnotes.forEach(dismiss(settings.dismissDelay)),
dismissAll,

documentTouch: () => {
if (settings.dismissOnDocumentTouch) {
dismissAll()
}
},

repositionAll: () => footnotes.forEach((current) => current.reposition()),

Expand Down

0 comments on commit b9fb437

Please sign in to comment.