Skip to content

Commit

Permalink
fix(Link): handle inverse variant for standalone links
Browse files Browse the repository at this point in the history
- update documentation to explain cases
- add warning for misaligned prop usages
- update snapshots and tests
  • Loading branch information
booc0mtaco committed Aug 23, 2024
1 parent d6ee15d commit c0566f5
Show file tree
Hide file tree
Showing 5 changed files with 68 additions and 8 deletions.
4 changes: 4 additions & 0 deletions src/components/Link/Link.module.css
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,10 @@
text-decoration: none;
}

&.link--variant-inverse {
color: var(--eds-theme-color-text-utility-inverse);
}

/**
* Sizes - using the presets for type ramp matching body-*
*/
Expand Down
25 changes: 20 additions & 5 deletions src/components/Link/Link.stories.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ type Story = StoryObj<typeof Link>;
export const Default: Story = {};

/**
* When using context, you can specify a trailing icon for the link.
* When using standalone context, you can specify a trailing icon for the link.
*
* **NOTE**: support for applying the chevron only works when `emphasis` is set to "low".
*/
Expand All @@ -40,7 +40,7 @@ export const LinkWithChevron: Story = {
};

/**
* When using context, you can specify a trailing icon for the link. When using the open icon, make sure that a new tab/window is opened.
* When using standalone context, you can specify a trailing icon for the link. When using the open icon, make sure that a new tab/window is opened.
*/
export const LinkWithOpenIcon: Story = {
args: {
Expand All @@ -51,7 +51,7 @@ export const LinkWithOpenIcon: Story = {
};

/**
* You can add formatting to the link if needed.
* You can add formatting to a link if needed.
*/
export const LinkWithFormattedChildren: Story = {
args: {
Expand All @@ -75,9 +75,11 @@ export const Emphasis: Story = {
<Link {...args} emphasis="default">
Default Emphasis
</Link>
<br />
<Link {...args} emphasis="high">
High Emphasis
</Link>
<br />
<Link {...args} emphasis="low">
Low Emphasis
</Link>
Expand All @@ -87,7 +89,20 @@ export const Emphasis: Story = {
};

/**
* We also include an inverse variant, for use on dark backgrounds.
* Standalone inverse links take on a specific inverse color.
*/
export const StandaloneInverseVariant: Story = {
args: {
context: 'standalone',
variant: 'inverse',
},
parameters: {
backgrounds: { default: 'background-utility-default-high-emphasis' },
},
};

/**
* Inline always take on the color of the container. When using an inline link, wrap the text with a proper color text token.
*/
export const InverseVariant: Story = {
args: {
Expand Down Expand Up @@ -129,7 +144,7 @@ export const LinkInParagraphContext: StoryObj<ExtendArgs> = {
};

/**
* Links will inherit the color from the surrounding text color definition (default emphasis, inline links)
* Inline links will inherit the color from the surrounding text color definition (default emphasis)
*/
export const InheritColor: Story = {
args: {
Expand Down
12 changes: 12 additions & 0 deletions src/components/Link/Link.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -121,5 +121,17 @@ describe('<Link />', () => {

expect(consoleMock).toHaveBeenCalledTimes(1);
});

it('warns when variant is used with context inline', () => {
const consoleMock = jest.spyOn(console, 'warn');
consoleMock.mockImplementation();
render(
<Link context="inline" variant="inverse">
Click
</Link>,
);

expect(consoleMock).toHaveBeenCalledTimes(1);
});
});
});
20 changes: 17 additions & 3 deletions src/components/Link/Link.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,9 @@ export type LinkProps<ExtendedElement = unknown> =
*
* **Default is `"inline"`**.
*
* Note: Icons will only be visible when `"standalone"` is used
* ----
*
* **Note**: This will only apply when `"standalone"` is used
*/
context?: 'inline' | 'standalone';
/**
Expand All @@ -42,13 +44,19 @@ export type LinkProps<ExtendedElement = unknown> =
/**
* The size of the link (when its context is "standalone").
*
* **NOTE**: when `context` is `"inline"`, size is ignored (and inherits from the associated text container)
* ----
*
* **Note**: This will only apply when `"standalone"` is used
*/
size?: Extract<Size, 'xs' | 'sm' | 'md' | 'lg' | 'xl'>;
/**
* The variant treatment for links (use "inverse" on dark backgrounds).
* The variant treatment for **standalone** links (use "inverse" on dark backgrounds).
*
* **Default is `"default"`**.
*
* ----
*
* **Note**: This will only apply when `"standalone"` is used
*/
variant?: 'default' | 'inverse';
} & ExtendedElement;
Expand All @@ -58,6 +66,7 @@ export type LinkProps<ExtendedElement = unknown> =
*
* Component for making styled anchor tags. Links allow users to navigate within or between a web page(s) or app(s).
*
* Inline links inherit the color of the surrounding container, while when using `context` set to `standalone`, links have specific colors.
*/
export const Link = forwardRef<HTMLAnchorElement, LinkProps>(
(
Expand Down Expand Up @@ -96,6 +105,11 @@ export const Link = forwardRef<HTMLAnchorElement, LinkProps>(
'Inline links cannot show icons',
);

assertEdsUsage(
[context === 'inline' && variant === 'inverse'],
'Variant can only be used when context is "standalone"',
);

assertEdsUsage(
[context === 'inline' && typeof size !== 'undefined'],
'Size can only be used when context is "standalone"',
Expand Down
15 changes: 15 additions & 0 deletions src/components/Link/__snapshots__/Link.test.tsx.snap
Original file line number Diff line number Diff line change
Expand Up @@ -24,12 +24,14 @@ exports[`<Link /> Emphasis story renders snapshot 1`] = `
>
Default Emphasis
</a>
<br />
<a
class="link link--context-standalone link--emphasis-high link--size-md"
href="http://example.org/"
>
High Emphasis
</a>
<br />
<a
class="link link--context-standalone link--emphasis-low link--size-md"
href="http://example.org/"
Expand Down Expand Up @@ -216,6 +218,19 @@ exports[`<Link /> LinkWithOpenIcon story renders snapshot 1`] = `
</div>
`;

exports[`<Link /> StandaloneInverseVariant story renders snapshot 1`] = `
<div
class="text-utility-default-primary"
>
<a
class="link link--context-standalone link--emphasis-default link--size-lg link--variant-inverse"
href="http://example.org/"
>
Link
</a>
</div>
`;

exports[`<Link /> UsingExtendedLink story renders snapshot 1`] = `
<div
class="text-utility-default-primary"
Expand Down

0 comments on commit c0566f5

Please sign in to comment.