Open History to see the last commit
1import { useLayoutEffect, useRef, useState } from 'react'
2import {
3 Link,
4 createFileRoute,
5 useLocation,
6 useNavigate,
7} from '@tanstack/react-router'
8β
9export const Route = createFileRoute('/anchor')({
10 component: AnchorComponent,
11})
12β
13const anchors: Array<{
14 id: string
15 title: string
16 hashScrollIntoView?: boolean | ScrollIntoViewOptions
17}> = [
18 {
19 id: 'default-anchor',
20 title: 'Default Anchor',
21 },
22 {
23 id: 'false-anchor',
24 title: 'No Scroll Into View',
25 hashScrollIntoView: false,
26 },
27 {
28 id: 'smooth-scroll',
29 title: 'Smooth Scroll',
30 hashScrollIntoView: { behavior: 'smooth' },
31 },
32] as const
33β
34function AnchorSection({ id, title }: { id: string; title: string }) {
35 const [hasShown, setHasShown] = useState(false)
36 const elementRef = useRef<HTMLHeadingElement>(null)
37β
38 useLayoutEffect(() => {
39 const observer = new IntersectionObserver(
40 ([entry]) => {
41 if (!hasShown && entry.isIntersecting) {
42 setHasShown(true)
43 }
44 },
45 { threshold: 0.01 },
46 )
47β
48 const currentRef = elementRef.current
49 if (currentRef) {
50 observer.observe(currentRef)
51 }
52β
53 return () => {
54 if (currentRef) {
55 observer.unobserve(currentRef)
56 }
57 }
58 }, [hasShown])
59β
60 return (
61 <div id={id} className="p-2 min-h-dvh">
62 <h1 className="font-bold text-xl pt-10" ref={elementRef}>
63 {title}
64 {hasShown ? ' (shown)' : ''}
65 </h1>
66 </div>
67 )
68}
69β
70function AnchorComponent() {
71 const navigate = useNavigate()
72 const location = useLocation()
73 const [withScroll, setWithScroll] = useState(true)
74β
75 return (
76 <div className="flex flex-col w-full">
77 <nav className="sticky top-0 z-10 p-2 bg-gray-50 dark:bg-gray-900 border-b">
78 <ul className="inline-flex gap-2">
79 {anchors.map((anchor) => (
80 <li key={anchor.id}>
81 <Link
82 from={Route.fullPath}
83 hash={anchor.id}
84 activeOptions={{ includeHash: true }}
85 activeProps={{
86 className: 'font-bold active',
87 }}
88 hashScrollIntoView={anchor.hashScrollIntoView}
89 >
90 {anchor.title}
91 </Link>
92 </li>
93 ))}
94 </ul>
95 </nav>
96 <main className="overflow-auto">
97 <form
98 className="p-2 space-y-2 min-h-dvh"
99 onSubmit={(event) => {
100 event.preventDefault()
101 event.stopPropagation()
102 const formData = new FormData(event.target as HTMLFormElement)
103β
104 const toHash = formData.get('hash') as string
105β
106 if (!toHash) {
107 return
108 }
109β
110 const hashScrollIntoView = withScroll
111 ? ({
112 behavior: formData.get('scrollBehavior') as ScrollBehavior,
113 block: formData.get('scrollBlock') as ScrollLogicalPosition,
114 inline: formData.get('scrollInline') as ScrollLogicalPosition,
115 } satisfies ScrollIntoViewOptions)
116 : false
117β
118 navigate({ hash: toHash, hashScrollIntoView })
119 }}
120 >
121 <h1 className="font-bold text-xl">Scroll with navigate</h1>
122 <div className="space-y-2">
123 <label>
124 <span>Target Anchor</span>
125 <select
126 className="border border-opacity-50 rounded p-2 w-full"
127 defaultValue={location.hash || anchors[0].id}
128 name="hash"
129 >
130 {anchors.map((anchor) => (
131 <option key={anchor.id} value={anchor.id}>
132 {anchor.title}
133 </option>
134 ))}
135 </select>
136 </label>
137 <div>
138 <label>
139 <input
140 checked={withScroll}
141 onChange={(e) => setWithScroll(e.target.checked)}
142 type="checkbox"
143 />{' '}
144 Scroll Into View
145 </label>
146 </div>
147 </div>
148 {withScroll ? (
149 <>
150 <div className="space-y-2">
151 <label>
152 <span>Behavior</span>
153 <select
154 className="border border-opacity-50 rounded p-2 w-full"
155 defaultValue="instant"
156 name="scrollBehavior"
157 >
158 <option value="instant">instant</option>
159 <option value="smooth">smooth</option>
160 <option value="auto">auto</option>
161 </select>
162 </label>
163 </div>
164β
165 <div className="space-y-2">
166 <label>
167 <span>Block</span>
168 <select
169 className="border border-opacity-50 rounded p-2 w-full"
170 defaultValue="start"
171 name="scrollBlock"
172 >
173 <option value="start">start</option>
174 <option value="center">center</option>
175 <option value="end">end</option>
176 <option value="nearest">nearest</option>
177 </select>
178 </label>
179 </div>
180β
181 <div className="space-y-2">
182 <label>
183 <span>Inline</span>
184 <select
185 className="border border-opacity-50 rounded p-2 w-full"
186 defaultValue="nearest"
187 name="scrollInline"
188 >
189 <option value="start">start</option>
190 <option value="center">center</option>
191 <option value="end">end</option>
192 <option value="nearest">nearest</option>
193 </select>
194 </label>
195 </div>
196 </>
197 ) : null}
198 <div>
199 <button className="bg-blue-500 rounded p-2 uppercase text-white font-black disabled:opacity-50">
200 Navigate
201 </button>
202 </div>
203 </form>
204β
205 {anchors.map((anchor) => (
206 <AnchorSection key={anchor.id} id={anchor.id} title={anchor.title} />
207 ))}
208 </main>
209 </div>
210 )
211}