Skip to content
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
11 changes: 11 additions & 0 deletions .changeset/follow-target-document.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
---
'@tanstack/hotkeys': patch
---

In React, Octane, Solid, and Svelte, an element-targeted hotkey now keeps working when its element moves to another document or shadow root, or is registered before its element is in the page. These adapters listen on the element's document, so that `onKeyDown` handlers run before the hotkey. They found that document once, at registration, so the hotkey went quiet in these cases:

- An app moves an existing element into a popup or Document Picture-in-Picture window, as a Solid `Portal` does when its `mount` changes.
- An element moves into or out of a shadow root.
- A hotkey registers while its element is in a container that isn't in the page yet. If only the container is attached later, the hotkey fired before handlers on the document, so they couldn't stop it. If the element is moved out of the container, the hotkey never fired.

The hotkey now moves its listener to the element's current document when focus enters the element, before the next key press there.
5 changes: 5 additions & 0 deletions .changeset/recorder-target-window.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@tanstack/hotkeys': minor
---

The recorder has a `target` option: the window to record key presses in, or its document. Pass a Picture-in-Picture window to record a hotkey from a button inside it. Recording listens only in that window, and `cancelOnBlur` cancels when that window loses focus. Closing the window cancels the recording, and a closed window doesn't start one. The option is read when recording starts and defaults to the page's `window`. Every adapter's recorder accepts it.
682 changes: 384 additions & 298 deletions .intent/review-state.json

Large diffs are not rendered by default.

3 changes: 3 additions & 0 deletions docs/framework/alpine/guides/custom-hotkey-recording.md
Original file line number Diff line number Diff line change
Expand Up @@ -134,6 +134,8 @@ Options can also be a getter, such as `createHotkeyRecorder(this.hotkeys, () =>

The recorder also cancels and calls `onCancel` when the window loses focus, such as when the user switches apps or tabs, opens browser devtools, or clicks into an iframe. Set `cancelOnBlur: false` to keep recording. While debugging with devtools open, Chrome's **Rendering > Emulate a focused page** setting keeps the page focused.

To record in another window, such as a Picture-in-Picture window that holds the recorder's button, pass that window or its document as `target`. The recorder reads `target` when recording starts, listens only in that window, and cancels when that window loses focus or closes. A closed window doesn't start recording.

A click elsewhere on the page doesn't cancel, because the recorder doesn't know which element is your recorder. To cancel on a click outside it, use Alpine's `.outside` modifier on `pointerdown`. Don't rely on the button's `blur` event: Safari and Firefox on macOS don't focus a button when it is clicked.

```html
Expand Down Expand Up @@ -315,6 +317,7 @@ Alpine.data('shortcutSettings', () => {
| `onRecord` | Required | Called with the recorded hotkey when a recording commits, and with the same context as `validate` |
| `onCancel` | None | Called when the user presses Escape, Tab, or Shift+Tab, when the window loses focus, or when you call `cancelRecording()` |
| `cancelOnBlur` | `true` | Cancels the recording when the window loses focus. Set `false` to keep recording. |
| `target` | `window` | The window, or its document, to record in, such as a Picture-in-Picture window. |
| `onClear` | None | Called when the user presses Backspace or Delete with nothing recorded |
| `maxSteps` | `1` | `1` records a chord. `Infinity` records a sequence. |
| `commitKeys` | `'enter'` | With `maxSteps: Infinity`, whether plain Enter commits (`'enter'`) or records as a step (`'none'`) |
Expand Down
9 changes: 7 additions & 2 deletions docs/framework/angular/guides/custom-hotkey-recording.md
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,8 @@ Options can also be a getter, such as `injectHotkeyRecorder(() => ({ maxSteps: I

The recorder also cancels and calls `onCancel` when the window loses focus, such as when the user switches apps or tabs, opens browser devtools, or clicks into an iframe. Set `cancelOnBlur: false` to keep recording. While debugging with devtools open, Chrome's **Rendering > Emulate a focused page** setting keeps the page focused.

To record in another window, such as a Picture-in-Picture window that holds the recorder's button, pass that window or its document as `target`. The recorder reads `target` when recording starts, listens only in that window, and cancels when that window loses focus or closes. A closed window doesn't start recording.

A click elsewhere on the page doesn't cancel, because the recorder doesn't know which element is your recorder. To cancel on a click outside it, listen for `pointerdown` while recording. Don't rely on the button's `blur` event: Safari and Firefox on macOS don't focus a button when it is clicked.

```ts
Expand All @@ -115,8 +117,10 @@ export class ShortcutRecorderComponent {
this.recorder.cancelRecording()
}
}
document.addEventListener('pointerdown', onPointerDown, true)
onCleanup(() => document.removeEventListener('pointerdown', onPointerDown, true))
// The button's document, which can be a Picture-in-Picture window
const doc = this.host.nativeElement.ownerDocument
doc.addEventListener('pointerdown', onPointerDown, true)
onCleanup(() => doc.removeEventListener('pointerdown', onPointerDown, true))
})
}
}
Expand Down Expand Up @@ -291,6 +295,7 @@ export class ShortcutSettingsComponent {
| `onRecord` | Required | Called with the recorded `hotkey` and `context` when a recording commits |
| `onCancel` | None | Called on `Escape`, `Tab`, `Shift+Tab`, the window losing focus, or `cancelRecording()` |
| `cancelOnBlur` | `true` | Cancels when the window loses focus. Set `false` to keep recording |
| `target` | `window` | The window, or its document, to record in, such as a Picture-in-Picture window |
| `onClear` | None | Called when `Backspace` or `Delete` is pressed with no recorded steps |
| `maxSteps` | `1` | `1` for chords, `Infinity` for sequences |
| `commitKeys` | `'enter'` | With `maxSteps: Infinity`, whether `Enter` commits (`'enter'`) or records as a step (`'none'`) |
Expand Down
9 changes: 7 additions & 2 deletions docs/framework/ember/guides/custom-hotkey-recording.md
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,8 @@ Options can also be a getter, such as `useHotkeyRecorder(this, () => ({ maxSteps

The recorder also cancels and calls `onCancel` when the window loses focus, such as when the user switches apps or tabs, opens browser devtools, or clicks into an iframe. Set `cancelOnBlur: false` to keep recording. While debugging with devtools open, Chrome's **Rendering > Emulate a focused page** setting keeps the page focused.

To record in another window, such as a Picture-in-Picture window that holds the recorder's button, pass that window or its document as `target`. The recorder reads `target` when recording starts, listens only in that window, and cancels when that window loses focus or closes. A closed window doesn't start recording.

A click elsewhere on the page doesn't cancel, because the recorder doesn't know which element is your recorder. To cancel on a click outside it, listen for `pointerdown` while recording, for example in a modifier. Don't rely on the button's `blur` event: Safari and Firefox on macOS don't focus a button when it is clicked.

```gts
Expand All @@ -110,8 +112,10 @@ export default class ShortcutRecorder extends Component {
const onPointerDown = (event: PointerEvent) => {
if (!element.contains(event.target as Node)) this.recorder.cancelRecording()
}
document.addEventListener('pointerdown', onPointerDown, true)
return () => document.removeEventListener('pointerdown', onPointerDown, true)
// The button's document, which can be a Picture-in-Picture window
const doc = element.ownerDocument
doc.addEventListener('pointerdown', onPointerDown, true)
return () => doc.removeEventListener('pointerdown', onPointerDown, true)
},
)

Expand Down Expand Up @@ -304,6 +308,7 @@ export default class ShortcutSettings extends Component {
| `onRecord` | Required | Called with the recorded hotkey when a recording commits, and with the same context as `validate` |
| `onCancel` | None | Called when the user presses Escape, Tab, or Shift+Tab, when the window loses focus, or when you call `cancelRecording()` |
| `cancelOnBlur` | `true` | Cancels the recording when the window loses focus. Set `false` to keep recording. |
| `target` | `window` | The window, or its document, to record in, such as a Picture-in-Picture window. |
| `onClear` | None | Called when the user presses Backspace or Delete with nothing recorded |
| `maxSteps` | `1` | `1` records a chord. `Infinity` records a sequence. |
| `commitKeys` | `'enter'` | With `maxSteps: Infinity`, whether plain Enter commits (`'enter'`) or records as a step (`'none'`) |
Expand Down
23 changes: 17 additions & 6 deletions docs/framework/lit/guides/custom-hotkey-recording.md
Original file line number Diff line number Diff line change
Expand Up @@ -106,24 +106,34 @@ Options can also be a getter, such as `new HotkeyRecorderController(this, () =>

The recorder also cancels and calls `onCancel` when the window loses focus, such as when the user switches apps or tabs, opens browser devtools, or clicks into an iframe. Set `cancelOnBlur: false` to keep recording. While debugging with devtools open, Chrome's **Rendering > Emulate a focused page** setting keeps the page focused.

To record in another window, such as a Picture-in-Picture window that holds the recorder's button, pass that window or its document as `target`. The recorder reads `target` when recording starts, listens only in that window, and cancels when that window loses focus or closes. A closed window doesn't start recording.

A click elsewhere on the page doesn't cancel, because the recorder doesn't know which element is your recorder. To cancel on a click outside it, listen for `pointerdown` while recording. Check `event.composedPath()`, because the document sees a click inside a shadow root as a click on its host. Don't rely on the button's `blur` event: Safari and Firefox on macOS don't focus a button when it is clicked.

```ts
private onPointerDown = (event: PointerEvent) => {
if (!event.composedPath().includes(this)) this.recorder.cancelRecording()
}

// The document the listener is on, kept in case the element moves
private listeningIn: Document | null = null

private stopListening() {
this.listeningIn?.removeEventListener('pointerdown', this.onPointerDown, true)
this.listeningIn = null
}

updated() {
if (this.recorder.isRecording) {
document.addEventListener('pointerdown', this.onPointerDown, true)
} else {
document.removeEventListener('pointerdown', this.onPointerDown, true)
}
if (!this.recorder.isRecording) return this.stopListening()
if (this.listeningIn) return
// The element's document, which can be a Picture-in-Picture window
this.listeningIn = this.ownerDocument
this.listeningIn.addEventListener('pointerdown', this.onPointerDown, true)
}

disconnectedCallback() {
super.disconnectedCallback()
document.removeEventListener('pointerdown', this.onPointerDown, true)
this.stopListening()
}
```

Expand Down Expand Up @@ -301,6 +311,7 @@ class ShortcutSettings extends LitElement {
| `onRecord` | Required | Called with the recorded hotkey when a recording commits, and with the same context as `validate` |
| `onCancel` | None | Called when the user presses Escape, Tab, or Shift+Tab, when the window loses focus, or when you call `cancelRecording()` |
| `cancelOnBlur` | `true` | Cancels the recording when the window loses focus. Set `false` to keep recording. |
| `target` | `window` | The window, or its document, to record in, such as a Picture-in-Picture window. |
| `onClear` | None | Called when the user presses Backspace or Delete with nothing recorded |
| `maxSteps` | `1` | `1` records a chord. `Infinity` records a sequence. |
| `commitKeys` | `'enter'` | With `maxSteps: Infinity`, whether plain Enter commits (`'enter'`) or records as a step (`'none'`) |
Expand Down
9 changes: 7 additions & 2 deletions docs/framework/octane/guides/custom-hotkey-recording.md
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,8 @@ If the user commits after a single chord, `useHotkeyRecorder` simplifies it to a

The recorder also cancels and calls `onCancel` when the window loses focus, such as when the user switches apps or tabs, opens browser devtools, or clicks into an iframe. Set `cancelOnBlur: false` to keep recording. While debugging with devtools open, Chrome's **Rendering > Emulate a focused page** setting keeps the page focused.

To record in another window, such as a Picture-in-Picture window that holds the recorder's button, pass that window or its document as `target`. The recorder reads `target` when recording starts, listens only in that window, and cancels when that window loses focus or closes. A closed window doesn't start recording.

A click elsewhere on the page doesn't cancel, because the recorder doesn't know which element is your recorder. To cancel on a click outside it, listen for `pointerdown` while recording. Don't rely on the button's `blur` event: Safari and Firefox on macOS don't focus a button when it is clicked.

```tsx
Expand All @@ -94,8 +96,10 @@ useEffect(() => {
const onPointerDown = (event: PointerEvent) => {
if (!ref.current?.contains(event.target as Node)) recorder.cancelRecording()
}
document.addEventListener('pointerdown', onPointerDown, true)
return () => document.removeEventListener('pointerdown', onPointerDown, true)
// The button's document, which can be a Picture-in-Picture window
const doc = ref.current?.ownerDocument ?? document
doc.addEventListener('pointerdown', onPointerDown, true)
return () => doc.removeEventListener('pointerdown', onPointerDown, true)
}, [recorder.isRecording, recorder.cancelRecording])

<button ref={ref} onClick={recorder.startRecording}>
Expand Down Expand Up @@ -265,6 +269,7 @@ function ShortcutSettings() @{
| `onRecord` | Required | Called with the recorded `hotkey` and `context` when committed |
| `onCancel` | None | Called on `Escape`, `Tab`, `Shift+Tab`, the window losing focus, or `cancelRecording()` |
| `cancelOnBlur` | `true` | Cancels when the window loses focus. Set `false` to keep recording |
| `target` | `window` | The window, or its document, to record in, such as a Picture-in-Picture window |
| `onClear` | None | Called when `Backspace` or `Delete` is pressed with no recorded steps |
| `maxSteps` | `1` | `1` for chords, `Infinity` for sequences |
| `commitKeys` | `'enter'` | With `maxSteps: Infinity`, whether `Enter` commits (`'enter'`) or records (`'none'`) |
Expand Down
9 changes: 7 additions & 2 deletions docs/framework/preact/guides/custom-hotkey-recording.md
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,8 @@ If the user commits after a single chord, `useHotkeyRecorder` simplifies it to a

The recorder also cancels and calls `onCancel` when the window loses focus, such as when the user switches apps or tabs, opens browser devtools, or clicks into an iframe. Set `cancelOnBlur: false` to keep recording. While debugging with devtools open, Chrome's **Rendering > Emulate a focused page** setting keeps the page focused.

To record in another window, such as a Picture-in-Picture window that holds the recorder's button, pass that window or its document as `target`. The recorder reads `target` when recording starts, listens only in that window, and cancels when that window loses focus or closes. A closed window doesn't start recording.

A click elsewhere on the page doesn't cancel, because the recorder doesn't know which element is your recorder. To cancel on a click outside it, listen for `pointerdown` while recording. Don't rely on the button's `blur` event: Safari and Firefox on macOS don't focus a button when it is clicked.

```tsx
Expand All @@ -97,8 +99,10 @@ useEffect(() => {
const onPointerDown = (event: PointerEvent) => {
if (!ref.current?.contains(event.target as Node)) recorder.cancelRecording()
}
document.addEventListener('pointerdown', onPointerDown, true)
return () => document.removeEventListener('pointerdown', onPointerDown, true)
// The button's document, which can be a Picture-in-Picture window
const doc = ref.current?.ownerDocument ?? document
doc.addEventListener('pointerdown', onPointerDown, true)
return () => doc.removeEventListener('pointerdown', onPointerDown, true)
}, [recorder.isRecording, recorder.cancelRecording])

return (
Expand Down Expand Up @@ -273,6 +277,7 @@ function ShortcutSettings() {
| `onRecord` | Required | Called with the recorded `hotkey` and `context` when committed |
| `onCancel` | None | Called on `Escape`, `Tab`, `Shift+Tab`, the window losing focus, or `cancelRecording()` |
| `cancelOnBlur` | `true` | Cancels when the window loses focus. Set `false` to keep recording |
| `target` | `window` | The window, or its document, to record in, such as a Picture-in-Picture window |
| `onClear` | None | Called when `Backspace` or `Delete` is pressed with no recorded steps |
| `maxSteps` | `1` | `1` for chords, `Infinity` for sequences |
| `commitKeys` | `'enter'` | With `maxSteps: Infinity`, whether `Enter` commits (`'enter'`) or records (`'none'`) |
Expand Down
9 changes: 7 additions & 2 deletions docs/framework/react/guides/custom-hotkey-recording.md
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,8 @@ If the user commits after a single chord, `useHotkeyRecorder` simplifies it to a

The recorder also cancels and calls `onCancel` when the window loses focus, such as when the user switches apps or tabs, opens browser devtools, or clicks into an iframe. Set `cancelOnBlur: false` to keep recording. While debugging with devtools open, Chrome's **Rendering > Emulate a focused page** setting keeps the page focused.

To record in another window, such as a Picture-in-Picture window that holds the recorder's button, pass that window or its document as `target`. The recorder reads `target` when recording starts, listens only in that window, and cancels when that window loses focus or closes. A closed window doesn't start recording.

A click elsewhere on the page doesn't cancel, because the recorder doesn't know which element is your recorder. To cancel on a click outside it, listen for `pointerdown` while recording. Don't rely on the button's `blur` event: Safari and Firefox on macOS don't focus a button when it is clicked.

```tsx
Expand All @@ -97,8 +99,10 @@ useEffect(() => {
const onPointerDown = (event: PointerEvent) => {
if (!ref.current?.contains(event.target as Node)) recorder.cancelRecording()
}
document.addEventListener('pointerdown', onPointerDown, true)
return () => document.removeEventListener('pointerdown', onPointerDown, true)
// The button's document, which can be a Picture-in-Picture window
const doc = ref.current?.ownerDocument ?? document
doc.addEventListener('pointerdown', onPointerDown, true)
return () => doc.removeEventListener('pointerdown', onPointerDown, true)
}, [recorder.isRecording, recorder.cancelRecording])

return (
Expand Down Expand Up @@ -273,6 +277,7 @@ function ShortcutSettings() {
| `onRecord` | Required | Called with the recorded `hotkey` and `context` when committed |
| `onCancel` | None | Called on `Escape`, `Tab`, `Shift+Tab`, the window losing focus, or `cancelRecording()` |
| `cancelOnBlur` | `true` | Cancels when the window loses focus. Set `false` to keep recording |
| `target` | `window` | The window, or its document, to record in, such as a Picture-in-Picture window |
| `onClear` | None | Called when `Backspace` or `Delete` is pressed with no recorded steps |
| `maxSteps` | `1` | `1` for chords, `Infinity` for sequences |
| `commitKeys` | `'enter'` | With `maxSteps: Infinity`, whether `Enter` commits (`'enter'`) or records (`'none'`) |
Expand Down
Loading
Loading