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

ExternalLink: Convert component to TypeScript #41681

Merged
merged 9 commits into from
Jun 14, 2022
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 packages/components/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
- `FontSizePicker`: updated to satisfy `react/exhuastive-deps` eslint rule ([#41600](https://github.com/WordPress/gutenberg/pull/41600)).
- `Dropdown`: Make sure cleanup (closing the dropdown) only runs when the menu has actually been opened.
- Enhance the TypeScript migration guidelines ([#41669](https://github.com/WordPress/gutenberg/pull/41669)).
- `ExternalLink`: Convert to TypeScript ([#41681](https://github.com/WordPress/gutenberg/pull/41681)).

## 19.12.0 (2022-06-01)

Expand Down
18 changes: 18 additions & 0 deletions packages/components/src/external-link/README.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
# ExternalLink

Link to an external resource.

## Usage

```jsx
Expand All @@ -9,3 +11,19 @@ const MyExternalLink = () => (
<ExternalLink href="https://wordpress.org">WordPress.org</ExternalLink>
);
```

## Props

The component accepts the following props. Any other props will be passed through to the `a`.

### `children`: `ReactNode`

The content to be displayed within the link.

- Required: Yes

### `href`: `string`

The URL of the external resource.

- Required: Yes
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
*/
import classnames from 'classnames';
import { compact, uniq } from 'lodash';
import type { ForwardedRef } from 'react';

/**
* WordPress dependencies
Expand All @@ -16,12 +17,18 @@ import { external } from '@wordpress/icons';
*/
import { VisuallyHidden } from '../visually-hidden';
import { StyledIcon } from './styles/external-link-styles';
import type { ExternalLinkProps } from './types';
import type { WordPressComponentProps } from '../ui/context';

export function ExternalLink(
{ href, children, className, rel = '', ...additionalProps },
ref
function UnforwardedExternalLink(
props: Omit<
WordPressComponentProps< ExternalLinkProps, 'a', false >,
'target'
>,
ref: ForwardedRef< HTMLAnchorElement >
) {
rel = uniq(
const { href, children, className, rel = '', ...additionalProps } = props;
const optimizedRel = uniq(
compact( [ ...rel.split( ' ' ), 'external', 'noreferrer', 'noopener' ] )
).join( ' ' );
const classes = classnames( 'components-external-link', className );
Expand All @@ -32,7 +39,7 @@ export function ExternalLink(
className={ classes }
href={ href }
target="_blank"
rel={ rel }
rel={ optimizedRel }
ref={ ref }
>
{ children }
Expand All @@ -51,4 +58,17 @@ export function ExternalLink(
);
}

export default forwardRef( ExternalLink );
/**
* Link to an external resource.
*
* ```jsx
* import { ExternalLink } from '@wordpress/components';
*
* const MyExternalLink = () => (
* <ExternalLink href="https://wordpress.org">WordPress.org</ExternalLink>
* );
* ```
*/
export const ExternalLink = forwardRef( UnforwardedExternalLink );

export default ExternalLink;
23 changes: 0 additions & 23 deletions packages/components/src/external-link/stories/index.js

This file was deleted.

36 changes: 36 additions & 0 deletions packages/components/src/external-link/stories/index.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
/**
* External dependencies
*/
import type { ComponentMeta, ComponentStory } from '@storybook/react';

/**
* Internal dependencies
*/
import ExternalLink from '..';

const meta: ComponentMeta< typeof ExternalLink > = {
component: ExternalLink,
title: 'Components/ExternalLink',
argTypes: {
children: { control: { type: 'text' } },
},
parameters: {
controls: {
expanded: true,
},
docs: { source: { state: 'open' } },
},
};
export default meta;

const Template: ComponentStory< typeof ExternalLink > = ( { ...args } ) => {
return <ExternalLink { ...args } />;
};

export const Default: ComponentStory< typeof ExternalLink > = Template.bind(
{}
);
Default.args = {
children: 'WordPress',
href: 'https://wordpress.org',
};
15 changes: 15 additions & 0 deletions packages/components/src/external-link/types.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
/**
* External dependencies
*/
import type { ReactNode } from 'react';

export type ExternalLinkProps = {
/**
* The content to be displayed within the link.
*/
children: ReactNode;
/**
* The URL of the external resource.
*/
href: string;
};
1 change: 1 addition & 0 deletions packages/components/tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@
"src/dropdown/**/*",
"src/dropdown-menu/**/*",
"src/elevation/**/*",
"src/external-link/**/*",
"src/flex/**/*",
"src/form-group/**/*",
"src/form-token-field/**/*",
Expand Down