@@ -9,6 +9,7 @@ import { gsap } from "./modules/gsap@3.12.5.min.mjs";
9
9
10
10
/**
11
11
* Adds `target="_blank"` to element links. This will open links in a new window.
12
+ * @returns {void }
12
13
*/
13
14
function addBlankTargetToElementLinks ( ) {
14
15
for ( const anchor of document . querySelectorAll ( ".element-content a" ) ) {
@@ -25,6 +26,7 @@ function addBlankTargetToElementLinks() {
25
26
26
27
/**
27
28
* Setups scroll to top functionality.
29
+ * @returns {void }
28
30
*/
29
31
function setupScrollToTop ( ) {
30
32
const navElement = document . querySelector ( "#priorities-navigation" ) ;
@@ -77,6 +79,71 @@ function setupScrollToTop() {
77
79
scrollToTopObserver . observe ( navElement ) ;
78
80
}
79
81
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
+
80
147
// ====
81
148
// Main
82
149
// ====
@@ -87,5 +154,7 @@ function setupScrollToTop() {
87
154
function main ( ) {
88
155
addBlankTargetToElementLinks ( ) ;
89
156
setupScrollToTop ( ) ;
157
+ openDetailsSelectedInUrl ( ) ;
158
+ handleNavigateEvent ( ) ;
90
159
}
91
160
main ( ) ;
0 commit comments