-
-
Notifications
You must be signed in to change notification settings - Fork 29
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #3 from Hugos68/feat/active-element
Feat: Active Element Hook
- Loading branch information
Showing
14 changed files
with
390 additions
and
5 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
--- | ||
"runed": minor | ||
--- | ||
|
||
add useActiveElement |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,3 @@ | ||
export * from "./useActiveElement/index.js"; | ||
export * from "./useDebounce/index.js"; | ||
export * from "./useElementSize/index.js"; |
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 @@ | ||
export * from "./useActiveElement.svelte.js"; |
21 changes: 21 additions & 0 deletions
21
packages/runed/src/lib/functions/useActiveElement/useActiveElement.svelte.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,21 @@ | ||
import { documentDefined } from "../../internal/utils/defined.js"; | ||
|
||
export function useActiveElement(): { value: Readonly<Element | null> } { | ||
const activeElement = $state({ value: documentDefined() ? document.activeElement : null }); | ||
|
||
function onFocusChange() { | ||
activeElement.value = document.activeElement; | ||
} | ||
|
||
$effect(() => { | ||
document.addEventListener("focusin", onFocusChange); | ||
document.addEventListener("focusout", onFocusChange); | ||
|
||
return () => { | ||
document.removeEventListener("focusin", onFocusChange); | ||
document.removeEventListener("focusout", onFocusChange); | ||
}; | ||
}); | ||
|
||
return activeElement; | ||
} |
24 changes: 24 additions & 0 deletions
24
packages/runed/src/lib/functions/useActiveElement/useActiveElement.test.svelte.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 { describe, expect } from "vitest"; | ||
import { tick } from "svelte"; | ||
import { useActiveElement } from "./index.js"; | ||
import { testWithEffect } from "$lib/test/util.svelte.js"; | ||
|
||
describe("useActiveElement", () => { | ||
testWithEffect("initializes with `document.activeElement`", () => { | ||
const activeElement = useActiveElement(); | ||
expect(activeElement.value).toBe(document.activeElement); | ||
}); | ||
testWithEffect( | ||
"updates accordingly when `document.activeElement` element changes", | ||
async () => { | ||
const input = document.createElement("input"); | ||
document.body.appendChild(input); | ||
input.focus(); | ||
|
||
const activeElement = useActiveElement(); | ||
await tick(); | ||
expect(document.activeElement).toBe(input); | ||
expect(activeElement.value).toBe(input); | ||
} | ||
); | ||
}); |
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,3 @@ | ||
export function documentDefined() { | ||
return typeof document !== "undefined"; | ||
} |
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,13 @@ | ||
import { it } from "vitest"; | ||
|
||
export function testWithEffect(name: string, fn: () => void) { | ||
it(name, async () => { | ||
let promise: Promise<void> | void; | ||
const cleanup = $effect.root(() => (promise = fn())); | ||
try { | ||
await promise!; | ||
} finally { | ||
cleanup(); | ||
} | ||
}); | ||
} |
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
Oops, something went wrong.