Skip to content

Commit

Permalink
Merge pull request #9 from svecosystem/improve-element-size
Browse files Browse the repository at this point in the history
Improve element size
  • Loading branch information
TGlide authored Apr 22, 2024
2 parents 70b9c50 + 424bf56 commit efab20f
Show file tree
Hide file tree
Showing 8 changed files with 30 additions and 36 deletions.
Original file line number Diff line number Diff line change
@@ -1,5 +1,13 @@
import { isBrowser } from "$lib/internal/utils/browser.js";


/**
* Returns a reactive value that is equal to `document.activeElement`.
* It automatically listens for changes, keeping the reference up to date.
*
* @export
* @returns {{ value: Readonly<Element | null> }}
*/
export function useActiveElement(): { value: Readonly<Element | null> } {
const activeElement = $state({ value: isBrowser() ? document.activeElement : null });

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,6 @@ import type { FunctionArgs, ValueOrGetter } from "$lib/internal/types.js";
* The second parameter is the time to wait before calling the original callback.
* Alternatively, it can also be a getter function that returns the time to wait.
*
* @export
* @template {FunctionArgs} Callback
* @param {Callback} callback
* @param {number} wait
*/
export function useDebounce<Callback extends FunctionArgs>(
callback: Callback,
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -9,20 +9,31 @@ type Options = {
box?: "content-box" | "border-box";
};


/**
* Returns a reactive value holding the size of `node`.
*
* @export
* @param {ValueOrGetter<HTMLElement | undefined>} node
* @param {Options} [options={
* box: "border-box",
* }]
* @returns {*}
*/
export function useElementSize(
_node: ValueOrGetter<HTMLElement | undefined>,
node: ValueOrGetter<HTMLElement | undefined>,
options: Options = {
box: "border-box",
}
) {
const node = boxed(_node);
const $node = boxed(node);
const size = $state({
width: options.initialSize?.width ?? 0,
height: options.initialSize?.height ?? 0,
});

$effect(() => {
if (!node.value) return;
if (!$node.value) return;

const observer = new ResizeObserver((entries) => {
for (const entry of entries) {
Expand All @@ -33,7 +44,7 @@ export function useElementSize(
size.height = boxSizeArr.reduce((acc, size) => Math.max(acc, size.blockSize), 0);
}
});
observer.observe(node.value);
observer.observe($node.value);

return () => {
observer.disconnect();
Expand Down

This file was deleted.

14 changes: 4 additions & 10 deletions sites/docs/content/functions/use-active-element.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,14 +20,8 @@ import { UseActiveElementDemo } from '$lib/components/demos';
const activeElement = useActiveElement();
</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>
<p>
Currently active element:
{activeElement.value?.localName ?? 'No active element found'}
</p>
```
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,7 @@
<p>
Currently active element:
<span class="font-bold">
{activeElement.value !== null
? activeElement.value.localName
: "No active element found"}
{activeElement.value?.localName ?? "No active element found"}
</span>
</p>
</div>
3 changes: 2 additions & 1 deletion sites/docs/src/lib/components/demos/use-debounce.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,8 @@

<div class="rounded-md bg-card p-8">
<button
class="relative z-20 inline-flex items-center justify-center rounded-md border bg-brand/50 px-3 py-1.5 text-sm font-medium transition-all hover:bg-brand/25 focus:outline-none"
class="relative z-20 inline-flex items-center justify-center rounded-md border bg-brand/50
px-3 py-1.5 text-sm font-medium transition-all hover:bg-brand/25 focus:outline-none active:bg-brand/15"
onclick={ding}>DING DING DING</button
>
<p>{logged || "Press the button!"}</p>
Expand Down

0 comments on commit efab20f

Please sign in to comment.