Skip to content
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

feat: add ExternalLink component #17

Merged
merged 1 commit into from
Oct 16, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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;
1 change: 1 addition & 0 deletions package-lock.json

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

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,7 @@
"@types/react-dom": "^17.0.2 || ^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
Loading