Skip to content

Commit 97a433d

Browse files
committed
Use dataset API for interacting with data- attrs
Please consider this PR only a suggestion. There is no functional difference between ```js const isRevealed = el.getAttribute("data-revealed"); ``` and ```js const isRevealed = el.dataset.revealed; ``` but to me the latter reads a little neater. Another nice feature is that `dataset.multipleWords` in JS translates to `data-multiple-words` in HTML.
1 parent 5b6bee1 commit 97a433d

10 files changed

Lines changed: 32 additions & 33 deletions

File tree

src/components/home/UseElixirFor.astro

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -732,9 +732,9 @@ const starConfig = JSON.stringify(
732732
const activeStar = stars[catId] ?? "#746284";
733733

734734
tabs.forEach((t) => {
735-
const match = t.getAttribute("data-tab") === catId;
735+
const match = t.dataset.tab === catId;
736736
if (match) {
737-
t.setAttribute("data-active", "");
737+
t.dataset.active = "";
738738
t.setAttribute("aria-pressed", "true");
739739
} else {
740740
t.removeAttribute("data-active");
@@ -743,7 +743,7 @@ const starConfig = JSON.stringify(
743743
});
744744

745745
cards.forEach((c, i) => {
746-
if (i === activeRow) c.setAttribute("data-active", "");
746+
if (i === activeRow) c.dataset.active = "";
747747
else c.removeAttribute("data-active");
748748
const starPaths = c.querySelectorAll("[data-star] path");
749749
starPaths.forEach((path) => {
@@ -754,7 +754,7 @@ const starConfig = JSON.stringify(
754754
panels.forEach((p) => {
755755
const panel = p.querySelector("[data-panel-cat]");
756756
if (!panel) return;
757-
const match = panel.getAttribute("data-panel-cat") === catId;
757+
const match = panel.dataset.panelCat === catId;
758758
panel.toggleAttribute("data-active", match);
759759
panel.setAttribute("aria-hidden", match ? "false" : "true");
760760
});
@@ -774,7 +774,7 @@ const starConfig = JSON.stringify(
774774

775775
tabs.forEach((t) => {
776776
t.addEventListener("click", () => {
777-
const cat = t.getAttribute("data-tab");
777+
const cat = t.dataset.tab;
778778
if (!cat) return;
779779
suppressSpyUntil = performance.now() + 1000;
780780
setActive(cat);

src/components/ui/FilteredListing.astro

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -96,7 +96,7 @@ const config = JSON.stringify({
9696
};
9797

9898
connectedCallback() {
99-
const raw = this.getAttribute("data-config");
99+
const raw = this.dataset.config;
100100
if (!raw) return;
101101
this.config = JSON.parse(raw) as Config;
102102
this.activeTab = this.config.defaultTab;
@@ -176,14 +176,14 @@ const config = JSON.stringify({
176176

177177
private refreshChildren() {
178178
if (this.tabFilter) {
179-
this.tabFilter.setAttribute("data-active", this.activeTab);
179+
this.tabFilter.dataset.active = this.activeTab;
180180
}
181181
if (this.pagination) {
182182
this.pagination.setAttribute(
183183
"data-total",
184184
String(this.totalPages(this.activeTab)),
185185
);
186-
this.pagination.setAttribute("data-page", String(this.activePage));
186+
this.pagination.dataset.page = String(this.activePage);
187187
}
188188
}
189189

src/components/ui/Pagination.astro

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -136,13 +136,13 @@ const {
136136
}
137137

138138
private getTotalPages(): number {
139-
const n = Number(this.getAttribute("data-total") ?? 1);
140-
return Number.isFinite(n) && n > 0 ? n : 1;
139+
const n = Number(this.dataset.total ?? 1);
140+
return Number.isInteger(n) && n > 0 ? n : 1;
141141
}
142142

143143
private getActivePage(): number {
144-
const n = Number(this.getAttribute("data-page") ?? 1);
145-
return Number.isFinite(n) && n > 0 ? n : 1;
144+
const n = Number(this.dataset.page ?? 1);
145+
return Number.isInteger(n) && n > 0 ? n : 1;
146146
}
147147

148148
private dispatchPageChange(page: number) {

src/components/ui/TabFilter.astro

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -94,9 +94,9 @@ const {
9494
this.querySelectorAll<HTMLButtonElement>("button[data-id]").forEach(
9595
(btn) => {
9696
btn.addEventListener("click", () => {
97-
const id = btn.getAttribute("data-id");
98-
if (!id || id === this.getAttribute("data-active")) return;
99-
this.setAttribute("data-active", id);
97+
const id = btn.dataset.id;
98+
if (!id || id === this.dataset.active) return;
99+
this.dataset.active = id;
100100
this.dispatchEvent(
101101
new CustomEvent("change", {
102102
detail: { id },
@@ -121,12 +121,11 @@ const {
121121
}
122122

123123
private syncActiveStyling() {
124-
const active = this.getAttribute("data-active");
124+
const active = this.dataset.active;
125125
this.querySelectorAll<HTMLButtonElement>("button[data-id]").forEach(
126126
(btn) => {
127-
const match = btn.getAttribute("data-id") === active;
128-
if (match) btn.setAttribute("data-active", "");
129-
else btn.removeAttribute("data-active");
127+
const match = btn.dataset.id === active;
128+
btn.toggleAttribute("data-active", match);
130129
btn.setAttribute("aria-selected", match ? "true" : "false");
131130
},
132131
);

src/pages/blog/index.astro

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -142,7 +142,7 @@ const seo = {
142142

143143
const posts = root.querySelectorAll("[data-post]");
144144
const emptyState = root.querySelector("[data-empty-state]");
145-
const config = JSON.parse(root.getAttribute("data-config") || "{}");
145+
const config = JSON.parse(root.dataset.config || "{}");
146146
const pageSize = config.pageSize || 5;
147147
const counts = config.counts || {};
148148

@@ -153,7 +153,7 @@ const seo = {
153153
const end = start + pageSize;
154154
let matchIdx = 0;
155155
posts.forEach((el) => {
156-
const cat = el.getAttribute("data-category");
156+
const cat = el.dataset.category;
157157
const match = activeTab === "All" || cat === activeTab;
158158
if (!match) {
159159
el.classList.add("hidden");
@@ -162,7 +162,7 @@ const seo = {
162162
const visible = matchIdx >= start && matchIdx < end;
163163
el.classList.toggle("hidden", !visible);
164164
if (!firstRender && visible && !el.hasAttribute("data-revealed")) {
165-
el.setAttribute("data-revealed", "true");
165+
el.dataset.revealed = "true";
166166
}
167167
matchIdx++;
168168
});

src/pages/cases/index.astro

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -113,11 +113,11 @@ const seo = {
113113
if (!root) return;
114114

115115
const caseLists = root.querySelectorAll("[data-case-list]");
116-
const config = JSON.parse(root.getAttribute("data-config") || "{}");
116+
const config = JSON.parse(root.dataset.config || "{}");
117117

118118
function renderCases(activeTab) {
119119
caseLists.forEach((list) => {
120-
const match = list.getAttribute("data-category") === activeTab;
120+
const match = list.dataset.category === activeTab;
121121
list.classList.toggle("hidden", !match);
122122
list.classList.toggle("grid", match);
123123
});

src/pages/install/index.astro

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -274,9 +274,9 @@ const seo = {
274274

275275
function setActive(id) {
276276
tabs.forEach((t) => {
277-
const match = t.getAttribute("data-tab") === id;
277+
const match = t.dataset.tab === id;
278278
if (match) {
279-
t.setAttribute("data-active", "");
279+
t.dataset.active = "";
280280
t.setAttribute("aria-pressed", "true");
281281
} else {
282282
t.removeAttribute("data-active");
@@ -298,7 +298,7 @@ const seo = {
298298
let suppressSpyUntil = 0;
299299
tabs.forEach((t) => {
300300
t.addEventListener("click", () => {
301-
const id = t.getAttribute("data-tab");
301+
const id = t.dataset.tab;
302302
if (!id) return;
303303
suppressSpyUntil = performance.now() + 1000;
304304
setActive(id);

src/pages/learning/index.astro

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -255,7 +255,7 @@ const seo = {
255255
const resources = root.querySelectorAll("[data-resource]");
256256
const bookHeadings = root.querySelectorAll("[data-book-heading]");
257257
const emptyState = root.querySelector("[data-empty-state]");
258-
const config = JSON.parse(root.getAttribute("data-config") || "{}");
258+
const config = JSON.parse(root.dataset.config || "{}");
259259
const counts = config.counts || {};
260260

261261
let firstRender = true;
@@ -266,7 +266,7 @@ const seo = {
266266
heading.classList.toggle("hidden", activeTab !== "books");
267267
});
268268
resources.forEach((el) => {
269-
const category = el.getAttribute("data-category");
269+
const category = el.dataset.category;
270270
const match =
271271
category === activeTab ||
272272
(activeTab === "books" && category === "in-depth-books");
@@ -277,7 +277,7 @@ const seo = {
277277
}
278278
el.classList.remove("hidden");
279279
if (!firstRender && !el.hasAttribute("data-revealed")) {
280-
el.setAttribute("data-revealed", "true");
280+
el.dataset.revealed = "true";
281281
}
282282
el.classList.toggle("xl:flex-row-reverse", matchIdx % 2 === 1);
283283
matchIdx++;

src/scripts/reveal.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ if (document.readyState === "loading") {
5959
}
6060

6161
document.addEventListener("astro:before-swap", () => {
62-
document.documentElement.setAttribute("data-spa-nav", "");
62+
document.documentElement.dataset.spaNav = "";
6363
});
6464

6565
document.addEventListener("astro:page-load", () => {

tests/filter.spec.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ function visiblePosts(page: Page) {
2525
/** Assert every visible post belongs to `category`. */
2626
async function expectAllVisibleInCategory(page: Page, category: string) {
2727
const cats = await visiblePosts(page).evaluateAll((els) =>
28-
els.map((e) => e.getAttribute("data-category")),
28+
els.map((e) => e.dataset.category),
2929
);
3030
expect(cats.length).toBeGreaterThan(0);
3131
for (const c of cats) expect(c).toBe(category);
@@ -137,7 +137,7 @@ test.describe("Learning filtering", () => {
137137
).toBeVisible();
138138
const cats = await page
139139
.locator("[data-resource]:not(.hidden)")
140-
.evaluateAll((els) => els.map((e) => e.getAttribute("data-category")));
140+
.evaluateAll((els) => els.map((e) => e.dataset.category));
141141
expect(cats.length).toBeGreaterThan(0);
142142
for (const c of cats) expect(c).toBe("courses");
143143
});

0 commit comments

Comments
 (0)