Skip to content

Commit 056e4fe

Browse files
committed
feat(card-navigation-title): add better handling of external links
1 parent b94d53d commit 056e4fe

File tree

6 files changed

+24
-12
lines changed

6 files changed

+24
-12
lines changed

projects/canopy-test-app/src/app/app.component.html

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -93,7 +93,7 @@
9393
<aside lg-card-group lgMarginBottom="xl">
9494
<lg-card>
9595
<lg-card-header>
96-
<lg-card-navigation-title title="The title" link="https://www.landg.com" [headingLevel]="2"></lg-card-navigation-title>
96+
<lg-card-navigation-title title="The title" link="/foo" [headingLevel]="2"></lg-card-navigation-title>
9797
</lg-card-header>
9898
<lg-card-content>
9999
Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.
@@ -115,7 +115,7 @@
115115
</lg-card>
116116
<lg-card>
117117
<lg-card-header>
118-
<lg-card-navigation-title title="The title" link="https://www.landg.com" headingLevel="2"></lg-card-navigation-title>
118+
<lg-card-navigation-title title="The title" link="/boo" headingLevel="2"></lg-card-navigation-title>
119119
</lg-card-header>
120120
<lg-card-content>
121121
Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur.
@@ -131,7 +131,7 @@
131131
</lg-card>
132132
<lg-card>
133133
<lg-card-header>
134-
<lg-card-navigation-title title="The title" link="https://www.landg.com" headingLevel="2"></lg-card-navigation-title>
134+
<lg-card-navigation-title title="The title" link="/test" headingLevel="2"></lg-card-navigation-title>
135135
</lg-card-header>
136136
<lg-card-content>
137137
Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.
Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
<lg-heading [level]="headingLevel" *ngIf="link && title && headingLevel">
2-
<a *ngIf="externalLink" [href]="link" (click)="linkClicked()">
2+
<a *ngIf="externalLink" [href]="link" (click)="linkClicked()" target="_blank">
33
<ng-container [ngTemplateOutlet]="linkContent"></ng-container>
44
</a>
55
<a *ngIf="!externalLink" [routerLink]="[link]" [queryParams]="queryParams" [queryParamsHandling]="queryParamsHandling" (click)="linkClicked()">
@@ -9,5 +9,5 @@
99

1010
<ng-template #linkContent>
1111
{{title}}
12-
<lg-icon name="arrow-right"></lg-icon>
12+
<lg-icon [name]="externalLink ? 'link-external' : 'arrow-right'"></lg-icon>
1313
</ng-template>

projects/canopy/src/lib/card/card-navigation-title/card-navigation-title.component.scss

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,10 @@
2020
background: var(--link-color);
2121
border-radius: 50%;
2222
color: var(--color-white);
23+
24+
> svg {
25+
padding: 0.125rem;
26+
}
2327
}
2428

2529
a {

projects/canopy/src/lib/card/card-navigation-title/card-navigation-title.component.spec.ts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,11 @@ describe('LgCardNavigationTitleComponent', () => {
8181
});
8282

8383
it('should know whether the link is external or internal', () => {
84+
expect(component.link).toBe('http://www.landg.com');
8485
expect(component['externalLink']).toBeTrue();
86+
let anchorEL = fixture.debugElement.query(By.css('a'));
87+
88+
expect(anchorEL.nativeElement.getAttribute('target')).toBe('_blank');
8589

8690
fixture = MockRender(
8791
`
@@ -99,6 +103,10 @@ describe('LgCardNavigationTitleComponent', () => {
99103
component = debugElement.children[0].componentInstance;
100104

101105
expect(component['externalLink']).toBeFalse();
106+
expect(component.link).toBe('/test-path');
107+
anchorEL = fixture.debugElement.query(By.css('a'));
108+
109+
expect(anchorEL.nativeElement.getAttribute('target')).toBeNull();
102110
});
103111
});
104112

projects/canopy/src/lib/card/card-navigation-title/card-navigation-title.component.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,18 +3,19 @@ import {
33
Component,
44
EventEmitter,
55
Input,
6+
OnChanges,
67
OnInit,
78
Output,
89
SimpleChanges,
9-
ViewEncapsulation, OnChanges,
10+
ViewEncapsulation,
1011
} from '@angular/core';
1112
import { Params, QueryParamsHandling, RouterLink } from '@angular/router';
1213
import { NgIf, NgTemplateOutlet } from '@angular/common';
1314

1415
import type { HeadingLevel } from '../../heading';
1516
import { LgHeadingComponent } from '../../heading';
16-
import isExternalURL from '../../utils/external-links';
1717
import { LgIconComponent } from '../../icon';
18+
import isExternalURL from '../../utils/external-links';
1819

1920
@Component({
2021
selector: 'lg-card-navigation-title',
@@ -38,14 +39,13 @@ export class LgCardNavigationTitleComponent implements OnInit, OnChanges {
3839

3940
ngOnInit(): void {
4041
if (!(this.headingLevel && this.title && this.link)) {
41-
4242
console.error('headingLevel, title and link must be set');
4343
}
4444
}
4545

4646
ngOnChanges({ link }: SimpleChanges): void {
4747
if (link?.currentValue) {
48-
this.externalLink = isExternalURL(link?.currentValue);
48+
this.externalLink = isExternalURL(link?.currentValue as string);
4949
}
5050
}
5151

projects/canopy/src/lib/card/docs/card/card.stories.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -318,7 +318,7 @@ const cardGroupTemplate = `
318318
<aside lg-card-group>
319319
<lg-card>
320320
<lg-card-header>
321-
<lg-card-navigation-title title="The title" link="https://www.landg.com"
321+
<lg-card-navigation-title title="Internal link title" link="/foo"
322322
[headingLevel]="2"></lg-card-navigation-title>
323323
</lg-card-header>
324324
<lg-card-content>
@@ -332,7 +332,7 @@ const cardGroupTemplate = `
332332
</lg-card>
333333
<lg-card *ngFor="let i of [].constructor(additionalCards)">
334334
<lg-card-header>
335-
<lg-card-navigation-title title="The title" link="https://www.landg.com"
335+
<lg-card-navigation-title title="External link title" link="https://www.landg.com"
336336
headingLevel="2"></lg-card-navigation-title>
337337
</lg-card-header>
338338
<lg-card-content>
@@ -453,7 +453,7 @@ export const DefaultCard = {
453453
export const NavigationCard = {
454454
name: 'Card navigation',
455455
args: {
456-
link: 'https://www.landg.com',
456+
link: '/foo',
457457
queryParams: null,
458458
queryParamsHandling: null,
459459
headingLevel: 2,

0 commit comments

Comments
 (0)