Skip to content

Commit e13245f

Browse files
committed
refactor: simplify code
1 parent 8b3bf70 commit e13245f

File tree

1 file changed

+68
-69
lines changed

1 file changed

+68
-69
lines changed

src/index.ts

Lines changed: 68 additions & 69 deletions
Original file line numberDiff line numberDiff line change
@@ -102,85 +102,84 @@ const useDimensions = <T extends HTMLElement | null>({
102102
return () => null;
103103
}
104104

105-
let rafId: number | null = null;
105+
let raf: number | null = null;
106106

107107
// eslint-disable-next-line compat/compat
108-
observerRef.current = new (polyfill || window.ResizeObserver)(
109-
([entry]: any) => {
110-
rafId = window.requestAnimationFrame(() => {
111-
const { contentBoxSize, borderBoxSize, contentRect } = entry;
112-
113-
let boxSize = contentBoxSize;
114-
if (useBorderBoxSize)
115-
if (borderBoxSize) {
116-
boxSize = borderBoxSize;
117-
} else if (!warnedRef.current) {
118-
console.warn(borderBoxWarn);
119-
warnedRef.current = true;
120-
}
121-
// @juggle/resize-observer polyfill has different data structure
122-
boxSize = Array.isArray(boxSize) ? boxSize[0] : boxSize;
123-
124-
const width = boxSize ? boxSize.inlineSize : contentRect.width;
125-
const height = boxSize ? boxSize.blockSize : contentRect.height;
126-
127-
if (
128-
width === prevSizeRef.current.width &&
129-
height === prevSizeRef.current.height
130-
)
131-
return;
132-
133-
prevSizeRef.current = { width, height };
134-
135-
const e = {
136-
currentBreakpoint: "",
137-
width,
138-
height,
139-
entry,
140-
observe,
141-
unobserve,
142-
};
143-
144-
if (breakpoints) {
145-
e.currentBreakpoint = getCurrentBreakpoint(breakpoints, width);
146-
147-
if (e.currentBreakpoint !== prevBreakpointRef.current) {
148-
if (onResizeRef.current) onResizeRef.current(e);
149-
prevBreakpointRef.current = e.currentBreakpoint;
150-
}
151-
} else if (onResizeRef.current) {
152-
onResizeRef.current(e);
108+
observerRef.current = new (polyfill || ResizeObserver)(([entry]: any) => {
109+
raf = requestAnimationFrame(() => {
110+
const { contentBoxSize, borderBoxSize, contentRect } = entry;
111+
112+
let boxSize = contentBoxSize;
113+
if (useBorderBoxSize)
114+
if (borderBoxSize) {
115+
boxSize = borderBoxSize;
116+
} else if (!warnedRef.current) {
117+
console.warn(borderBoxWarn);
118+
warnedRef.current = true;
153119
}
154-
155-
const next = {
156-
currentBreakpoint: e.currentBreakpoint,
157-
width,
158-
height,
159-
entry,
160-
};
161-
162-
if (shouldUpdateRef.current && !shouldUpdateRef.current(next)) return;
163-
164-
if (
165-
!shouldUpdateRef.current &&
166-
breakpoints &&
167-
updateOnBreakpointChange
168-
) {
169-
setState((prev) =>
170-
prev.currentBreakpoint !== next.currentBreakpoint ? next : prev
171-
);
172-
return;
120+
// @juggle/resize-observer polyfill has different data structure
121+
boxSize = Array.isArray(boxSize) ? boxSize[0] : boxSize;
122+
123+
const width = boxSize ? boxSize.inlineSize : contentRect.width;
124+
const height = boxSize ? boxSize.blockSize : contentRect.height;
125+
126+
if (
127+
width === prevSizeRef.current.width &&
128+
height === prevSizeRef.current.height
129+
)
130+
return;
131+
132+
prevSizeRef.current = { width, height };
133+
134+
const e = {
135+
currentBreakpoint: "",
136+
width,
137+
height,
138+
entry,
139+
observe,
140+
unobserve,
141+
};
142+
143+
if (breakpoints) {
144+
e.currentBreakpoint = getCurrentBreakpoint(breakpoints, width);
145+
146+
if (e.currentBreakpoint !== prevBreakpointRef.current) {
147+
if (onResizeRef.current) onResizeRef.current(e);
148+
prevBreakpointRef.current = e.currentBreakpoint;
173149
}
174-
175-
setState(next);
176-
})
150+
} else if (onResizeRef.current) {
151+
onResizeRef.current(e);
152+
}
153+
154+
const next = {
155+
currentBreakpoint: e.currentBreakpoint,
156+
width,
157+
height,
158+
entry,
159+
};
160+
161+
if (shouldUpdateRef.current && !shouldUpdateRef.current(next)) return;
162+
163+
if (
164+
!shouldUpdateRef.current &&
165+
breakpoints &&
166+
updateOnBreakpointChange
167+
) {
168+
setState((prev) =>
169+
prev.currentBreakpoint !== next.currentBreakpoint ? next : prev
170+
);
171+
return;
172+
}
173+
174+
setState(next);
175+
});
177176
});
178177

179178
observe();
180179

181180
return () => {
182181
unobserve();
183-
if (rafId) window.cancelAnimationFrame(rafId);
182+
if (raf) cancelAnimationFrame(raf);
184183
};
185184
// eslint-disable-next-line react-hooks/exhaustive-deps
186185
}, [

0 commit comments

Comments
 (0)