-
Notifications
You must be signed in to change notification settings - Fork 116
feat: add shared truncated address component #463
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
Godfrey-Delight
wants to merge
2
commits into
Stellar-Ecosystem:main
Choose a base branch
from
Godfrey-Delight:feat/address-truncation-copy-component
base: main
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 1 commit
Commits
Show all changes
2 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
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,82 @@ | ||
| import React from 'react'; | ||
| import { render, screen, fireEvent, act } from '@testing-library/react'; | ||
| import StellarAddress from '../components/StellarAddress'; | ||
|
|
||
| describe('StellarAddress Component', () => { | ||
| const mockAddress = 'GAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAWFL'; | ||
|
|
||
| beforeEach(() => { | ||
| jest.clearAllMocks(); | ||
| }); | ||
|
|
||
| it('renders truncated address correctly', () => { | ||
| render(<StellarAddress address={mockAddress} />); | ||
|
|
||
| expect(screen.getByText('GAAAAA...AWFL')).toBeInTheDocument(); | ||
| }); | ||
|
|
||
| it('provides the correct explorer link', () => { | ||
| render(<StellarAddress address={mockAddress} />); | ||
|
|
||
| const link = screen.getByRole('link', { name: /view account/i }); | ||
| expect(link).toHaveAttribute('href', `https://stellar.expert/explorer/testnet/account/${mockAddress}`); | ||
| expect(link).toHaveAttribute('target', '_blank'); | ||
| expect(link).toHaveAttribute('rel', 'noopener noreferrer'); | ||
| }); | ||
|
|
||
| it('provides the full address for screen readers and tooltips', () => { | ||
| render(<StellarAddress address={mockAddress} />); | ||
|
|
||
| const srOnlySpan = screen.getByText(`Stellar address: ${mockAddress}`); | ||
| expect(srOnlySpan).toBeInTheDocument(); | ||
| expect(srOnlySpan).toHaveClass('sr-only'); | ||
|
|
||
| expect(screen.getByText(mockAddress)).toBeInTheDocument(); | ||
| }); | ||
|
|
||
| it('supports custom classNames', () => { | ||
| const { container } = render(<StellarAddress address={mockAddress} className="test-custom-class" />); | ||
|
|
||
| expect(container.firstChild).toHaveClass('test-custom-class'); | ||
| }); | ||
|
|
||
| it('handles clipboard copy with visual confirmation and timer reset', async () => { | ||
| jest.useFakeTimers(); | ||
|
|
||
| const writeTextMock = jest.fn().mockResolvedValue(undefined); | ||
| Object.assign(navigator, { | ||
| clipboard: { | ||
| writeText: writeTextMock, | ||
| }, | ||
| }); | ||
|
|
||
| render(<StellarAddress address={mockAddress} />); | ||
|
|
||
| const copyButton = screen.getByRole('button', { name: /copy stellar address/i }); | ||
| expect(screen.queryByText('Copied')).not.toBeInTheDocument(); | ||
|
|
||
| fireEvent.click(copyButton); | ||
|
|
||
| expect(writeTextMock).toHaveBeenCalledWith(mockAddress); | ||
|
|
||
| // Flush clipboard writeText microtasks | ||
| await act(async () => { | ||
| await Promise.resolve(); | ||
| }); | ||
|
|
||
| expect(screen.getByText('Copied')).toBeInTheDocument(); | ||
|
|
||
| act(() => { | ||
| jest.advanceTimersByTime(1500); | ||
| }); | ||
|
|
||
| expect(screen.queryByText('Copied')).not.toBeInTheDocument(); | ||
|
|
||
| jest.useRealTimers(); | ||
| }); | ||
|
|
||
| it('returns null if no address is provided', () => { | ||
| const { container } = render(<StellarAddress address="" />); | ||
| expect(container.firstChild).toBeNull(); | ||
| }); | ||
| }); |
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
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,88 @@ | ||
| 'use client'; | ||
|
|
||
| import { useState } from 'react'; | ||
|
|
||
| interface Props { | ||
| address: string; | ||
| className?: string; | ||
| } | ||
|
|
||
| const EXPLORER_URL = | ||
| process.env.NEXT_PUBLIC_EXPLORER_URL ?? 'https://stellar.expert/explorer/testnet'; | ||
|
|
||
| export default function StellarAddress({ address, className = '' }: Props) { | ||
| const [copied, setCopied] = useState(false); | ||
|
|
||
| const copyToClipboard = async () => { | ||
| if (!address) return; | ||
| try { | ||
| await navigator.clipboard.writeText(address); | ||
| setCopied(true); | ||
| setTimeout(() => setCopied(false), 1500); | ||
| } catch (err) { | ||
| console.error('Failed to copy address:', err); | ||
| } | ||
| }; | ||
|
|
||
| if (!address) return null; | ||
|
|
||
| const truncated = address.length >= 10 | ||
| ? `${address.slice(0, 6)}...${address.slice(-4)}` | ||
| : address; | ||
|
|
||
| return ( | ||
| <div className={`inline-flex items-center gap-1.5 mono ${className}`}> | ||
| {/* Screen reader only full address description */} | ||
| <span className="sr-only">Stellar address: {address}</span> | ||
|
|
||
| {/* Address Link & Tooltip wrapper */} | ||
| <div className="relative group"> | ||
| <a | ||
| href={`${EXPLORER_URL}/account/${address}`} | ||
| target="_blank" | ||
| rel="noopener noreferrer" | ||
| className="text-secondary hover:text-primary transition-colors hover:underline" | ||
| aria-label={`View account ${address} on Stellar Expert`} | ||
| > | ||
| {truncated} | ||
| </a> | ||
|
|
||
| {/* Custom Premium Tooltip */} | ||
| <div className="absolute bottom-full left-1/2 -translate-x-1/2 mb-2 hidden group-hover:block bg-primary text-white text-xs px-2.5 py-1.5 rounded-lg whitespace-nowrap z-50 shadow-lg pointer-events-none transition-opacity duration-200"> | ||
| <span className="font-mono text-[11px] select-all">{address}</span> | ||
|
Godfrey-Delight marked this conversation as resolved.
|
||
| {/* Arrow indicator */} | ||
| <div className="absolute top-full left-1/2 -translate-x-1/2 border-4 border-transparent border-t-primary" /> | ||
| </div> | ||
| </div> | ||
|
|
||
| {/* Copy Action Button */} | ||
| <button | ||
| onClick={copyToClipboard} | ||
| className="p-1 rounded text-secondary hover:text-primary hover:bg-border/30 transition-all focus:outline-none focus:ring-1 focus:ring-primary/20 shrink-0" | ||
| title="Copy full address" | ||
| aria-label={copied ? "Address copied" : "Copy Stellar address"} | ||
| > | ||
| {copied ? ( | ||
| <span className="text-[10px] font-medium text-success bg-success/10 px-1.5 py-0.5 rounded leading-none"> | ||
| Copied | ||
| </span> | ||
| ) : ( | ||
| <svg | ||
| className="w-3.5 h-3.5" | ||
| fill="none" | ||
| stroke="currentColor" | ||
| viewBox="0 0 24 24" | ||
| xmlns="http://www.w3.org/2000/svg" | ||
| > | ||
| <path | ||
| strokeLinecap="round" | ||
| strokeLinejoin="round" | ||
| strokeWidth="2" | ||
| d="M8 5H6a2 2 0 00-2 2v12a2 2 0 002 2h10a2 2 0 002-2v-1M8 5a2 2 0 002 2h2a2 2 0 002-2M8 5a2 2 0 012-2h2a2 2 0 012 2m0 0h2a2 2 0 012 2v3m2 4H10m0 0l3-3m-3 3l3 3" | ||
| /> | ||
| </svg> | ||
| )} | ||
| </button> | ||
| </div> | ||
| ); | ||
| } | ||
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.
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.