Skip to content

Commit ffc6e7e

Browse files
author
chenyulu02
committed
feat: add clientWidth/clientHeight for createElementSize
1 parent 81a8348 commit ffc6e7e

File tree

2 files changed

+24
-6
lines changed

2 files changed

+24
-6
lines changed

packages/resize-observer/src/index.ts

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,9 @@ export type ResizeHandler<T extends Element = Element> = (
2525
) => void;
2626

2727
export type Size = { width: number; height: number };
28-
export type NullableSize = { width: number; height: number } | { width: null; height: null };
28+
export type NullableSize =
29+
| { width: number; height: number; clientWidth: number; clientHeight: number }
30+
| { width: null; height: null; clientWidth: null; clientHeight: null };
2931

3032
/**
3133
* Instantiate a new ResizeObserver that automatically get's disposed on cleanup.
@@ -139,7 +141,12 @@ export function createWindowSize(): Readonly<Size> {
139141
export const useWindowSize: typeof createWindowSize =
140142
/*#__PURE__*/ createHydratableSingletonRoot(createWindowSize);
141143

142-
const ELEMENT_SIZE_FALLBACK = { width: null, height: null } as const satisfies NullableSize;
144+
const ELEMENT_SIZE_FALLBACK = {
145+
width: null,
146+
height: null,
147+
clientWidth: null,
148+
clientHeight: null,
149+
} as const satisfies NullableSize;
143150

144151
/**
145152
* @param target html element
@@ -150,7 +157,8 @@ export function getElementSize(target: Element | false | undefined | null): Null
150157
return { ...ELEMENT_SIZE_FALLBACK };
151158
}
152159
const { width, height } = target.getBoundingClientRect();
153-
return { width, height };
160+
const { clientWidth, clientHeight } = target;
161+
return { width, height, clientWidth, clientHeight };
154162
}
155163

156164
/**
@@ -163,7 +171,7 @@ export function getElementSize(target: Element | false | undefined | null): Null
163171
* console.log(size.width, size.height)
164172
* })
165173
*/
166-
export function createElementSize(target: Element): Readonly<Size>;
174+
export function createElementSize(target: Element): Readonly<NullableSize>;
167175
export function createElementSize(
168176
target: Accessor<Element | false | undefined | null>,
169177
): Readonly<NullableSize>;

packages/resize-observer/test/index.test.ts

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -134,8 +134,18 @@ describe("getWindowSize", () => {
134134

135135
describe("getElementSize", () => {
136136
test("returns window size", () => {
137-
expect(getElementSize(document.createElement("div"))).toEqual({ width: 0, height: 0 });
138-
expect(getElementSize(undefined)).toEqual({ width: null, height: null });
137+
expect(getElementSize(document.createElement("div"))).toEqual({
138+
width: 0,
139+
height: 0,
140+
clientWidth: 0,
141+
clientHeight: 0,
142+
});
143+
expect(getElementSize(undefined)).toEqual({
144+
width: null,
145+
height: null,
146+
clientWidth: null,
147+
clientHeight: null,
148+
});
139149
});
140150
});
141151

0 commit comments

Comments
 (0)