Skip to content

Commit

Permalink
Merge pull request #10 from svecosystem/improve-element-size
Browse files Browse the repository at this point in the history
I forgot to commit
  • Loading branch information
TGlide authored Apr 22, 2024
2 parents efab20f + 4f7f297 commit 9c5c777
Show file tree
Hide file tree
Showing 4 changed files with 197 additions and 53 deletions.
5 changes: 5 additions & 0 deletions .changeset/thin-chefs-mix.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"runed": patch
---

fix element size & improve JSDoc
2 changes: 1 addition & 1 deletion packages/runed/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@
"@types/node": "^20.10.6",
"jsdom": "^24.0.0",
"publint": "^0.1.9",
"svelte": "^5.0.0-next.75",
"svelte": "^5.0.0-next.110",
"svelte-check": "^3.6.0",
"tslib": "^2.4.1",
"typescript": "^5.0.0",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,30 +10,30 @@ type Options = {
};



/**
* Returns a reactive value holding the size of `node`.
*
* @export
* @param {ValueOrGetter<HTMLElement | undefined>} node
* @param {Options} [options={
* box: "border-box",
* }]
* @returns {*}
*
* Accepts an `options` object with the following properties:
* - `initialSize`: The initial size of the element. Defaults to `{ width: 0, height: 0 }`.
* - `box`: The box model to use. Can be either `"content-box"` or `"border-box"`. Defaults to `"border-box"`.
*
* @returns an object with `width` and `height` properties.
*/
export function useElementSize(
node: ValueOrGetter<HTMLElement | undefined>,
_node: ValueOrGetter<HTMLElement | undefined>,
options: Options = {
box: "border-box",
}
) {
const $node = boxed(node);
): { width: number; height: number; } {
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 @@ -44,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
Loading

0 comments on commit 9c5c777

Please sign in to comment.