|
| 1 | +import { createEffect, createMemo, createSignal, onCleanup } from 'solid-js'; |
| 2 | +import { createStore, reconcile } from 'solid-js/store'; |
| 3 | + |
| 4 | +type ObserverRect = Omit<DOMRectReadOnly, 'toJSON'>; |
| 5 | + |
| 6 | +const defaultState: ObserverRect = { |
| 7 | + x: 0, |
| 8 | + y: 0, |
| 9 | + width: 0, |
| 10 | + height: 0, |
| 11 | + top: 0, |
| 12 | + left: 0, |
| 13 | + bottom: 0, |
| 14 | + right: 0, |
| 15 | +}; |
| 16 | + |
| 17 | +/** |
| 18 | + * A hook that returns a resize observer. |
| 19 | + * |
| 20 | + * @example |
| 21 | + * ```ts |
| 22 | + * const [ref, rectStore] = useResizeObserver(); |
| 23 | + * |
| 24 | + * return (<div ref={ref}>{rectStore.width}</div>); |
| 25 | + */ |
| 26 | +export function useResizeObserver<T extends HTMLElement = any>(options?: ResizeObserverOptions) { |
| 27 | + let frameID = 0; |
| 28 | + const [ref, setRef] = createSignal<T | null>(null); |
| 29 | + |
| 30 | + /** |
| 31 | + * SolidJS stores always need a deeper object for the 'setter' and 'reconcile' to work, |
| 32 | + * hence why I wrapped it in { rect: ObserverRect }. When originally, it sohuld just be `ObserverRect`. |
| 33 | + */ |
| 34 | + const [rectStore, setRectStore] = createStore<{ rect: ObserverRect }>({ rect: defaultState }); |
| 35 | + |
| 36 | + const observer = createMemo(() => { |
| 37 | + if (typeof window === 'undefined') { |
| 38 | + return null; |
| 39 | + } |
| 40 | + |
| 41 | + return new ResizeObserver((entries: any) => { |
| 42 | + const entry = entries[0]; |
| 43 | + |
| 44 | + if (entry) { |
| 45 | + cancelAnimationFrame(frameID); |
| 46 | + |
| 47 | + frameID = requestAnimationFrame(() => { |
| 48 | + if (ref()) { |
| 49 | + setRectStore('rect', reconcile(entry.contentRect)); |
| 50 | + } |
| 51 | + }); |
| 52 | + } |
| 53 | + }); |
| 54 | + }); |
| 55 | + |
| 56 | + createEffect(() => { |
| 57 | + if (ref()) { |
| 58 | + observer()?.observe(ref()!, options); |
| 59 | + } |
| 60 | + |
| 61 | + onCleanup(() => { |
| 62 | + observer()?.disconnect(); |
| 63 | + |
| 64 | + if (frameID) { |
| 65 | + cancelAnimationFrame(frameID); |
| 66 | + } |
| 67 | + }); |
| 68 | + }); |
| 69 | + |
| 70 | + return [setRef, rectStore] as const; |
| 71 | +} |
| 72 | + |
| 73 | +/** |
| 74 | + * A hook that returns the current size of an element. |
| 75 | + * |
| 76 | + * Plus points on SolidJS for this: `width()` and `height()` only emit a change when it actually changed its value. |
| 77 | + * This is thanks to useResizeObserver using a `store` with `reconcile` under the hood. |
| 78 | + * |
| 79 | + * @example |
| 80 | + * ```ts |
| 81 | + * const { ref, width, height } = useElementSize(); |
| 82 | + * |
| 83 | + * return (<div ref={ref}>{width()} | {height()}</div>); |
| 84 | + */ |
| 85 | +export function useElementSize<T extends HTMLElement = any>(options?: ResizeObserverOptions) { |
| 86 | + const [ref, rectStore] = useResizeObserver<T>(options); |
| 87 | + |
| 88 | + return { |
| 89 | + ref, |
| 90 | + width: createMemo(() => rectStore.rect.width), |
| 91 | + height: createMemo(() => rectStore.rect.height), |
| 92 | + }; |
| 93 | +} |
0 commit comments