MCPcopy Create free account
hub / github.com/CommandCodeAI/BaseAI / usePreventLayoutShift

Function usePreventLayoutShift

apps/baseai.dev/src/components/Code.tsx:253–284  ·  view source on GitHub ↗
()

Source from the content-addressed store, hash-verified

251}
252
253function usePreventLayoutShift() {
254 let positionRef = useRef<HTMLElement>(null);
255 let rafRef = useRef<number | undefined>(undefined);
256
257 useEffect(() => {
258 return () => {
259 if (typeof rafRef.current !== 'undefined') {
260 window.cancelAnimationFrame(rafRef.current);
261 }
262 };
263 }, []);
264
265 return {
266 positionRef,
267 preventLayoutShift(callback: () => void) {
268 if (!positionRef.current) {
269 return;
270 }
271
272 let initialTop = positionRef.current.getBoundingClientRect().top;
273
274 callback();
275
276 rafRef.current = window.requestAnimationFrame(() => {
277 let newTop =
278 positionRef.current?.getBoundingClientRect().top ??
279 initialTop;
280 window.scrollBy(0, newTop - initialTop);
281 });
282 }
283 };
284}
285
286const usePreferredLanguageStore = create<{
287 preferredLanguages: Array<string>;

Callers 1

useTabGroupPropsFunction · 0.85

Calls

no outgoing calls

Tested by

no test coverage detected