Skip to content

Commit

Permalink
docs: update the readme with ts examples
Browse files Browse the repository at this point in the history
  • Loading branch information
maciekgrzybek committed Jun 13, 2022
1 parent e5f28b2 commit 6e9be81
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 15 deletions.
31 changes: 17 additions & 14 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -47,10 +47,10 @@ This is the most basic use case for `svelte-inview`. Just add the action to the

```html

<script>
<script lang="ts">
import { inview } from 'svelte-inview';
let isInView;
let isInView: boolean;
const options = {};
</script>

Expand Down Expand Up @@ -78,16 +78,17 @@ This is the most basic use case for `svelte-inview`. Just add the action to the
Svelte Inview lets you easily lazy load images. For a better UX we can pass a `rootMargin="50px"` props, so the image will be loaded when scroll is 50px before the viewport. After it appears in the viewport, you don't want to observe it anymore, hence the `unobserveOnEnter` props set to true.

```html
<script>
<script lang="ts">
import { inview } from 'svelte-inview';
import type { ObserverEventDetails, Options } from 'svelte-inview';
let isInView;
const options = {
const options: Options = {
rootMargin: '50px',
unobserveOnEnter: true,
};
const handleChange = ({ detail }) => (isInView = detail.inView);
const handleChange = ({ detail }: CustomEvent<ObserverEventDetails>) => (isInView = detail.inView);
</script>

<div use:inview="{options}" on:change="{handleChange}">
Expand All @@ -104,11 +105,12 @@ Svelte Inview lets you easily lazy load images. For a better UX we can pass a `r
You can play/pause a video when it's in/out of the viewport. Simply pass correct methods in `on:enter` and `on:leave` callbacks.

```html
<script>
<script lang="ts">
import { inview } from 'svelte-inview';
import type { ObserverEventDetails } from 'svelte-inview';
let isInView;
let videoRef;
let isInView: boolean;
let videoRef: HTMLElement;
</script>

<div
Expand All @@ -128,25 +130,26 @@ You can also add some cool animations when an element enters the viewport. To ma

```html

<script>
<script lang="ts">
import { inview } from 'svelte-inview';
import type { ObserverEventDetails, ScrollDirection, Options } from 'svelte-inview';
let isInView;
let scrollDirection;
const options = {
let isInView: boolean;
let scrollDirection: ScrollDirection;
const options: Options = {
rootMargin: '-50px',
unobserveOnEnter: true,
};
const handleChange = ({ detail }) => {
const handleChange = ({ detail }: CustomEvent<ObserverEventDetails>) => {
isInView = detail.inView;
scrollDirection = detail.scrollDirection.vertical;
};
</script>

<div use:inview={options} on:change={handleChange}>
<div
class:animate={inView}
class:animate={isInView}
class:animateFromBottom={scrollDirection === 'down'}
class:animateFromTop={scrollDirection === 'top'}>
Animate me!
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "svelte-inview",
"version": "3.0.0",
"version": "3.0.1",
"description": "A Svelte action that monitors an element enters or leaves the viewport or a parent element. Performant and efficient thanks to using Intersection Observer under the hood.",
"homepage": "https://github.com/maciekgrzybek/svelte-inview",
"bugs": "https://github.com/maciekgrzybek/svelte-inview/issues",
Expand Down

0 comments on commit 6e9be81

Please sign in to comment.