-
Notifications
You must be signed in to change notification settings - Fork 1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
ensure we see the "relatedTarget" in Safari
Safari doesn't fire a `focus` event when clicking a button, therefore it does not become the `document.activeElement`, and events like `blur` or `focus` doesn't set the button as the `event.relatedTarget`. Keeping track of a history like this solves that problem. We already had the code for the `FocusTrap` component.
- Loading branch information
1 parent
318485a
commit 07f05f5
Showing
6 changed files
with
70 additions
and
63 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
24 changes: 24 additions & 0 deletions
24
packages/@headlessui-react/src/utils/active-element-history.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
import { onDocumentReady } from './document-ready' | ||
|
||
export let history: HTMLElement[] = [] | ||
onDocumentReady(() => { | ||
function handle(e: Event) { | ||
if (!(e.target instanceof HTMLElement)) return | ||
if (e.target === document.body) return | ||
if (history[0] === e.target) return | ||
|
||
history.unshift(e.target) | ||
|
||
// Filter out DOM Nodes that don't exist anymore | ||
history = history.filter((x) => x != null && x.isConnected) | ||
history.splice(10) // Only keep the 10 most recent items | ||
} | ||
|
||
window.addEventListener('click', handle, { capture: true }) | ||
window.addEventListener('mousedown', handle, { capture: true }) | ||
window.addEventListener('focus', handle, { capture: true }) | ||
|
||
document.body.addEventListener('click', handle, { capture: true }) | ||
document.body.addEventListener('mousedown', handle, { capture: true }) | ||
document.body.addEventListener('focus', handle, { capture: true }) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
24 changes: 24 additions & 0 deletions
24
packages/@headlessui-vue/src/utils/active-element-history.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
import { onDocumentReady } from './document-ready' | ||
|
||
export let history: HTMLElement[] = [] | ||
onDocumentReady(() => { | ||
function handle(e: Event) { | ||
if (!(e.target instanceof HTMLElement)) return | ||
if (e.target === document.body) return | ||
if (history[0] === e.target) return | ||
|
||
history.unshift(e.target) | ||
|
||
// Filter out DOM Nodes that don't exist anymore | ||
history = history.filter((x) => x != null && x.isConnected) | ||
history.splice(10) // Only keep the 10 most recent items | ||
} | ||
|
||
window.addEventListener('click', handle, { capture: true }) | ||
window.addEventListener('mousedown', handle, { capture: true }) | ||
window.addEventListener('focus', handle, { capture: true }) | ||
|
||
document.body.addEventListener('click', handle, { capture: true }) | ||
document.body.addEventListener('mousedown', handle, { capture: true }) | ||
document.body.addEventListener('focus', handle, { capture: true }) | ||
}) |