diff --git a/.changeset/gold-boats-dream.md b/.changeset/gold-boats-dream.md new file mode 100644 index 00000000..5fc78dcc --- /dev/null +++ b/.changeset/gold-boats-dream.md @@ -0,0 +1,5 @@ +--- +'@reuters-graphics/graphics-components': patch +--- + +Updates Visible to allow unit specification for top, bototm, right, left and adds a demo diff --git a/src/components/Visible/Visible.mdx b/src/components/Visible/Visible.mdx index f3c25b59..bb5d2bcd 100644 --- a/src/components/Visible/Visible.mdx +++ b/src/components/Visible/Visible.mdx @@ -19,7 +19,8 @@ By default, `visible` will switch between `false` and `true` whenever the elemen import { Visible } from '@reuters-graphics/graphics-components'; - + + {#snippet children(visible)} {#if visible}

Visible!

diff --git a/src/components/Visible/Visible.stories.svelte b/src/components/Visible/Visible.stories.svelte index 2529755b..cefa60d8 100644 --- a/src/components/Visible/Visible.stories.svelte +++ b/src/components/Visible/Visible.stories.svelte @@ -2,6 +2,7 @@ import { defineMeta } from '@storybook/addon-svelte-csf'; import Visible from './Visible.svelte'; + import VisibleDemo from './demo/VisibleDemo.svelte'; const { Story } = defineMeta({ title: 'Components/Utilities/Visible', @@ -9,14 +10,6 @@ }); - - - {#snippet children(visible)} - {#if visible} -

Visible!

- {:else} -

Not yet visible.

- {/if} - {/snippet} -
+ + diff --git a/src/components/Visible/Visible.svelte b/src/components/Visible/Visible.svelte index e1dd30a4..b95bcc63 100644 --- a/src/components/Visible/Visible.svelte +++ b/src/components/Visible/Visible.svelte @@ -9,14 +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`. */ - top?: number; - /** Set Intersection Observer [rootMargin](https://developer.mozilla.org/en-US/docs/Web/API/Intersection_Observer_API#rootmargin) `bottom`. */ - bottom?: number; - /** Set Intersection Observer [rootMargin](https://developer.mozilla.org/en-US/docs/Web/API/Intersection_Observer_API#rootmargin) `left`. */ - left?: number; - /** Set Intersection Observer [rootMargin](https://developer.mozilla.org/en-US/docs/Web/API/Intersection_Observer_API#rootmargin) `right`. */ - right?: number; + /** Set Intersection Observer [rootMargin](https://developer.mozilla.org/en-US/docs/Web/API/Intersection_Observer_API#rootmargin) `top` with units. Units must be `px` or other [absolute lengths units](https://arc.net/l/quote/jkukcxqq), or `%`. */ + top?: string; + /** Set Intersection Observer [rootMargin](https://developer.mozilla.org/en-US/docs/Web/API/Intersection_Observer_API#rootmargin) `bottom` with units. Units must be `px` or other [absolute lengths units](https://arc.net/l/quote/jkukcxqq), or `%`. */ + bottom?: string; + /** Set Intersection Observer [rootMargin](https://developer.mozilla.org/en-US/docs/Web/API/Intersection_Observer_API#rootmargin) `left` with units. Units must be `px` or other [absolute lengths units](https://arc.net/l/quote/jkukcxqq), or `%`. */ + left?: string; + /** Set Intersection Observer [rootMargin](https://developer.mozilla.org/en-US/docs/Web/API/Intersection_Observer_API#rootmargin) `right` with units. Units must be `px` or other [absolute lengths units](https://arc.net/l/quote/jkukcxqq), 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]>; @@ -24,10 +24,10 @@ let { once = false, - top = 0, - bottom = 0, - left = 0, - right = 0, + top = '0px', + bottom = '0px', + left = '0px', + right = '0px', threshold = 0, children, }: Props = $props(); @@ -37,7 +37,7 @@ onMount(() => { if (typeof IntersectionObserver !== 'undefined') { - const rootMargin = `${bottom}px ${left}px ${top}px ${right}px`; + const rootMargin = `${bottom} ${left} ${top} ${right}`; const observer = new IntersectionObserver( (entries) => { @@ -53,28 +53,18 @@ ); if (container) observer.observe(container); return () => { - if (container) observer.observe(container); + if (container) observer.unobserve(container); }; } - function handler() { - if (container) { - const bcr = container.getBoundingClientRect(); - visible = - bcr.bottom + bottom > 0 && - bcr.right + right > 0 && - bcr.top - top < window.innerHeight && - bcr.left - left < window.innerWidth; - } - if (visible && once) { - window.removeEventListener('scroll', handler); - } - } - window.addEventListener('scroll', handler); - return () => window.removeEventListener('scroll', handler); }); -
+
{#if children} {@render children(visible)} {/if} diff --git a/src/components/Visible/demo/VisibleDemo.svelte b/src/components/Visible/demo/VisibleDemo.svelte new file mode 100644 index 00000000..02f65c77 --- /dev/null +++ b/src/components/Visible/demo/VisibleDemo.svelte @@ -0,0 +1,46 @@ + + + + + + + + + + + + {#snippet children(visible)} + {#if visible} + + {:else} +

Not yet visible.

+ {/if} + {/snippet} +
+ + + + +