-
Notifications
You must be signed in to change notification settings - Fork 1
feat(sra): TokenChainPill component #299
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
Merged
Merged
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
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
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,8 @@ | ||
| import type { StorybookConfig } from '@storybook/react-vite' | ||
|
|
||
| const config: StorybookConfig = { | ||
| stories: ['../src/**/*.mdx', '../src/**/*.stories.@(js|jsx|mjs|ts|tsx)'], | ||
| addons: ['@storybook/addon-a11y', '@storybook/addon-docs'], | ||
| framework: '@storybook/react-vite', | ||
| } | ||
| export default config |
15 changes: 15 additions & 0 deletions
15
packages/smart-routing-address-react-ui/.storybook/preview.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,15 @@ | ||
| import type { Preview } from '@storybook/react-vite' | ||
| import '../src/styles.css' | ||
|
|
||
| const preview: Preview = { | ||
| parameters: { | ||
| controls: { | ||
| matchers: { | ||
| color: /(background|color)$/i, | ||
| date: /Date$/i, | ||
| }, | ||
| }, | ||
| }, | ||
| } | ||
|
|
||
| export default preview |
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,5 @@ | ||
| export default { | ||
| plugins: { | ||
| '@tailwindcss/postcss': {}, | ||
| }, | ||
| } |
79 changes: 79 additions & 0 deletions
79
...s/smart-routing-address-react-ui/src/components/TokenChainPill/TokenChainPill.stories.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,79 @@ | ||
| import type { Meta, StoryObj } from '@storybook/react-vite' | ||
| import { TokenChainPill } from './index' | ||
|
|
||
| const meta: Meta<typeof TokenChainPill> = { | ||
| title: 'SmartRoutingAddress/TokenChainPill', | ||
| component: TokenChainPill, | ||
| parameters: { layout: 'centered' }, | ||
| decorators: [ | ||
| (Story) => ( | ||
| <div style={{ width: 162 }}> | ||
| <Story /> | ||
| </div> | ||
| ), | ||
| ], | ||
| argTypes: { | ||
| logoBg: { control: 'color' }, | ||
| onClick: { action: 'clicked' }, | ||
| }, | ||
| } | ||
|
|
||
| export default meta | ||
| type Story = StoryObj<typeof meta> | ||
|
|
||
| /** Interactive source-token pill — the default variant shown in Figma. */ | ||
| export const InteractiveToken: Story = { | ||
| args: { | ||
| label: 'USDC', | ||
| logoBg: '#2775CA', | ||
| logoInitial: 'U', | ||
| onClick: () => {}, | ||
| }, | ||
| } | ||
|
|
||
| /** Interactive source-chain pill. */ | ||
| export const InteractiveChain: Story = { | ||
| args: { | ||
| label: 'Base', | ||
| logoBg: '#0052FF', | ||
| logoInitial: 'B', | ||
| onClick: () => {}, | ||
| }, | ||
| } | ||
|
|
||
| /** | ||
| * Display variant — the destination pill from the "Arrives as" card | ||
| * (Figma 17777:81278). Renders on a 5% white surface with no chevron. | ||
| * Achieved by omitting `onClick`; setting `disabled: true` alongside an | ||
| * `onClick` handler produces the same visual. | ||
| */ | ||
| export const Display: Story = { | ||
| args: { | ||
| label: 'Arbitrum One', | ||
| logoBg: '#28A0F0', | ||
| logoInitial: 'A', | ||
| }, | ||
| } | ||
|
|
||
| /** Same display variant, but with `disabled: true` forcing a passed | ||
| * `onClick` handler to be ignored — useful for "temporarily unavailable" | ||
| * states without unmounting/remounting props. */ | ||
| export const DisplayForcedDisabled: Story = { | ||
| args: { | ||
| label: 'Arbitrum One', | ||
| logoBg: '#28A0F0', | ||
| logoInitial: 'A', | ||
| onClick: () => {}, | ||
| disabled: true, | ||
| }, | ||
| } | ||
|
|
||
| /** With an external logo image supplied. */ | ||
| export const WithLogoImage: Story = { | ||
| args: { | ||
| label: 'USDC', | ||
| logoUri: | ||
| 'https://raw.githubusercontent.com/trustwallet/assets/master/blockchains/ethereum/assets/0xA0b86991c6218b36c1d19D4a2e9Eb0cE3606eB48/logo.png', | ||
| onClick: () => {}, | ||
| }, | ||
| } |
67 changes: 67 additions & 0 deletions
67
...ages/smart-routing-address-react-ui/src/components/TokenChainPill/TokenChainPill.test.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,67 @@ | ||
| /** | ||
| * @vitest-environment happy-dom | ||
| */ | ||
| import { cleanup, fireEvent, render, screen } from '@testing-library/react' | ||
| import { afterEach, describe, expect, it, vi } from 'vitest' | ||
| import { TokenChainPill } from './index' | ||
|
|
||
| afterEach(cleanup) | ||
|
|
||
| describe('TokenChainPill', () => { | ||
| it('renders the label', () => { | ||
| render(<TokenChainPill label="USDC" />) | ||
| expect(screen.getByText('USDC')).toBeDefined() | ||
| }) | ||
|
|
||
| it('renders the logo image when logoUri is provided', () => { | ||
| render( | ||
| <TokenChainPill label="USDC" logoUri="https://example.com/usdc.png" />, | ||
| ) | ||
| const img = screen.getByTestId('token-chain-pill-logo') | ||
| expect(img.getAttribute('src')).toBe('https://example.com/usdc.png') | ||
| }) | ||
|
|
||
| it('renders the initial placeholder when logoUri is absent', () => { | ||
| render(<TokenChainPill label="Base" logoInitial="B" />) | ||
| expect(screen.getByText('B')).toBeDefined() | ||
| }) | ||
|
|
||
| it('is not interactive by default (no chevron, no button role)', () => { | ||
| render(<TokenChainPill label="USDC" />) | ||
| expect(screen.queryByTestId('token-chain-pill-chevron')).toBeNull() | ||
| expect(screen.queryByRole('button')).toBeNull() | ||
| }) | ||
|
|
||
| it('becomes an interactive button when onClick is supplied', () => { | ||
| const onClick = vi.fn() | ||
| render(<TokenChainPill label="USDC" onClick={onClick} />) | ||
| const button = screen.getByRole('button') | ||
| expect(button).toBeDefined() | ||
| expect(screen.getByTestId('token-chain-pill-chevron')).toBeDefined() | ||
| fireEvent.click(button) | ||
| expect(onClick).toHaveBeenCalledTimes(1) | ||
| }) | ||
|
|
||
| it('triggers onClick on Enter and Space keys', () => { | ||
| const onClick = vi.fn() | ||
| render(<TokenChainPill label="USDC" onClick={onClick} />) | ||
| const button = screen.getByRole('button') | ||
| fireEvent.keyDown(button, { key: 'Enter' }) | ||
| fireEvent.keyDown(button, { key: ' ' }) | ||
| expect(onClick).toHaveBeenCalledTimes(2) | ||
| }) | ||
|
|
||
| it('ignores unrelated keys', () => { | ||
| const onClick = vi.fn() | ||
| render(<TokenChainPill label="USDC" onClick={onClick} />) | ||
| fireEvent.keyDown(screen.getByRole('button'), { key: 'a' }) | ||
| expect(onClick).not.toHaveBeenCalled() | ||
| }) | ||
|
|
||
| it('renders as non-interactive when disabled, even with onClick', () => { | ||
| const onClick = vi.fn() | ||
| render(<TokenChainPill label="Arbitrum One" onClick={onClick} disabled />) | ||
| expect(screen.queryByRole('button')).toBeNull() | ||
| expect(screen.queryByTestId('token-chain-pill-chevron')).toBeNull() | ||
| }) | ||
| }) |
108 changes: 108 additions & 0 deletions
108
packages/smart-routing-address-react-ui/src/components/TokenChainPill/index.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,108 @@ | ||
| import { cn, Icon, Text, Wrapper } from '@zerodev/react-ui' | ||
| import type { KeyboardEvent } from 'react' | ||
|
|
||
| export interface TokenChainPillProps { | ||
| /** Text label rendered next to the logo (e.g., "USDC", "Base"). */ | ||
| label: string | ||
| /** URL of the logo image; when omitted, a `logoBg` + `logoInitial` placeholder is drawn. */ | ||
| logoUri?: string | ||
| /** Fallback background color when no `logoUri` is supplied. */ | ||
| logoBg?: string | ||
| /** Fallback initial letter shown inside the placeholder circle. */ | ||
| logoInitial?: string | ||
| /** Click handler; when supplied and not `disabled`, the pill becomes a keyboard-accessible button. */ | ||
| onClick?: () => void | ||
| /** When true, renders as a dimmed, non-interactive pill (no chevron). */ | ||
| disabled?: boolean | ||
| className?: string | ||
| } | ||
|
|
||
| export function TokenChainPill({ | ||
| label, | ||
| logoUri, | ||
| logoBg = '#E6EFFB', | ||
| logoInitial, | ||
| onClick, | ||
| disabled, | ||
| className, | ||
| }: TokenChainPillProps) { | ||
| const interactive = Boolean(onClick) && !disabled | ||
|
|
||
| const handleKeyDown = (event: KeyboardEvent<HTMLDivElement>) => { | ||
| if (!interactive) return | ||
| if (event.key === 'Enter' || event.key === ' ') { | ||
| event.preventDefault() | ||
| onClick?.() | ||
| } | ||
| } | ||
|
|
||
| return ( | ||
| <Wrapper | ||
| variant="solid" | ||
| // Override Wrapper's variant-based bg color for the display variant — | ||
| // Figma spec is exactly rgba(255,255,255,0.05), which none of the | ||
| // Wrapper variants match. Inline style beats Wrapper's own style. | ||
| style={ | ||
| interactive | ||
| ? undefined | ||
| : { backgroundColor: 'rgba(255, 255, 255, 0.05)' } | ||
| } | ||
| className={cn( | ||
| // Sizing/padding matches Figma: outer pl-1 pr-2 py-1, rounded-2xl. | ||
| // Height is content-driven — 44px logo + 4px vertical padding = 52px. | ||
| 'zd:relative zd:flex zd:w-full zd:items-center zd:justify-between zd:overflow-hidden zd:rounded-2xl zd:pl-1 zd:pr-2 zd:py-1', | ||
| // Universal inner shadow from the Figma design token. | ||
| 'zd:shadow-[inset_0_-4px_4px_0_rgba(255,255,255,0.1),inset_0_3px_4px_0_rgba(0,0,0,0.02)]', | ||
| interactive && 'zd:cursor-pointer', | ||
| className, | ||
| )} | ||
| {...(interactive && { | ||
| role: 'button', | ||
| tabIndex: 0, | ||
| onClick, | ||
| onKeyDown: handleKeyDown, | ||
| })} | ||
| > | ||
| <div className="zd:flex zd:items-center zd:gap-1.5"> | ||
| {/* 44×44 logo well with a 34×34 centered inner disc. Figma places the | ||
| image with translate-1/2 offsets so the smaller disc sits inside | ||
| the larger well — the 10px reveal reads as the pill's "logo pad". */} | ||
| <div | ||
| className="zd:relative zd:size-11 zd:shrink-0 zd:rounded-xl" | ||
| aria-hidden={!logoUri} | ||
| > | ||
| <div | ||
| className={cn( | ||
| 'zd:absolute zd:top-1/2 zd:left-1/2 zd:-translate-x-1/2 zd:-translate-y-1/2', | ||
| 'zd:flex zd:size-8.5 zd:items-center zd:justify-center zd:overflow-hidden zd:rounded-full', | ||
| )} | ||
| style={logoUri ? undefined : { backgroundColor: logoBg }} | ||
| > | ||
| {logoUri ? ( | ||
| <img | ||
| alt="" | ||
| src={logoUri} | ||
| className="zd:size-full zd:object-cover" | ||
| data-testid="token-chain-pill-logo" | ||
| /> | ||
| ) : logoInitial ? ( | ||
| <span className="zd:text-body2 zd:font-medium zd:text-white"> | ||
| {logoInitial} | ||
| </span> | ||
| ) : null} | ||
| </div> | ||
| </div> | ||
| <Text className="zd:whitespace-nowrap zd:text-body1">{label}</Text> | ||
| </div> | ||
| {interactive && ( | ||
| <div className="zd:flex zd:shrink-0 zd:items-center zd:rounded-full zd:p-2"> | ||
| <Icon | ||
| name="chevronDown" | ||
| className="zd:size-4 zd:text-greyScale" | ||
| data-testid="token-chain-pill-chevron" | ||
| /> | ||
| </div> | ||
| )} | ||
| </Wrapper> | ||
| ) | ||
| } | ||
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
Oops, something went wrong.
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Isn't this basically a
Selectprimitive with an optional image? Based on its looks and function, it seems like it should be part ofreact-ui?Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
If we did that, we could have this component as
Selectas well. It just disabled and has chevron hidden.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
That component is already included
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Currently it is being used in SRA only. If we needed somewhere else, it can then be refactored to
react-ui