Skip to content

Commit 1f90b2d

Browse files
committed
Document request lifecycle events
1 parent 2360f4c commit 1f90b2d

File tree

1 file changed

+18
-0
lines changed

1 file changed

+18
-0
lines changed

README.md

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,24 @@ The server must respond with an HTML fragment to replace the `include-fragment`
4444

4545
If the URL fails to load, the `include-fragment` element is left in the page and tagged with an `is-error` CSS class that can be used for styling.
4646

47+
### Events
48+
49+
Request lifecycle events are dispatched on the `<include-fragment>` element.
50+
51+
- `loadstart` - The server fetch has started.
52+
- `load` - The request completed successfully.
53+
- `error` - The request failed.
54+
- `loadend` - The request has completed.
55+
56+
```js
57+
const loader = document.querySelector('include-fragment')
58+
const container = loader.parentElement
59+
loader.addEventListener('loadstart', () => container.classList.add('is-loading'))
60+
loader.addEventListener('loadend', () => container.classList.remove('is-loading'))
61+
loader.addEventListener('load', () => container.classList.add('is-success'))
62+
loader.addEventListener('error', () => container.classList.add('is-error'))
63+
```
64+
4765
### Options
4866

4967
Attribute | Options | Description

0 commit comments

Comments
 (0)