Skip to content

Commit d230ac7

Browse files
authored
Merge pull request #524 from appuniversum/new-tab-argument
Add a `@newTab` argument to the `AuLinkExternal` component
2 parents f59b7db + f7ef396 commit d230ac7

File tree

3 files changed

+57
-2
lines changed

3 files changed

+57
-2
lines changed

addon/components/au-link-external.gts

+7-2
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ export interface AuLinkExternalSignature {
1414
hideText?: boolean;
1515
icon?: AuIconSignature['Args']['icon'];
1616
iconAlignment?: 'left' | 'right';
17+
newTab?: boolean;
1718
skin?:
1819
| 'primary'
1920
| 'secondary'
@@ -61,12 +62,16 @@ export default class AuLinkExternal extends Component<AuLinkExternalSignature> {
6162
return '';
6263
}
6364

65+
get newTab() {
66+
return typeof this.args.newTab === 'boolean' ? this.args.newTab : true;
67+
}
68+
6469
// We don't want whitespace between our component and the outer template tag since that's visible in the app (inline element): https://github.com/emberjs/rfcs/issues/982
6570
// prettier-ignore
6671
<template><a
6772
class="{{this.skinClass}} {{this.widthClass}} {{this.iconOnlyClass}}"
68-
target="_blank"
69-
rel="noopener noreferrer"
73+
target={{if this.newTab "_blank"}}
74+
rel={{if this.newTab "noopener noreferrer"}}
7075
href=""
7176
...attributes
7277
>

stories/5-components/Links/AuLinkExternal.stories.js

+6
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,11 @@ export default {
2828
control: 'boolean',
2929
description: 'Hides the link text visually',
3030
},
31+
newTab: {
32+
control: 'boolean',
33+
description:
34+
'Should clicking the link open a new tab. Defaults to `true`.',
35+
},
3136
width: {
3237
control: 'select',
3338
options: ['', 'block'],
@@ -47,6 +52,7 @@ const Template = (args) => ({
4752
@icon={{this.icon}}
4853
@iconAlignment={{this.iconAlignment}}
4954
@hideText={{this.hideText}}
55+
@newTab={{this.newTab}}
5056
@width={{this.width}}
5157
>
5258
{{this.text}}

tests/integration/components/au-link-external-test.gts

+44
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@ import { module, test } from 'qunit';
22
import { setupRenderingTest } from 'ember-qunit';
33
import { render } from '@ember/test-helpers';
44
import AuLinkExternal from '@appuniversum/ember-appuniversum/components/au-link-external';
5+
import { tracked } from '@glimmer/tracking';
6+
import { settled } from '@ember/test-helpers';
57

68
module('Integration | Component | au-link-external', function (hooks) {
79
setupRenderingTest(hooks);
@@ -17,4 +19,46 @@ module('Integration | Component | au-link-external', function (hooks) {
1719

1820
assert.dom('[data-test-link-external]').hasText('template block text');
1921
});
22+
23+
test('it accepts a `@newTab` argument', async function (assert) {
24+
await render(
25+
<template>
26+
<AuLinkExternal data-test-link-external>
27+
template block text
28+
</AuLinkExternal>
29+
</template>,
30+
);
31+
32+
assert
33+
.dom('[data-test-link-external]')
34+
.hasAttribute('target')
35+
.hasAttribute('rel');
36+
37+
const state = new TestState();
38+
39+
await render(
40+
<template>
41+
<AuLinkExternal @newTab={{state.newTab}} data-test-link-external>
42+
template block text
43+
</AuLinkExternal>
44+
</template>,
45+
);
46+
47+
assert
48+
.dom('[data-test-link-external]')
49+
.hasNoAttribute('target')
50+
.hasNoAttribute('rel');
51+
52+
state.newTab = true;
53+
await settled();
54+
55+
assert
56+
.dom('[data-test-link-external]')
57+
.hasAttribute('target')
58+
.hasAttribute('rel');
59+
});
2060
});
61+
62+
class TestState {
63+
@tracked newTab = false;
64+
}

0 commit comments

Comments
 (0)