Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

resize-observer: Use getBoundingClientRect instead of contentRect #509

Merged
merged 1 commit into from
Sep 8, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/eleven-roses-smash.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@solid-primitives/resize-observer": patch
---

Use `getBoundingClientRect` to get element size on resize
2 changes: 2 additions & 0 deletions packages/resize-observer/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -144,6 +144,8 @@ If you want a reactive interface for media-queries, please checkout [the media p

Creates a reactive store-like object of current width and height dimensions of html element.

It uses `ResizeObserver` under the hood—to observe when the element size changes—and `getBoundingClientRect` to get the current size.

### How to use it

`createElementSize` needs to be provided a target. It can be an HTML element, or a reactive signal returning one. Target also takes falsy values to disable tracking.
Expand Down
2 changes: 1 addition & 1 deletion packages/resize-observer/dev/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ const App: Component = () => {
<div class="center-child min-h-82">
<div
ref={e => (ref = e)}
class="shadow-bg-gray-900 center-child h-24 w-24 rounded-md bg-orange-500 shadow-lg"
class="shadow-bg-gray-900 center-child h-24 w-24 rounded-md border-8 border-white bg-orange-500 shadow-lg"
style={{
width: `${width()}px`,
height: `${height()}px`,
Expand Down
5 changes: 1 addition & 4 deletions packages/resize-observer/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -175,10 +175,7 @@ export function createElementSize(
sharedConfig.context || isFn ? ELEMENT_SIZE_FALLBACK : getElementSize(target),
);

const ro = new ResizeObserver(([e]) => {
const { width, height } = e!.contentRect;
setSize({ width, height });
});
const ro = new ResizeObserver(([e]) => setSize(getElementSize(e!.target)));
onCleanup(() => ro.disconnect());

if (isFn) {
Expand Down