diff --git a/src/components/Visible/Visible.mdx b/src/components/Visible/Visible.mdx new file mode 100644 index 00000000..13342628 --- /dev/null +++ b/src/components/Visible/Visible.mdx @@ -0,0 +1,31 @@ +import { Meta, Canvas } from '@storybook/blocks'; + +import * as VisibleStories from './Visible.stories.svelte'; + + + +# Visible + +The `Visible` component wraps around other components or HTML elements and uses the [Intersection Observer API](https://developer.mozilla.org/en-US/docs/Web/API/Intersection_Observer_API) to determine if they are visible on the page. + +This is useful for lazy loading elements, especially expensive media files or components that fetch lots of data. You can also use it trigger animations or play media once a reader scrolls a component into view. + +By default, `visible` will switch between `false` and `true` whenever the element is in our out of view. To trigger this just once, set the prop `once` to `true`. This is useful for loading expensive media when they first come into view and then keeping them around once they're loaded. + +> **Note:** Don't use this for content that's "above the fold" at the top of the page. That'll just slow down the first load of important visible content. + +```svelte + + + + {#if visible} +

Visible!

+ {:else} +

Not yet visible.

+ {/if} +
+``` + + diff --git a/src/components/Visible/Visible.stories.svelte b/src/components/Visible/Visible.stories.svelte index 38068151..2529755b 100644 --- a/src/components/Visible/Visible.stories.svelte +++ b/src/components/Visible/Visible.stories.svelte @@ -1,36 +1,22 @@ - - - - - + + + {#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 4584c12e..e1dd30a4 100644 --- a/src/components/Visible/Visible.svelte +++ b/src/components/Visible/Visible.svelte @@ -1,6 +1,6 @@
- - {@render children?.({ visible })} + {#if children} + {@render children(visible)} + {/if}
diff --git a/src/components/Visible/stories/docs/component.md b/src/components/Visible/stories/docs/component.md deleted file mode 100644 index 8061e116..00000000 --- a/src/components/Visible/stories/docs/component.md +++ /dev/null @@ -1,19 +0,0 @@ -Wrap components or other HTML elements to determine if they are visible on the page using the [Intersection Observer API](https://developer.mozilla.org/en-US/docs/Web/API/Intersection_Observer_API). - -This is really useful for lazy loading elements, especially expensive media files or components that fetch lots of data. You can also use it trigger animations or play media once a reader scrolls a component into view. - -> **Pro tip:** Don't use this for content that's "above the fold" at the top of the page. That'll just slow down the first load of important visible content. - -```svelte - - - - {#if visible} -

Visible!

- {:else} -

Not yet visible.

- {/if} -
-```