Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add events to use:inertia directive #237

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 12 additions & 23 deletions packages/inertia-svelte/src/Link.svelte
Original file line number Diff line number Diff line change
@@ -1,8 +1,5 @@
<script>
import { Inertia, shouldIntercept } from '@inertiajs/inertia'
import { createEventDispatcher } from 'svelte'

const dispatch = createEventDispatcher()
import inertia from './link'

export let
data = {},
Expand All @@ -13,26 +10,18 @@
preserveState = false,
only = [],
headers = {}

function visit(event) {
dispatch('click', event)

if (shouldIntercept(event)) {
event.preventDefault()

Inertia.visit(href, {
data,
method,
preserveScroll,
preserveState,
replace,
only,
headers
})
}
}
</script>

<a {...$$restProps} {href} on:click={visit}>
<a
use:inertia={{ ...$$props }}
{...$$restProps}
{href}
on:click
on:cancelToken
on:start
on:progress
on:finish
on:cancel
on:success>
<slot />
</a>
11 changes: 11 additions & 0 deletions packages/inertia-svelte/src/link.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,17 @@ import { createEventDispatcher } from 'svelte'
export default (node, options = {}) => {
const dispatch = createEventDispatcher()

options.onCancelToken = cancelToken => fireEvent('cancelToken', { detail: { cancelToken } })
options.onStart = visit => fireEvent('start', { cancelable: true, detail: { visit } })
options.onProgress = progress => fireEvent('progress', { detail: { progress } })
options.onFinish = () => fireEvent('finish')
options.onCancel = () => fireEvent('cancel')
options.onSuccess = page => fireEvent('success', { detail: { page } })

function fireEvent(name, eventOptions) {
return node.dispatchEvent(new CustomEvent(name, eventOptions))
}

function visit(event) {
dispatch('click', event)

Expand Down