feat(desktop): add open-with menu component for links

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
This commit is contained in:
程序员阿江(Relakkes) 2026-05-29 12:42:30 +08:00
parent d9ebf4c2f1
commit 029c2a3f8f
2 changed files with 38 additions and 0 deletions

View File

@ -0,0 +1,17 @@
import '@testing-library/jest-dom'
import { fireEvent, render, screen } from '@testing-library/react'
import { describe, expect, it, vi } from 'vitest'
import { OpenWithMenu } from './OpenWithMenu'
describe('OpenWithMenu', () => {
it('offers in-app and system browser choices', () => {
const onInApp = vi.fn(); const onSystem = vi.fn()
render(<OpenWithMenu url="https://example.com" onOpenInApp={onInApp} onOpenSystem={onSystem} />)
fireEvent.click(screen.getByLabelText('打开方式'))
fireEvent.click(screen.getByText('应用内浏览器'))
expect(onInApp).toHaveBeenCalledWith('https://example.com')
fireEvent.click(screen.getByLabelText('打开方式'))
fireEvent.click(screen.getByText('系统浏览器'))
expect(onSystem).toHaveBeenCalledWith('https://example.com')
})
})

View File

@ -0,0 +1,21 @@
import { useState } from 'react'
import { ChevronDown } from 'lucide-react'
type Props = { url: string; onOpenInApp: (url: string) => void; onOpenSystem: (url: string) => void }
export function OpenWithMenu({ url, onOpenInApp, onOpenSystem }: Props) {
const [open, setOpen] = useState(false)
return (
<span className="relative inline-block">
<button aria-label="打开方式" onClick={() => setOpen((v) => !v)} className="inline-flex items-center gap-0.5 text-xs">
<ChevronDown size={12} />
</button>
{open && (
<div className="absolute z-10 mt-1 rounded-md border border-[var(--color-border)] bg-[var(--color-surface)] py-1 text-xs shadow">
<button className="block w-full px-3 py-1 text-left hover:bg-[var(--color-surface-hover)]" onClick={() => { setOpen(false); onOpenInApp(url) }}></button>
<button className="block w-full px-3 py-1 text-left hover:bg-[var(--color-surface-hover)]" onClick={() => { setOpen(false); onOpenSystem(url) }}></button>
</div>
)}
</span>
)
}