MCPcopy Create free account
hub / github.com/benjitaylor/agentation / handleHash

Function handleHash

package/example/src/hooks/useActiveScrollElement.ts:119–163  ·  view source on GitHub ↗

* Detect clicks on hash links to set the active item. * We do this manually instead of using the `hashchange` event, because * clicking on a link that matches the current hash doesn't fire the event, * but we still want to update the active item.

(e: MouseEvent)

Source from the content-addressed store, hash-verified

117 * but we still want to update the active item.
118 */
119 function handleHash(e: MouseEvent) {
120 const target = e.target as HTMLElement | null;
121
122 if (!target) {
123 return;
124 }
125
126 const anchorElement = target.closest('a');
127 const href = anchorElement?.getAttribute('href');
128
129 if (!anchorElement || !href || !href.startsWith('#')) {
130 return;
131 }
132
133 const hash = href?.slice(1);
134
135 // Ignore hash links that don't match the ids we're tracking
136 if (!ids.includes(hash)) {
137 return;
138 }
139
140 // Immediately set active
141 setActive(hash);
142
143 // Pause updates caused by scrolling or intersection observer for a short time
144 updatesPaused.current = true;
145
146 if (hasScrollEnd) {
147 // Use the scrollend event when supported
148 document.addEventListener(
149 'scrollend',
150 () => {
151 updatesPaused.current = false;
152 },
153 { once: true },
154 );
155 } else {
156 clearTimeout(timer);
157 // If smooth scroll is enabled, use a longer timeout
158 const ms = rootStyle.scrollBehavior === 'smooth' ? 1000 : 60;
159 timer = setTimeout(() => {
160 updatesPaused.current = false;
161 }, ms);
162 }
163 }
164
165 const handleResize = debounce(() => {
166 maxScroll =

Callers

nothing calls this directly

Calls

no outgoing calls

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…