| 251 | } |
| 252 | |
| 253 | function 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 | |
| 286 | const usePreferredLanguageStore = create<{ |
| 287 | preferredLanguages: Array<string>; |