|
| 1 | +import { createSignal } from 'solid-js' |
| 2 | +import { render } from 'solid-js/web' |
| 3 | +import { |
| 4 | + Link, |
| 5 | + Outlet, |
| 6 | + RouterProvider, |
| 7 | + createRootRoute, |
| 8 | + createRoute, |
| 9 | + createRouter, |
| 10 | + useBlocker, |
| 11 | +} from '@tanstack/solid-router' |
| 12 | +import { TanStackRouterDevtools } from '@tanstack/solid-router-devtools' |
| 13 | +import './styles.css' |
| 14 | + |
| 15 | +const rootRoute = createRootRoute({ |
| 16 | + component: RootComponent, |
| 17 | +}) |
| 18 | + |
| 19 | +function RootComponent() { |
| 20 | + // block going from editor-1 to /foo/123?hello=world |
| 21 | + const blocker = useBlocker({ |
| 22 | + shouldBlockFn: ({ current, next }) => { |
| 23 | + if ( |
| 24 | + current.routeId === '/editor-1' && |
| 25 | + next.fullPath === '/foo/$id' && |
| 26 | + next.params.id === '123' && |
| 27 | + next.search.hello === 'world' |
| 28 | + ) { |
| 29 | + return true |
| 30 | + } |
| 31 | + return false |
| 32 | + }, |
| 33 | + enableBeforeUnload: false, |
| 34 | + withResolver: true, |
| 35 | + }) |
| 36 | + |
| 37 | + return ( |
| 38 | + <> |
| 39 | + <div class="p-2 flex gap-2 text-lg"> |
| 40 | + <Link |
| 41 | + to="/" |
| 42 | + activeProps={{ |
| 43 | + class: 'font-bold', |
| 44 | + }} |
| 45 | + activeOptions={{ exact: true }} |
| 46 | + > |
| 47 | + Home |
| 48 | + </Link>{' '} |
| 49 | + <Link |
| 50 | + to="/editor-1" |
| 51 | + activeProps={{ |
| 52 | + class: 'font-bold', |
| 53 | + }} |
| 54 | + > |
| 55 | + Editor 1 |
| 56 | + </Link>{' '} |
| 57 | + <Link |
| 58 | + to={'/editor-1/editor-2'} |
| 59 | + activeProps={{ |
| 60 | + class: 'font-bold', |
| 61 | + }} |
| 62 | + > |
| 63 | + Editor 2 |
| 64 | + </Link>{' '} |
| 65 | + <Link |
| 66 | + to="/foo/$id" |
| 67 | + params={{ id: '123' }} |
| 68 | + search={{ hello: 'world' }} |
| 69 | + activeProps={{ |
| 70 | + class: 'font-bold', |
| 71 | + }} |
| 72 | + activeOptions={{ exact: true, includeSearch: true }} |
| 73 | + > |
| 74 | + foo 123 |
| 75 | + </Link>{' '} |
| 76 | + <Link |
| 77 | + to="/foo/$id" |
| 78 | + params={{ id: '456' }} |
| 79 | + search={{ hello: 'universe' }} |
| 80 | + activeProps={{ |
| 81 | + class: 'font-bold', |
| 82 | + }} |
| 83 | + activeOptions={{ exact: true, includeSearch: true }} |
| 84 | + > |
| 85 | + foo 456 |
| 86 | + </Link>{' '} |
| 87 | + </div> |
| 88 | + <hr /> |
| 89 | + |
| 90 | + {blocker().status === 'blocked' && ( |
| 91 | + <div class="mt-2"> |
| 92 | + <div> |
| 93 | + Are you sure you want to leave editor 1 for /foo/123?hello=world ? |
| 94 | + </div> |
| 95 | + <button |
| 96 | + class="bg-lime-500 text-white rounded-sm p-1 px-2 mr-2" |
| 97 | + onClick={blocker().proceed} |
| 98 | + > |
| 99 | + YES |
| 100 | + </button> |
| 101 | + <button |
| 102 | + class="bg-red-500 text-white rounded-sm p-1 px-2" |
| 103 | + onClick={blocker().reset} |
| 104 | + > |
| 105 | + NO |
| 106 | + </button> |
| 107 | + </div> |
| 108 | + )} |
| 109 | + <Outlet /> |
| 110 | + <TanStackRouterDevtools position="bottom-right" /> |
| 111 | + </> |
| 112 | + ) |
| 113 | +} |
| 114 | + |
| 115 | +const indexRoute = createRoute({ |
| 116 | + getParentRoute: () => rootRoute, |
| 117 | + path: '/', |
| 118 | + component: IndexComponent, |
| 119 | +}) |
| 120 | + |
| 121 | +function IndexComponent() { |
| 122 | + return ( |
| 123 | + <div class="p-2"> |
| 124 | + <h3>Welcome Home!</h3> |
| 125 | + </div> |
| 126 | + ) |
| 127 | +} |
| 128 | + |
| 129 | +const fooRoute = createRoute({ |
| 130 | + getParentRoute: () => rootRoute, |
| 131 | + path: 'foo/$id', |
| 132 | + validateSearch: (search) => ({ hello: search.hello }) as { hello: string }, |
| 133 | + component: () => <>foo {fooRoute.useParams()().id}</>, |
| 134 | +}) |
| 135 | + |
| 136 | +const editor1Route = createRoute({ |
| 137 | + getParentRoute: () => rootRoute, |
| 138 | + path: 'editor-1', |
| 139 | + component: Editor1Component, |
| 140 | +}) |
| 141 | + |
| 142 | +function Editor1Component() { |
| 143 | + const [value, setValue] = createSignal('') |
| 144 | + |
| 145 | + // Block leaving editor-1 if there is text in the input |
| 146 | + const blocker = useBlocker({ |
| 147 | + shouldBlockFn: () => value() !== '', |
| 148 | + enableBeforeUnload: () => value() !== '', |
| 149 | + withResolver: true, |
| 150 | + }) |
| 151 | + |
| 152 | + return ( |
| 153 | + <div class="flex flex-col p-2"> |
| 154 | + <h3>Editor 1</h3> |
| 155 | + <div> |
| 156 | + <input |
| 157 | + value={value()} |
| 158 | + onInput={(e) => setValue(e.currentTarget.value)} |
| 159 | + class="border" |
| 160 | + /> |
| 161 | + </div> |
| 162 | + <hr class="m-2" /> |
| 163 | + <Link to="/editor-1/editor-2">Go to Editor 2</Link> |
| 164 | + <Outlet /> |
| 165 | + |
| 166 | + {blocker().status === 'blocked' && ( |
| 167 | + <div class="mt-2"> |
| 168 | + <div>Are you sure you want to leave editor 1?</div> |
| 169 | + <div> |
| 170 | + You are going from {blocker().current?.pathname} to{' '} |
| 171 | + {blocker().next?.pathname} |
| 172 | + </div> |
| 173 | + <button |
| 174 | + class="bg-lime-500 text-white rounded-sm p-1 px-2 mr-2" |
| 175 | + onClick={blocker().proceed} |
| 176 | + > |
| 177 | + YES |
| 178 | + </button> |
| 179 | + <button |
| 180 | + class="bg-red-500 text-white rounded-sm p-1 px-2" |
| 181 | + onClick={blocker().reset} |
| 182 | + > |
| 183 | + NO |
| 184 | + </button> |
| 185 | + </div> |
| 186 | + )} |
| 187 | + </div> |
| 188 | + ) |
| 189 | +} |
| 190 | + |
| 191 | +const editor2Route = createRoute({ |
| 192 | + getParentRoute: () => editor1Route, |
| 193 | + path: 'editor-2', |
| 194 | + component: Editor2Component, |
| 195 | +}) |
| 196 | + |
| 197 | +function Editor2Component() { |
| 198 | + const [value, setValue] = createSignal('') |
| 199 | + |
| 200 | + return ( |
| 201 | + <div class="p-2"> |
| 202 | + <h3>Editor 2</h3> |
| 203 | + <input |
| 204 | + value={value()} |
| 205 | + onInput={(e) => setValue(e.currentTarget.value)} |
| 206 | + class="border" |
| 207 | + /> |
| 208 | + </div> |
| 209 | + ) |
| 210 | +} |
| 211 | + |
| 212 | +const routeTree = rootRoute.addChildren([ |
| 213 | + indexRoute, |
| 214 | + fooRoute, |
| 215 | + editor1Route.addChildren([editor2Route]), |
| 216 | +]) |
| 217 | + |
| 218 | +// Set up a Router instance |
| 219 | +const router = createRouter({ |
| 220 | + routeTree, |
| 221 | + defaultPreload: 'intent', |
| 222 | + scrollRestoration: true, |
| 223 | +}) |
| 224 | + |
| 225 | +const rootElement = document.getElementById('app')! |
| 226 | + |
| 227 | +if (!rootElement.innerHTML) { |
| 228 | + render(() => <RouterProvider router={router} />, rootElement) |
| 229 | +} |
0 commit comments