Skip to content

Commit

Permalink
Added active element
Browse files Browse the repository at this point in the history
  • Loading branch information
Hugos68 committed Mar 30, 2024
1 parent cd23e44 commit bad9393
Show file tree
Hide file tree
Showing 7 changed files with 78 additions and 0 deletions.
1 change: 1 addition & 0 deletions packages/runed/src/lib/functions/index.ts
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
export * from "./withActiveElement.svelte.js";
export * from "./withDebounce.svelte.js";
export * from "./withElementSize.svelte.js";
21 changes: 21 additions & 0 deletions packages/runed/src/lib/functions/withActiveElement.svelte.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import { documentDefined } from "$lib/internal/utils/defined.js";

export function withActiveElement(): { 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;
}
3 changes: 3 additions & 0 deletions packages/runed/src/lib/internal/utils/defined.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
export function documentDefined() {
return typeof document !== "undefined";
}
33 changes: 33 additions & 0 deletions sites/docs/content/functions/with-active-element.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
---
title: WithActiveElement
description: A function that returns the currently active element.
---

<script>
import { WithActiveElementDemo } from '$lib/components/demos';
</script>

## Demo

<WithActiveElementDemo />

## Usage

```svelte
<script lang="ts">
import { withActiveElement } from "runed";
const activeElement = withActiveElement();
</script>
<div class="rounded-md bg-card p-8">
<p>
Currently active element:
<span class="font-bold">
{activeElement.value !== null
? activeElement.value.localName
: "No active element found"}
</span>
</p>
</div>
```
1 change: 1 addition & 0 deletions sites/docs/src/lib/components/demos/index.ts
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
export { default as WithActiveElementDemo } from "./with-active-element.svelte";
export { default as WithDebounceDemo } from "./with-debounce.svelte";
export { default as WithElementSizeDemo } from "./with-element-size.svelte";
14 changes: 14 additions & 0 deletions sites/docs/src/lib/components/demos/with-active-element.svelte
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<script lang="ts">
import { withActiveElement } from "runed";
const activeElement = withActiveElement();
</script>

<div class="rounded-md bg-card p-8">
<p>
Currently active element:
<span class="font-bold">
{activeElement.value !== null ? activeElement.value.localName : "No active element found"}
</span>
</p>
</div>
5 changes: 5 additions & 0 deletions sites/docs/src/lib/config/navigation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,11 @@ export const navigation: Navigation = {
title: "Functions",
collapsible: true,
items: [
{
title: "withActiveElement",
href: "/docs/functions/with-active-element",
items: [],
},
{
title: "withDebounce",
href: "/docs/functions/with-debounce",
Expand Down

0 comments on commit bad9393

Please sign in to comment.