Skip to content

Commit e1989fc

Browse files
authored
fix(site): open menus where they fit and stop resetting scroll on reload (#62396)
1 parent 1a5b723 commit e1989fc

10 files changed

Lines changed: 107 additions & 55 deletions

File tree

‎services/site/DESIGN.md‎

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
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.
44

5-
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.
5+
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.
66

77
To add a rule, add one line under its group. Mark a rule that is still being decided with _(provisional)_.
88

@@ -54,6 +54,7 @@ To add a rule, add one line under its group. Mark a rule that is still being dec
5454
- Make icon tiles as tall as their text block. → [Product list](/dev/components#product-list)
5555
- Make action buttons in product headers equal width and right-aligned.
5656
- Keep product and companion tile sizes as they are until the size is decided. _(provisional)_ → [Companion shelf](/dev/components#companions)
57+
- 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`).
5758
- Cause no layout shift (CLS 0). Reserve the space, and use skeletons where content streams in.
5859
- 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)_
5960

@@ -70,6 +71,7 @@ To add a rule, add one line under its group. Mark a rule that is still being dec
7071
- End every `target="_blank"` link with `<NewTab />` for screen readers. → [Icons](/dev/components#icons)
7172
- Use `component/faq.tsx` for every accordion. → [Accordion](/dev/components#faq)
7273
- 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.
74+
- 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.
7375
- Turn dropdowns into the shared bottom sheet (`sheet.tsx`) on touch phones. → [Bottom sheet](/dev/components#sheet)
7476
- Give long pages the Contents rail (`OnThisPage`) and its phone bar (`ContentsMenu`). → [Contents](/dev/components#contents)
7577
- Draw breadcrumbs with muted ancestors, `|` separators and the current page in ink. → [Breadcrumbs](/dev/components#breadcrumb)

‎services/site/src/app.tsx‎

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import { Link, Title, Meta } from "@solidjs/meta"
22
import { Router, useCurrentMatches, useLocation } from "@solidjs/router"
33
import { FileRoutes } from "@solidjs/start/router"
44
import { Show, Suspense } from "solid-js"
5+
import { isServer } from "solid-js/web"
56
import { Favicon } from "@opencode/ui/favicon"
67
import { Font } from "@opencode/ui/font"
78
import "./style/ibm-plex-mono.dev.css"
@@ -69,14 +70,26 @@ function AppMeta() {
6970
)
7071
}
7172

73+
// The router's scroll restoration (below) sets history.scrollRestoration to "manual" and restores
74+
// only after the app has loaded, so a page loaded again (a reload, or back from another site or a
75+
// plain link) would paint at the top and then jump. Leaving a document hands its entry back to the
76+
// browser, which restores it as the page loads; a page restored from the back/forward cache, still
77+
// running, takes the router's manual mode again for its in-site back and forward.
78+
if (!isServer) {
79+
window.addEventListener("pagehide", () => (history.scrollRestoration = "auto"))
80+
window.addEventListener("pageshow", (event) => {
81+
if (event.persisted) history.scrollRestoration = "manual"
82+
})
83+
}
84+
7285
export default function App() {
7386
return (
7487
<Router
7588
explicitLinks={true}
7689
transformUrl={strip}
77-
// The router restores scroll on back and forward itself, once the page has rendered. The
78-
// browser's own restore lands before that, on the page being left, and shows as a jump at
79-
// the start of the hero's shrink back into its card.
90+
// On in-site back and forward the router restores scroll itself, once the page has rendered.
91+
// The browser's own restore lands right after popstate, on the page being left, and shows as
92+
// a jump at the start of the hero's shrink back into its card.
8093
scrollRestoration
8194
root={(props) => (
8295
<LanguageProvider>

‎services/site/src/component/site/changelog-entry.tsx‎

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -168,7 +168,6 @@ function ChangelogSubscribe(props: { filter: string[] }) {
168168
<div data-slot="subscribe">
169169
<SiteMenu
170170
label={i18n.t("changelog.subscribe")}
171-
placement="below"
172171
align="end"
173172
trigger={
174173
<>
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
let anchors = 0
2+
3+
/**
4+
* Opens a dropdown's menu where it fits. Call it as soon as the menu is in the page, laid out by its
5+
* CSS against its positioning box (the trigger, or a split button): beside it, at its gap, with its
6+
* width and inline alignment. The menu opens below unless only above has room; with room on neither
7+
* side it takes the taller one and scrolls. Sideways it keeps 16px clear of the window's edges, and
8+
* on phones takes that whole width (long rows end in an ellipsis).
9+
*
10+
* The menu then moves to the top layer (a manual popover), so no scrolling box, such as the product
11+
* carousel's track, cuts it off, and stays tied to its box by CSS anchor positioning as the page
12+
* scrolls. A browser without anchor positioning keeps it in its box.
13+
*/
14+
export function placeMenu(menu: HTMLElement) {
15+
const box = menu.offsetParent
16+
if (!(box instanceof HTMLElement)) return
17+
// Measured as laid out, not as drawn: the opening animation scales the menu from a corner.
18+
menu.style.animation = "none"
19+
const left = 16
20+
const right = document.documentElement.clientWidth - 16
21+
menu.style.maxWidth = `${right - left}px`
22+
if (window.matchMedia("(max-width: 40rem)").matches) menu.style.width = `${right - left}px`
23+
const anchor = box.getBoundingClientRect()
24+
const laid = menu.getBoundingClientRect()
25+
const gap = Math.max(0, laid.top >= anchor.bottom ? laid.top - anchor.bottom : anchor.top - laid.bottom)
26+
const below = window.innerHeight - anchor.bottom - gap - 8
27+
const above = anchor.top - gap - 8
28+
const side = laid.height <= below || (laid.height > above && below >= above) ? "below" : "above"
29+
const room = side === "below" ? below : above
30+
const shift = Math.max(left - laid.left, Math.min(0, right - laid.right))
31+
32+
menu.dataset.side = side
33+
menu.style.setProperty("--menu-shift", side === "below" ? "-4px" : "4px")
34+
menu.style.transformOrigin = `${getComputedStyle(menu).transformOrigin.split(" ")[0]} ${side === "below" ? "top" : "bottom"}`
35+
menu.style.maxHeight = laid.height > room ? `${Math.floor(room)}px` : ""
36+
menu.style.overflowY = laid.height > room ? "auto" : ""
37+
if (!CSS.supports("anchor-name: --menu") || typeof menu.showPopover !== "function") {
38+
menu.style.top = side === "below" ? `calc(100% + ${gap}px)` : "auto"
39+
menu.style.bottom = side === "above" ? `calc(100% + ${gap}px)` : "auto"
40+
menu.style.translate = shift ? `${shift}px 0` : ""
41+
menu.style.animation = ""
42+
return
43+
}
44+
const name = box.style.getPropertyValue("anchor-name") || `--menu-${++anchors}`
45+
box.style.setProperty("anchor-name", name)
46+
menu.style.setProperty("position-anchor", name)
47+
menu.style.position = "fixed"
48+
menu.style.inset = "auto"
49+
menu.style.left = `calc(anchor(left) + ${laid.left + shift - anchor.left}px)`
50+
if (side === "below") menu.style.top = `calc(anchor(bottom) + ${gap}px)`
51+
if (side === "above") menu.style.bottom = `calc(anchor(top) + ${gap}px)`
52+
// Its width as laid out against its box; a percentage would now be of the window.
53+
menu.style.width = `${laid.width}px`
54+
menu.style.minWidth = "0"
55+
menu.popover = "manual"
56+
menu.showPopover()
57+
menu.style.animation = ""
58+
}

‎services/site/src/component/site/product-page.css‎

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -275,14 +275,11 @@
275275
}
276276
}
277277

278-
/* The intro has room below it, so the menu opens downward from the right edge. */
278+
/* The menu opens from the right edge. */
279279
[data-slot="menu"] {
280280
inset-inline-start: auto;
281281
inset-inline-end: 0;
282-
top: calc(100% + 0.5rem);
283-
bottom: auto;
284282
transform-origin: top var(--x-end, right);
285-
--menu-shift: -4px;
286283
}
287284

288285
/* The exposed install commands need two columns: the copy takes the first two. */

‎services/site/src/component/site/products.tsx‎

Lines changed: 3 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ import { copyOnClick, copySelection } from "~/lib/clipboard"
3434
import { detectPlatform, isMobile, requestPlatform } from "~/lib/platform"
3535
import { Chevron, NewTab, PlatformIcon } from "./icons"
3636
import { onCloseMenus } from "./menu-open"
37+
import { placeMenu } from "./place-menu"
3738
import { Sheet, prefersSheet, type SheetControl } from "./sheet"
3839
import { holdPopstate } from "./popstate"
3940
import {
@@ -1102,7 +1103,7 @@ export function ProductAction(props: { slug: PageSlug; split?: boolean; both?: b
11021103
if (openSheet()) return
11031104
setOpen(!open())
11041105
const panel = menu?.querySelector<HTMLElement>('[data-slot="menu"]')
1105-
if (panel) fitMenu(panel)
1106+
if (panel) placeMenu(panel)
11061107
// From the keyboard, focus moves into the menu.
11071108
if (open() && event.detail === 0)
11081109
menu?.querySelector<HTMLElement>('[role="menuitem"]')?.focus({ preventScroll: true })
@@ -1279,7 +1280,7 @@ export function ProductAction(props: { slug: PageSlug; split?: boolean; both?: b
12791280
if (openSheet()) return
12801281
setOpen(!open())
12811282
const panel = menu?.querySelector<HTMLElement>('[data-slot="menu"]')
1282-
if (panel) fitMenu(panel)
1283+
if (panel) placeMenu(panel)
12831284
}}
12841285
>
12851286
{props.slug === "mobile" ? i18n.t("download.action.download") : i18n.t("download.action.install")}
@@ -1520,26 +1521,6 @@ function elementReady(selector: string) {
15201521
})
15211522
}
15221523

1523-
/**
1524-
* Keeps an open menu inside the carousel's visible area (or the window, outside the carousel),
1525-
* which would otherwise cut it off: no wider than that area and 8px clear of the window's edges
1526-
* (long install commands wrap), and slid sideways just enough.
1527-
*/
1528-
function fitMenu(panel: HTMLElement) {
1529-
const area = panel.closest('[data-slot="track"]')?.getBoundingClientRect() ?? new DOMRect(0, 0, window.innerWidth)
1530-
// Never closer than 16px to a screen edge; on phones the menu takes that whole width, so the
1531-
// install commands get as much room as there is (each still one line, cut with an ellipsis).
1532-
const left = Math.max(area.left, 16)
1533-
const right = Math.min(area.right, window.innerWidth - 16)
1534-
panel.style.maxWidth = `${right - left}px`
1535-
panel.style.width = window.matchMedia("(max-width: 40rem)").matches ? `${right - left}px` : ""
1536-
// Laid out, not as drawn: the opening animation scales the menu from a corner.
1537-
const parent = (panel.offsetParent ?? document.body).getBoundingClientRect()
1538-
const start = parent.left + panel.offsetLeft
1539-
const shift = Math.max(left - start, Math.min(0, right - (start + panel.offsetWidth)))
1540-
panel.style.translate = shift ? `${Math.round(shift)}px 0` : ""
1541-
}
1542-
15431524
// The card transition running now. A newer one (back during a grow) owns the page's markers.
15441525
let current: ViewTransition | undefined
15451526

‎services/site/src/component/site/site-menu.tsx‎

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import { For, Show, createSignal, createUniqueId, onCleanup, onMount, type JSX } from "solid-js"
22
import { ExternalArrow, NewTab } from "./icons"
33
import { onCloseMenus } from "./menu-open"
4+
import { placeMenu } from "./place-menu"
45
import { Sheet, prefersSheet, type SheetControl } from "./sheet"
56

67
/** 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 }
910
/**
1011
* The site's dropdown: a trigger and a menu of links in the shared menu look (site.css's
1112
* `site-menu`: rows edge to edge, the grey hover tint and the bar on the left, the popover colours,
12-
* shadow and opening animation). It opens above the trigger or below it, from its start or end edge.
13+
* shadow and opening animation). It opens where it fits (placeMenu), from the trigger's start or end edge.
1314
* Arrow keys, Home and End move between rows; Escape and a click outside close it and Escape gives
1415
* focus back to the trigger; opened from the keyboard, focus moves to the first row. On a touch
1516
* phone the trigger opens the site's bottom sheet (sheet.tsx) instead. Every dropdown uses this.
@@ -20,7 +21,6 @@ export function SiteMenu(props: {
2021
/** The menu's accessible name. */
2122
label: string
2223
items: SiteMenuItem[]
23-
placement?: "above" | "below"
2424
align?: "start" | "end"
2525
}) {
2626
const [open, setOpen] = createSignal(false)
@@ -54,6 +54,8 @@ export function SiteMenu(props: {
5454
const toggle = (event: MouseEvent) => {
5555
if (sheet() && sheetControl) return sheetControl.open(trigger)
5656
setOpen(!open())
57+
const menu = root.querySelector<HTMLElement>('[data-slot="menu"]')
58+
if (menu) placeMenu(menu)
5759
if (open() && event.detail === 0) rows()[0]?.focus({ preventScroll: true })
5860
}
5961

@@ -70,12 +72,7 @@ export function SiteMenu(props: {
7072
item.external ? { target: "_blank", rel: "noopener noreferrer" } : { target: undefined, rel: undefined }
7173

7274
return (
73-
<div
74-
ref={root}
75-
data-component="site-menu"
76-
data-placement={props.placement ?? "above"}
77-
data-align={props.align ?? "start"}
78-
>
75+
<div ref={root} data-component="site-menu" data-align={props.align ?? "start"}>
7976
<button
8077
ref={trigger}
8178
type="button"

‎services/site/src/component/site/site.css‎

Lines changed: 13 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -792,8 +792,9 @@ html:has([data-site][data-theme="dark"]) {
792792
margin: 0;
793793
}
794794

795-
/* Opens upward because the carousel track clips anything below the cards (SiteMenu's
796-
placement="below" opens it downward; align="end" from the trigger's end edge). */
795+
/* Laid out above the trigger, from its start edge (align="end": its end edge). placeMenu
796+
(place-menu.ts) then opens it on the side with room, in the top layer, so the carousel's
797+
track doesn't cut it off. */
797798
[data-slot="menu"] {
798799
position: absolute;
799800
inset-inline-start: 0;
@@ -827,21 +828,11 @@ html:has([data-site][data-theme="dark"]) {
827828
}
828829
}
829830

830-
&[data-placement="below"] [data-slot="menu"] {
831-
top: calc(100% + 0.375rem);
832-
bottom: auto;
833-
transform-origin: top var(--x-start, left);
834-
}
835-
836831
&[data-align="end"] [data-slot="menu"] {
837832
inset-inline: auto 0;
838833
transform-origin: bottom var(--x-end, right);
839834
}
840835

841-
&[data-placement="below"][data-align="end"] [data-slot="menu"] {
842-
transform-origin: top var(--x-end, right);
843-
}
844-
845836
/* SiteMenu's link rows: the label, then the external arrow for a link that opens a new tab. */
846837
[data-slot="menu"] [data-slot="link"] {
847838
display: flex;
@@ -891,8 +882,8 @@ html:has([data-site][data-theme="dark"]) {
891882
color: var(--text-strong);
892883
}
893884

894-
/* Phones: menu rows at the body size, 44px tall; ProductAction's fitMenu keeps the menu 16px
895-
from both screen edges. Commands stay on one line and end in an ellipsis. */
885+
/* Phones: menu rows at the body size, 44px tall; placeMenu widens the menu to 16px from both
886+
screen edges. Commands stay on one line and end in an ellipsis. */
896887
@media (max-width: 40rem) {
897888
[data-slot="menu"] > :is([data-slot="install"], [data-slot="store"], [data-slot="item"], [data-slot="link"]) {
898889
min-height: 2.75rem;
@@ -925,6 +916,14 @@ html:has([data-site][data-theme="dark"]) {
925916
}
926917
}
927918

919+
/* A menu placeMenu moved to the top layer keeps its own look, without a popover's frame. */
920+
:where([data-side][popover]) {
921+
margin: 0;
922+
padding: 0;
923+
border: 0;
924+
overflow: visible;
925+
}
926+
928927
/* Page header: the large OpenTUI Mono title and its Inter description. Every page starts with one. */
929928
[data-component="hero"] {
930929
display: flex;

‎services/site/src/routes/brand/index.css‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -199,7 +199,8 @@
199199
}
200200
}
201201

202-
/* As wide as its button and lined up with its left edge; a long label ends in "…". Rows as in
202+
/* As wide as its button and lined up with its left edge (placeMenu opens it where it fits, and
203+
on phones as wide as the screen less 16px a side); a long label ends in "…". Rows as in
203204
the logo menu (site.css), but OpenTUI Mono at 14px rather than its 12px (David found 12px too small);
204205
0.5rem × 0.875rem padding, 14px icons, the popover
205206
background and shadow, and the grey menu states (a tint and a mid-grey bar on hover and

‎services/site/src/routes/brand/index.tsx‎

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@ import { ContentsMenu, OnThisPage, SectionHeading, slug } from "~/component/site
4040
import { PageTitle } from "~/component/site/sticky-title"
4141
import { AgentRulesButton } from "~/component/site/agent-rules-button"
4242
import { inlineKey } from "~/lib/direction"
43+
import { placeMenu } from "~/component/site/place-menu"
4344

4445
export default function Brand() {
4546
const i18n = useI18n()
@@ -344,7 +345,11 @@ export default function Brand() {
344345
? i18n.t("brand.ui.moreFormats", { detail: props.detail })
345346
: i18n.t("brand.ui.downloadAsset", { detail: props.detail })
346347
}
347-
onClick={() => setOpen(!open())}
348+
onClick={() => {
349+
setOpen(!open())
350+
const menu = state.root?.querySelector<HTMLElement>('[data-slot="menu"]')
351+
if (menu) placeMenu(menu)
352+
}}
348353
>
349354
<Show when={!props.chevronOnly}>{i18n.t("download.action.download")}</Show>
350355
<ChevronIcon />

0 commit comments

Comments
 (0)