Skip to content

Commit 2d98b23

Browse files
committed
Add details open functions
1 parent edc2a82 commit 2d98b23

File tree

1 file changed

+69
-0
lines changed

1 file changed

+69
-0
lines changed

assets/js/priorities.mjs

+69
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import { gsap } from "./modules/gsap@3.12.5.min.mjs";
99

1010
/**
1111
* Adds `target="_blank"` to element links. This will open links in a new window.
12+
* @returns {void}
1213
*/
1314
function addBlankTargetToElementLinks() {
1415
for (const anchor of document.querySelectorAll(".element-content a")) {
@@ -25,6 +26,7 @@ function addBlankTargetToElementLinks() {
2526

2627
/**
2728
* Setups scroll to top functionality.
29+
* @returns {void}
2830
*/
2931
function setupScrollToTop() {
3032
const navElement = document.querySelector("#priorities-navigation");
@@ -77,6 +79,71 @@ function setupScrollToTop() {
7779
scrollToTopObserver.observe(navElement);
7880
}
7981

82+
// ============================
83+
// Open details selected in URL
84+
// ============================
85+
/**
86+
* If there's a hash to the URL, make sure that the appropritate details tag is open.
87+
* @returns {void}
88+
*/
89+
function openDetailsSelectedInUrl() {
90+
if (window.location.hash === "") {
91+
return;
92+
}
93+
94+
/** @type {HTMLDetailsElement | null} */
95+
const element = document.querySelector(`${window.location.hash} details.element-main`);
96+
if (element == null) {
97+
return;
98+
}
99+
element.open = true;
100+
}
101+
102+
// ========================
103+
// Handle navigation events
104+
// ========================
105+
/**
106+
* Opens the current hash when the navigation changes.
107+
* The API is still quite new, not supported by some browsers. (2024-11-21)
108+
* @returns {void}
109+
*/
110+
function handleNavigateEvent() {
111+
// The API is not yet supported by all browsers.
112+
if (!("Navigation" in window)) {
113+
//openOnHandleClick();
114+
handleHashChangeEvent();
115+
return;
116+
}
117+
118+
/**
119+
* @param {NavigateEvent} event
120+
*/
121+
navigation.addEventListener("navigate", (event) => {
122+
const url = new URL(event.destination.url);
123+
/** @type {HTMLDetailsElement | null} */
124+
const element = document.querySelector(`${url.hash} details.element-main`);
125+
if (element == null) {
126+
return;
127+
}
128+
element.open = true;
129+
});
130+
}
131+
132+
/**
133+
* More common API, listens to "hashchange" event.
134+
* @returns {void}
135+
*/
136+
function handleHashChangeEvent() {
137+
window.addEventListener("hashchange", () => {
138+
/** @type {HTMLDetailsElement | null} */
139+
const element = document.querySelector(`${window.location.hash} details.element-main`);
140+
if (element == null) {
141+
return;
142+
}
143+
element.open = true;
144+
});
145+
}
146+
80147
// ====
81148
// Main
82149
// ====
@@ -87,5 +154,7 @@ function setupScrollToTop() {
87154
function main() {
88155
addBlankTargetToElementLinks();
89156
setupScrollToTop();
157+
openDetailsSelectedInUrl();
158+
handleNavigateEvent();
90159
}
91160
main();

0 commit comments

Comments
 (0)