feat(ui): preview port menu, terminal auto-start, and project actions tabs

This commit is contained in:
Chris Scott committed 2026-10-04 01:34:57 -04:00
1 parent cf9fa61118
commit 62e36eb3b2
31 files changed
+1332 -489

No files matched your search

@@ -0,0 +1,56 @@
import { describe, expect, it, vi } from 'vitest'
import { render, screen } from '@testing-library/react'
import userEvent from '@testing-library/user-event'
import { Combobox } from './combobox'
const options = [
{ value: 'builtin', label: 'Built-in Browser' },
{ value: 'external', label: 'External API' },
]
function renderCombobox(onOpen = vi.fn()) {
render(
<div>
<Combobox value="builtin" onChange={vi.fn()} options={options} allowCustomValue={false} ariaLabel="Provider" onOpen={onOpen} />
<button type="button">Outside</button>
</div>,
)
return screen.getByRole('combobox', { name: 'Provider' })
}
describe('Combobox', () => {
it('restores the selected label, not the raw value, on Escape', async () => {
const user = userEvent.setup()
const input = renderCombobox()
await user.clear(input)
await user.type(input, 'Ext')
await user.keyboard('{Escape}')
expect(input).toHaveValue('Built-in Browser')
})
it('restores the selected label, not the raw value, on an outside click', async () => {
const user = userEvent.setup()
const input = renderCombobox()
await user.clear(input)
await user.type(input, 'Ext')
await user.click(screen.getByRole('button', { name: 'Outside' }))
expect(input).toHaveValue('Built-in Browser')
})
it('calls onOpen each time the options open', async () => {
const user = userEvent.setup()
const onOpen = vi.fn()
const input = renderCombobox(onOpen)
await user.click(input)
await user.keyboard('{Escape}')
await user.click(screen.getByRole('button', { name: 'Outside' }))
await user.click(input)
expect(onOpen).toHaveBeenCalledTimes(2)
})
})
+33 -11
View File
@@ -18,6 +18,12 @@ interface ComboboxProps {
className?: string
allowCustomValue?: boolean
showClear?: boolean
ariaLabel?: string
onOpen?: () => void
}
function getOptionLabel(options: ComboboxOption[], value: string): string {
return options.find(o => o.value === value)?.label || value
}
export function Combobox({
@@ -29,12 +35,11 @@ export function Combobox({
className,
allowCustomValue = true,
showClear = false,
ariaLabel,
onOpen,
}: ComboboxProps) {
const [isOpen, setIsOpen] = useState(false)
const [inputValue, setInputValue] = useState(() => {
const selectedOption = options.find(o => o.value === value)
return selectedOption?.label || value
})
const [inputValue, setInputValue] = useState(() => getOptionLabel(options, value))
const [selectedIndex, setSelectedIndex] = useState(0)
const [isUserTyping, setIsUserTyping] = useState(false)
const inputRef = useRef<HTMLInputElement>(null)
@@ -43,11 +48,24 @@ export function Combobox({
useEffect(() => {
if (!isUserTyping) {
const selectedOption = options.find(o => o.value === value)
setInputValue(selectedOption?.label || value)
setInputValue(getOptionLabel(options, value))
}
}, [value, options, isUserTyping])
const onOpenRef = useRef(onOpen)
useEffect(() => {
onOpenRef.current = onOpen
}, [onOpen])
useEffect(() => {
if (isOpen) onOpenRef.current?.()
}, [isOpen])
const restoreSelectedLabel = useCallback(() => {
setInputValue(getOptionLabel(options, value))
setIsUserTyping(false)
}, [options, value])
const isExactMatch = options.some(o => o.value === value || o.label === inputValue)
const filteredOptions = (isExactMatch && !isUserTyping) ? options : options.filter(option =>
@@ -77,15 +95,15 @@ export function Combobox({
setIsOpen(false)
if (allowCustomValue) {
onChange(inputValue)
} else if (!options.some(o => o.value === inputValue)) {
setInputValue(value)
} else {
restoreSelectedLabel()
}
}
}
document.addEventListener('mousedown', handleClickOutside)
return () => document.removeEventListener('mousedown', handleClickOutside)
}, [isOpen, inputValue, value, onChange, options, allowCustomValue])
}, [isOpen, inputValue, onChange, allowCustomValue, restoreSelectedLabel])
useEffect(() => {
if (!isOpen || !listRef.current) return
@@ -135,7 +153,7 @@ export function Combobox({
case 'Escape':
e.preventDefault()
setIsOpen(false)
setInputValue(value)
restoreSelectedLabel()
break
case 'Tab':
setIsOpen(false)
@@ -144,7 +162,7 @@ export function Combobox({
}
break
}
}, [isOpen, selectedIndex, flatFilteredOptions, handleSelect, allowCustomValue, inputValue, value, onChange])
}, [isOpen, selectedIndex, flatFilteredOptions, handleSelect, allowCustomValue, inputValue, onChange, restoreSelectedLabel])
const handleInputChange = useCallback((e: React.ChangeEvent<HTMLInputElement>) => {
const newValue = e.target.value
@@ -174,6 +192,10 @@ export function Combobox({
onKeyDown={handleKeyDown}
placeholder={placeholder}
disabled={disabled}
role="combobox"
aria-label={ariaLabel}
aria-autocomplete="list"
aria-expanded={isOpen}
className={cn(
'flex h-9 w-full rounded-md border border-input bg-transparent px-3 py-1 text-[16px] md:text-sm shadow-sm transition-colors',
'file:border-0 file:bg-transparent file:text-sm file:font-medium',
@@ -1,7 +1,9 @@
import { describe, it, expect, vi, beforeEach, afterEach } from "vitest";
import { useState } from "react";
import { render, screen, act, fireEvent, waitFor } from "@testing-library/react";
import userEvent from "@testing-library/user-event";
import { stubMatchMedia } from "@/test/test-utils";
import { Combobox } from "./combobox";
import {
Dialog,
DialogContent,
@@ -33,6 +35,29 @@ describe("DialogContent", () => {
})
})
it("closes an open combobox on Escape before closing the dialog", async () => {
const user = userEvent.setup();
const onOpenChange = vi.fn();
render(
<Dialog open onOpenChange={onOpenChange}>
<DialogContent>
<DialogTitle>Test Dialog</DialogTitle>
<Combobox value="" onChange={vi.fn()} options={[{ value: "a", label: "Alpha" }]} ariaLabel="Choice" />
</DialogContent>
</Dialog>
);
const combobox = screen.getByRole("combobox", { name: "Choice" });
await user.click(combobox);
expect(combobox).toHaveAttribute("aria-expanded", "true");
await user.keyboard("{Escape}");
expect(combobox).toHaveAttribute("aria-expanded", "false");
expect(onOpenChange).not.toHaveBeenCalled();
await user.keyboard("{Escape}");
expect(onOpenChange).toHaveBeenCalledWith(false);
});
it("applies safe-area padding when fullscreen prop is true", () => {
render(
<Dialog open>
+8 -1
View File
@@ -77,7 +77,7 @@ interface DialogContentProps
const DialogContent = React.forwardRef<
React.ElementRef<typeof DialogPrimitive.Content>,
DialogContentProps
>(({ className, children, hideCloseButton, fullscreen, mobileFullscreen, mobileSwipeToClose, keyboardAware, canSwipeBack, onSwipeBack, overlayClassName, style, onCloseAutoFocus, ...props }, ref) => {
>(({ className, children, hideCloseButton, fullscreen, mobileFullscreen, mobileSwipeToClose, keyboardAware, canSwipeBack, onSwipeBack, overlayClassName, style, onCloseAutoFocus, onEscapeKeyDown, ...props }, ref) => {
const isMobileFullscreenMode = fullscreen || mobileFullscreen
const isDialogOpen = React.useContext(DialogOpenContext)
const returnFocus = React.useContext(DialogReturnFocusContext)
@@ -87,6 +87,12 @@ const DialogContent = React.forwardRef<
event.preventDefault()
returnFocus.focus()
}
const handleEscapeKeyDown = (event: KeyboardEvent) => {
onEscapeKeyDown?.(event)
if (event.target instanceof Element && event.target.matches('[role="combobox"][aria-expanded="true"]')) {
event.preventDefault()
}
}
const [isMobile, setIsMobile] = React.useState(() => typeof window !== 'undefined' ? window.innerWidth < 768 : false)
const shouldEnableMobileSwipe = mobileSwipeToClose !== false && isMobile && isDialogOpen
const shouldAnimateSwipe = shouldEnableMobileSwipe && isMobileFullscreenMode
@@ -150,6 +156,7 @@ const DialogContent = React.forwardRef<
style={Object.keys(mergedStyle).length > 0 ? mergedStyle : undefined}
{...props}
onCloseAutoFocus={handleCloseAutoFocus}
onEscapeKeyDown={handleEscapeKeyDown}
>
{children}
{shouldEnableMobileSwipe && (
+2 -13
View File
@@ -1,4 +1,5 @@
import { useMemo } from 'react'
import { shortenPath } from '@/lib/utils'
interface PathDisplayProps {
path: string
@@ -7,19 +8,7 @@ interface PathDisplayProps {
}
export function PathDisplay({ path, maxSegments = 3, className = '' }: PathDisplayProps) {
const displayPath = useMemo(() => {
if (!path || path === '/') return '/'
const normalizedPath = path.startsWith('/') ? path : '/' + path
const segments = normalizedPath.split('/').filter(Boolean)
if (segments.length <= maxSegments) {
return '/' + segments.join('/')
}
const visibleSegments = segments.slice(-maxSegments)
return '/.../' + visibleSegments.join('/')
}, [path, maxSegments])
const displayPath = useMemo(() => shortenPath(path, maxSegments), [path, maxSegments])
return (
<span
+34 -23
View File
@@ -94,6 +94,38 @@ export function SettingsList({
)
}
interface SettingsListRowActionsMenuProps {
actions: SettingsListRowAction[]
label?: string
}
export function SettingsListRowActionsMenu({ actions, label }: SettingsListRowActionsMenuProps) {
return (
<DropdownMenu>
<DropdownMenuTrigger asChild>
<Button type="button" variant="ghost" size="icon" className="h-8 w-8 [[data-opencode-settings]_&]:max-sm:h-11 [[data-opencode-settings]_&]:max-sm:w-11" aria-label={label}>
<MoreHorizontal className="h-4 w-4" />
</Button>
</DropdownMenuTrigger>
<DropdownMenuContent align="end">
{actions.map((action, i) => (
<Fragment key={action.label}>
{action.separatorBefore && i > 0 && <DropdownMenuSeparator />}
<DropdownMenuItem
disabled={action.disabled}
onSelect={() => setTimeout(action.onClick, 0)}
className={action.destructive ? 'text-destructive focus:text-destructive' : undefined}
>
{action.icon}
{action.label}
</DropdownMenuItem>
</Fragment>
))}
</DropdownMenuContent>
</DropdownMenu>
)
}
export function SettingsListRow({
title,
titleClassName,
@@ -136,7 +168,7 @@ export function SettingsListRow({
{belowDescription}
</div>
<div
className="flex w-full shrink-0 items-center justify-end gap-1 sm:w-auto sm:justify-start [[data-opencode-settings]_&]:w-auto"
className="flex shrink-0 items-center justify-end gap-1 sm:justify-start"
onClick={(e) => e.stopPropagation()}
>
{trailing}
@@ -151,28 +183,7 @@ export function SettingsListRow({
</Button>
)}
{overflowActions && overflowActions.length > 0 && (
<DropdownMenu>
<DropdownMenuTrigger asChild>
<Button type="button" variant="ghost" size="icon" className="h-8 w-8 [[data-opencode-settings]_&]:max-sm:h-11 [[data-opencode-settings]_&]:max-sm:w-11" aria-label={actionsLabel}>
<MoreHorizontal className="h-4 w-4" />
</Button>
</DropdownMenuTrigger>
<DropdownMenuContent align="end">
{overflowActions.map((action, i) => (
<Fragment key={action.label}>
{action.separatorBefore && i > 0 && <DropdownMenuSeparator />}
<DropdownMenuItem
disabled={action.disabled}
onSelect={() => setTimeout(action.onClick, 0)}
className={action.destructive ? 'text-destructive focus:text-destructive' : undefined}
>
{action.icon}
{action.label}
</DropdownMenuItem>
</Fragment>
))}
</DropdownMenuContent>
</DropdownMenu>
<SettingsListRowActionsMenu actions={overflowActions} label={actionsLabel} />
)}
</div>
</div>