|
| 1 | +import { animate, AnimationEvent, state, style, transition, trigger } from '@angular/animations'; |
| 2 | +import { |
| 3 | + Component, |
| 4 | + computed, |
| 5 | + HostBinding, |
| 6 | + HostListener, |
| 7 | + inject, |
| 8 | + input, |
| 9 | + InputSignal, |
| 10 | + InputSignalWithTransform, |
| 11 | + numberAttribute, |
| 12 | + output, |
| 13 | + OutputEmitterRef, |
| 14 | + signal |
| 15 | +} from '@angular/core'; |
| 16 | +import { TabsService } from '../tabs.service'; |
| 17 | + |
| 18 | +type AnimateType = 'hide' | 'show'; |
| 19 | +type VisibleChangeEvent = { itemKey: string | number; visible: boolean }; |
| 20 | + |
| 21 | +@Component({ |
| 22 | + selector: 'c-tab-panel', |
| 23 | + standalone: true, |
| 24 | + template: '<ng-content />', |
| 25 | + host: { |
| 26 | + '[class]': 'hostClasses()', |
| 27 | + '[tabindex]': 'visible() ? tabindex(): -1', |
| 28 | + '[attr.aria-labelledby]': 'attrAriaLabelledBy()', |
| 29 | + '[id]': 'propId()', |
| 30 | + role: 'tabpanel' |
| 31 | + }, |
| 32 | + animations: [ |
| 33 | + trigger('fadeInOut', [ |
| 34 | + state('show', style({ opacity: 1 })), |
| 35 | + state('hide', style({ opacity: 0 })), |
| 36 | + state('void', style({ opacity: 0 })), |
| 37 | + transition('* => *', [animate('150ms linear')]) |
| 38 | + ]) |
| 39 | + ] |
| 40 | +}) |
| 41 | +export class TabPanelComponent { |
| 42 | + readonly tabsService = inject(TabsService); |
| 43 | + |
| 44 | + /** |
| 45 | + * aria-labelledby attribute |
| 46 | + * @type string |
| 47 | + * @default undefined |
| 48 | + */ |
| 49 | + readonly ariaLabelledBy: InputSignal<string | undefined> = input<string | undefined>(undefined, { |
| 50 | + alias: 'aria-labelledby' |
| 51 | + }); |
| 52 | + |
| 53 | + /** |
| 54 | + * Element id attribute |
| 55 | + * @type string |
| 56 | + * @default undefined |
| 57 | + */ |
| 58 | + readonly id: InputSignal<string | undefined> = input<string>(); |
| 59 | + |
| 60 | + /** |
| 61 | + * Item key. |
| 62 | + * @type string | number |
| 63 | + * @required |
| 64 | + */ |
| 65 | + readonly itemKey: InputSignal<string | number> = input.required(); |
| 66 | + |
| 67 | + /** |
| 68 | + * tabindex attribute. |
| 69 | + * @type number |
| 70 | + * @default 0 |
| 71 | + */ |
| 72 | + readonly tabindex: InputSignalWithTransform<number, unknown> = input(0, { transform: numberAttribute }); |
| 73 | + |
| 74 | + /** |
| 75 | + * Enable fade in transition. |
| 76 | + * @type boolean |
| 77 | + * @default true |
| 78 | + */ |
| 79 | + readonly transition: InputSignal<boolean> = input(true); |
| 80 | + |
| 81 | + /** |
| 82 | + * visible change output |
| 83 | + * @type OutputEmitterRef<VisibleChangeEvent> |
| 84 | + */ |
| 85 | + readonly visibleChange: OutputEmitterRef<VisibleChangeEvent> = output<VisibleChangeEvent>(); |
| 86 | + |
| 87 | + readonly show = signal(false); |
| 88 | + |
| 89 | + readonly visible = computed(() => { |
| 90 | + const visible = this.tabsService.activeItemKey() === this.itemKey() && !this.tabsService.activeItem()?.disabled; |
| 91 | + this.visibleChange.emit({ itemKey: this.itemKey(), visible }); |
| 92 | + return visible; |
| 93 | + }); |
| 94 | + |
| 95 | + readonly propId = computed(() => this.id() ?? `${this.tabsService.id()}-panel-${this.itemKey()}`); |
| 96 | + |
| 97 | + readonly attrAriaLabelledBy = computed( |
| 98 | + () => this.ariaLabelledBy() ?? `${this.tabsService.id()}-tab-${this.itemKey()}` |
| 99 | + ); |
| 100 | + |
| 101 | + readonly hostClasses = computed(() => ({ |
| 102 | + 'tab-pane': true, |
| 103 | + active: this.show(), |
| 104 | + fade: this.transition(), |
| 105 | + show: this.show(), |
| 106 | + invisible: this.tabsService.activeItem()?.disabled |
| 107 | + })); |
| 108 | + |
| 109 | + @HostBinding('@.disabled') |
| 110 | + get animationDisabled(): boolean { |
| 111 | + return !this.transition(); |
| 112 | + } |
| 113 | + |
| 114 | + @HostBinding('@fadeInOut') |
| 115 | + get animateType(): AnimateType { |
| 116 | + return this.visible() ? 'show' : 'hide'; |
| 117 | + } |
| 118 | + |
| 119 | + @HostListener('@fadeInOut.done', ['$event']) |
| 120 | + onAnimationDone($event: AnimationEvent): void { |
| 121 | + this.show.set(this.visible()); |
| 122 | + } |
| 123 | +} |
0 commit comments