Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: dismiss a footnote when touching outside it #1692

Merged
merged 1 commit into from
Sep 27, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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])
})