|
| 1 | +import { ContentChildren, Directive, HostBinding, HostListener, inject, QueryList } from '@angular/core'; |
| 2 | +import { RouterLinkActive } from '@angular/router'; |
| 3 | +import { SixSidebarItem, SixSidebarItemGroup } from '../stencil-generated/components'; |
| 4 | + |
| 5 | +/** |
| 6 | + * Enables Angular router integration for the six-sidebar component. |
| 7 | + * |
| 8 | + * When this directive is added to a six-sidebar component using the 'sixRouterLinkActive' attribute, |
| 9 | + * it activates automatic route-based selection for sidebar items and groups. |
| 10 | + * |
| 11 | + * @recommended Add this directive to enable automatic route-based navigation in sidebars. |
| 12 | + * |
| 13 | + * @example |
| 14 | + * ```html |
| 15 | + * <six-sidebar sixRouterLinkActive> |
| 16 | + * <six-sidebar-item routerLink="/home">Home</six-sidebar-item> |
| 17 | + * <six-sidebar-item-group> |
| 18 | + * <six-sidebar-item routerLink="/settings/profile">Profile</six-sidebar-item> |
| 19 | + * </six-sidebar-item-group> |
| 20 | + * </six-sidebar> |
| 21 | + * ``` |
| 22 | + */ |
| 23 | +@Directive({ |
| 24 | + selector: 'six-sidebar[sixRouterLinkActive]', |
| 25 | +}) |
| 26 | +export class ActiveSidebarDirective {} |
| 27 | + |
| 28 | +/** |
| 29 | + * Enhances six-sidebar-item with Angular router integration. |
| 30 | + * |
| 31 | + * This directive automatically manages the 'selected' state of sidebar items based on the current route. |
| 32 | + * When used with ActiveSidebarDirective, it switches from manual selection to route-based selection. |
| 33 | + * |
| 34 | + * @requires RouterLinkActive |
| 35 | + * @optional ActiveSidebarDirective - If present, enables route-based selection |
| 36 | + */ |
| 37 | +@Directive({ |
| 38 | + selector: 'six-sidebar-item', |
| 39 | + hostDirectives: [RouterLinkActive], |
| 40 | +}) |
| 41 | +export class ActiveSidebarItemDirective { |
| 42 | + private routerLinkActive = inject(RouterLinkActive); |
| 43 | + |
| 44 | + private sidebarItem = inject(SixSidebarItem); |
| 45 | + private activeSidebarDirective = inject(ActiveSidebarDirective, { optional: true }); |
| 46 | + |
| 47 | + @HostBinding('selected') |
| 48 | + get selected() { |
| 49 | + if (this.activeSidebarDirective == null) { |
| 50 | + return this.sidebarItem.selected; |
| 51 | + } else { |
| 52 | + return this.routerLinkActive.isActive; |
| 53 | + } |
| 54 | + } |
| 55 | +} |
| 56 | + |
| 57 | +/** |
| 58 | + * Enhances six-sidebar-item-group with Angular router integration. |
| 59 | + * |
| 60 | + * This directive automatically manages the 'open' state of sidebar groups based on the active route. |
| 61 | + * When a child route is active, the group automatically expands to show the active item. |
| 62 | + * |
| 63 | + * @requires RouterLinkActive |
| 64 | + * @optional ActiveSidebarDirective - If present, enables route-based expansion |
| 65 | + */ |
| 66 | +@Directive({ |
| 67 | + selector: 'six-sidebar-item-group', |
| 68 | + hostDirectives: [RouterLinkActive], |
| 69 | +}) |
| 70 | +export class ActiveSidebarItemGroupDirective { |
| 71 | + private routerLinkActive = inject(RouterLinkActive); |
| 72 | + private sidebarItemGroup = inject(SixSidebarItemGroup); |
| 73 | + private activeSidebarDirective = inject(ActiveSidebarDirective, { optional: true }); |
| 74 | + |
| 75 | + @ContentChildren(SixSidebarItem) private sidebarItems!: QueryList<SixSidebarItem>; |
| 76 | + |
| 77 | + @HostBinding('open') |
| 78 | + get open() { |
| 79 | + if (this.activeSidebarDirective == null) { |
| 80 | + return this.sidebarItemGroup.open; |
| 81 | + } |
| 82 | + |
| 83 | + if (this.sidebarItems?.length > 0) { |
| 84 | + return this.routerLinkActive.isActive ? true : this.sidebarItemGroup.open; |
| 85 | + } |
| 86 | + |
| 87 | + return this.routerLinkActive.isActive; |
| 88 | + } |
| 89 | +} |
0 commit comments