Skip to content

Commit

Permalink
feat: add ExternalLink component
Browse files Browse the repository at this point in the history
  • Loading branch information
petermakowski committed Oct 13, 2023
1 parent f2cc5bf commit 9ec5d63
Show file tree
Hide file tree
Showing 9 changed files with 69 additions and 4 deletions.
1 change: 1 addition & 0 deletions .storybook/preview.scss
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
@import "vanilla-framework";
@include vanilla;
5 changes: 3 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 3 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -96,10 +96,11 @@
"peerDependencies": {
"classnames": "^2.3.2",
"@canonical/react-components": "^0.47.0",
"@types/react": "^17.0.2 || ^18.0.0",
"@types/react-dom": "^17.0.2 || ^18.0.0",
"@types/react": "^18.0.0",
"@types/react-dom": "^18.0.0",
"react": "18.2.0",
"react-dom": "18.2.0",
"react-router-dom": "^6.0.0",
"vanilla-framework": "^4.3.0"
}
}
17 changes: 17 additions & 0 deletions src/lib/elements/ExternalLink/ExternalLink.stories.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import type { Meta } from "@storybook/react";

import { ExternalLink } from ".";

const meta: Meta<typeof ExternalLink> = {
title: "Components/ExternalLink",
component: ExternalLink,
tags: ["autodocs"],
};
export default meta;

export const Example = {
args: {
children: "maas.io",
to: "https://maas.io",
},
};
30 changes: 30 additions & 0 deletions src/lib/elements/ExternalLink/ExternalLink.test.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
import { render, screen } from "@testing-library/react";
import { userEvent } from "@testing-library/user-event";
import { vi } from "vitest";

import { ExternalLink } from "./ExternalLink";

test("renders with correct attributes", () => {
render(<ExternalLink to="https://example.com">Example Link</ExternalLink>);

const linkElement = screen.getByText(/example link/i);
expect(linkElement).toBeInTheDocument();
expect(linkElement).toHaveAttribute("rel", "noreferrer noopener");
expect(linkElement).toHaveAttribute("href", "https://example.com");
expect(linkElement).toHaveAttribute("target", "_blank");
});

test("calls onClick handler when pressed", async () => {
const handleClick = vi.fn();

render(
<ExternalLink onClick={handleClick} to="https://example.com">
Example Link
</ExternalLink>,
);

const linkElement = screen.getByText(/example link/i);
await userEvent.click(linkElement);

expect(handleClick).toHaveBeenCalled();
});
10 changes: 10 additions & 0 deletions src/lib/elements/ExternalLink/ExternalLink.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import { Link, LinkProps } from "@canonical/react-components";
import type { LinkProps as RouterLinkProps } from "react-router-dom";

type ExternalLinkProps = Pick<LinkProps, "children"> &
Omit<RouterLinkProps, "children"> & { to: string };
export const ExternalLink = ({ children, to, ...props }: ExternalLinkProps) => (
<Link {...props} href={to} rel="noreferrer noopener" target="_blank">
{children}
</Link>
);
1 change: 1 addition & 0 deletions src/lib/elements/ExternalLink/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export * from "./ExternalLink";
1 change: 1 addition & 0 deletions src/lib/elements/index.ts
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
export * from "./Meter";
export * from "./ExternalLink";
3 changes: 3 additions & 0 deletions vite.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,12 +30,15 @@ export default defineConfig({
"react/jsx-runtime",
"react-dom",
"vanilla-framework",
"react-router-dom",
],
output: {
globals: {
react: "React",
"@canonical/react-components": "@canonical/react-components",
"react/jsx-runtime": "react/jsx-runtime",
"react-dom": "ReactDOM",
"react-router-dom": "react-router-dom",
},
},
},
Expand Down

0 comments on commit 9ec5d63

Please sign in to comment.