mirror of
https://github.com/stablyai/orca.git
synced 2026-11-19 04:23:57 +00:00
fix(tab-bar): fit the + menu agent list to the screen and point the simulator tip at the phone (#43481)
* fix(tab-bar): fit the + menu agent list to the screen and point the simulator tip at the phone The agent list was capped at exactly 7 rows, so it cut on a row edge and looked complete. It now grows into the room Radix gives the menu (up to 12 rows) and, only when it still overflows, cuts through a row with a fade. Tiny windows hide the tip first, then let the whole menu scroll. The simulator tip's arrow was fixed under the first icon; it now follows the phone icon wherever the customizable row puts it. * fix(tab-bar): re-measure a hidden + menu tip when the menu width changes A tip hidden at a narrow width kept its taller wrapped height, so it could stay hidden after the menu widened even when the shorter tip would fit. Key the cached height to the menu's outer width (which a scrollbar the tip causes does not change) and measure the tip again at a new width. * refactor(tab-bar): keep a yielded + menu tip hidden until the menu reopens Drop the width-keyed tip height cache and the restore path. Once the tip gives its room to the agent list it stays hidden for that menu session; the next open measures it fresh. A tip that stops being eligible simply unmounts, so its room is freed without any extra bookkeeping.
This commit is contained in:
@@ -1946,7 +1946,9 @@ html.native-shell .app-layout {
|
||||
content: '';
|
||||
position: absolute;
|
||||
top: -7px;
|
||||
left: 0.8125rem;
|
||||
/* Why: centred on the phone icon, measured by use-intro-arrow. */
|
||||
inset-inline-start: calc(var(--intro-arrow-x, 1.25rem) - 7px);
|
||||
display: var(--intro-arrow-display, block);
|
||||
border-style: solid;
|
||||
border-width: 0 7px 7px;
|
||||
/* Why: use foreground + border to create a subtle shadow that adapts to theme */
|
||||
@@ -1958,7 +1960,8 @@ html.native-shell .app-layout {
|
||||
content: '';
|
||||
position: absolute;
|
||||
top: -6px;
|
||||
left: 0.8125rem;
|
||||
inset-inline-start: calc(var(--intro-arrow-x, 1.25rem) - 6px);
|
||||
display: var(--intro-arrow-display, block);
|
||||
border-style: solid;
|
||||
border-width: 0 6px 6px;
|
||||
border-color: transparent transparent var(--muted) transparent;
|
||||
|
||||
@@ -9,6 +9,7 @@ import { Input } from '@/components/ui/input'
|
||||
import { Terminal } from 'lucide-react'
|
||||
import { agentPickerBlankTerminalMatches } from '@/lib/agent-catalog/agent-picker-search'
|
||||
import { AgentDefaultContextMenu } from './agent-combobox-items'
|
||||
import { AgentPickerResultsScroller } from './AgentPickerResultsScroller'
|
||||
import { AgentPickerMenuRow, type AgentPickerMenuLaunch } from './AgentLaunchPickerMenuRow'
|
||||
import { DropdownMenuItem } from '@/components/ui/dropdown-menu'
|
||||
import { choiceMatches, type AgentPickerChoice } from './agent-picker-search-rows'
|
||||
@@ -210,7 +211,7 @@ export function AgentLaunchPickerMenu({
|
||||
/>
|
||||
</div>
|
||||
) : null}
|
||||
<div data-agent-picker-results className="max-h-64 overflow-y-auto scrollbar-sleek">
|
||||
<AgentPickerResultsScroller>
|
||||
<div data-agent-picker-agent-results>
|
||||
{showBlankTerminal ? (
|
||||
<AgentDefaultContextMenu
|
||||
@@ -289,7 +290,7 @@ export function AgentLaunchPickerMenu({
|
||||
</DropdownMenuItem>
|
||||
) : null}
|
||||
</div>
|
||||
</div>
|
||||
</AgentPickerResultsScroller>
|
||||
</div>
|
||||
)
|
||||
}
|
||||
|
||||
@@ -0,0 +1,64 @@
|
||||
import { createContext, useContext, useLayoutEffect, useState, type ReactNode } from 'react'
|
||||
|
||||
/** How a host menu sizes the agent list; absent everywhere but menus that fit it to the screen. */
|
||||
export type AgentPickerListFit = {
|
||||
registerList: (node: HTMLDivElement | null) => void
|
||||
/** null: no cap of its own, the host menu scrolls. */
|
||||
maxHeight: number | null
|
||||
scrollPaddingBottom: number | null
|
||||
fadeVisible: boolean
|
||||
}
|
||||
|
||||
export const AgentPickerListFitContext = createContext<AgentPickerListFit | null>(null)
|
||||
|
||||
// Its own component so fit updates (e.g. the fade on scroll) never re-render the rows.
|
||||
export function AgentPickerResultsScroller({ children }: { children: ReactNode }) {
|
||||
const fit = useContext(AgentPickerListFitContext)
|
||||
if (!fit) {
|
||||
return (
|
||||
<div data-agent-picker-results className="max-h-64 overflow-y-auto scrollbar-sleek">
|
||||
{children}
|
||||
</div>
|
||||
)
|
||||
}
|
||||
return <FittedResultsScroller fit={fit}>{children}</FittedResultsScroller>
|
||||
}
|
||||
|
||||
function FittedResultsScroller({
|
||||
fit: { registerList, maxHeight, scrollPaddingBottom, fadeVisible },
|
||||
children
|
||||
}: {
|
||||
fit: AgentPickerListFit
|
||||
children: ReactNode
|
||||
}) {
|
||||
const [node, setNode] = useState<HTMLDivElement | null>(null)
|
||||
useLayoutEffect(() => {
|
||||
if (!node) {
|
||||
return
|
||||
}
|
||||
registerList(node)
|
||||
return () => registerList(null)
|
||||
}, [registerList, node])
|
||||
return (
|
||||
<div className="relative">
|
||||
<div
|
||||
ref={setNode}
|
||||
data-agent-picker-results
|
||||
className={maxHeight !== null ? 'overflow-y-auto scrollbar-sleek' : undefined}
|
||||
style={
|
||||
maxHeight !== null
|
||||
? { maxHeight, scrollPaddingBottom: scrollPaddingBottom ?? undefined }
|
||||
: undefined
|
||||
}
|
||||
>
|
||||
{children}
|
||||
</div>
|
||||
{fadeVisible ? (
|
||||
<div
|
||||
aria-hidden
|
||||
className="pointer-events-none absolute inset-x-0 bottom-0 h-4 bg-linear-to-b from-transparent to-popover"
|
||||
/>
|
||||
) : null}
|
||||
</div>
|
||||
)
|
||||
}
|
||||
+11
-1
@@ -1,15 +1,25 @@
|
||||
import { useState } from 'react'
|
||||
import { X } from 'lucide-react'
|
||||
import { Button } from '@/components/ui/button'
|
||||
import { Tooltip, TooltipContent, TooltipTrigger } from '@/components/ui/tooltip'
|
||||
import { useTranslate } from '@/i18n/use-translate'
|
||||
import { useMobileEmulatorTabIntroActions } from './use-mobile-emulator-tab-intro-actions'
|
||||
import { CREATE_ROW_MOBILE_EMULATOR_ID } from '../../tab-bar/create-menu/tab-bar-create-row-item'
|
||||
import { useIntroArrow, type IntroArrowAnchor } from './use-intro-arrow'
|
||||
|
||||
export function MobileEmulatorTabIntroCallout(): React.JSX.Element {
|
||||
export function MobileEmulatorTabIntroCallout({
|
||||
anchor
|
||||
}: {
|
||||
anchor: IntroArrowAnchor
|
||||
}): React.JSX.Element {
|
||||
const t = useTranslate()
|
||||
const [callout, setCallout] = useState<HTMLDivElement | null>(null)
|
||||
useIntroArrow(callout, anchor, CREATE_ROW_MOBILE_EMULATOR_ID)
|
||||
const { keepIntro, hideIntro, dismissIntro } = useMobileEmulatorTabIntroActions()
|
||||
|
||||
return (
|
||||
<div
|
||||
ref={setCallout}
|
||||
className="mobile-emulator-tab-intro-callout--menu mt-1 flex items-center gap-2 rounded-lg border-x border-b border-border/70 bg-muted px-2 py-1.5 dark:bg-accent/80"
|
||||
// Prevent Radix outside-selection so Keep/Hide/Dismiss actions leave the callout’s menu open.
|
||||
onPointerDown={(event) => event.preventDefault()}
|
||||
|
||||
@@ -0,0 +1,67 @@
|
||||
import { describe, expect, it } from 'vitest'
|
||||
import { introArrowInset, revealScrollDelta } from './intro-arrow-geometry'
|
||||
|
||||
// A 280px callout at x=104 with 1px borders under a row of 36px icons (2px gaps) from x=110.
|
||||
const callout = { left: 104, right: 384 }
|
||||
const view = { left: 110, right: 360 }
|
||||
const base = { callout, borderStart: 1, width: 278, view, scale: 1, rtl: false }
|
||||
|
||||
describe('introArrowInset', () => {
|
||||
it('centres the arrow under the phone wherever it sits in the row', () => {
|
||||
expect(introArrowInset({ ...base, iconCenter: 128 })).toEqual({ inset: 23, visible: true })
|
||||
expect(introArrowInset({ ...base, iconCenter: 242 })).toEqual({ inset: 137, visible: true })
|
||||
expect(introArrowInset({ ...base, iconCenter: 280 })).toEqual({ inset: 175, visible: true })
|
||||
})
|
||||
|
||||
it('gives the settled position while the menu is still scaling open', () => {
|
||||
// Scaled 0.98 about the callout's left edge: viewport distances shrink, local ones must not.
|
||||
const scaled = { left: 104, right: 104 + 280 * 0.98 }
|
||||
expect(
|
||||
introArrowInset({ ...base, callout: scaled, iconCenter: 104 + 176 * 0.98, scale: 0.98 }).inset
|
||||
).toBeCloseTo(175)
|
||||
})
|
||||
|
||||
it('measures from the right edge in right-to-left layouts', () => {
|
||||
expect(introArrowInset({ ...base, rtl: true, iconCenter: 360 })).toEqual({
|
||||
inset: 23,
|
||||
visible: true
|
||||
})
|
||||
})
|
||||
|
||||
it('keeps the arrow off the rounded corners', () => {
|
||||
expect(introArrowInset({ ...base, iconCenter: 106 }).inset).toBe(12)
|
||||
expect(introArrowInset({ ...base, iconCenter: 390 }).inset).toBe(266)
|
||||
})
|
||||
|
||||
it('hides the arrow once the phone is scrolled out of the row', () => {
|
||||
expect(introArrowInset({ ...base, iconCenter: 380 }).visible).toBe(false)
|
||||
expect(introArrowInset({ ...base, iconCenter: 100 }).visible).toBe(false)
|
||||
})
|
||||
})
|
||||
|
||||
describe('revealScrollDelta', () => {
|
||||
const row = { view, margin: 24, scale: 1 }
|
||||
|
||||
it('scrolls an icon under the end fade into view', () => {
|
||||
expect(revealScrollDelta({ ...row, item: { left: 350, right: 386 } })).toBe(50)
|
||||
})
|
||||
|
||||
it('scrolls an icon under the start fade into view', () => {
|
||||
expect(revealScrollDelta({ ...row, item: { left: 90, right: 126 } })).toBe(-44)
|
||||
})
|
||||
|
||||
it('leaves a clear icon alone', () => {
|
||||
expect(revealScrollDelta({ ...row, item: { left: 200, right: 236 } })).toBe(0)
|
||||
})
|
||||
|
||||
it('returns local px while the menu is scaled', () => {
|
||||
expect(
|
||||
revealScrollDelta({
|
||||
item: { left: 190, right: 208 },
|
||||
view: { left: 0, right: 200 },
|
||||
margin: 24,
|
||||
scale: 0.5
|
||||
})
|
||||
).toBe(40)
|
||||
})
|
||||
})
|
||||
@@ -0,0 +1,62 @@
|
||||
type Span = { left: number; right: number }
|
||||
|
||||
/** Keeps the 7px arrow clear of the callout's rounded corners. */
|
||||
const MIN_ARROW_INSET = 12
|
||||
|
||||
/**
|
||||
* Where the tip's arrow centre sits, in the callout's local px from its inline-start padding edge.
|
||||
* Rects are viewport px, so they are divided by the menu's open-animation scale.
|
||||
*/
|
||||
export function introArrowInset({
|
||||
callout,
|
||||
borderStart,
|
||||
width,
|
||||
iconCenter,
|
||||
view,
|
||||
scale,
|
||||
rtl
|
||||
}: {
|
||||
/** Callout border box, viewport px. */
|
||||
callout: Span
|
||||
/** Inline-start border width, local px. */
|
||||
borderStart: number
|
||||
/** Callout padding-box width, local px. */
|
||||
width: number
|
||||
/** Phone icon centre x, viewport px. */
|
||||
iconCenter: number
|
||||
/** The icon row's visible box, viewport px. */
|
||||
view: Span
|
||||
scale: number
|
||||
rtl: boolean
|
||||
}): { inset: number; visible: boolean } {
|
||||
const fromStart = rtl ? callout.right - iconCenter : iconCenter - callout.left
|
||||
const inset = fromStart / scale - borderStart
|
||||
return {
|
||||
inset: Math.min(Math.max(inset, MIN_ARROW_INSET), width - MIN_ARROW_INSET),
|
||||
visible: iconCenter >= view.left && iconCenter <= view.right
|
||||
}
|
||||
}
|
||||
|
||||
/** Physical scrollLeft change that brings the icon clear of the row's edge fades; 0 if it already is. */
|
||||
export function revealScrollDelta({
|
||||
item,
|
||||
view,
|
||||
margin,
|
||||
scale
|
||||
}: {
|
||||
item: Span
|
||||
view: Span
|
||||
/** The row's edge-fade width, local px. */
|
||||
margin: number
|
||||
scale: number
|
||||
}): number {
|
||||
const start = view.left + margin * scale
|
||||
const end = view.right - margin * scale
|
||||
if (item.left < start) {
|
||||
return (item.left - start) / scale
|
||||
}
|
||||
if (item.right > end) {
|
||||
return (item.right - end) / scale
|
||||
}
|
||||
return 0
|
||||
}
|
||||
@@ -0,0 +1,86 @@
|
||||
import { useLayoutEffect } from 'react'
|
||||
import { introArrowInset, revealScrollDelta } from './intro-arrow-geometry'
|
||||
|
||||
/** The create row's edge-fade width (main.css .terminal-tab-strip--fade-*). */
|
||||
const ROW_FADE_PX = 24
|
||||
|
||||
export type IntroArrowAnchor = {
|
||||
/** The create row's horizontally scrolling icon strip. */
|
||||
scroller: HTMLElement | null
|
||||
/** Changes when the icons are reordered, shown or hidden. */
|
||||
order: string
|
||||
}
|
||||
|
||||
/** Points the callout's arrow at an icon in the create row, revealing the icon first. */
|
||||
export function useIntroArrow(
|
||||
callout: HTMLElement | null,
|
||||
anchor: IntroArrowAnchor,
|
||||
itemId: string
|
||||
): void {
|
||||
const { scroller, order } = anchor
|
||||
useLayoutEffect(() => {
|
||||
if (!callout || !scroller) {
|
||||
return
|
||||
}
|
||||
const icon = scroller.querySelector<HTMLElement>(
|
||||
`[data-create-row-item="${CSS.escape(itemId)}"]`
|
||||
)
|
||||
if (!icon) {
|
||||
return
|
||||
}
|
||||
const scaleOf = (): number => {
|
||||
const width = callout.getBoundingClientRect().width
|
||||
return callout.offsetWidth > 0 && width > 0 ? width / callout.offsetWidth : 1
|
||||
}
|
||||
const delta = revealScrollDelta({
|
||||
item: icon.getBoundingClientRect(),
|
||||
view: scroller.getBoundingClientRect(),
|
||||
margin: ROW_FADE_PX,
|
||||
scale: scaleOf()
|
||||
})
|
||||
if (delta !== 0) {
|
||||
scroller.scrollBy({ left: delta, behavior: 'instant' })
|
||||
}
|
||||
const measure = (): void => {
|
||||
if (callout.offsetWidth === 0) {
|
||||
return
|
||||
}
|
||||
const box = callout.getBoundingClientRect()
|
||||
const iconBox = icon.getBoundingClientRect()
|
||||
const style = getComputedStyle(callout)
|
||||
const { inset, visible } = introArrowInset({
|
||||
callout: box,
|
||||
borderStart: Number.parseFloat(style.borderInlineStartWidth) || 0,
|
||||
width: callout.clientWidth,
|
||||
iconCenter: (iconBox.left + iconBox.right) / 2,
|
||||
view: scroller.getBoundingClientRect(),
|
||||
scale: box.width / callout.offsetWidth,
|
||||
rtl: style.direction === 'rtl'
|
||||
})
|
||||
callout.style.setProperty('--intro-arrow-x', `${inset}px`)
|
||||
callout.style.setProperty('--intro-arrow-display', visible ? 'block' : 'none')
|
||||
}
|
||||
measure()
|
||||
let frame: number | null = null
|
||||
const schedule = (): void => {
|
||||
if (frame === null) {
|
||||
frame = requestAnimationFrame(() => {
|
||||
frame = null
|
||||
measure()
|
||||
})
|
||||
}
|
||||
}
|
||||
const resize = new ResizeObserver(schedule)
|
||||
resize.observe(callout)
|
||||
resize.observe(scroller)
|
||||
scroller.addEventListener('scroll', schedule, { passive: true })
|
||||
return () => {
|
||||
resize.disconnect()
|
||||
scroller.removeEventListener('scroll', schedule)
|
||||
if (frame !== null) {
|
||||
cancelAnimationFrame(frame)
|
||||
}
|
||||
}
|
||||
// `order` re-runs this after customizing moves the icon.
|
||||
}, [callout, scroller, order, itemId])
|
||||
}
|
||||
@@ -25,6 +25,7 @@ import type { TabBarCreateRowItem } from './tab-bar-create-row-item'
|
||||
import { TabBarCreateRowCustomizer } from './TabBarCreateRowCustomizer'
|
||||
import { CREATE_ROW_HIGHLIGHT_CLASS, TabBarCreateRowIcons } from './TabBarCreateRowIcons'
|
||||
import { useCreateRowScroller } from './use-create-row-scroller'
|
||||
import type { IntroArrowAnchor } from '../../emulator-pane/tab-intro/use-intro-arrow'
|
||||
|
||||
const EMPTY_LAYOUT: NewTabMenuLayout = { order: [], hidden: [] }
|
||||
|
||||
@@ -43,7 +44,7 @@ export function TabBarCreateRow({
|
||||
customizable: boolean
|
||||
layoutSetting: keyof NewTabMenuSettings
|
||||
/** Content tied to shown items, e.g. an intro for an icon the user has not hidden. */
|
||||
renderBelow?: (shownIds: ReadonlySet<string>) => React.ReactNode
|
||||
renderBelow?: (shownIds: ReadonlySet<string>, anchor: IntroArrowAnchor) => React.ReactNode
|
||||
}): React.JSX.Element {
|
||||
const t = useTranslate()
|
||||
const closeMenu = useDropdownMenuClose()
|
||||
@@ -63,12 +64,13 @@ export function TabBarCreateRow({
|
||||
const [keyboardNav, setKeyboardNav] = useState(false)
|
||||
const [hoveredIndex, setHoveredIndex] = useState<number | null>(null)
|
||||
const pointerInside = useRef(false)
|
||||
const { scrollerRef, attachScroller, overflow, revealIcon } = useCreateRowScroller()
|
||||
const { scrollerRef, scroller, attachScroller, overflow, revealIcon } = useCreateRowScroller()
|
||||
const [recordingActionId, setRecordingActionId] = useState<KeybindingActionId | null>(null)
|
||||
const customizeIndex = visible.length
|
||||
const active = Math.min(activeIndex, customizeIndex)
|
||||
const keyboardActive = rowFocused && keyboardNav
|
||||
const below = renderBelow?.(new Set(visible.map((item) => item.id)))
|
||||
const visibleIds = visible.map((item) => item.id)
|
||||
const below = renderBelow?.(new Set(visibleIds), { scroller, order: visibleIds.join(' ') })
|
||||
|
||||
useShortcutRecorderFocus(recordingActionId !== null)
|
||||
|
||||
|
||||
@@ -65,6 +65,7 @@ export function TabBarCreateRowIcons({
|
||||
type="button"
|
||||
tabIndex={-1}
|
||||
aria-label={item.label}
|
||||
data-create-row-item={item.id}
|
||||
className={cn(
|
||||
'flex h-7 w-9 shrink-0 items-center justify-center rounded-[7px] text-muted-foreground outline-hidden',
|
||||
highlighted && CREATE_ROW_HIGHLIGHT_CLASS
|
||||
|
||||
@@ -2,6 +2,7 @@ import React from 'react'
|
||||
import { FilePlus, FileText, Globe, Smartphone, TerminalSquare } from 'lucide-react'
|
||||
import { useTranslate } from '@/i18n/use-translate'
|
||||
import { MobileEmulatorTabIntroCallout } from '../../emulator-pane/tab-intro/MobileEmulatorTabIntroCallout'
|
||||
import { NewTabMenuTipSlot } from '../menu-fit/NewTabMenuTipSlot'
|
||||
import { ShellIcon } from '../../terminal/shells/ShellIcon'
|
||||
import { useAppStore } from '@/store/index'
|
||||
import { usePluginNewFiles } from '@/store/plugins/file-creation/plugin-new-files'
|
||||
@@ -203,11 +204,15 @@ export function TabBarStaticCreateMenu({
|
||||
items={items}
|
||||
customizable={!terminalOnly}
|
||||
layoutSetting={NEW_TAB_MENU_LAYOUT_SETTING[newTabMenuSurface]}
|
||||
renderBelow={(shownIds) =>
|
||||
showMobileEmulatorIntroCallout && isMacOs && shownIds.has(CREATE_ROW_MOBILE_EMULATOR_ID) ? (
|
||||
<MobileEmulatorTabIntroCallout />
|
||||
) : null
|
||||
}
|
||||
renderBelow={(shownIds, anchor) => (
|
||||
<NewTabMenuTipSlot
|
||||
eligible={
|
||||
showMobileEmulatorIntroCallout && isMacOs && shownIds.has(CREATE_ROW_MOBILE_EMULATOR_ID)
|
||||
}
|
||||
>
|
||||
<MobileEmulatorTabIntroCallout anchor={anchor} />
|
||||
</NewTabMenuTipSlot>
|
||||
)}
|
||||
/>
|
||||
)
|
||||
}
|
||||
|
||||
@@ -42,6 +42,7 @@ export function useCreateRowScroller() {
|
||||
|
||||
return {
|
||||
scrollerRef,
|
||||
scroller,
|
||||
overflow,
|
||||
attachScroller: (node: HTMLDivElement | null): void => {
|
||||
scrollerRef.current = node
|
||||
|
||||
@@ -0,0 +1,26 @@
|
||||
import React, { useCallback, useState } from 'react'
|
||||
import { DropdownMenuContent } from '@/components/ui/dropdown-menu'
|
||||
import { cn } from '@/lib/class-names'
|
||||
import { NewTabMenuFitProvider } from './NewTabMenuFitProvider'
|
||||
|
||||
/** The + menu's content: fits its agent list to the screen for the session it is open. */
|
||||
export function NewTabMenuContent({
|
||||
className,
|
||||
children,
|
||||
...props
|
||||
}: React.ComponentProps<typeof DropdownMenuContent>): React.JSX.Element {
|
||||
// Why: Radix re-prefers bottom on every update, so an upward menu would jump below "+" as a search shrinks it.
|
||||
const [preferTop, setPreferTop] = useState(false)
|
||||
const onPlacedSide = useCallback((side: string | null) => setPreferTop(side === 'top'), [])
|
||||
return (
|
||||
<DropdownMenuContent
|
||||
collisionPadding={8}
|
||||
{...props}
|
||||
side={preferTop ? 'top' : props.side}
|
||||
// While the whole menu scrolls, keep keyboard focus clear of its bottom fade.
|
||||
className={cn('has-[>[data-new-tab-menu-fit=menu]]:scroll-pb-[34px]', className)}
|
||||
>
|
||||
<NewTabMenuFitProvider onPlacedSide={onPlacedSide}>{children}</NewTabMenuFitProvider>
|
||||
</DropdownMenuContent>
|
||||
)
|
||||
}
|
||||
@@ -0,0 +1,188 @@
|
||||
import React, { useEffect, useEffectEvent, useMemo, useRef, useState } from 'react'
|
||||
import {
|
||||
AgentPickerListFitContext,
|
||||
type AgentPickerListFit
|
||||
} from '@/components/agent/AgentPickerResultsScroller'
|
||||
import { NewTabMenuTipRoomContext, type NewTabMenuTipRoom } from './new-tab-menu-tip-room'
|
||||
import {
|
||||
NEW_TAB_MENU_LIST_MAX_ROWS,
|
||||
fadeScrollPadding,
|
||||
planNewTabMenuFit,
|
||||
sameNewTabMenuFitPlan,
|
||||
type NewTabMenuFitMode,
|
||||
type NewTabMenuFitPlan
|
||||
} from './new-tab-menu-fit-plan'
|
||||
|
||||
const FALLBACK_ROW = 36
|
||||
|
||||
type FitLayout = { plan: NewTabMenuFitPlan; row: number }
|
||||
|
||||
function sameLayout(a: FitLayout | null, b: FitLayout): boolean {
|
||||
return a !== null && a.row === b.row && sameNewTabMenuFitPlan(a.plan, b.plan)
|
||||
}
|
||||
|
||||
function hasMoreBelow(scroller: HTMLElement): boolean {
|
||||
return scroller.scrollTop + scroller.clientHeight < scroller.scrollHeight - 1
|
||||
}
|
||||
|
||||
function verticalFrame(element: HTMLElement): number {
|
||||
const style = getComputedStyle(element)
|
||||
return (
|
||||
Number.parseFloat(style.paddingTop) +
|
||||
Number.parseFloat(style.paddingBottom) +
|
||||
Number.parseFloat(style.borderTopWidth) +
|
||||
Number.parseFloat(style.borderBottomWidth)
|
||||
)
|
||||
}
|
||||
|
||||
/** The list scrolls in peek mode, the whole menu content in menu mode. */
|
||||
function scrollerFor(
|
||||
mode: NewTabMenuFitMode | null,
|
||||
box: HTMLElement | null,
|
||||
list: HTMLElement | null
|
||||
): HTMLElement | null {
|
||||
return mode === 'peek' ? list : mode === 'menu' ? (box?.parentElement ?? null) : null
|
||||
}
|
||||
|
||||
/**
|
||||
* Lives inside the + menu's content, so it mounts on open and unmounts on the real close:
|
||||
* everything it learns (tip height, placed side) lasts one menu session.
|
||||
*/
|
||||
export function NewTabMenuFitProvider({
|
||||
onPlacedSide,
|
||||
children
|
||||
}: {
|
||||
/** The side Radix first placed the menu on; null once the session ends. Must be stable. */
|
||||
onPlacedSide: (side: string | null) => void
|
||||
children: React.ReactNode
|
||||
}): React.JSX.Element {
|
||||
const [box, setBox] = useState<HTMLDivElement | null>(null)
|
||||
const [list, setList] = useState<HTMLDivElement | null>(null)
|
||||
const [tip, setTip] = useState<HTMLElement | null>(null)
|
||||
// Once the tip gives its room to the list it stays hidden until the menu next opens.
|
||||
const [tipYielded, setTipYielded] = useState(false)
|
||||
const [layout, setLayout] = useState<FitLayout | null>(null)
|
||||
const [fadeVisible, setFadeVisible] = useState(false)
|
||||
const placedSide = useRef<string | null>(null)
|
||||
const mode = layout?.plan.mode ?? null
|
||||
|
||||
const measure = useEffectEvent(() => {
|
||||
const content = box?.parentElement
|
||||
const wrapper = content?.parentElement
|
||||
if (!box || !content || !wrapper) {
|
||||
return
|
||||
}
|
||||
if (placedSide.current === null && !wrapper.style.transform.includes('-200%')) {
|
||||
placedSide.current = content.dataset.side ?? 'bottom'
|
||||
onPlacedSide(placedSide.current)
|
||||
}
|
||||
let planMode = mode
|
||||
const available = Number.parseFloat(
|
||||
wrapper.style.getPropertyValue('--radix-popper-available-height')
|
||||
)
|
||||
if (list && Number.isFinite(available)) {
|
||||
// The slot is a flow-root box, so its height includes the tip's own margin.
|
||||
const tipHeight = tip?.isConnected ? tip.offsetHeight : 0
|
||||
const row = list.querySelector<HTMLElement>('[role="menuitem"]')?.offsetHeight || FALLBACK_ROW
|
||||
const chrome = box.offsetHeight + verticalFrame(content) - list.offsetHeight - tipHeight
|
||||
const next: FitLayout = {
|
||||
row,
|
||||
plan: planNewTabMenuFit({
|
||||
// Why floor and -1: offsetHeight rounds, and a 1px overshoot would make the menu scroll too.
|
||||
available: Math.floor(available) - 1,
|
||||
chrome,
|
||||
tip: tipHeight,
|
||||
natural: list.scrollHeight,
|
||||
row
|
||||
})
|
||||
}
|
||||
planMode = next.plan.mode
|
||||
if (next.plan.tipHidden && tipHeight > 0) {
|
||||
setTipYielded(true)
|
||||
}
|
||||
setLayout((current) => (sameLayout(current, next) ? current : next))
|
||||
}
|
||||
// Content can grow or shrink without a scroll event, so the fade is re-checked on every pass.
|
||||
const scroller = scrollerFor(planMode, box, list)
|
||||
const more = scroller ? hasMoreBelow(scroller) : false
|
||||
setFadeVisible((current) => (current === more ? current : more))
|
||||
})
|
||||
|
||||
// Observers follow the nodes' real lifetime: the list and tip unmount while a search shows results.
|
||||
useEffect(() => {
|
||||
const wrapper = box?.parentElement?.parentElement
|
||||
if (!box || !wrapper) {
|
||||
return
|
||||
}
|
||||
let frame: number | null = null
|
||||
const schedule = (): void => {
|
||||
if (frame === null) {
|
||||
frame = requestAnimationFrame(() => {
|
||||
frame = null
|
||||
measure()
|
||||
})
|
||||
}
|
||||
}
|
||||
const resize = new ResizeObserver(schedule)
|
||||
for (const node of [box, list, list?.firstElementChild, tip]) {
|
||||
if (node) {
|
||||
resize.observe(node)
|
||||
}
|
||||
}
|
||||
// Radix rewrites the wrapper style only when it repositions (resize, flip).
|
||||
const reposition = new MutationObserver(schedule)
|
||||
reposition.observe(wrapper, { attributes: true, attributeFilter: ['style'] })
|
||||
schedule()
|
||||
return () => {
|
||||
resize.disconnect()
|
||||
reposition.disconnect()
|
||||
if (frame !== null) {
|
||||
cancelAnimationFrame(frame)
|
||||
}
|
||||
}
|
||||
}, [box, list, tip])
|
||||
|
||||
useEffect(() => {
|
||||
const scroller = scrollerFor(mode, box, list)
|
||||
if (!scroller) {
|
||||
return
|
||||
}
|
||||
const onScroll = (): void => {
|
||||
const more = hasMoreBelow(scroller)
|
||||
setFadeVisible((current) => (current === more ? current : more))
|
||||
}
|
||||
scroller.addEventListener('scroll', onScroll, { passive: true })
|
||||
return () => scroller.removeEventListener('scroll', onScroll)
|
||||
}, [mode, box, list])
|
||||
|
||||
useEffect(() => () => onPlacedSide(null), [onPlacedSide])
|
||||
|
||||
const listFit = useMemo<AgentPickerListFit>(
|
||||
() => ({
|
||||
registerList: setList,
|
||||
maxHeight: layout ? layout.plan.listMaxHeight : NEW_TAB_MENU_LIST_MAX_ROWS * FALLBACK_ROW,
|
||||
scrollPaddingBottom: layout?.plan.mode === 'peek' ? fadeScrollPadding(layout.row) : null,
|
||||
fadeVisible: layout?.plan.mode === 'peek' && fadeVisible
|
||||
}),
|
||||
[layout, fadeVisible]
|
||||
)
|
||||
const tipRoom = useMemo<NewTabMenuTipRoom>(
|
||||
() => ({ tipHidden: tipYielded, registerTip: setTip }),
|
||||
[tipYielded]
|
||||
)
|
||||
|
||||
return (
|
||||
<div ref={setBox} className="flow-root" data-new-tab-menu-fit={mode ?? undefined}>
|
||||
<NewTabMenuTipRoomContext value={tipRoom}>
|
||||
<AgentPickerListFitContext value={listFit}>{children}</AgentPickerListFitContext>
|
||||
</NewTabMenuTipRoomContext>
|
||||
{mode === 'menu' && fadeVisible ? (
|
||||
<div
|
||||
aria-hidden
|
||||
// Why -bottom-1: cover the content's 4px bottom padding, which rows also scroll through.
|
||||
className="pointer-events-none sticky -bottom-1 -mt-4 h-4 bg-linear-to-b from-transparent to-popover"
|
||||
/>
|
||||
) : null}
|
||||
</div>
|
||||
)
|
||||
}
|
||||
@@ -0,0 +1,33 @@
|
||||
import React, { useLayoutEffect, useState } from 'react'
|
||||
import { useNewTabMenuTipRoom } from './new-tab-menu-tip-room'
|
||||
|
||||
/** Shows an eligible tip unless the + menu gave its room to the agent list. */
|
||||
export function NewTabMenuTipSlot({
|
||||
eligible,
|
||||
children
|
||||
}: {
|
||||
eligible: boolean
|
||||
children: React.ReactNode
|
||||
}): React.ReactNode {
|
||||
const room = useNewTabMenuTipRoom()
|
||||
const registerTip = room?.registerTip
|
||||
const [node, setNode] = useState<HTMLDivElement | null>(null)
|
||||
useLayoutEffect(() => {
|
||||
if (!registerTip || !node) {
|
||||
return
|
||||
}
|
||||
registerTip(node)
|
||||
return () => registerTip(null)
|
||||
}, [registerTip, node])
|
||||
if (!eligible || room?.tipHidden) {
|
||||
return null
|
||||
}
|
||||
// flow-root keeps the tip's top margin inside the measured box.
|
||||
return room ? (
|
||||
<div ref={setNode} className="flow-root">
|
||||
{children}
|
||||
</div>
|
||||
) : (
|
||||
children
|
||||
)
|
||||
}
|
||||
@@ -0,0 +1,79 @@
|
||||
import { describe, expect, it } from 'vitest'
|
||||
import { planNewTabMenuFit } from './new-tab-menu-fit-plan'
|
||||
|
||||
const ROW = 36
|
||||
const rows = (count: number): number => count * ROW
|
||||
// The list alone gets `space` px: no chrome, no tip.
|
||||
const listIn = (space: number, agents: number, row = ROW) =>
|
||||
planNewTabMenuFit({ available: space, chrome: 0, tip: 0, natural: agents * row, row })
|
||||
|
||||
describe('planNewTabMenuFit', () => {
|
||||
it('shows a short list whole with no peek', () => {
|
||||
expect(listIn(700, 5)).toEqual({ tipHidden: false, mode: 'fit', listMaxHeight: 432 })
|
||||
expect(listIn(200, 5)).toEqual({ tipHidden: false, mode: 'fit', listMaxHeight: 200 })
|
||||
expect(listIn(180, 5)).toEqual({ tipHidden: false, mode: 'fit', listMaxHeight: 180 })
|
||||
})
|
||||
|
||||
it('peeks half of the last visible row once the list overflows', () => {
|
||||
expect(listIn(700, 16)).toEqual({ tipHidden: false, mode: 'peek', listMaxHeight: 414 })
|
||||
expect(listIn(400, 16)).toEqual({ tipHidden: false, mode: 'peek', listMaxHeight: 378 })
|
||||
expect(listIn(300, 10)).toEqual({ tipHidden: false, mode: 'peek', listMaxHeight: 270 })
|
||||
expect(listIn(250, 10)).toEqual({ tipHidden: false, mode: 'peek', listMaxHeight: 198 })
|
||||
})
|
||||
|
||||
it('caps the list at 12 rows even with more room', () => {
|
||||
expect(listIn(2000, 12)).toEqual({ tipHidden: false, mode: 'fit', listMaxHeight: 432 })
|
||||
expect(listIn(2000, 13)).toEqual({ tipHidden: false, mode: 'peek', listMaxHeight: 414 })
|
||||
})
|
||||
|
||||
it('keeps a 2.5-row peek down to the last budget that fits it, then the menu scrolls', () => {
|
||||
expect(listIn(144, 10).listMaxHeight).toBe(126)
|
||||
expect(listIn(143, 10).listMaxHeight).toBe(90)
|
||||
expect(listIn(126, 10).listMaxHeight).toBe(90)
|
||||
expect(listIn(108, 10).listMaxHeight).toBe(90)
|
||||
expect(listIn(107, 10).listMaxHeight).toBe(90)
|
||||
expect(listIn(90, 10)).toEqual({ tipHidden: false, mode: 'peek', listMaxHeight: 90 })
|
||||
expect(listIn(89, 10)).toEqual({ tipHidden: true, mode: 'menu', listMaxHeight: null })
|
||||
})
|
||||
|
||||
it('scales the peek floor with the row height', () => {
|
||||
expect(listIn(88, 10, 35)).toEqual({ tipHidden: false, mode: 'peek', listMaxHeight: 87.5 })
|
||||
expect(listIn(87, 10, 35)).toEqual({ tipHidden: true, mode: 'menu', listMaxHeight: null })
|
||||
})
|
||||
|
||||
it('hands scrolling to the menu when there is no room at all', () => {
|
||||
for (const space of [54, 40, 0, -20]) {
|
||||
expect(listIn(space, 3)).toEqual({ tipHidden: true, mode: 'menu', listMaxHeight: null })
|
||||
}
|
||||
})
|
||||
|
||||
it('subtracts the menu chrome from the room', () => {
|
||||
expect(
|
||||
planNewTabMenuFit({ available: 500, chrome: 140, tip: 0, natural: rows(16), row: ROW })
|
||||
).toEqual({ tipHidden: false, mode: 'peek', listMaxHeight: 342 })
|
||||
})
|
||||
|
||||
it('keeps the tip while the list still gets a 2.5-row peek beside it', () => {
|
||||
expect(
|
||||
planNewTabMenuFit({ available: 270, chrome: 120, tip: 60, natural: rows(16), row: ROW })
|
||||
).toEqual({ tipHidden: false, mode: 'peek', listMaxHeight: 90 })
|
||||
})
|
||||
|
||||
it('hides the tip before the list drops under 2.5 rows', () => {
|
||||
expect(
|
||||
planNewTabMenuFit({ available: 269, chrome: 120, tip: 60, natural: rows(16), row: ROW })
|
||||
).toEqual({ tipHidden: true, mode: 'peek', listMaxHeight: 126 })
|
||||
})
|
||||
|
||||
it('hides the tip when that lets the whole list fit', () => {
|
||||
expect(
|
||||
planNewTabMenuFit({ available: 240, chrome: 120, tip: 60, natural: rows(3), row: ROW })
|
||||
).toEqual({ tipHidden: true, mode: 'fit', listMaxHeight: 120 })
|
||||
})
|
||||
|
||||
it('never yields a tip that has no footprint', () => {
|
||||
expect(
|
||||
planNewTabMenuFit({ available: 269, chrome: 120, tip: 0, natural: rows(16), row: ROW })
|
||||
).toEqual({ tipHidden: false, mode: 'peek', listMaxHeight: 126 })
|
||||
})
|
||||
})
|
||||
@@ -0,0 +1,64 @@
|
||||
export type NewTabMenuFitMode = 'fit' | 'peek' | 'menu'
|
||||
|
||||
export type NewTabMenuFitPlan = {
|
||||
tipHidden: boolean
|
||||
/** fit: the list shows whole; peek: the list scrolls with a half row showing; menu: the whole menu scrolls. */
|
||||
mode: NewTabMenuFitMode
|
||||
listMaxHeight: number | null
|
||||
}
|
||||
|
||||
export const NEW_TAB_MENU_LIST_MAX_ROWS = 12
|
||||
const MIN_PEEK_ROWS = 2.5
|
||||
|
||||
/**
|
||||
* Sizes the + menu's agent list to the room Radix gives the menu. Yield order: hide the tip,
|
||||
* shrink the list to a 2.5-row peek, then hand scrolling to the whole menu.
|
||||
*/
|
||||
export function planNewTabMenuFit({
|
||||
available,
|
||||
chrome,
|
||||
tip,
|
||||
natural,
|
||||
row,
|
||||
maxRows = NEW_TAB_MENU_LIST_MAX_ROWS
|
||||
}: {
|
||||
/** Height Radix allows the menu on the side it opened. */
|
||||
available: number
|
||||
/** Everything in the menu but the list and the tip. */
|
||||
chrome: number
|
||||
/** The tip's footprint; 0 when there is no tip to show. */
|
||||
tip: number
|
||||
/** The list's full content height. */
|
||||
natural: number
|
||||
row: number
|
||||
maxRows?: number
|
||||
}): NewTabMenuFitPlan {
|
||||
const minPeek = MIN_PEEK_ROWS * row
|
||||
const cap = (space: number): number => Math.min(space, maxRows * row)
|
||||
const peek = (space: number): number =>
|
||||
Math.max(minPeek, (Math.floor(cap(space) / row) - 0.5) * row)
|
||||
const withTip = available - chrome - tip
|
||||
if (natural <= cap(withTip)) {
|
||||
return { tipHidden: false, mode: 'fit', listMaxHeight: cap(withTip) }
|
||||
}
|
||||
if (cap(withTip) >= minPeek) {
|
||||
return { tipHidden: false, mode: 'peek', listMaxHeight: peek(withTip) }
|
||||
}
|
||||
const space = available - chrome
|
||||
if (natural <= cap(space)) {
|
||||
return { tipHidden: true, mode: 'fit', listMaxHeight: cap(space) }
|
||||
}
|
||||
if (cap(space) >= minPeek) {
|
||||
return { tipHidden: true, mode: 'peek', listMaxHeight: peek(space) }
|
||||
}
|
||||
return { tipHidden: true, mode: 'menu', listMaxHeight: null }
|
||||
}
|
||||
|
||||
export function sameNewTabMenuFitPlan(a: NewTabMenuFitPlan, b: NewTabMenuFitPlan): boolean {
|
||||
return a.tipHidden === b.tipHidden && a.mode === b.mode && a.listMaxHeight === b.listMaxHeight
|
||||
}
|
||||
|
||||
/** Bottom scroll padding so keyboard focus never rests under the 16px fade. */
|
||||
export function fadeScrollPadding(row: number): number {
|
||||
return 16 + row / 2
|
||||
}
|
||||
@@ -0,0 +1,13 @@
|
||||
import { createContext, useContext } from 'react'
|
||||
|
||||
export type NewTabMenuTipRoom = {
|
||||
/** Gave its room to the agent list; stays hidden until the menu next opens. */
|
||||
tipHidden: boolean
|
||||
registerTip: (node: HTMLElement | null) => void
|
||||
}
|
||||
|
||||
export const NewTabMenuTipRoomContext = createContext<NewTabMenuTipRoom | null>(null)
|
||||
|
||||
export function useNewTabMenuTipRoom(): NewTabMenuTipRoom | null {
|
||||
return useContext(NewTabMenuTipRoomContext)
|
||||
}
|
||||
@@ -6,7 +6,6 @@ import type { Translate } from '@/i18n/use-translate'
|
||||
import { Button } from '@/components/ui/button'
|
||||
import {
|
||||
DropdownMenu,
|
||||
DropdownMenuContent,
|
||||
DropdownMenuSeparator,
|
||||
DropdownMenuTrigger
|
||||
} from '@/components/ui/dropdown-menu'
|
||||
@@ -25,6 +24,7 @@ import type { TabBarItemProjection } from '../model/use-tab-bar-item-projection'
|
||||
import type { TabBarItemActions } from '../model/use-tab-bar-item-actions'
|
||||
import { renderTabBarItems } from '../model/tab-bar-item-surface'
|
||||
import { TabBarStaticCreateMenu } from '../create-menu/tab-bar-static-create-menu'
|
||||
import { NewTabMenuContent } from '../menu-fit/NewTabMenuContent'
|
||||
import ClientHostedBrowserTabRows from '../browser-tabs/ClientHostedBrowserTabRows'
|
||||
import type { ClientHostedBrowserRow } from '../../../../../shared/browser/client-hosting/client-hosted-browser-rows'
|
||||
import { advanceAgentSessionsTourPastNewTab } from '@/components/contextual-tours/agent-sessions-tour-new-tab'
|
||||
@@ -228,7 +228,7 @@ export function renderTabBarSurface(
|
||||
<Plus className="w-3.5 h-3.5" />
|
||||
</button>
|
||||
</DropdownMenuTrigger>
|
||||
<DropdownMenuContent
|
||||
<NewTabMenuContent
|
||||
align="start"
|
||||
sideOffset={6}
|
||||
className="w-72 max-w-[calc(100vw-1rem)] rounded-[11px] border-border/80 p-1 shadow-[0_16px_36px_rgba(0,0,0,0.24)]"
|
||||
@@ -298,7 +298,7 @@ export function renderTabBarSurface(
|
||||
/>
|
||||
</>
|
||||
) : null}
|
||||
</DropdownMenuContent>
|
||||
</NewTabMenuContent>
|
||||
</DropdownMenu>
|
||||
</div>
|
||||
)
|
||||
|
||||
Reference in New Issue
Block a user