diff --git a/src/components/Visible/Visible.svelte b/src/components/Visible/Visible.svelte index 38e4ec75..d48a3be5 100644 --- a/src/components/Visible/Visible.svelte +++ b/src/components/Visible/Visible.svelte @@ -9,26 +9,14 @@ * Useful for loading expensive images or other media and then keeping them around once they're first loaded. */ once?: boolean; - /** - * Set Intersection Observer [rootMargin](https://developer.mozilla.org/en-US/docs/Web/API/Intersection_Observer_API#rootmargin) `top`. - * Specify a pixel value. - */ - top?: number; - /** - * Set Intersection Observer [rootMargin](https://developer.mozilla.org/en-US/docs/Web/API/Intersection_Observer_API#rootmargin) `bottom`. - * Specify a pixel value. - */ - bottom?: number; - /** - * Set Intersection Observer [rootMargin](https://developer.mozilla.org/en-US/docs/Web/API/Intersection_Observer_API#rootmargin) `left`. - * Specify a pixel value. - */ - left?: number; - /** - * Set Intersection Observer [rootMargin](https://developer.mozilla.org/en-US/docs/Web/API/Intersection_Observer_API#rootmargin) `right`. - * Specify a pixel value. - */ - right?: number; + /** Set Intersection Observer [rootMargin](https://developer.mozilla.org/en-US/docs/Web/API/Intersection_Observer_API#rootmargin) `top` with units. Accepted units are `px` or `%`. */ + top?: string; + /** Set Intersection Observer [rootMargin](https://developer.mozilla.org/en-US/docs/Web/API/Intersection_Observer_API#rootmargin) `bottom` with units. Accepted units are `px` or `%`. */ + bottom?: string; + /** Set Intersection Observer [rootMargin](https://developer.mozilla.org/en-US/docs/Web/API/Intersection_Observer_API#rootmargin) `left` with units. Accepted units are `px` or `%`. */ + left?: string; + /** Set Intersection Observer [rootMargin](https://developer.mozilla.org/en-US/docs/Web/API/Intersection_Observer_API#rootmargin) `right` with units. Accepted units are `px` or `%`. */ + right?: string; /** Set the Intersection Observer [threshold](https://developer.mozilla.org/en-US/docs/Web/API/Intersection_Observer_API#threshold). */ threshold?: number; children?: Snippet<[boolean]>; @@ -36,48 +24,25 @@ let { once = false, - top = 0, - bottom = 0, - left = 0, - right = 0, + top = '0px', + bottom = '0px', + left = '0px', + right = '0px', threshold = 0, children, }: Props = $props(); let visible = $state(false); - let visibleOnce = $state(false); let container: HTMLElement | undefined = $state(undefined); - let observer: IntersectionObserver | undefined = $state(undefined); - - // function scrollHandler() { - // // Only trigger if `visibleOnce` is false - // if (container && observer && !visibleOnce) { - // const bcr = container.getBoundingClientRect(); - // visible = - // bcr.bottom + bottom > 0 && - // bcr.right + right > 0 && - // bcr.top - top < window.innerHeight && - // bcr.left - left < window.innerWidth; - - // if (container) { - // // console.log('Observing container:', observer); - // observer.observe(container); - // } - - // // If `once` is true, set `visibleOnce` to true once `visible` becomes true - // if (once && visible) visibleOnce = true; - // } - // } - onMount(() => { if (typeof IntersectionObserver !== 'undefined') { - const rootMargin = `${top}px ${right}px ${bottom}px ${left}px`; + const rootMargin = `${bottom} ${left} ${top} ${right}`; const observer = new IntersectionObserver( (entries) => { visible = entries[0].isIntersecting; - if (visible && once && container && observer) { + if (visible && once && container) { observer.unobserve(container); } }, @@ -86,27 +51,17 @@ threshold, } ); - - // Unobserve when the component is unmounted + if (container) observer.observe(container); return () => { - if (container && observer) observer.unobserve(container); + if (container) observer.observe(container); }; } }); - $effect(() => { - console.log('visible:', visible); - }); + $effect(() => console.log('visible', visible)); - - -