This commit is contained in:
eewing
2026-02-17 14:10:16 -06:00
parent 2bca5834c5
commit cf73cd3b4c
11246 changed files with 1690552 additions and 0 deletions
@@ -0,0 +1,13 @@
import { type AnyFn } from "svelte-toolbelt";
/**
* Detects whether the user is currently using the keyboard
* or not by listening to keyboard and pointer events. Uses shared global
* state to avoid listener duplication.
*/
export declare class IsUsingKeyboard {
static _refs: number;
static _cleanup?: AnyFn;
constructor();
get current(): boolean;
set current(value: boolean);
}
@@ -0,0 +1,51 @@
import { executeCallbacks } from "svelte-toolbelt";
import { on } from "svelte/events";
// Using global state to avoid multiple listeners.
let isUsingKeyboard = $state(false);
/**
* Detects whether the user is currently using the keyboard
* or not by listening to keyboard and pointer events. Uses shared global
* state to avoid listener duplication.
*/
export class IsUsingKeyboard {
static _refs = 0; // Reference counting to avoid multiple listeners.
static _cleanup;
constructor() {
$effect(() => {
if (IsUsingKeyboard._refs === 0) {
IsUsingKeyboard._cleanup = $effect.root(() => {
const callbacksToDispose = [];
const handlePointer = (_) => {
isUsingKeyboard = false;
};
const handleKeydown = (_) => {
isUsingKeyboard = true;
};
callbacksToDispose.push(on(document, "pointerdown", handlePointer, {
capture: true,
}), on(document, "pointermove", handlePointer, {
capture: true,
}), on(document, "keydown", handleKeydown, {
capture: true,
}));
// Don't forget to spread and call twice.
return executeCallbacks(...callbacksToDispose);
});
}
IsUsingKeyboard._refs++;
return () => {
IsUsingKeyboard._refs--;
if (IsUsingKeyboard._refs === 0) {
isUsingKeyboard = false;
IsUsingKeyboard._cleanup?.();
}
};
});
}
get current() {
return isUsingKeyboard;
}
set current(value) {
isUsingKeyboard = value;
}
}