fix(site): open menus where they fit and stop resetting scroll on reload (#62396)

This commit is contained in:
Adam
2026-11-06 12:04:47 -06:00
committed by GitHub
Unverified
parent 1a5b7239be
commit e1989fcce4
10 changed files with 107 additions and 55 deletions
+3 -1
View File
@@ -2,7 +2,7 @@
Design-system rules for the opencode.ai site (`services/site`), compiled from David's reviews and decisions. Read them before you change UI or copy. Every component they mention is live at [/dev/components](/dev/components) on the dev server.
Version 2026-11-05. Last changed 2026-11-05, OpenTUI Mono as the site's mono font, disabled splits share one line at the join, focus rings drawn inside adjacent and split buttons, and split buttons shown only as a pair.
Version 2026-11-06. Last changed 2026-11-06, menus open on the side with room (`placeMenu`) and nothing moves the scroll position on load.
To add a rule, add one line under its group. Mark a rule that is still being decided with _(provisional)_.
@@ -54,6 +54,7 @@ To add a rule, add one line under its group. Mark a rule that is still being dec
- Make icon tiles as tall as their text block. → [Product list](/dev/components#product-list)
- Make action buttons in product headers equal width and right-aligned.
- Keep product and companion tile sizes as they are until the size is decided. _(provisional)_ → [Companion shelf](/dev/components#companions)
- Never move the scroll position on load. The browser restores it on reload and on return from another page, and the router on in-site back and forward (`app.tsx`).
- Cause no layout shift (CLS 0). Reserve the space, and use skeletons where content streams in.
- Decide when a layout stacks by the width of its own container (container queries) where the sidebar or the Contents rail narrows the column, not by the screen width alone. _(provisional)_
@@ -70,6 +71,7 @@ To add a rule, add one line under its group. Mark a rule that is still being dec
- End every `target="_blank"` link with `<NewTab />` for screen readers. → [Icons](/dev/components#icons)
- Use `component/faq.tsx` for every accordion. → [Accordion](/dev/components#faq)
- Every dropdown uses the shared Dropdown component; no one-off menus. On the site that is `SiteMenu` (`component/site/site-menu.tsx`), which brings the shared menu look, keyboard support and the phone sheet.
- Open every dropdown with `placeMenu` (`component/site/place-menu.ts`; SiteMenu already does). It opens below the trigger, above only when below has no room, and in the top layer, so a scrolling box such as the carousel never cuts it off.
- Turn dropdowns into the shared bottom sheet (`sheet.tsx`) on touch phones. → [Bottom sheet](/dev/components#sheet)
- Give long pages the Contents rail (`OnThisPage`) and its phone bar (`ContentsMenu`). → [Contents](/dev/components#contents)
- Draw breadcrumbs with muted ancestors, `|` separators and the current page in ink. → [Breadcrumbs](/dev/components#breadcrumb)
+16 -3
View File
@@ -2,6 +2,7 @@ import { Link, Title, Meta } from "@solidjs/meta"
import { Router, useCurrentMatches, useLocation } from "@solidjs/router"
import { FileRoutes } from "@solidjs/start/router"
import { Show, Suspense } from "solid-js"
import { isServer } from "solid-js/web"
import { Favicon } from "@opencode/ui/favicon"
import { Font } from "@opencode/ui/font"
import "./style/ibm-plex-mono.dev.css"
@@ -69,14 +70,26 @@ function AppMeta() {
)
}
// The router's scroll restoration (below) sets history.scrollRestoration to "manual" and restores
// only after the app has loaded, so a page loaded again (a reload, or back from another site or a
// plain link) would paint at the top and then jump. Leaving a document hands its entry back to the
// browser, which restores it as the page loads; a page restored from the back/forward cache, still
// running, takes the router's manual mode again for its in-site back and forward.
if (!isServer) {
window.addEventListener("pagehide", () => (history.scrollRestoration = "auto"))
window.addEventListener("pageshow", (event) => {
if (event.persisted) history.scrollRestoration = "manual"
})
}
export default function App() {
return (
<Router
explicitLinks={true}
transformUrl={strip}
// The router restores scroll on back and forward itself, once the page has rendered. The
// browser's own restore lands before that, on the page being left, and shows as a jump at
// the start of the hero's shrink back into its card.
// On in-site back and forward the router restores scroll itself, once the page has rendered.
// The browser's own restore lands right after popstate, on the page being left, and shows as
// a jump at the start of the hero's shrink back into its card.
scrollRestoration
root={(props) => (
<LanguageProvider>
@@ -168,7 +168,6 @@ function ChangelogSubscribe(props: { filter: string[] }) {
<div data-slot="subscribe">
<SiteMenu
label={i18n.t("changelog.subscribe")}
placement="below"
align="end"
trigger={
<>
@@ -0,0 +1,58 @@
let anchors = 0
/**
* Opens a dropdown's menu where it fits. Call it as soon as the menu is in the page, laid out by its
* CSS against its positioning box (the trigger, or a split button): beside it, at its gap, with its
* width and inline alignment. The menu opens below unless only above has room; with room on neither
* side it takes the taller one and scrolls. Sideways it keeps 16px clear of the window's edges, and
* on phones takes that whole width (long rows end in an ellipsis).
*
* The menu then moves to the top layer (a manual popover), so no scrolling box, such as the product
* carousel's track, cuts it off, and stays tied to its box by CSS anchor positioning as the page
* scrolls. A browser without anchor positioning keeps it in its box.
*/
export function placeMenu(menu: HTMLElement) {
const box = menu.offsetParent
if (!(box instanceof HTMLElement)) return
// Measured as laid out, not as drawn: the opening animation scales the menu from a corner.
menu.style.animation = "none"
const left = 16
const right = document.documentElement.clientWidth - 16
menu.style.maxWidth = `${right - left}px`
if (window.matchMedia("(max-width: 40rem)").matches) menu.style.width = `${right - left}px`
const anchor = box.getBoundingClientRect()
const laid = menu.getBoundingClientRect()
const gap = Math.max(0, laid.top >= anchor.bottom ? laid.top - anchor.bottom : anchor.top - laid.bottom)
const below = window.innerHeight - anchor.bottom - gap - 8
const above = anchor.top - gap - 8
const side = laid.height <= below || (laid.height > above && below >= above) ? "below" : "above"
const room = side === "below" ? below : above
const shift = Math.max(left - laid.left, Math.min(0, right - laid.right))
menu.dataset.side = side
menu.style.setProperty("--menu-shift", side === "below" ? "-4px" : "4px")
menu.style.transformOrigin = `${getComputedStyle(menu).transformOrigin.split(" ")[0]} ${side === "below" ? "top" : "bottom"}`
menu.style.maxHeight = laid.height > room ? `${Math.floor(room)}px` : ""
menu.style.overflowY = laid.height > room ? "auto" : ""
if (!CSS.supports("anchor-name: --menu") || typeof menu.showPopover !== "function") {
menu.style.top = side === "below" ? `calc(100% + ${gap}px)` : "auto"
menu.style.bottom = side === "above" ? `calc(100% + ${gap}px)` : "auto"
menu.style.translate = shift ? `${shift}px 0` : ""
menu.style.animation = ""
return
}
const name = box.style.getPropertyValue("anchor-name") || `--menu-${++anchors}`
box.style.setProperty("anchor-name", name)
menu.style.setProperty("position-anchor", name)
menu.style.position = "fixed"
menu.style.inset = "auto"
menu.style.left = `calc(anchor(left) + ${laid.left + shift - anchor.left}px)`
if (side === "below") menu.style.top = `calc(anchor(bottom) + ${gap}px)`
if (side === "above") menu.style.bottom = `calc(anchor(top) + ${gap}px)`
// Its width as laid out against its box; a percentage would now be of the window.
menu.style.width = `${laid.width}px`
menu.style.minWidth = "0"
menu.popover = "manual"
menu.showPopover()
menu.style.animation = ""
}
@@ -275,14 +275,11 @@
}
}
/* The intro has room below it, so the menu opens downward from the right edge. */
/* The menu opens from the right edge. */
[data-slot="menu"] {
inset-inline-start: auto;
inset-inline-end: 0;
top: calc(100% + 0.5rem);
bottom: auto;
transform-origin: top var(--x-end, right);
--menu-shift: -4px;
}
/* The exposed install commands need two columns: the copy takes the first two. */
+3 -22
View File
@@ -34,6 +34,7 @@ import { copyOnClick, copySelection } from "~/lib/clipboard"
import { detectPlatform, isMobile, requestPlatform } from "~/lib/platform"
import { Chevron, NewTab, PlatformIcon } from "./icons"
import { onCloseMenus } from "./menu-open"
import { placeMenu } from "./place-menu"
import { Sheet, prefersSheet, type SheetControl } from "./sheet"
import { holdPopstate } from "./popstate"
import {
@@ -1102,7 +1103,7 @@ export function ProductAction(props: { slug: PageSlug; split?: boolean; both?: b
if (openSheet()) return
setOpen(!open())
const panel = menu?.querySelector<HTMLElement>('[data-slot="menu"]')
if (panel) fitMenu(panel)
if (panel) placeMenu(panel)
// From the keyboard, focus moves into the menu.
if (open() && event.detail === 0)
menu?.querySelector<HTMLElement>('[role="menuitem"]')?.focus({ preventScroll: true })
@@ -1279,7 +1280,7 @@ export function ProductAction(props: { slug: PageSlug; split?: boolean; both?: b
if (openSheet()) return
setOpen(!open())
const panel = menu?.querySelector<HTMLElement>('[data-slot="menu"]')
if (panel) fitMenu(panel)
if (panel) placeMenu(panel)
}}
>
{props.slug === "mobile" ? i18n.t("download.action.download") : i18n.t("download.action.install")}
@@ -1520,26 +1521,6 @@ function elementReady(selector: string) {
})
}
/**
* Keeps an open menu inside the carousel's visible area (or the window, outside the carousel),
* which would otherwise cut it off: no wider than that area and 8px clear of the window's edges
* (long install commands wrap), and slid sideways just enough.
*/
function fitMenu(panel: HTMLElement) {
const area = panel.closest('[data-slot="track"]')?.getBoundingClientRect() ?? new DOMRect(0, 0, window.innerWidth)
// Never closer than 16px to a screen edge; on phones the menu takes that whole width, so the
// install commands get as much room as there is (each still one line, cut with an ellipsis).
const left = Math.max(area.left, 16)
const right = Math.min(area.right, window.innerWidth - 16)
panel.style.maxWidth = `${right - left}px`
panel.style.width = window.matchMedia("(max-width: 40rem)").matches ? `${right - left}px` : ""
// Laid out, not as drawn: the opening animation scales the menu from a corner.
const parent = (panel.offsetParent ?? document.body).getBoundingClientRect()
const start = parent.left + panel.offsetLeft
const shift = Math.max(left - start, Math.min(0, right - (start + panel.offsetWidth)))
panel.style.translate = shift ? `${Math.round(shift)}px 0` : ""
}
// The card transition running now. A newer one (back during a grow) owns the page's markers.
let current: ViewTransition | undefined
@@ -1,6 +1,7 @@
import { For, Show, createSignal, createUniqueId, onCleanup, onMount, type JSX } from "solid-js"
import { ExternalArrow, NewTab } from "./icons"
import { onCloseMenus } from "./menu-open"
import { placeMenu } from "./place-menu"
import { Sheet, prefersSheet, type SheetControl } from "./sheet"
/** One row: a link, with the external arrow and a new-tab note when it opens in a new tab. */
@@ -9,7 +10,7 @@ export type SiteMenuItem = { label: string; href: string; external?: boolean }
/**
* The site's dropdown: a trigger and a menu of links in the shared menu look (site.css's
* `site-menu`: rows edge to edge, the grey hover tint and the bar on the left, the popover colours,
* shadow and opening animation). It opens above the trigger or below it, from its start or end edge.
* shadow and opening animation). It opens where it fits (placeMenu), from the trigger's start or end edge.
* Arrow keys, Home and End move between rows; Escape and a click outside close it and Escape gives
* focus back to the trigger; opened from the keyboard, focus moves to the first row. On a touch
* phone the trigger opens the site's bottom sheet (sheet.tsx) instead. Every dropdown uses this.
@@ -20,7 +21,6 @@ export function SiteMenu(props: {
/** The menu's accessible name. */
label: string
items: SiteMenuItem[]
placement?: "above" | "below"
align?: "start" | "end"
}) {
const [open, setOpen] = createSignal(false)
@@ -54,6 +54,8 @@ export function SiteMenu(props: {
const toggle = (event: MouseEvent) => {
if (sheet() && sheetControl) return sheetControl.open(trigger)
setOpen(!open())
const menu = root.querySelector<HTMLElement>('[data-slot="menu"]')
if (menu) placeMenu(menu)
if (open() && event.detail === 0) rows()[0]?.focus({ preventScroll: true })
}
@@ -70,12 +72,7 @@ export function SiteMenu(props: {
item.external ? { target: "_blank", rel: "noopener noreferrer" } : { target: undefined, rel: undefined }
return (
<div
ref={root}
data-component="site-menu"
data-placement={props.placement ?? "above"}
data-align={props.align ?? "start"}
>
<div ref={root} data-component="site-menu" data-align={props.align ?? "start"}>
<button
ref={trigger}
type="button"
+13 -14
View File
@@ -792,8 +792,9 @@ html:has([data-site][data-theme="dark"]) {
margin: 0;
}
/* Opens upward because the carousel track clips anything below the cards (SiteMenu's
placement="below" opens it downward; align="end" from the trigger's end edge). */
/* Laid out above the trigger, from its start edge (align="end": its end edge). placeMenu
(place-menu.ts) then opens it on the side with room, in the top layer, so the carousel's
track doesn't cut it off. */
[data-slot="menu"] {
position: absolute;
inset-inline-start: 0;
@@ -827,21 +828,11 @@ html:has([data-site][data-theme="dark"]) {
}
}
&[data-placement="below"] [data-slot="menu"] {
top: calc(100% + 0.375rem);
bottom: auto;
transform-origin: top var(--x-start, left);
}
&[data-align="end"] [data-slot="menu"] {
inset-inline: auto 0;
transform-origin: bottom var(--x-end, right);
}
&[data-placement="below"][data-align="end"] [data-slot="menu"] {
transform-origin: top var(--x-end, right);
}
/* SiteMenu's link rows: the label, then the external arrow for a link that opens a new tab. */
[data-slot="menu"] [data-slot="link"] {
display: flex;
@@ -891,8 +882,8 @@ html:has([data-site][data-theme="dark"]) {
color: var(--text-strong);
}
/* Phones: menu rows at the body size, 44px tall; ProductAction's fitMenu keeps the menu 16px
from both screen edges. Commands stay on one line and end in an ellipsis. */
/* Phones: menu rows at the body size, 44px tall; placeMenu widens the menu to 16px from both
screen edges. Commands stay on one line and end in an ellipsis. */
@media (max-width: 40rem) {
[data-slot="menu"] > :is([data-slot="install"], [data-slot="store"], [data-slot="item"], [data-slot="link"]) {
min-height: 2.75rem;
@@ -925,6 +916,14 @@ html:has([data-site][data-theme="dark"]) {
}
}
/* A menu placeMenu moved to the top layer keeps its own look, without a popover's frame. */
:where([data-side][popover]) {
margin: 0;
padding: 0;
border: 0;
overflow: visible;
}
/* Page header: the large OpenTUI Mono title and its Inter description. Every page starts with one. */
[data-component="hero"] {
display: flex;
+2 -1
View File
@@ -199,7 +199,8 @@
}
}
/* As wide as its button and lined up with its left edge; a long label ends in "…". Rows as in
/* As wide as its button and lined up with its left edge (placeMenu opens it where it fits, and
on phones as wide as the screen less 16px a side); a long label ends in "…". Rows as in
the logo menu (site.css), but OpenTUI Mono at 14px rather than its 12px (David found 12px too small);
0.5rem × 0.875rem padding, 14px icons, the popover
background and shadow, and the grey menu states (a tint and a mid-grey bar on hover and
+6 -1
View File
@@ -40,6 +40,7 @@ import { ContentsMenu, OnThisPage, SectionHeading, slug } from "~/component/site
import { PageTitle } from "~/component/site/sticky-title"
import { AgentRulesButton } from "~/component/site/agent-rules-button"
import { inlineKey } from "~/lib/direction"
import { placeMenu } from "~/component/site/place-menu"
export default function Brand() {
const i18n = useI18n()
@@ -344,7 +345,11 @@ export default function Brand() {
? i18n.t("brand.ui.moreFormats", { detail: props.detail })
: i18n.t("brand.ui.downloadAsset", { detail: props.detail })
}
onClick={() => setOpen(!open())}
onClick={() => {
setOpen(!open())
const menu = state.root?.querySelector<HTMLElement>('[data-slot="menu"]')
if (menu) placeMenu(menu)
}}
>
<Show when={!props.chevronOnly}>{i18n.t("download.action.download")}</Show>
<ChevronIcon />