Skip to content
Source
Sign inSign up
TanStack/router
Open History to see the last commit

anchor.tsx

222 lines6.5 KB

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