Skip to content

Commit e795624

Browse files
committed
Update aria-current="page" using custom element
1 parent 8e50149 commit e795624

File tree

4 files changed

+42
-4
lines changed

4 files changed

+42
-4
lines changed

assets/js/app.js

+1
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import topbar from "../vendor/topbar";
1111

1212
import "./customElements/enhanced-navigation";
1313
import "./customElements/filter-items";
14+
import "./customElements/phx-current-page";
1415
import "./customElements/wasm-simple-html";
1516
import "./customElements/wasm-chunked-html";
1617
import "./customElements/wasm-state-machine";

assets/js/customElements/filter-items.js

-1
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@ customElements.define('filter-items', class extends HTMLElement {
1414
}
1515

1616
connectedCallback() {
17-
console.log(this);
1817
const aborter = new AbortController();
1918
const signal = aborter.signal;
2019
const id = this.id || `filter-items-${crypto.randomUUID()}`;
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
customElements.define('phx-current-page', class extends HTMLElement {
2+
linksWithHref(href) {
3+
return Array.from(this.querySelectorAll("a[href]"))
4+
.filter(a => a.href === href);
5+
}
6+
linksWithCurrentPage() {
7+
return Array.from(this.querySelectorAll("a[aria-current=page]"));
8+
}
9+
10+
connectedCallback() {
11+
const aborter = new AbortController();
12+
const signal = aborter.signal;
13+
14+
const update = () => {
15+
for (const el of this.linksWithCurrentPage()) {
16+
el.removeAttribute("aria-current");
17+
}
18+
for (const el of this.linksWithHref(window.location.href)) {
19+
el.setAttribute("aria-current", "page");
20+
}
21+
}
22+
23+
requestAnimationFrame(update);
24+
window.addEventListener("phx:page-loading-stop", update, { signal });
25+
26+
this.disconnectedCallback = () => {
27+
aborter.abort();
28+
}
29+
}
30+
31+
// disconnectedCallback() {
32+
// if (this.aborter) {
33+
// this.aborter.abort();
34+
// this.aborter = undefined;
35+
// }
36+
// }
37+
});

lib/components_guide_web/components/browser_compat_components.ex

+4-3
Original file line numberDiff line numberDiff line change
@@ -132,15 +132,16 @@ defmodule ComponentsGuideWeb.BrowserCompatComponents do
132132
~H"""
133133
<div class="relative grid grid-cols-[max-content_auto] not-prose bg-white/5">
134134
<nav class="flex flex-col text-left bg-white/5">
135-
<filter-items class="contents">
135+
<filter-items class="contents" id={@id <> "-filter-items"} phx-update="ignore">
136136
<input
137137
type="search"
138138
placeholder="Filter"
139139
class="bg-black text-white border-0 px-4 font-mono"
140140
id={@id <> "-search"}
141-
phx-update="ignore"
142141
/>
143-
<%= render_slot(@nav_items) %>
142+
<phx-current-page class="contents">
143+
<%= render_slot(@nav_items) %>
144+
</phx-current-page>
144145
</filter-items>
145146
</nav>
146147
<article class="">

0 commit comments

Comments
 (0)