Skip to content

Commit 3d2a28e

Browse files
authored
Avoid :has selector in CSS. (#395)
This changes how section visibility is controlled. All sections are still `display:none` by default. Rather than becoming `display:block` when they match `:target`, they now become `display:block` if their ID matches the value of the root element's `data-visible-section` attribute - we hardcode the four section names "home", "running", "summary" and "details". This lets us default to the home section just by setting `<html data-visible-section="home">` in index.html, and we can avoid the `:has` selector. Now Speedometer can run in older browsers which don't support `:has`.
1 parent 406042a commit 3d2a28e

File tree

3 files changed

+12
-4
lines changed

3 files changed

+12
-4
lines changed

index.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
<!DOCTYPE html>
2-
<html>
2+
<html data-visible-section="home">
33
<head>
44
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
55
<meta name="viewport" content="width=850" />

resources/main.css

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -304,9 +304,11 @@ section {
304304
border-radius: 20px;
305305
}
306306

307-
section:target,
308307
section.visible,
309-
body:not(body:has(section:target)) #home {
308+
:root[data-visible-section="home"] #home,
309+
:root[data-visible-section="running"] #running,
310+
:root[data-visible-section="summary"] #summary,
311+
:root[data-visible-section="details"] #details {
310312
display: block;
311313
}
312314

@@ -468,7 +470,7 @@ section#instructions .section-content > * {
468470
padding-left: calc((var(--viewport-width) - var(--text-width)) / 2);
469471
}
470472

471-
section#details:target {
473+
:root[data-visible-section="details"] section#details {
472474
display: flex;
473475
flex-direction: column;
474476
}

resources/main.mjs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -355,9 +355,15 @@ class MainBenchmarkClient {
355355
} else {
356356
window.location.hash = hash;
357357
}
358+
this._updateVisibleSectionAttribute(hash);
358359
this._updateDocumentTitle(hash);
359360
}
360361

362+
_updateVisibleSectionAttribute(hash) {
363+
const sectionId = hash.substring(1);
364+
document.documentElement.setAttribute("data-visible-section", sectionId);
365+
}
366+
361367
_updateDocumentTitle(hash) {
362368
const maybeSection = document.querySelector(hash);
363369
const sectionTitle = maybeSection?.getAttribute("data-title") ?? "";

0 commit comments

Comments
 (0)