Skip to content

Commit

Permalink
feat: create callback event after component loads (#28)
Browse files Browse the repository at this point in the history
* feat: create callback event after component loads

- Implement a `on:loaded` event with `createEventDispatcher` that
  triggers after the component has been loaded
- Bump dependencies
- Add `eslint-plugin-svelte3` to avoid eslint warning about undeclared
  variables between `<script>` and `<script context="module">`
- Remove .eslintignore as it isn't being used

* docs: document `on:load` #28
  • Loading branch information
Henrique Borges committed May 12, 2020
1 parent e9c1979 commit 1b8609b
Show file tree
Hide file tree
Showing 6 changed files with 653 additions and 399 deletions.
Empty file removed .eslintignore
Empty file.
8 changes: 7 additions & 1 deletion .eslintrc
Original file line number Diff line number Diff line change
@@ -1,7 +1,13 @@
{
"root": true,
"extends": ["eslint:recommended", "prettier", "prettier/standard"],
"plugins": ["prettier", "html"],
"plugins": ["prettier", "html", "svelte3"],
"overrides": [
{
"files": ["**/*.svelte"],
"processor": "svelte3/svelte3"
}
],
"parser": "babel-eslint",
"settings": {
"html/html-extensions": [".html", ".svelte"],
Expand Down
9 changes: 6 additions & 3 deletions Loadable.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@
</script>

<script>
import { onMount, getContext } from 'svelte'
import { onMount, getContext, createEventDispatcher } from 'svelte'
export let delay = 200
export let timeout = null
Expand All @@ -72,6 +72,8 @@
componentProps = rest
}
const dispatch = createEventDispatcher()
const capture = getContext('svelte-loadable-capture')
if (typeof capture === 'function' && ALL_LOADERS.has(loader)) {
capture(loader)
Expand Down Expand Up @@ -125,8 +127,9 @@
state = STATES.SUCCESS
component = LOADED.get(loader)
} else {
onMount(() => {
load()
onMount(async () => {
await load()
dispatch('load')
if (unloader) {
return () => {
LOADED.delete(loader)
Expand Down
14 changes: 14 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,20 @@ If the default slot is used, it's up to the developer to render the component:
</Loadable>
```

### Events

- `on:load`: a function which is executed right after the `<Loadable>` component is loaded.

```html
<Loadable on:load={() => console.log('The component has been loaded')} loader={...} />
```

Otherwise, if your callback contains more code, you can wrap it into a function, and call it without parentheses

```html
<Loadable on:load={callback} loader={...} />
```

### Slots

- `loading`: customizes the loading state;
Expand Down
Loading

0 comments on commit 1b8609b

Please sign in to comment.