Skip to content

Commit 71425bb

Browse files
committed
feat: update readme
1 parent 37c3b34 commit 71425bb

File tree

1 file changed

+77
-60
lines changed

1 file changed

+77
-60
lines changed

README.md

Lines changed: 77 additions & 60 deletions
Original file line numberDiff line numberDiff line change
@@ -53,17 +53,22 @@ export default MyImage;
5353

5454
They rely on CSS and the corresponding CSS file must be imported:
5555

56-
```javascript
57-
import React from 'react';
56+
```vue
57+
<template>
58+
<div>
59+
<MyImage :image="image" />
60+
</div>
61+
</template>
62+
<script lang='ts' setup>
5863
import { LazyLoadImage } from 'vue-lazy-load-image-component';
5964
import 'vue-lazy-load-image-component/lib/style.css';
60-
61-
const MyImage = ({ image }) => (
62-
<LazyLoadImage
63-
alt={image.alt}
64-
effect="blur"
65-
src={image.src} />
66-
);
65+
const image = ref({
66+
alt: 'My image',
67+
height: 100,
68+
src: 'https://example.com/image.jpg',
69+
width: 100,
70+
});
71+
</script>
6772
```
6873

6974
The current available effects are:
@@ -82,21 +87,18 @@ The current available effects are:
8287

8388
## `LazyLoadComponent` usage
8489

85-
```javascript
86-
import React from 'react';
87-
import { LazyLoadComponent } from 'vue-lazy-load-image-component';
88-
import { ArticleContent, ArticleComments } from 'my-app';
89-
90-
const Article = ({ articleId }) => (
90+
```vue
91+
<template>
9192
<div>
92-
<ArticleContent id={articleId} />
9393
<LazyLoadComponent>
94-
<ArticleComments id={articleId} />
94+
<MyComponent />
9595
</LazyLoadComponent>
9696
</div>
97-
);
98-
99-
export default Article;
97+
</template>
98+
<script lang='ts' setup>
99+
import { LazyLoadComponent } from 'vue-lazy-load-image-component';
100+
import MyComponent from './MyComponent.vue';
101+
</script>
100102
```
101103

102104
### Props
@@ -118,30 +120,37 @@ When you have many elements to lazy load in the same page, you might get poor pe
118120

119121
For example, if we have an `App` which renders a `Gallery`, we would wrap the `Gallery` component with the HOC.
120122

121-
```javascript
122-
import React from 'react';
123-
import { LazyLoadImage, trackWindowScroll }
124-
from 'vue-lazy-load-image-component';
125-
126-
const Gallery = ({ images, scrollPosition }) => (
123+
```vue
124+
<template>
127125
<div>
128-
{images.map((image) =>
129-
<LazyLoadImage
130-
key={image.key}
131-
alt={image.alt}
132-
height={image.height}
133-
// Make sure to pass down the scrollPosition,
134-
// this will be used by the component to know
135-
// whether it must track the scroll position or not
136-
scrollPosition={scrollPosition}
137-
src={image.src}
138-
width={image.width} />
139-
)}
126+
<GalleryWithScrollTracking :images="images" />
140127
</div>
141-
);
128+
</template>
129+
<script lang='ts' setup>
130+
import { trackWindowScroll } from 'vue-lazy-load-image-component';
131+
import Gallery from './Gallery.vue';
142132
// Wrap Gallery with trackWindowScroll HOC so it receives
143133
// a scrollPosition prop to pass down to the images
144-
export default trackWindowScroll(Gallery);
134+
const images = ref([
135+
{
136+
alt: 'My image',
137+
height: 100,
138+
src: 'https://example.com/image.jpg',
139+
width: 100,
140+
scrollPosition:{x:0,y:0}
141+
},
142+
{
143+
alt: 'My image 2',
144+
height: 100,
145+
src: 'https://example.com/image2.jpg',
146+
width: 100,
147+
scrollPosition:{x:0,y:0}
148+
},
149+
]);
150+
151+
const GalleryWithScrollTracking = trackWindowScroll(Gallery);
152+
153+
</script>
145154
```
146155

147156
You must set the prop `scrollPosition` to the lazy load components. This way, they will know the scroll/resize events are tracked by a parent component and will not subscribe to them.
@@ -179,27 +188,35 @@ Imagine you are going to lazy-load an image you have already loaded in the same
179188

180189
Maybe the following code snippet will make it more clear:
181190

182-
```javascript
183-
import React from 'react';
184-
import { LazyLoadImage, trackWindowScroll }
185-
from 'vue-lazy-load-image-component';
186-
187-
const Gallery = ({ images, scrollPosition }) => (
191+
```vue
192+
<template>
188193
<div>
189-
// We are loading landscape.jpg here
190194
<img src="/landscape.jpg" alt="Beautiful landscape" />
191-
{images.map((image) =>
192-
<LazyLoadImage
193-
key={image.key}
194-
alt={image.alt}
195-
scrollPosition={scrollPosition}
196-
src={image.src}
197-
// If the image we are creating here has the same src than before,
198-
// we can directly display it with no need to lazy-load.
199-
visibleByDefault={image.src === '/landscape.jpg'} />
200-
)}
195+
<GalleryWithScrollTracking :images="images" />
201196
</div>
202-
);
203-
204-
export default trackWindowScroll(Gallery);
197+
</template>
198+
<script lang='ts' setup>
199+
import { trackWindowScroll } from 'vue-lazy-load-image-component';
200+
import Gallery from './Gallery.vue';
201+
const images = ref([
202+
{
203+
alt: 'My image',
204+
height: 100,
205+
src: 'https://example.com/image.jpg',
206+
width: 100,
207+
},
208+
{
209+
alt: 'My image 2',
210+
height: 100,
211+
src: 'https://example.com/image2.jpg',
212+
width: 100,
213+
// If the image we are creating here has the same src than before,
214+
// we can directly display it with no need to lazy-load.
215+
visibleByDefault:image.src === '/landscape.jpg',
216+
},
217+
]);
218+
219+
const GalleryWithScrollTracking = trackWindowScroll(Gallery);
220+
221+
</script>
205222
```

0 commit comments

Comments
 (0)