Skip to content

Commit

Permalink
fix: immediately dismissing popovers on iOS (#1893)
Browse files Browse the repository at this point in the history
#57

I believe this bug happens because littlefoot handles both `touchend`
and `click` event types with the same handler. On iOS and possibly other
touch browsers, both events could be delivered each time the user taps
the screen. The best practice is to call `event.preventDefault()` when
handling the touch event to prevent the subsequent simulated mouse event
from firing.
  • Loading branch information
kevin1 committed May 16, 2024
1 parent 5c7633e commit 98fd2d3
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 1 deletion.
2 changes: 2 additions & 0 deletions src/dom/events.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,8 @@ export function addListeners(useCases: UseCases): () => void {
const element = closestTarget(event, SELECTOR_BUTTON)
const id = getFootnoteId(element)
if (id) {
// Prevent running the handler more than once.
event.preventDefault()
useCases.toggle(id)
} else if (!closestTarget(event, SELECTOR_POPOVER)) {
useCases.touchOutside()
Expand Down
11 changes: 10 additions & 1 deletion test/activate.test.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { test, expect, afterEach, beforeEach, vi } from 'vitest'
import { fireEvent } from '@testing-library/dom'
import { fireEvent, createEvent } from '@testing-library/dom'
import {
setDocumentBody,
waitToStopChanging,
Expand Down Expand Up @@ -31,6 +31,15 @@ test('activate footnote when clicking the button', async () => {
expect(popover).toHaveClass('is-active')
})

test('activation touch event has preventDefault (#57)', async () => {
littlefoot(TEST_SETTINGS)
const button = getButton('1')

const event = createEvent.touchEnd(button)
fireEvent(button, event)
expect(event.defaultPrevented).toBe(true)
})

test('does not insert empty paragraphs in the footnote content (#187)', async () => {
littlefoot(TEST_SETTINGS)
const button = getButton('1')
Expand Down

0 comments on commit 98fd2d3

Please sign in to comment.