-
Notifications
You must be signed in to change notification settings - Fork 1
/
htmx-template.js
44 lines (40 loc) · 1.74 KB
/
htmx-template.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
/**
* HTMX Template Extension
* by Katrina Scialdone (https://github.com/katrinakitten)
*
* Enables HTMX to retrieve the contents of <template> elements rather than make an HTTP request.
*
* <p hx-get="template:lorem" hx-trigger="load delay:3s">Loading...</p>
* <template id="lorem">Lorem ipsum dolor sit amet...</template>
*
* Note that in order to respect HTMX swapping rules, the template's innerHTML is used, rather than
* "properly" making a copy of its content nodes. This means, among other things, that <slot>
* elements will not work - this is not a web component, but rather a "faked" HTTP response body
* that is handled by HTMX in the same way as any other request.
*/
{
let htmxApi
htmx.defineExtension('template', {
init(api) { htmxApi = api },
onEvent(name, evt) {
let path = evt.detail.path ?? evt.detail.requestConfig?.path
if(path?.startsWith('template:')) switch(name) {
// On beforeRequest, stop the normal process and manually continue without sending the request
case 'htmx:beforeRequest':
htmxApi.triggerEvent(evt.detail.elt, 'htmx:beforeSend', evt.detail.responseInfo)
evt.detail.xhr.onload()
return false
// On beforeSwap, replace the "request body" with the targeted template's innerHTML
case 'htmx:beforeSwap':
let tempName = path.slice(9)
let template = document.querySelector(`template:is(#${tempName}, [name="${tempName}"], [data-name="${tempName}"])`)
if(!template) console.error(`Requested template '${tempName}' does not exist`)
else {
evt.detail.shouldSwap = !!template
evt.detail.serverResponse = template?.innerHTML
}
return true
}
}
})
}