aboutsummaryrefslogtreecommitdiff
path: root/frontend
diff options
context:
space:
mode:
authorWilliam Hergès <william@herges.fr>2025-10-03 18:40:45 +0200
committerWilliam Hergès <william@herges.fr>2025-10-03 18:40:45 +0200
commit0de89e6bc6a467b2cc4261ae65464f40119cc0ff (patch)
tree47ca3e894444950152b3ba0cf587f713d5aa7eaa /frontend
parent270f592fc65cd5553ccf7c0dc81431c3efdf67ab (diff)
feat(frontend): use htmx to dynamically navigate between pages
Diffstat (limited to 'frontend')
-rw-r--r--frontend/index.ts26
1 files changed, 26 insertions, 0 deletions
diff --git a/frontend/index.ts b/frontend/index.ts
new file mode 100644
index 0000000..cd3c455
--- /dev/null
+++ b/frontend/index.ts
@@ -0,0 +1,26 @@
+import htmx from "htmx.org";
+
+function setupAnchors() {
+ document.querySelectorAll("a").forEach(e => {
+ if (!e.href.startsWith(window.location.origin)) {
+ e.target = "_blank";
+ return
+ }
+ if (e.hasAttribute("hx-trigger")) return;
+ e.setAttribute("hx-get", e.href)
+ e.setAttribute("hx-trigger", "click")
+ e.setAttribute("hx-target", "#content")
+ e.setAttribute("hx-swap", "outerHTML show:top")
+ htmx.process(e)
+ });
+}
+
+// updating history and window title
+document.addEventListener("htmx:afterSettle", e => {
+ const title = e.detail.xhr.getResponseHeader("Updated-Title")
+ if (title?.length != 0) document.title = title
+ window.history.pushState({}, "", e.detail.pathInfo.finalRequestPath)
+ setupAnchors()
+})
+
+setupAnchors()