Skip to content

Commit d80ea1f

Browse files
author
Matheus Grieger
committed
Updates and improved docs
1 parent c31a119 commit d80ea1f

File tree

4 files changed

+128
-81
lines changed

4 files changed

+128
-81
lines changed

CHANGELOG.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,12 @@
11
# Changelog
22

3+
## v0.4.0
4+
5+
- **NEW** custom loading, loaded and error class naming
6+
- **NEW** support for CORS (thanks to @gerardo-rodriguez)
7+
- **FIX** last update was not present on NPM, somehow
8+
- **FIX** improved docs
9+
310
## v0.3.0
411

512
- **NEW** extra `component` export so you can install the component where needed, not only globally

README.md

Lines changed: 110 additions & 72 deletions
Original file line numberDiff line numberDiff line change
@@ -10,64 +10,60 @@ Component-based image lazy loader for Vue.js 2
1010
[![GitHub pull requests](https://img.shields.io/badge/PR-welcome-green.svg)]()
1111
[![GitHub stars](https://img.shields.io/github/stars/matheusgrieger/vue-clazy-load.svg?style=social&label=Star)]()
1212

13-
This component approach swaps slots based on the image's loading state. Thus allowing you to use anything as a preloader, such as [component frameworks](https://github.com/vuejs/awesome-vue#frameworks)' spinners and progress bars. Such method also enables transitioning between states, a designer's dream.
13+
Swaps between your image and another component when loading images, allowing you to use loaders from [component frameworks](https://github.com/vuejs/awesome-vue#frameworks) such as spinners and progress bars. This method also allows transitioning between the two components.
1414

1515
## Demo
1616

1717
Check out the [example page](https://matheusgrieger.github.io/vue-clazy-load/example/index.html).
1818

1919
## Installation
2020

21-
Select one:
21+
Install with npm or yarn:
2222

23-
1. Install using **npm**
24-
25-
```sh
26-
npm install vue-clazy-load --save
27-
```
28-
You can omit the `--save` option if using `npm@^5`.
29-
30-
You can also use Yarn (recommended) as:
31-
32-
```sh
33-
yarn add vue-clazy-load
34-
```
23+
```sh
24+
npm install vue-clazy-load
25+
yarn add vue-clazy-load
26+
```
3527

36-
Then in your JavaScript file:
28+
Then simply import it to your project through the method that suits you best
3729

30+
* ES6+
3831
```js
39-
import VueClazyLoad from 'vue-clazy-load' // ES6 (Babel and others)
32+
import VueClazyLoad from 'vue-clazy-load'
4033
```
4134

42-
\- or -
43-
35+
* Common/Require
4436
```js
4537
var VueClazyLoad = require('vue-clazy-load')
4638
```
4739

48-
2. Embed script tag
40+
And install into your Vue instance
4941

50-
Locally installed via npm/yarn:
42+
```js
43+
Vue.use(VueClazyLoad)
44+
```
5145

52-
```html
53-
<script src="node_modules/vue-clazy-load/dist/vue-clazy-load.min.js"></script>
54-
```
46+
You can import it into specific components if you don't want to register Clazy Load globally
5547

56-
Or from CDN:
48+
```js
49+
import { VueClazyLoad } from 'vue-clazy-load'
5750

58-
```html
59-
<script src="https://unpkg.com/vue-clazy-load/dist/vue-clazy-load.min.js"></script>
60-
```
51+
export default {
52+
components: {
53+
VueClazyLoad
54+
}
55+
}
56+
```
6157

62-
Lastly, register the component into Vue:
58+
Also available through Unpkg CDN
6359

64-
```js
65-
Vue.use(VueClazyLoad)
60+
```html
61+
<script src="https://unpkg.com/vue-clazy-load/dist/vue-clazy-load.min.js"></script>
6662
```
6763

6864
## Documentation
6965

70-
Using Clazy Load is easy. The HTML code you need is the following:
66+
Clazy Load works without any JS configuration as is, all you need is the basic HTML markup:
7167

7268
```html
7369
<clazy-load src="https://unsplash.it/500">
@@ -77,68 +73,110 @@ Using Clazy Load is easy. The HTML code you need is the following:
7773
</div>
7874
</clazy-load>
7975
```
80-
81-
And no JS code is needed whatsoever, Clazy Load works out of the box.
76+
The only required prop you must set is `src` that must correspond to your image's.
8277

8378
### Props
8479

85-
The component needs some configuration, though. There's only one required option, keeping it pretty simple.
80+
All props supported by Clazy Load are listed below with their types and explanation.
81+
82+
#### `src`
83+
84+
* Type: string
85+
* Default: none
86+
* _Required_
87+
88+
Source of the image to be loaded. Must match your `<img>` tag src.
89+
90+
#### `tag`
91+
92+
* Type: string
93+
* Default: `"div"`
94+
95+
What tag the component should render to.
96+
97+
#### `element`
98+
99+
* Type: string
100+
* Default: `null`
86101

87-
| Prop | Description | Type | Default |
88-
|------|-------------|------|---------|
89-
| src | Image source that will be loaded. | String | _required_ |
90-
| tag | What tag the component will render to, like [vue-router router-link component](https://router.vuejs.org/en/api/router-link.html). | String | div |
91-
| element | Selector for IntersectionObserver's root element. Leave blank to use _viewport_. See below for more details. | String | `null` |
92-
| threshold | Values for IntersectionObserver's threshold option. See below for more details. | Array/Number | `[0, 0.5, 1]` |
93-
| ratio | Element visibility percentage to compare and trigger loading. Must be between 0 and 1. | Number | `0.4` |
94-
| margin | IntersectionObserver's margin option. See original documentation for more details. | String | '0px' |
102+
Selector for Intersecion Observer's root element. Leave blank/null to use _viewport_.
95103

96-
### Classes
104+
#### `threshold`
97105

98-
Custom classes on the Clazy Load component are passed to the rendered output.
106+
* Type: Array | number
107+
* Default: `[0, 0.5, 1]`
99108

100-
Also, the component adds `loading` and `loaded` classes to the **root** element regarding the two possible states, enabling further customization with CSS.
109+
Values for Intersection Observer's threshold option.
110+
111+
#### `ratio`
112+
113+
* Type: number
114+
* Default: `0.4`
115+
116+
Percent of the element that needs to be visible to trigger loading. Must be > 0 and <= 1.
117+
118+
#### `margin`
119+
120+
* Type: string
121+
* Default: `"0px"`
122+
123+
Intersection Observer's margin option.
124+
125+
You can read more on [MDN](https://developer.mozilla.org/en-US/docs/Web/API/Intersection_Observer_API) about every Intersection Observer-specific prop and what they do.
126+
127+
#### `crossorigin`
128+
129+
* Type: string
130+
* Default: `null`
131+
* Valid options: `"anonymous"` | `"use-credentials"`
132+
133+
Sets image's `crossOrigin` option and allows loading external images. Useful for Service Workers and caching.
134+
135+
#### `loadedClass`
136+
137+
* Type: string
138+
* Default: `"loaded"`
139+
140+
#### `loadingClass`
141+
142+
* Type: string
143+
* Default: `"loading"`
144+
145+
#### `errorClass`
146+
147+
* Type: string
148+
* Default: `null`
149+
150+
All classes are added to the **root** element, not the image itself.
101151

102152
### Events
103153

104-
There is currently one event available for you to listen to on the component.
154+
#### `loading`
105155

106-
| Event | Description |
107-
|-------|-------------|
108-
| load | Event emitted when image finishes loading |
109-
| error | Event emitted if the image fails to load for whatever reason |
156+
Image started loading and placeholder is visible.
110157

111-
### Sub-components and elements
158+
#### `load`
112159

113-
The reason the component has a `src` prop while the `<img>` does as well is to unbind them and allow you to use whatever you want in the `default` slot. For instance, a simple case in which you would separate the image from the loader:
160+
* Param: native load event
114161

115-
```html
116-
<clazy-load src="imgsrc">
117-
<figure class="image-wrapper">
118-
<img src="imgsrc">
119-
</figure>
120-
<div class="preloader-wrapper" slot="placeholder">
121-
<preloader-component></preloader-component>
122-
</div>
123-
</clazy-load>
124-
```
162+
Image finished loading and is now visible.
163+
164+
#### `error`
125165

126-
Another case would be to use transitions. Check out the [example](example/index.html) for details in using transitions.
166+
* Param: native error event
127167

128-
### Scroll watching behavior
168+
Could not load image. **Image is not shown, placeholder still visible.**
129169

130-
Clazy Load uses the new IntersectionObserver API to watch for element visibility on screen. This way it is not only optimized due to use native browser API, it has no proprietary code or gimmicks watching for scroll and making checks, so it will be easier to maintain.
170+
### Compatibility
131171

132-
The downside is that this API is quite recent, so there still is small browser compatibility. Gladly, [Polyfill.io](https://polyfill.io/) covers this use case, and if you need to support older browsers, such as IE, you can include the following tag in your page:
172+
Vue Clazy Load uses the Intersection Observer API to watch for the element visibility on screen. The advantages are native optimization from each browser and no need to implement a custom solution that may be buggy and increase file size. The only caveat to this approach is that this API is quite new, so older browsers do not support it.
173+
174+
If your application needs to be backwards compatible with IE and others, there are polyfills available. I personally recommend [Polyfill.io](https://polyfill.io/). You can check their documentation on how to add it to your website, or simply include the following tag if you're not using any other polyfills:
133175

134176
```html
135177
<script src="https://cdn.polyfill.io/v2/polyfill.min.js?features=IntersectionObserver"></script>
136178
```
137179

138-
Or simply add `IntersectionObserver` to the `?features=` query of the URL if already using Polyfill.
139-
140-
The two configurable props `element` and `threshold` are bound to IntersectionObserver. `element` is used in a `document.querySelector` to pass the element to the `root` option, and `threshold` is used as it is. More details on those two options are available on [MDN](https://developer.mozilla.org/en-US/docs/Web/API/Intersection_Observer_API).
141-
142180
## Changelog
143181

144182
Check out [changelog](CHANGELOG.md) file.
@@ -149,10 +187,10 @@ Check [roadmap](ROADMAP.md) file.
149187

150188
## Contributing
151189

152-
Issues, questions and pull requests are always welcome. Just don't be unpleasant.
190+
Issues, questions and feature requests are welcome. If you can cover some problem, pull requests are also very welcome!
153191

154192
## License
155193

156194
[MIT](http://opensource.org/licenses/MIT)
157195

158-
Copyright (c) 2017, Matheus Grieger
196+
Copyright (c) 2017-2018, Matheus Grieger

ROADMAP.md

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,6 @@ These are planned features for Vue Clazy Load in the future.
44

55
---
66

7-
## 0.x.x
8-
9-
- Improved documentation
10-
- Custom state class naming
11-
127
## 1.0.0
138

149
- Remove Webpack in favor of Vue CLI Service, targetted as `lib`

src/clazy-load.js

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,10 @@ const ClazyLoadComponent = {
6868
*/
6969
crossorigin: {
7070
type: String,
71-
default: null
71+
default: null,
72+
validator(value) {
73+
return value === 'anonymous' || value === 'use-credentials'
74+
}
7275
},
7376
/**
7477
* Class added to element when it finishes loading
@@ -92,7 +95,8 @@ const ClazyLoadComponent = {
9295
* @type {String}
9396
*/
9497
errorClass: {
95-
type: String
98+
type: String,
99+
default: null
96100
}
97101
},
98102
data() {
@@ -107,6 +111,9 @@ const ClazyLoadComponent = {
107111
* Start loading image
108112
*/
109113
load() {
114+
// emits 'loading' event upwards
115+
this.$emit('loading')
116+
110117
// disconnect observer
111118
// so it doesn't load more than once
112119
this.observer.disconnect()
@@ -153,7 +160,7 @@ const ClazyLoadComponent = {
153160
* Creates IntersectionObserver instance and observe current element
154161
*/
155162
observe() {
156-
let options = {
163+
const options = {
157164
threshold: this.threshold,
158165
root: this.element ? document.querySelector(this.element) : null,
159166
rootMargin: this.margin
@@ -205,4 +212,4 @@ const ClazyLoad = {
205212
export default ClazyLoad
206213

207214
// Component object
208-
export const component = ClazyLoadComponent
215+
export const VueClazyLoad = ClazyLoadComponent

0 commit comments

Comments
 (0)