Skip to content

Commit ea5205b

Browse files
authored
refactor(tab): from active ink to border (#277)
1 parent 6778440 commit ea5205b

File tree

3 files changed

+9
-50
lines changed

3 files changed

+9
-50
lines changed

projects/ngverse/src/lib/tab/tab-group-header.component.ts

+7-5
Original file line numberDiff line numberDiff line change
@@ -26,16 +26,16 @@ import { TabComponent } from './tab.component';
2626
display: flex;
2727
justify-content: center;
2828
cursor: pointer;
29-
position: relative;
30-
outline: none;
31-
background: transparent;
32-
border: 0;
33-
font-size: 1rem;
29+
border-bottom: 2px solid transparent;
30+
&.selected {
31+
border-bottom-color: var(--color-primary);
32+
}
3433
}
3534
`,
3635
changeDetection: ChangeDetectionStrategy.OnPush,
3736
host: {
3837
'[class.is-active]': 'enabledTabIndex()',
38+
'[class.selected]': 'isSelected()',
3939
},
4040
})
4141
export class TabGroupHeaderComponent implements Highlightable {
@@ -47,6 +47,8 @@ export class TabGroupHeaderComponent implements Highlightable {
4747
element = inject<ElementRef<HTMLElement>>(ElementRef<HTMLElement>)
4848
.nativeElement;
4949
enabledTabIndex = signal(false);
50+
isSelected = input.required<boolean>();
51+
5052
setActiveStyles(): void {
5153
this.enabledTabIndex.set(true);
5254
this.element.focus();

projects/ngverse/src/lib/tab/tab-group.component.html

+1-6
Original file line numberDiff line numberDiff line change
@@ -8,16 +8,11 @@
88
@for (tab of tabs(); track $index) {
99
<app-tab-group-header
1010
[tab]="tab"
11+
[isSelected]="$index === selectedIndex()"
1112
#tabHeader
1213
(click)="selectTab($index)"
1314
></app-tab-group-header>
1415
}
15-
<div
16-
#tabInk
17-
class="tab-ink"
18-
[style.width.px]="tabInkWidth()"
19-
[style.left.px]="tabInkLeft()"
20-
></div>
2116
</div>
2217
<!--we need explicitely set ng-content, in order ssr work properly see (https://github.com/angular/angular/issues/50543)-->
2318
<ng-content></ng-content>

projects/ngverse/src/lib/tab/tab-group.component.ts

+1-39
Original file line numberDiff line numberDiff line change
@@ -3,20 +3,16 @@ import { ActiveDescendantKeyManager } from '@angular/cdk/a11y';
33
import { Directionality } from '@angular/cdk/bidi';
44
import { NgTemplateOutlet } from '@angular/common';
55
import {
6-
afterNextRender,
76
ChangeDetectionStrategy,
87
Component,
98
computed,
109
contentChildren,
11-
ElementRef,
1210
inject,
1311
Injector,
1412
input,
1513
model,
16-
OnDestroy,
1714
output,
1815
signal,
19-
viewChild,
2016
viewChildren,
2117
} from '@angular/core';
2218
import { TabGroupHeaderComponent } from './tab-group-header.component';
@@ -37,20 +33,13 @@ import { TabComponent } from './tab.component';
3733
]),
3834
],
3935
})
40-
export class TabGroupComponent implements OnDestroy {
36+
export class TabGroupComponent {
4137
tabs = contentChildren(TabComponent);
4238
selectedIndex = model(0);
4339
bodyGap = input(true);
4440

4541
tabHeaders = viewChildren(TabGroupHeaderComponent);
4642

47-
tabHeadersEl = computed(() =>
48-
this.tabHeaders().map((tabHeader) => tabHeader.element)
49-
);
50-
51-
tabGroupHeader =
52-
viewChild.required<ElementRef<HTMLElement>>('tabGroupHeader');
53-
5443
direction = inject(Directionality);
5544

5645
keyManager = new ActiveDescendantKeyManager(
@@ -74,42 +63,15 @@ export class TabGroupComponent implements OnDestroy {
7463
tabInkWidth = signal(0);
7564
tabInkLeft = signal(0);
7665

77-
resizeObserver: ResizeObserver | undefined;
78-
79-
constructor() {
80-
afterNextRender(() => {
81-
this.resizeObserver = new ResizeObserver(() => this.moveInk());
82-
this.resizeObserver.observe(this.tabGroupHeader().nativeElement);
83-
});
84-
}
85-
8666
onTabGroupFocus() {
8767
if (!this.keyManager.activeItem) {
8868
this.keyManager.setFirstItemActive();
8969
}
9070
}
9171

92-
private moveInk() {
93-
const index = this.selectedIndex();
94-
const tabHeader = this.tabHeadersEl()[index];
95-
const tabGroupHeader = this.tabGroupHeader();
96-
if (tabHeader) {
97-
const rects = tabHeader.getBoundingClientRect();
98-
const tabGroupRects =
99-
tabGroupHeader.nativeElement.getBoundingClientRect();
100-
this.tabInkLeft.set(rects.left - tabGroupRects.left);
101-
this.tabInkWidth.set(rects.width);
102-
}
103-
}
104-
10572
selectTab($event: number) {
10673
this.keyManager.setActiveItem($event);
10774
this.selectedIndex.set($event);
10875
this.tabChanged.emit($event);
109-
this.moveInk();
110-
}
111-
112-
ngOnDestroy(): void {
113-
this.resizeObserver?.disconnect();
11476
}
11577
}

0 commit comments

Comments
 (0)