@@ -165,6 +165,8 @@ window.addEventListener('load', () => {
165
165
insertElementAfter ( searchInput , searchLabel ) ;
166
166
} ) ;
167
167
168
+ const FIXED_HEADER_HEIGHT = 80 ;
169
+
168
170
const tocbotOptions = {
169
171
// Where to render the table of contents.
170
172
tocSelector : '.article-toc' ,
@@ -188,14 +190,51 @@ const tocbotOptions = {
188
190
activeLinkClass : 'selected-article' ,
189
191
190
192
// Headings offset between the headings and the top of the document (requires scrollSmooth enabled)
191
- headingsOffset : 80 ,
192
- scrollSmoothOffset : - 80 ,
193
+ headingsOffset : FIXED_HEADER_HEIGHT ,
194
+ scrollSmoothOffset : - FIXED_HEADER_HEIGHT ,
193
195
scrollSmooth : true ,
194
196
195
197
// If there is a fixed article scroll container, set to calculate titles' offset
196
198
scrollContainer : 'content-area' ,
199
+
200
+ onClick : ( e ) => {
201
+ e . preventDefault ( ) ;
202
+ const hashText = e . target . href . split ( '#' ) . pop ( ) ;
203
+ // Append hashText to the current URL without saving to history
204
+ const newUrl = `${ window . location . pathname } #${ hashText } ` ;
205
+ history . replaceState ( null , '' , newUrl ) ;
206
+ } ,
197
207
} ;
198
208
209
+ // Define the media query string for the mobile breakpoint
210
+ const mobileBreakpoint = window . matchMedia ( '(max-width: 799px)' ) ;
211
+
212
+ // Function to update tocbot options and refresh
213
+ function updateTocbotOptions ( headingsOffset , scrollSmoothOffset ) {
214
+ tocbotOptions . headingsOffset = headingsOffset ;
215
+ tocbotOptions . scrollSmoothOffset = scrollSmoothOffset ;
216
+ window . tocbot . refresh ( {
217
+ ...tocbotOptions ,
218
+ } ) ;
219
+ }
220
+
221
+ function handleBreakpointChange ( ) {
222
+ const isMobile = mobileBreakpoint . matches ;
223
+ const headingsOffset = isMobile ? FIXED_HEADER_HEIGHT : 0 ;
224
+ const scrollSmoothOffset = isMobile ? - FIXED_HEADER_HEIGHT : 0 ;
225
+
226
+ // Update tocbot options only if there is a change in offsets
227
+ if ( tocbotOptions . headingsOffset !== headingsOffset || tocbotOptions . scrollSmoothOffset !== scrollSmoothOffset ) {
228
+ updateTocbotOptions ( headingsOffset , scrollSmoothOffset ) ;
229
+ }
230
+ }
231
+
232
+ // Add listener for changes to the media query status using addEventListener
233
+ mobileBreakpoint . addEventListener ( 'change' , handleBreakpointChange ) ;
234
+
235
+ // Initial check
236
+ handleBreakpointChange ( ) ;
237
+
199
238
function selectNewExpensify ( newExpensifyTab , newExpensifyContent , expensifyClassicTab , expensifyClassicContent ) {
200
239
newExpensifyTab . classList . add ( 'active' ) ;
201
240
newExpensifyContent . classList . remove ( 'hidden' ) ;
0 commit comments