-
Notifications
You must be signed in to change notification settings - Fork 75
feat: add Link component to next #4601
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
12 commits
Select commit
Hold shift + click to select a range
d1c22f3
feat: add Link component to next
pomfrida f9f5a3b
fix: add focus-visible color, remove dead code, add icon variant warning
pomfrida 753de49
refactor: remove startIcon/endIcon props in favor of composition
pomfrida f34f84f
fix: add text color token to storybook dark mode wrapper
pomfrida 5dbbbc5
fix: add data-font-size to standalone Link for gap token
pomfrida ce247af
chore: convert stories to CSF3 and add data-font-size tests
pomfrida 181e682
fix: use .eds-icon selector instead of > svg in Link
pomfrida de72c03
fix: use stroke token instead of hardcoded 1px in Link CSS
pomfrida 7d88090
fix: let Link icons inherit link color instead of overriding to text-…
pomfrida 00887a3
fix: address review feedback on Link component
pomfrida 52949b5
Revert "fix(eds-core-react): add eds-text-field class to TextField ro…
pomfrida f910a73
fix(eds-core-react): use spacing tokens instead of stroke tokens in L…
pomfrida 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
Some comments aren't visible on the classic Files Changed page.
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
20 changes: 20 additions & 0 deletions
20
packages/eds-core-react/src/components/next/Link/Link.figma.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,20 @@ | ||
| import figma from '@figma/code-connect' | ||
| import { Link } from '.' | ||
|
|
||
| figma.connect( | ||
| Link, | ||
| 'https://www.figma.com/design/dz0XQdc5j7AAtjXr1gTfVR?node-id=2010:2899', | ||
| { | ||
| props: { | ||
| variant: figma.enum('.Type', { | ||
| Inline: 'inline', | ||
| Standalone: 'standalone', | ||
| }), | ||
| }, | ||
| example: ({ variant }) => ( | ||
| <Link href="#" variant={variant}> | ||
| Link | ||
| </Link> | ||
| ), | ||
| }, | ||
| ) |
92 changes: 92 additions & 0 deletions
92
packages/eds-core-react/src/components/next/Link/Link.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,92 @@ | ||
| import type { Meta, StoryObj } from '@storybook/react-vite' | ||
| import { Link } from '.' | ||
| import { Icon } from '../Icon' | ||
| import { external_link, arrow_forward } from '@equinor/eds-icons' | ||
|
|
||
| const meta: Meta<typeof Link> = { | ||
| title: 'EDS 2.0 (beta)/Link', | ||
| component: Link, | ||
| tags: ['beta'], | ||
| parameters: { | ||
| docs: { | ||
| description: { | ||
| component: ` | ||
| ⚠️ **Beta Component** - This component is under active development. | ||
|
|
||
| \`\`\`tsx | ||
| import { Link } from '@equinor/eds-core-react/next' | ||
| \`\`\` | ||
| `, | ||
| }, | ||
| }, | ||
| }, | ||
| } | ||
|
|
||
| export default meta | ||
|
|
||
| type Story = StoryObj<typeof Link> | ||
|
|
||
| export const Introduction: Story = { | ||
| args: { | ||
| href: '#', | ||
| children: 'Link', | ||
| }, | ||
| } | ||
|
|
||
| export const Inline: Story = { | ||
| render: () => ( | ||
| <p> | ||
| This is a paragraph with an <Link href="#">inline link</Link> embedded in | ||
| text. | ||
| </p> | ||
| ), | ||
| } | ||
|
|
||
| export const Standalone: Story = { | ||
| render: () => ( | ||
| <Link href="#" variant="standalone"> | ||
| Standalone link | ||
| </Link> | ||
| ), | ||
| } | ||
|
|
||
| export const WithIcons: Story = { | ||
| render: () => ( | ||
| <div | ||
| style={{ | ||
| display: 'flex', | ||
| flexDirection: 'column', | ||
| alignItems: 'flex-start', | ||
| gap: '16px', | ||
| }} | ||
| > | ||
| <Link href="#" variant="standalone"> | ||
| <Icon data={arrow_forward} /> | ||
| Link with start icon | ||
| </Link> | ||
| <Link href="#" variant="standalone"> | ||
| Link with end icon | ||
| <Icon data={external_link} /> | ||
| </Link> | ||
| <Link href="#" variant="standalone"> | ||
| <Icon data={arrow_forward} /> | ||
| Link with both icons | ||
| <Icon data={external_link} /> | ||
| </Link> | ||
| </div> | ||
| ), | ||
| } | ||
|
|
||
| export const ExternalLink: Story = { | ||
| render: () => ( | ||
| <Link | ||
| href="https://example.com" | ||
| variant="standalone" | ||
| target="_blank" | ||
| rel="noreferrer" | ||
| > | ||
| External link | ||
| <Icon data={external_link} /> | ||
| </Link> | ||
| ), | ||
| } |
158 changes: 158 additions & 0 deletions
158
packages/eds-core-react/src/components/next/Link/Link.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,158 @@ | ||
| import { render, screen } from '@testing-library/react' | ||
| import '@testing-library/jest-dom' | ||
| import { axe } from 'jest-axe' | ||
| import { Link } from '.' | ||
|
|
||
| describe('Link (next)', () => { | ||
| describe('Rendering', () => { | ||
| it('renders with default props', () => { | ||
| render(<Link href="#">Click me</Link>) | ||
| expect(screen.getByRole('link')).toBeInTheDocument() | ||
| expect(screen.getByRole('link')).toHaveTextContent('Click me') | ||
| }) | ||
|
|
||
| it('renders inline variant by default', () => { | ||
| render( | ||
| <Link href="#" data-testid="eds-link"> | ||
| Link | ||
| </Link>, | ||
| ) | ||
| expect(screen.getByTestId('eds-link')).toHaveAttribute( | ||
| 'data-variant', | ||
| 'inline', | ||
| ) | ||
| }) | ||
|
|
||
| it('renders standalone variant', () => { | ||
| render( | ||
| <Link href="#" variant="standalone" data-testid="eds-link"> | ||
| Link | ||
| </Link>, | ||
| ) | ||
| expect(screen.getByTestId('eds-link')).toHaveAttribute( | ||
| 'data-variant', | ||
| 'standalone', | ||
| ) | ||
| }) | ||
|
|
||
| it('renders children as composition', () => { | ||
| render( | ||
| <Link href="#" variant="standalone"> | ||
| <span data-testid="icon">icon</span> | ||
| Link text | ||
| </Link>, | ||
| ) | ||
| expect(screen.getByTestId('icon')).toBeInTheDocument() | ||
| expect(screen.getByRole('link')).toHaveTextContent('Link text') | ||
| }) | ||
|
|
||
| it('applies custom className', () => { | ||
| render( | ||
| <Link href="#" data-testid="eds-link" className="custom"> | ||
| Link | ||
| </Link>, | ||
| ) | ||
| expect(screen.getByTestId('eds-link')).toHaveClass('eds-link', 'custom') | ||
| }) | ||
|
|
||
| it('forwards ref', () => { | ||
| const ref = { current: null as HTMLAnchorElement | null } | ||
| render( | ||
| <Link ref={ref} href="#"> | ||
| Link | ||
| </Link>, | ||
| ) | ||
| expect(ref.current).toBeInstanceOf(HTMLAnchorElement) | ||
| }) | ||
|
|
||
| it('spreads additional props', () => { | ||
| render( | ||
| <Link href="#" data-testid="test" data-custom="value"> | ||
| Link | ||
| </Link>, | ||
| ) | ||
| expect(screen.getByTestId('test')).toHaveAttribute('data-custom', 'value') | ||
| }) | ||
|
|
||
| it('sets href attribute', () => { | ||
| render(<Link href="https://example.com">Link</Link>) | ||
| expect(screen.getByRole('link')).toHaveAttribute( | ||
| 'href', | ||
| 'https://example.com', | ||
| ) | ||
| }) | ||
|
|
||
| it('sets data-font-size for standalone variant', () => { | ||
| render( | ||
| <Link href="#" variant="standalone"> | ||
| Link | ||
| </Link>, | ||
| ) | ||
| expect(screen.getByRole('link')).toHaveAttribute('data-font-size', 'md') | ||
| }) | ||
|
|
||
| it('does not set data-font-size for inline variant', () => { | ||
| render(<Link href="#">Link</Link>) | ||
| expect(screen.getByRole('link')).not.toHaveAttribute('data-font-size') | ||
| }) | ||
|
|
||
| it('sets data-font-family for standalone variant', () => { | ||
| render( | ||
| <Link href="#" variant="standalone"> | ||
| Link | ||
| </Link>, | ||
| ) | ||
| expect(screen.getByRole('link')).toHaveAttribute('data-font-family', 'ui') | ||
| }) | ||
|
|
||
| it('does not set data-font-family for inline variant', () => { | ||
| render(<Link href="#">Link</Link>) | ||
| expect(screen.getByRole('link')).not.toHaveAttribute('data-font-family') | ||
| }) | ||
|
|
||
| it('sets data-line-height for standalone variant', () => { | ||
| render( | ||
| <Link href="#" variant="standalone"> | ||
| Link | ||
| </Link>, | ||
| ) | ||
| expect(screen.getByRole('link')).toHaveAttribute( | ||
| 'data-line-height', | ||
| 'squished', | ||
| ) | ||
| }) | ||
|
|
||
| it('does not set data-line-height for inline variant', () => { | ||
| render(<Link href="#">Link</Link>) | ||
| expect(screen.getByRole('link')).not.toHaveAttribute('data-line-height') | ||
| }) | ||
| }) | ||
|
|
||
| describe('Accessibility', () => { | ||
| it('has no accessibility violations (inline)', async () => { | ||
| const { container } = render(<Link href="#">Link</Link>) | ||
| expect(await axe(container)).toHaveNoViolations() | ||
| }) | ||
|
|
||
| it('has no accessibility violations (standalone)', async () => { | ||
| const { container } = render( | ||
| <Link href="#" variant="standalone"> | ||
| Link | ||
| </Link>, | ||
| ) | ||
| expect(await axe(container)).toHaveNoViolations() | ||
| }) | ||
|
|
||
| it('supports aria attributes', () => { | ||
| render( | ||
| <Link href="#" aria-label="Custom label"> | ||
| Link | ||
| </Link>, | ||
| ) | ||
| expect(screen.getByRole('link')).toHaveAttribute( | ||
| 'aria-label', | ||
| 'Custom label', | ||
| ) | ||
| }) | ||
| }) | ||
| }) |
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,25 @@ | ||
| import { forwardRef } from 'react' | ||
| import type { LinkProps } from './Link.types' | ||
|
|
||
| export const Link = forwardRef<HTMLAnchorElement, LinkProps>(function Link( | ||
| { variant = 'inline', className, children, ...rest }, | ||
| ref, | ||
| ) { | ||
| const classes = ['eds-link', className].filter(Boolean).join(' ') | ||
|
|
||
| return ( | ||
| <a | ||
| ref={ref} | ||
| className={classes} | ||
| data-variant={variant} | ||
| data-font-family={variant === 'standalone' ? 'ui' : undefined} | ||
| data-font-size={variant === 'standalone' ? 'md' : undefined} | ||
| data-line-height={variant === 'standalone' ? 'squished' : undefined} | ||
| {...rest} | ||
| > | ||
| {children} | ||
| </a> | ||
| ) | ||
| }) | ||
|
|
||
| Link.displayName = 'Link' |
13 changes: 13 additions & 0 deletions
13
packages/eds-core-react/src/components/next/Link/Link.types.ts
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,13 @@ | ||
| import type { AnchorHTMLAttributes } from 'react' | ||
|
|
||
| export type LinkVariant = 'inline' | 'standalone' | ||
|
|
||
| export type LinkProps = { | ||
| /** Link destination URL */ | ||
| href: string | ||
| /** Visual variant | ||
| * - `inline` (default): used within text, inherits surrounding font size | ||
| * - `standalone`: used on its own with flex layout, compose icons as children | ||
| */ | ||
|
pomfrida marked this conversation as resolved.
|
||
| variant?: LinkVariant | ||
| } & Omit<AnchorHTMLAttributes<HTMLAnchorElement>, 'href'> | ||
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,2 @@ | ||
| export { Link } from './Link' | ||
| export type { LinkProps, LinkVariant } from './Link.types' |
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,56 @@ | ||
| @layer eds-components { | ||
| .eds-link { | ||
| color: var(--eds-color-text-link); | ||
| text-decoration: underline; | ||
| text-decoration-thickness: var(--eds-sizing-stroke-thin); | ||
| text-underline-offset: var(--eds-spacing-vertical-4xs); | ||
|
|
||
| transition: color 150ms ease-in-out; | ||
| } | ||
|
|
||
| .eds-link:hover { | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Should it be a .eds-link:visited styling as well? I cant see it in figma, but its very helpful to see what links youve already visited. |
||
| color: var(--eds-color-text-strong); | ||
| } | ||
|
|
||
| .eds-link:focus-visible { | ||
| border-radius: var(--eds-spacing-border-radius-rounded); | ||
| color: var(--eds-color-text-strong); | ||
| outline: var(--eds-sizing-stroke-thin) solid var(--eds-color-border-focus); | ||
| outline-offset: var(--eds-spacing-vertical-3xs); | ||
| } | ||
|
|
||
| /* Standalone variant: flex layout for icon + text */ | ||
| .eds-link[data-variant='standalone'] { | ||
| position: relative; | ||
|
|
||
| gap: var(--eds-typography-gap-horizontal); | ||
| align-items: center; | ||
|
|
||
| border-bottom: var(--eds-sizing-stroke-thin) solid currentcolor; | ||
|
|
||
| text-decoration: none; | ||
| } | ||
|
|
||
| /* Use pseudo-element for focus ring so it can have border-radius | ||
| while border-bottom stays straight */ | ||
| .eds-link[data-variant='standalone']:focus-visible { | ||
| border-radius: 0; | ||
| outline: none; | ||
| } | ||
|
|
||
| .eds-link[data-variant='standalone']:focus-visible::after { | ||
| pointer-events: none; | ||
| content: ''; | ||
|
|
||
| position: absolute; | ||
| inset: calc(var(--eds-spacing-vertical-3xs) * -1); | ||
|
|
||
| border: var(--eds-sizing-stroke-thin) solid var(--eds-color-border-focus); | ||
| border-radius: var(--eds-spacing-border-radius-rounded); | ||
| } | ||
| } | ||
|
|
||
| /* Outside @layer to override typography.css [data-font-family] { display: block } */ | ||
| .eds-link[data-variant='standalone'] { | ||
| display: inline-flex; | ||
| } | ||
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.
Added this to support dark mode in stories (for text)