Skip to content

Commit

Permalink
style: bring back tab component order
Browse files Browse the repository at this point in the history
  • Loading branch information
davidlj95 committed Jul 18, 2024
1 parent fa9df72 commit 93ebd6b
Showing 1 changed file with 38 additions and 38 deletions.
76 changes: 38 additions & 38 deletions src/app/header/tabs/tabs.component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,23 +29,30 @@ import { TabComponent } from '../tab/tab.component'
changeDetection: ChangeDetectionStrategy.OnPush,
})
export class TabsComponent implements OnDestroy {
public _intersectionObserver!: IntersectionObserver
protected readonly MaterialSymbol = {
KeyboardDoubleArrowLeft,
KeyboardDoubleArrowRight,
}
protected _prevButtonDisabled = signal(true)
protected _nextButtonDisabled = signal(true)

private _tabs = contentChildren(TabComponent, {
descendants: false,
})

// Pagination
private _tabList = viewChild<ElementRef<HTMLElement>>('tabList')
private _firstTab?: ElementRef<HTMLElement>
private _lastTab?: ElementRef<HTMLElement>
protected _prevButtonDisabled = signal(true)
protected _nextButtonDisabled = signal(true)
public _intersectionObserver!: IntersectionObserver

// Selected management
private _currentTabs: ReadonlyArray<TabComponent> = []
private _indexToSelect?: number
private _selectedIndex?: number
@Input({ transform: numberAttribute }) set selectedIndex(index: number) {
this._indexToSelect = index
}
/// Scroll to selected
private _indexToScrollTo?: number

Expand All @@ -67,41 +74,6 @@ export class TabsComponent implements OnDestroy {
effect(this._onTabsChanged.bind(this))
}

private _selectedIndex?: number

@Input({ transform: numberAttribute }) set selectedIndex(index: number) {
this._indexToSelect = index
}

ngOnDestroy(): void {
this._intersectionObserver?.disconnect()
}

protected _scrollABit(scrollDirection: ScrollDirection) {
const tabListContainer = this._tabList()?.nativeElement
/* istanbul ignore next */
if (!tabListContainer) {
if (isDevMode) {
console.log(
'TabsComponent: Not scrolling. Reason: tab list container element is missing',
)
}
return
}
//👇 Amount to scroll extracted from MatTab
// https://github.com/angular/components/blob/18.0.5/src/material/tabs/paginated-tab-header.ts#L473-L488
const tabListContainerLength = tabListContainer.offsetWidth
const scrollAmount = tabListContainerLength / 3
const newScrollPosition = Math.max(
tabListContainer.scrollLeft + scrollAmount * scrollDirection,
0,
)
tabListContainer.scrollTo({
behavior: 'smooth',
left: newScrollPosition,
})
}

private _updateSelectedIfNeeded(): void {
if (
this._indexToSelect === undefined ||
Expand Down Expand Up @@ -134,6 +106,10 @@ export class TabsComponent implements OnDestroy {
this._indexToScrollTo = undefined
}

ngOnDestroy(): void {
this._intersectionObserver?.disconnect()
}

private _onTabsChanged() {
const tabs = this._tabs()
if (!this._intersectionObserver || tabs.length === 0) {
Expand Down Expand Up @@ -174,6 +150,30 @@ export class TabsComponent implements OnDestroy {
},
)
}
protected _scrollABit(scrollDirection: ScrollDirection) {
const tabListContainer = this._tabList()?.nativeElement
/* istanbul ignore next */
if (!tabListContainer) {
if (isDevMode) {
console.log(
'TabsComponent: Not scrolling. Reason: tab list container element is missing',
)
}
return
}
//👇 Amount to scroll extracted from MatTab
// https://github.com/angular/components/blob/18.0.5/src/material/tabs/paginated-tab-header.ts#L473-L488
const tabListContainerLength = tabListContainer.offsetWidth
const scrollAmount = tabListContainerLength / 3
const newScrollPosition = Math.max(
tabListContainer.scrollLeft + scrollAmount * scrollDirection,
0,
)
tabListContainer.scrollTo({
behavior: 'smooth',
left: newScrollPosition,
})
}
}

const INTERSECTION_THRESHOLD = 0.8
Expand Down

0 comments on commit 93ebd6b

Please sign in to comment.