Skip to content

Commit f4ddbd3

Browse files
author
Rúben Carvalho
authored
feat: Add 'rel' attribute to Button (#757)
1 parent e66b674 commit f4ddbd3

File tree

5 files changed

+22
-1
lines changed

5 files changed

+22
-1
lines changed

src/__tests__/__snapshots__/documenter.test.ts.snap

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2757,6 +2757,13 @@ It prevents users from clicking the button.",
27572757
"optional": true,
27582758
"type": "string",
27592759
},
2760+
Object {
2761+
"description": "Adds a \`rel\` attribute to the link. By default, the component sets the \`rel\` attribute to \\"noopener noreferrer\\" when \`target\` is \`\\"_blank\\"\`.
2762+
If the \`rel\` property is provided, it overrides the default behavior.",
2763+
"name": "rel",
2764+
"optional": true,
2765+
"type": "string",
2766+
},
27602767
Object {
27612768
"description": "Specifies where to open the linked URL (for example, to open in a new browser window or tab use \`_blank\`).
27622769
This property only applies when an \`href\` is provided.",

src/button/__tests__/button.test.tsx

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -368,6 +368,11 @@ describe('Button Component', () => {
368368
expect(wrapper.getElement()).not.toHaveAttribute('rel');
369369
});
370370

371+
test('does not set the default "rel" attribute for "target=_blank" if a "rel" prop is provided', () => {
372+
const wrapper = renderButton({ href: 'https://amazon.com', target: '_blank', rel: 'nofollow' });
373+
expect(wrapper.getElement()).toHaveAttribute('rel', 'nofollow');
374+
});
375+
371376
test('can add a download attribute if it is a link', () => {
372377
let wrapper = renderButton({ download: 'fileName' });
373378
expect(wrapper.getElement()).not.toHaveAttribute('download');

src/button/index.tsx

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ const Button = React.forwardRef(
2525
wrapText = true,
2626
href,
2727
target,
28+
rel,
2829
download,
2930
formAction = 'submit',
3031
ariaLabel,
@@ -54,6 +55,7 @@ const Button = React.forwardRef(
5455
wrapText={wrapText}
5556
href={href}
5657
target={target}
58+
rel={rel}
5759
download={download}
5860
formAction={formAction}
5961
ariaLabel={ariaLabel}

src/button/interfaces.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,12 @@ export interface ButtonProps extends BaseComponentProps {
8181
*/
8282
target?: string;
8383

84+
/**
85+
* Adds a `rel` attribute to the link. By default, the component sets the `rel` attribute to "noopener noreferrer" when `target` is `"_blank"`.
86+
* If the `rel` property is provided, it overrides the default behavior.
87+
*/
88+
rel?: string;
89+
8490
/**
8591
* Specifies whether the linked URL, when selected, will prompt the user to download instead of navigate.
8692
* You can specify a string value that will be suggested as the name of the downloaded file.

src/button/internal.tsx

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@ export const InternalButton = React.forwardRef(
4040
wrapText = true,
4141
href,
4242
target,
43+
rel,
4344
download,
4445
formAction = 'submit',
4546
ariaLabel,
@@ -125,7 +126,7 @@ export const InternalButton = React.forwardRef(
125126
href={href}
126127
target={target}
127128
// security recommendation: https://developer.mozilla.org/en-US/docs/Web/HTML/Element/a#target
128-
rel={target === '_blank' ? 'noopener noreferrer' : undefined}
129+
rel={rel ?? (target === '_blank' ? 'noopener noreferrer' : undefined)}
129130
tabIndex={isDisabled ? -1 : undefined}
130131
aria-disabled={isDisabled ? true : undefined}
131132
download={download}

0 commit comments

Comments
 (0)