@wentools/overlay 0.1.3 → 0.1.5

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (2) hide show
  1. package/package.json +1 -1
  2. package/src/popover.svelte +20 -18
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@wentools/overlay",
3
- "version": "0.1.3",
3
+ "version": "0.1.5",
4
4
  "description": "Svelte 5 popover, dropdown menu, and tooltip components with CSS custom property theming",
5
5
  "license": "MIT",
6
6
  "repository": {
@@ -225,20 +225,7 @@ Perfect for: building custom popover patterns, tooltips, custom dropdowns.
225
225
  actualPosition = newPosition
226
226
  }
227
227
 
228
- // Ignore click-outside for one frame after opening, so a programmatic open
229
- // from pointerdown isn't immediately closed by the click from the same tap
230
- let ignoreClickOutside = false
231
-
232
- $effect(() => {
233
- if (open) {
234
- ignoreClickOutside = true
235
- requestAnimationFrame(() => { ignoreClickOutside = false })
236
- }
237
- })
238
-
239
228
  const handleClickOutside = (event: MouseEvent) => {
240
- if (!open || !closeOnClickOutside || ignoreClickOutside) return
241
-
242
229
  const target = event.target as Node
243
230
  if (
244
231
  popoverElement &&
@@ -250,6 +237,26 @@ Perfect for: building custom popover patterns, tooltips, custom dropdowns.
250
237
  }
251
238
  }
252
239
 
240
+ // Ignore clicks shortly after opening so a programmatic open from pointerdown
241
+ // isn't closed by the click from the same tap. On touch devices, pointerdown
242
+ // and the compatibility click fire in separate browser tasks, so rAF-based
243
+ // deferral isn't sufficient — a time-based guard is needed instead.
244
+ $effect(() => {
245
+ if (!open || !closeOnClickOutside) return
246
+
247
+ const openedAt = performance.now()
248
+ const guardedHandler = (event: MouseEvent) => {
249
+ if (performance.now() - openedAt < 300) return
250
+ handleClickOutside(event)
251
+ }
252
+
253
+ document.addEventListener('click', guardedHandler)
254
+
255
+ return () => {
256
+ document.removeEventListener('click', guardedHandler)
257
+ }
258
+ })
259
+
253
260
  const handleKeydown = (event: KeyboardEvent) => {
254
261
  if (!open || !closeOnEscape) return
255
262
 
@@ -271,11 +278,6 @@ Perfect for: building custom popover patterns, tooltips, custom dropdowns.
271
278
  onMount(() => {
272
279
  const cleanup: (() => void)[] = []
273
280
 
274
- if (closeOnClickOutside) {
275
- document.addEventListener('click', handleClickOutside)
276
- cleanup.push(() => document.removeEventListener('click', handleClickOutside))
277
- }
278
-
279
281
  if (closeOnEscape) {
280
282
  document.addEventListener('keydown', handleKeydown)
281
283
  cleanup.push(() => document.removeEventListener('keydown', handleKeydown))