Update hooks.js

This commit is contained in:
Wout De Puysseleir 2023-02-26 12:18:39 -08:00
parent 9e31210184
commit da4979fdfb
No known key found for this signature in database
GPG key ID: 3DE9371B50FEC46A

View file

@ -13,6 +13,13 @@ function parsedProps(el) {
return props ? JSON.parse(props) : {}
}
function extraProps(ref) {
return {
pushEvent: (event, data, callback) => ref.pushEvent(event, data, callback),
goto: href => liveSocket.pushHistoryPatch(href, 'push', ref.el)
}
}
const SvelteComponent = {
mounted() {
const componentName = this.el.getAttribute('data-name')
@ -22,34 +29,18 @@ const SvelteComponent = {
const Component = components[componentName]
if (!Component) {
throw new Error(`Unable to find ${componentName} component. Did you forget to import it into hooks.js?`)
}
const pushEvent = (event, data, callback) => {
this.pushEvent(event, data, callback)
}
const goto = href => {
liveSocket.pushHistoryPatch(href, 'push', this.el)
throw new Error(`Unable to find ${componentName} component.`)
}
this._instance = new Component({
target: this.el,
props: {...parsedProps(this.el), pushEvent, goto},
props: {...parsedProps(this.el), ...extraProps(this)},
hydrate: true
})
},
updated() {
const pushEvent = (event, data, callback) => {
this.pushEvent(event, data, callback)
}
const goto = href => {
liveSocket.pushHistoryPatch(href, 'push', this.el)
}
this._instance.$$set({...parsedProps(this.el), pushEvent, goto})
this._instance.$$set({...parsedProps(this.el), ...extraProps(this)})
},
destroyed() {