BrowserRouter unstable
The browser's Router: window.history behind the effect/ui Router
contract, plus link interception, scroll, route-change announcements and
focus.
Details
Provide layer (or layerHash, for static hosting) next to the host. The
root holds the route: mounting it with Router.props gives its props the
initial one, and Router.subscription delivers each change as a message.
Links are plain anchors with a printed route. A click is intercepted only
when the routes parse its href and it has no modifier key, target or
download. The browser handles other clicks. The router intercepts a
form submission only if it is not already cancelled, uses GET, targets
the same window and has a URL the routes parse. It leaves other
submissions alone; the host still cancels handled and dialog forms.
Add rel="external" to an anchor or GET form for a full page load even
when the routes parse its URL. These anchors also skip route intent.
Following a link or form to the current route sends a
message without scrolling to the top, announcing or moving focus. A #hash
link still scrolls to its target, even when it's the current hash, and a
bare # stays in the URL and scrolls to the top. Both work the same in a
root mounted inside a shadow root, open or closed.
Once a navigation settles, the router scrolls, announces the page in a
polite live region and moves focus. Settlement waits for the receiving
root to flush, Layer builds begun since navigation and state decoding to
finish, and all Router.unsettled holds to release. A push goes to the top
or to its #hash element, and back and forward restore the saved position.
Focus goes to the page's [autofocus] element, else to the
#hash element of a push. Otherwise it is reset to the top of the document
when it is still on the followed link, such as one in a persistent nav, and
stays where the page moved it while rendering.
An initial #hash element that renders after load is scrolled to and
focused once it appears, unless the reader interacts or navigates first.
A quiet navigation (see Router.Settled), such as Router.patchSearch,
stays on the page: nothing is announced and focus stays. Back and forward
still restore the saved position, and a patchSearch given
{ scroll: true } scrolls to the top.
A popstate with hasUAVisualTransition: true suppresses a second view
transition for the browser-animated traversal.
While a Router.blocked block is held, BrowserRouter requests a browser
warning before the page unloads unless warnOnUnload is false. See
Router.blocked for the warning's limits. While a block with a mapper is
held, it also stops and reports HTTP(S) links and GET forms marked
rel="external" or whose URLs the routes cannot parse, including other sites.
Example A two-page app
import { BrowserHost, BrowserRouter, Html } from "effect/ui/html"import { Effect, Layer, Schema } from "effect"import { Route, Router, UiSchema } from "effect/ui"import { TestHost } from "effect/ui/testing"
const AppRoute = Route.union({ Home: Route.make("/"), Person: Route.make("/people/:id", { params: { id: Schema.Number } }), NotFound: Route.make("/*")})type AppRoute = typeof AppRoute.Type
const Message = UiSchema.TaggedEnum({ ChangedRoute: { route: AppRoute } })
const App = Html.component({ name: "App", props: Schema.Struct({ route: AppRoute }), model: AppRoute, message: Message}).make({ init: ($) => $.props.route, update: (_, message) => message.route, subscriptions: ({ model }) => [Router.subscription(AppRoute, model, (route) => Message.ChangedRoute({ route }))], view: (h, route) => h.div( h.a.attr("href", AppRoute.Person.href({ params: { id: 1 } })).children("Ada"), h.h1(route._tag) )})
// Router.props reads the initial `{ route }` from the browser Router's URL.const main = (container: Element) => BrowserHost.mount(App, Router.props(AppRoute), container).pipe( Layer.provide(BrowserRouter.layer({ routes: AppRoute })), Layer.launch )
// Exercise the same component without a browser.await Effect.runPromise(Effect.gen(function*() { const root = yield* TestHost.mount(App, Router.props(AppRoute)) yield* TestHost.settle root.model()?._tag // => "Person"}).pipe( Effect.provide(Router.memory({ routes: AppRoute, url: AppRoute.Person.href({ params: { id: 1 } }) })), Effect.scoped, Effect.provide(TestHost.layer())))Layers
The browser Router, with routes in the URL's path.
Details
There's one per document: building a second fails as a defect, and the claim is released when the Layer's Scope closes.
Reports link intent to Router.prefetch declarations through intent
after 50 ms of hover or focus on the Effect Clock, or immediately on
touch. Only links a click would follow count.
Replaces that stay on the page, such as a Router.patchSearch per
keystroke, are written to window.history at most once per 200 ms on the
Effect Clock, to stay under the browsers' rate limits. The route changes
at once; the address bar gets the latest URL at the end of the interval, or
before a navigation, a link click, a form submission or pagehide.
Signature
declare function layer(options: Options): Layer<Services>The browser Router for static hosting, with routes in the URL's fragment,
as /#/people/1.
Details
Parsing and printing work as with layer, with the base "#". As the
fragment holds the route, there are no in-page #hash links. To hydrate a
prerendered shell, set routes.setBase("#") before rendering on the server
with Router.static at "#/". Hydration adopts that shell, then delivers
the browser route without reporting an initial RouteMismatch. Client-only
hash roots are still checked.
Signature
declare function layerHash(options: Omit<Options, "base">): Layer<Services>Models
The options of layer.
Signature
interface Options { readonly announce?: boolean; readonly base?: string | (segment: string) => boolean; readonly focus?: boolean; readonly restore?: boolean; readonly routes: AnyUnion; readonly scroll?: boolean; readonly warnOnUnload?: boolean; readonly window?: Window;}