Skip to content

Commit

Permalink
feat: dismiss a footnote when touching outside it (#1692)
Browse files Browse the repository at this point in the history
  • Loading branch information
jminer authored Sep 27, 2023
1 parent 056d93d commit 49a6157
Show file tree
Hide file tree
Showing 4 changed files with 69 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
56 changes: 56 additions & 0 deletions test/options/dismissOnDocumentTouch.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
import { test, expect } from 'vitest'
import { fireEvent } from '@testing-library/dom'
import {
setDocumentBody,
getPopover,
waitToStopChanging,
getAllActiveButtons,
getButton,
} from '../helper'
import littlefoot from '../../src/littlefoot'

test('do not dismiss footnote when clicking the document body', async () => {
setDocumentBody('single.html')
littlefoot({ dismissOnDocumentTouch: false, activateDelay: 0, dismissDelay: 0 })

const button = getButton('1')
fireEvent.click(button)
await waitToStopChanging(button)
const popover = getPopover('1')

fireEvent.click(document.body)

expect(popover).toBeInTheDocument()
expect(button).toHaveClass('is-active')
})

test('dismiss footnote when clicking the button again', async () => {
setDocumentBody('single.html')
littlefoot({ dismissOnDocumentTouch: false, activateDelay: 0, dismissDelay: 0 })
const button = getButton('1')
fireEvent.click(button)
await waitToStopChanging(button)

fireEvent.click(button)

expect(button).toHaveClass('is-changing')
await waitToStopChanging(button)
const popover = getPopover('1')
expect(popover).not.toBeInTheDocument()
expect(button).not.toHaveClass('is-active')
})

test('disallow multiple activations', async () => {
setDocumentBody('default.html')
littlefoot({ dismissOnDocumentTouch: false, activateDelay: 0, dismissDelay: 0 })

const one = getButton('1')
fireEvent.click(one)
await waitToStopChanging(one)

const two = getButton('2')
fireEvent.click(two)
await waitToStopChanging(two)

expect(getAllActiveButtons()).toEqual([two])
})

0 comments on commit 49a6157

Please sign in to comment.