@@ -23,7 +23,7 @@ export function openDetailsWithAnchor() {
2323function updateUrlForDropdown ( details : HTMLDetailsElement , isOpening : boolean ) {
2424 const dropdownId = details . id
2525 if ( ! dropdownId ) return
26-
26+
2727 if ( isOpening ) {
2828 // Update URL to show the dropdown anchor (like clicking a heading link)
2929 window . history . pushState ( null , '' , `#${ dropdownId } ` )
@@ -36,24 +36,31 @@ function updateUrlForDropdown(details: HTMLDetailsElement, isOpening: boolean) {
3636export function initOpenDetailsWithAnchor ( ) {
3737 // Handle initial page load
3838 openDetailsWithAnchor ( )
39-
39+
4040 // Handle hash changes within the same page (e.g., clicking anchor links)
4141 window . addEventListener ( 'hashchange' , openDetailsWithAnchor )
42-
42+
4343 // Handle manual dropdown toggling to update URL
4444 // Use event delegation to catch all toggle events
45- document . addEventListener ( 'toggle' , ( event ) => {
46- const target = event . target as HTMLElement
47-
48- // Check if the target is a details element with dropdown class
49- if ( target . tagName === 'DETAILS' && target . classList . contains ( 'dropdown' ) ) {
50- const details = target as HTMLDetailsElement
51- const isOpening = details . open
52-
53- // Use setTimeout to ensure the toggle state has been processed
54- setTimeout ( ( ) => {
55- updateUrlForDropdown ( details , isOpening )
56- } , 0 )
57- }
58- } , true ) // Use capture phase to ensure we catch the event
45+ document . addEventListener (
46+ 'toggle' ,
47+ ( event ) => {
48+ const target = event . target as HTMLElement
49+
50+ // Check if the target is a details element with dropdown class
51+ if (
52+ target . tagName === 'DETAILS' &&
53+ target . classList . contains ( 'dropdown' )
54+ ) {
55+ const details = target as HTMLDetailsElement
56+ const isOpening = details . open
57+
58+ // Use setTimeout to ensure the toggle state has been processed
59+ setTimeout ( ( ) => {
60+ updateUrlForDropdown ( details , isOpening )
61+ } , 0 )
62+ }
63+ } ,
64+ true
65+ ) // Use capture phase to ensure we catch the event
5966}
0 commit comments