-
Notifications
You must be signed in to change notification settings - Fork 229
feat(ui): shared UI foundation (utilities, source/research primitives, markdown upgrade) #331
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
Manushpm8
wants to merge
4
commits into
NVIDIA-AI-Blueprints:develop
Choose a base branch
from
Manushpm8:feat/ui-shared-lib-foundations
base: develop
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
230e425
feat(ui): add shared className, humanize, and motion utilities
Manushpm8 3ed93d6
feat(ui): add shared source, research-trace, and markdown primitives
Manushpm8 9914d3c
fix(ui): address shared-primitive review feedback
Manushpm8 28e1fc4
fix(ui): address shared-primitive review comments
Manushpm8 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
35 changes: 35 additions & 0 deletions
35
frontends/ui/src/shared/components/Actions/CopyButton.spec.tsx
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,35 @@ | ||
| // SPDX-FileCopyrightText: Copyright (c) 2025-2026, NVIDIA CORPORATION & AFFILIATES. All rights reserved. | ||
| // SPDX-License-Identifier: Apache-2.0 | ||
|
|
||
| import { fireEvent, render, screen, waitFor } from '@testing-library/react' | ||
| import { beforeEach, describe, expect, test, vi } from 'vitest' | ||
| import { CopyButton } from './CopyButton' | ||
|
|
||
| const writeText = vi.fn().mockResolvedValue(undefined) | ||
|
|
||
| describe('CopyButton', () => { | ||
| beforeEach(() => { | ||
| writeText.mockClear() | ||
| Object.defineProperty(navigator, 'clipboard', { value: { writeText }, configurable: true }) | ||
| }) | ||
|
|
||
| test('copies the provided text and confirms with a checkmark', async () => { | ||
| render(<CopyButton text="There are 11,463 users." />) | ||
| fireEvent.click(screen.getByRole('button', { name: 'Copy' })) | ||
| expect(writeText).toHaveBeenCalledWith('There are 11,463 users.') | ||
| await waitFor(() => expect(screen.getByRole('button', { name: 'Copied' })).toBeInTheDocument()) | ||
| }) | ||
|
|
||
| test('renders a custom label', () => { | ||
| render(<CopyButton text="x" label="Copy answer" />) | ||
| expect(screen.getByRole('button', { name: 'Copy answer' })).toBeInTheDocument() | ||
| }) | ||
|
|
||
| test('does not throw when the clipboard API rejects', async () => { | ||
| writeText.mockRejectedValueOnce(new Error('blocked')) | ||
| render(<CopyButton text="x" />) | ||
| fireEvent.click(screen.getByRole('button', { name: 'Copy' })) | ||
| await waitFor(() => expect(writeText).toHaveBeenCalled()) | ||
| expect(screen.getByRole('button', { name: 'Copy' })).toBeInTheDocument() | ||
| }) | ||
| }) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,55 @@ | ||
| // SPDX-FileCopyrightText: Copyright (c) 2025-2026, NVIDIA CORPORATION & AFFILIATES. All rights reserved. | ||
| // SPDX-License-Identifier: Apache-2.0 | ||
|
|
||
| 'use client' | ||
|
|
||
| import { type ReactNode, useCallback, useEffect, useRef, useState } from 'react' | ||
| import { Button } from '@/adapters/ui' | ||
| import { Check, Copy } from '@/adapters/ui/icons' | ||
|
|
||
| interface CopyButtonProps { | ||
| text: string | ||
| label?: string | ||
| } | ||
|
|
||
| const RESET_MS = 1500 | ||
|
|
||
| /** | ||
| * Tertiary icon button that copies `text` to the clipboard and briefly swaps to a | ||
| * check to confirm. Silently no-ops when the clipboard API is unavailable | ||
| * (e.g. an insecure context) so the answer surface never errors. | ||
| */ | ||
| export function CopyButton({ text, label = 'Copy' }: CopyButtonProps): ReactNode { | ||
| const [copied, setCopied] = useState(false) | ||
| const resetTimer = useRef<ReturnType<typeof setTimeout> | null>(null) | ||
|
|
||
| useEffect( | ||
| () => () => { | ||
| if (resetTimer.current != null) clearTimeout(resetTimer.current) | ||
| }, | ||
| [], | ||
| ) | ||
|
|
||
| const handleCopy = useCallback(async () => { | ||
| try { | ||
| await navigator.clipboard.writeText(text) | ||
| setCopied(true) | ||
| if (resetTimer.current != null) clearTimeout(resetTimer.current) | ||
| resetTimer.current = setTimeout(() => setCopied(false), RESET_MS) | ||
| } catch { | ||
| setCopied(false) | ||
| } | ||
| }, [text]) | ||
|
coderabbitai[bot] marked this conversation as resolved.
|
||
|
|
||
| return ( | ||
| <Button | ||
| kind="tertiary" | ||
| size="tiny" | ||
| onClick={handleCopy} | ||
| aria-label={copied ? 'Copied' : label} | ||
| title={copied ? 'Copied' : label} | ||
| > | ||
| {copied ? <Check className="h-3.5 w-3.5" /> : <Copy className="h-3.5 w-3.5" />} | ||
| </Button> | ||
| ) | ||
| } | ||
53 changes: 53 additions & 0 deletions
53
frontends/ui/src/shared/components/CollapsibleBlock/CollapsibleBlock.spec.tsx
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,53 @@ | ||
| // SPDX-FileCopyrightText: Copyright (c) 2025-2026, NVIDIA CORPORATION & AFFILIATES. All rights reserved. | ||
| // SPDX-License-Identifier: Apache-2.0 | ||
|
|
||
| import { useState } from 'react' | ||
| import { fireEvent, render, screen } from '@/test-utils' | ||
| import { describe, expect, test } from 'vitest' | ||
| import { CollapsibleBlock } from './CollapsibleBlock' | ||
|
|
||
| function Harness({ initialOpen = false }: { initialOpen?: boolean }) { | ||
| const [open, setOpen] = useState(initialOpen) | ||
| return ( | ||
| <CollapsibleBlock title="Trace" open={open} onOpenChange={setOpen}> | ||
| <p>Body content</p> | ||
| </CollapsibleBlock> | ||
| ) | ||
| } | ||
|
|
||
| describe('CollapsibleBlock', () => { | ||
| test('hides the body and reflects a collapsed state when closed', () => { | ||
| render(<Harness />) | ||
| const toggle = screen.getByRole('button', { name: 'Trace' }) | ||
| expect(toggle).toHaveAttribute('aria-expanded', 'false') | ||
| expect(screen.queryByText('Body content')).not.toBeInTheDocument() | ||
| }) | ||
|
|
||
| test('toggles the body open and reveals it via the header button', () => { | ||
| render(<Harness />) | ||
| const toggle = screen.getByRole('button', { name: 'Trace' }) | ||
| fireEvent.click(toggle) | ||
| expect(toggle).toHaveAttribute('aria-expanded', 'true') | ||
| expect(screen.getByText('Body content')).toBeInTheDocument() | ||
| }) | ||
|
|
||
| test('wires aria-controls to the disclosed region id', () => { | ||
| render(<Harness initialOpen />) | ||
| const toggle = screen.getByRole('button', { name: 'Trace' }) | ||
| const regionId = toggle.getAttribute('aria-controls') | ||
| expect(regionId).toBeTruthy() | ||
| const region = document.getElementById(regionId as string) | ||
| expect(region).not.toBeNull() | ||
| expect(region).toHaveTextContent('Body content') | ||
| }) | ||
|
|
||
| test('renders static without a toggle when not collapsible', () => { | ||
| render( | ||
| <CollapsibleBlock title="Static" open collapsible={false}> | ||
| <p>Always visible</p> | ||
| </CollapsibleBlock>, | ||
| ) | ||
| expect(screen.queryByRole('button')).not.toBeInTheDocument() | ||
| expect(screen.getByText('Always visible')).toBeInTheDocument() | ||
| }) | ||
| }) |
96 changes: 96 additions & 0 deletions
96
frontends/ui/src/shared/components/CollapsibleBlock/CollapsibleBlock.tsx
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,96 @@ | ||
| // SPDX-FileCopyrightText: Copyright (c) 2025-2026, NVIDIA CORPORATION & AFFILIATES. All rights reserved. | ||
| // SPDX-License-Identifier: Apache-2.0 | ||
|
|
||
| /** | ||
| * CollapsibleBlock | ||
| * | ||
| * The shared header + body disclosure used by agent surfaces (thinking trace, | ||
| * plan/approval blocks). A header button toggles an AnimatePresence height / | ||
| * opacity body so every block opens and closes with the same motion and reads | ||
| * uniform regardless of which surface renders it. Controlled: the parent owns | ||
| * the `open` state and is notified via `onOpenChange`. | ||
| */ | ||
|
|
||
| 'use client' | ||
|
|
||
| import type { FC, ReactNode } from 'react' | ||
| import { useId } from 'react' | ||
| import { AnimatePresence, motion } from 'motion/react' | ||
| import { Flex, AnimatedChevron } from '@/adapters/ui' | ||
| import { useReducedMotion } from '@/hooks/use-reduced-motion' | ||
|
|
||
| export interface CollapsibleBlockProps { | ||
| /** Leading glyph rendered before the title (status atom, source icon, etc.). */ | ||
| icon?: ReactNode | ||
| /** Primary header label. */ | ||
| title: ReactNode | ||
| /** Trailing header content (counts, durations, the collapse affordance). */ | ||
| meta?: ReactNode | ||
| /** Controlled open state. */ | ||
| open: boolean | ||
| /** Notified when the header toggles (only when `collapsible`). */ | ||
| onOpenChange?: (open: boolean) => void | ||
| /** When false the block renders static (no toggle, body always shown). */ | ||
| collapsible?: boolean | ||
| children: ReactNode | ||
| } | ||
|
|
||
| export const CollapsibleBlock: FC<CollapsibleBlockProps> = ({ | ||
| icon, | ||
| title, | ||
| meta, | ||
| open, | ||
| onOpenChange, | ||
| collapsible = true, | ||
| children, | ||
| }) => { | ||
| const regionId = useId() | ||
| const prefersReducedMotion = useReducedMotion() | ||
| const header = ( | ||
| <> | ||
| <Flex align="center" gap="2" className="min-w-0"> | ||
| {icon != null && <span className="grid h-7 w-7 shrink-0 place-items-center">{icon}</span>} | ||
| {title} | ||
| </Flex> | ||
| {(meta != null || collapsible) && ( | ||
| <Flex align="center" gap="1" className="shrink-0"> | ||
| {meta} | ||
| {collapsible && <AnimatedChevron />} | ||
| </Flex> | ||
| )} | ||
| </> | ||
| ) | ||
|
|
||
| return ( | ||
| <div className="w-full py-1"> | ||
| {collapsible ? ( | ||
| <button | ||
| type="button" | ||
| onClick={() => onOpenChange?.(!open)} | ||
| aria-expanded={open} | ||
| aria-controls={regionId} | ||
| className="group flex w-full items-center justify-between rounded-[var(--radius-card)] py-1.5 text-left transition-colors" | ||
| > | ||
| {header} | ||
| </button> | ||
|
coderabbitai[bot] marked this conversation as resolved.
|
||
| ) : ( | ||
| <div className="flex w-full items-center justify-between py-1.5">{header}</div> | ||
| )} | ||
|
|
||
| <AnimatePresence initial={false}> | ||
| {(open || !collapsible) && ( | ||
| <motion.div | ||
| id={regionId} | ||
| initial={{ height: 0, opacity: 0 }} | ||
| animate={{ height: 'auto', opacity: 1 }} | ||
| exit={{ height: 0, opacity: 0 }} | ||
| transition={prefersReducedMotion ? { duration: 0 } : { duration: 0.2, ease: [0.22, 1, 0.36, 1] }} | ||
|
Manushpm8 marked this conversation as resolved.
|
||
| className="overflow-hidden" | ||
| > | ||
| {children} | ||
| </motion.div> | ||
| )} | ||
| </AnimatePresence> | ||
|
coderabbitai[bot] marked this conversation as resolved.
|
||
| </div> | ||
| ) | ||
| } | ||
|
Manushpm8 marked this conversation as resolved.
|
||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.