|
6 | 6 | * found in the LICENSE file at https://angular.dev/license |
7 | 7 | */ |
8 | 8 |
|
9 | | -import { |
10 | | - afterNextRender, |
11 | | - afterRenderEffect, |
12 | | - Component, |
13 | | - computed, |
14 | | - DestroyRef, |
15 | | - ElementRef, |
16 | | - input, |
17 | | - output, |
18 | | - signal, |
19 | | - untracked, |
20 | | - viewChild, |
21 | | -} from '@angular/core'; |
22 | | -import {MatIcon} from '@angular/material/icon'; |
| 9 | +import {afterRenderEffect, Component, input, output, untracked, viewChild} from '@angular/core'; |
23 | 10 |
|
24 | 11 | import {FlatNode} from '../component-data-source'; |
25 | | -import {Debouncer} from '../../../../shared/utils/debouncer'; |
26 | | - |
27 | | -const RESIZE_DEBOUNCE = 100; |
| 12 | +import {HorizontalScrollerComponent} from '../../../../shared/horizontal-scroller/horizontal-scroller.component'; |
28 | 13 |
|
29 | 14 | @Component({ |
30 | 15 | selector: 'ng-breadcrumbs', |
31 | 16 | templateUrl: './breadcrumbs.component.html', |
32 | 17 | styleUrls: ['./breadcrumbs.component.scss'], |
33 | | - imports: [MatIcon], |
| 18 | + imports: [HorizontalScrollerComponent], |
34 | 19 | }) |
35 | 20 | export class BreadcrumbsComponent { |
36 | | - readonly parents = input.required<FlatNode[]>(); |
37 | | - readonly handleSelect = output<FlatNode>(); |
38 | | - readonly mouseOverNode = output<FlatNode>(); |
39 | | - readonly mouseLeaveNode = output<FlatNode>(); |
40 | | - |
41 | | - readonly breadcrumbsScrollContent = viewChild.required<ElementRef>('breadcrumbs'); |
42 | | - |
43 | | - readonly showScrollLeftButton = computed(() => { |
44 | | - const value = this.breadcrumbsScrollLayout(); |
45 | | - return value && value.scrollLeft > 0; |
46 | | - }); |
47 | | - |
48 | | - readonly showScrollRightButton = computed(() => { |
49 | | - const value = this.breadcrumbsScrollLayout(); |
50 | | - if (!value) { |
51 | | - return false; |
52 | | - } |
53 | | - const {clientWidth, scrollWidth, scrollLeft} = value; |
54 | | - return scrollWidth > clientWidth && scrollLeft + clientWidth < scrollWidth; |
55 | | - }); |
| 21 | + protected readonly scoller = viewChild<HorizontalScrollerComponent>('scroller'); |
56 | 22 |
|
57 | | - private readonly breadcrumbsScrollLayout = signal< |
58 | | - | { |
59 | | - clientWidth: number; |
60 | | - scrollWidth: number; |
61 | | - scrollLeft: number; |
62 | | - } |
63 | | - | undefined |
64 | | - >(undefined); |
65 | | - |
66 | | - constructor(destroyRef: DestroyRef) { |
67 | | - const debouncer = new Debouncer(); |
68 | | - const observer = new ResizeObserver( |
69 | | - debouncer.debounce(() => { |
70 | | - this.updateScrollButtonVisibility(); |
71 | | - }, RESIZE_DEBOUNCE), |
72 | | - ); |
73 | | - |
74 | | - afterNextRender(() => { |
75 | | - observer.observe(this.breadcrumbsScrollContent().nativeElement); |
76 | | - }); |
| 23 | + protected readonly parents = input.required<FlatNode[]>(); |
| 24 | + protected readonly handleSelect = output<FlatNode>(); |
| 25 | + protected readonly mouseOverNode = output<FlatNode>(); |
| 26 | + protected readonly mouseLeaveNode = output<FlatNode>(); |
77 | 27 |
|
| 28 | + constructor() { |
78 | 29 | afterRenderEffect({ |
79 | 30 | read: () => { |
80 | 31 | // We use the parents as a dependency to trigger a layout |
81 | 32 | // update that would show the navigation buttons. |
82 | 33 | this.parents(); |
83 | | - untracked(() => this.updateScrollButtonVisibility()); |
| 34 | + untracked(() => this.scoller()?.updateScrollButtonVisibility()); |
84 | 35 | }, |
85 | 36 | }); |
86 | | - |
87 | | - destroyRef.onDestroy(() => { |
88 | | - debouncer.cancel(); |
89 | | - observer.disconnect(); |
90 | | - }); |
91 | | - } |
92 | | - |
93 | | - scroll(pixels: number): void { |
94 | | - this.breadcrumbsScrollContent().nativeElement.scrollLeft += pixels; |
95 | | - this.updateScrollButtonVisibility(); |
96 | | - } |
97 | | - |
98 | | - updateScrollButtonVisibility(): void { |
99 | | - const {clientWidth, scrollWidth, scrollLeft} = this.breadcrumbsScrollContent().nativeElement; |
100 | | - this.breadcrumbsScrollLayout.set({clientWidth, scrollWidth, scrollLeft}); |
101 | 37 | } |
102 | 38 | } |
0 commit comments