How-to
Website
Web app
Make navigation responsive
A row of links fits a laptop but not a phone. To show the links on a wide screen and a menu button that opens them on a narrow one, branch on viewport:
Needs: nothing besides this code. It is a whole project's app and pages: paste it into a new project, replacing src/App.wf and the starter's pages.
app {
state open = false
if viewport.md {
Row(gap: .md) {
Link("Home", to: "/")
Link("Docs", to: "/docs")
Link("Pricing", to: "/pricing")
}
} else {
IconButton(icon: "menu", label: "Open navigation", aria-expanded: open, aria-controls: "nav-menu") {
on click { open = !open }
}
show open {
Stack(gap: .sm, animate: .slideDown, id: "nav-menu") {
on click { open = false }
Link("Home", to: "/")
Link("Docs", to: "/docs")
Link("Pricing", to: "/pricing")
}
}
}
Router
}
page Home(path: "/", title: "Home", description: "Navigation that fits the screen.") { Heading("Home").h1 }
page Docs(path: "/docs", title: "Docs", description: "How it works.") { Heading("Docs").h1 }
page Pricing(path: "/pricing", title: "Pricing", description: "What it costs.") { Heading("Pricing").h1 }
How it works
On a wide window the three links sit in a row above every page. Narrow the window and they give way to a menu button; press it and the links slide down beneath it.
viewport.mdis true while the window is at least themdbreakpoint wide. The runtime updates it when a breakpoint is crossed, so theifswaps the row for the button as the window narrows.- On a narrow screen, the
IconButtontogglesopen. Itslabel:is the button's accessible name, since the icon has no text, andaria-expanded: opentells a screen reader whether the menu it controls (aria-controls, theStack'sid) is open. show openkeeps the links in the page and hides them whileopenis false.animate: .slideDownplays as they appear, and a click on a link closes the menu again.- The navigation is in
app, around theRouter, so every page shows it andopenlives on from page to page.
The pre-rendered page does not know the window's width, so what depends on viewport is drawn once the page is live. For layout alone — a row that becomes a column — prefer responsive values, which are right in the first paint.
The three pages are there so every link lands on one; in a site of your own the links name the pages you have.