Press n or j to go to the next uncovered block, b, p or k for the previous block.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 | 1x 11x 11x 11x 11x 11x 11x 11x 11x 11x 12x 11x 11x 11x 11x 11x 11x 11x 11x 11x 11x 11x 11x 11x 11x 11x 11x 11x 11x 11x 11x 11x 11x 11x 11x 11x 11x 11x 1x 1x 1x 1x 17x 15x 2x 13x 2x 11x 11x 11x 11x 11x 2x 2x 2x 2x 2x 2x 11x 12x 11x 14x 11x 15x 11x 2x 1x 1x 3x 3x 2x 2x 3x 1x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 12x 12x 12x 1x 12x 14x 14x 14x 2x 2x 2x 12x 1x 1x 18x 16x 16x 2x 2x 2x 2x 2x 2x 2x 2x 2x 18x 3x 3x | /** * Copyright (c) Siemens 2016 - 2025 * SPDX-License-Identifier: MIT */ import { CdkPortalOutlet, Portal, PortalModule } from '@angular/cdk/portal'; import { isPlatformBrowser } from '@angular/common'; import { booleanAttribute, ChangeDetectorRef, Component, computed, DestroyRef, DOCUMENT, ElementRef, inject, input, model, OnChanges, OnDestroy, OnInit, output, PLATFORM_ID, signal, SimpleChanges, viewChild } from '@angular/core'; import { takeUntilDestroyed } from '@angular/core/rxjs-interop'; import { BOOTSTRAP_BREAKPOINTS, Breakpoints, ElementDimensions, ResizeObserverService } from '@siemens/element-ng/resize-observer'; import { t } from '@siemens/element-translate-ng/translate'; import { Subject } from 'rxjs'; import { debounceTime } from 'rxjs/operators'; import { SiSidePanelService } from './si-side-panel.service'; import { SidePanelMode, SidePanelSize } from './side-panel.model'; @Component({ selector: 'si-side-panel', imports: [PortalModule], templateUrl: './si-side-panel.component.html', styleUrl: './si-side-panel.component.scss', host: { class: 'si-layout-inner', '[class.enable-mobile]': 'enableMobile()', '[class.rpanel-size--regular]': 'this.size() === "regular"', '[class.rpanel-size--wide]': 'this.size() === "wide"', '[class.rpanel-mode--over]': 'this.mode() === "over"', '[class.rpanel-mode--scroll]': 'isScrollMode()', '[class.rpanel-collapsed]': 'isCollapsed()', '[class.ready]': 'ready()', '[class.collapsible]': 'collapsible() && !this.showTempContent()', '[class.collapsible-temp]': 'collapsible() && this.showTempContent()', '[class.rpanel-hidden]': 'isHidden()', '[class.rpanel-resize-xs]': 'isXs()', '[class.rpanel-resize-sm]': 'isSm()', '[class.rpanel-resize-md]': 'isMd()', '[class.rpanel-resize-lg]': 'isLg()', '[class.rpanel-resize-xl]': 'isXl()' } }) export class SiSidePanelComponent implements OnInit, OnDestroy, OnChanges { /** * @defaultValue false */ readonly collapsible = input(false, { transform: booleanAttribute }); /** * Default state of navigation */ readonly collapsed = model<boolean>(); /** * Mode of side panel * (ignored below a certain width) * * @defaultValue 'over' */ readonly mode = input<SidePanelMode>('over'); /** * Size of side-panel * * @defaultValue 'regular' */ readonly size = input<SidePanelSize>('regular'); /** * Toggle icon aria-label, required for a11y * * @defaultValue * ``` * t(() => $localize`:@@SI_SIDE_PANEL.TOGGLE:Toggle`) * ``` */ readonly toggleItemLabel = input(t(() => $localize`:@@SI_SIDE_PANEL.TOGGLE:Toggle`)); /** * Specifies custom breakpoints to automatically switch mode. * The `smMinimum` specifies the breakpoint for the mobile view. * The `lgMinimum` specifies the breakpoint to allow scroll mode * (below automatically uses over mode). * The `xlMinimum` specifies the breakpoint to allow scroll mode * with wide size (below automatically uses over mode). */ readonly breakpoints = input<Breakpoints>(); /** * Enable mobile drawer for small screen sizes. Should not be used in conjunction with the vertical navbar. * * @defaultValue false */ readonly enableMobile = input(false, { transform: booleanAttribute }); /** * Emits when the panel is closed */ readonly closed = output(); /** * Emits whenever the content is resized due to opening and closing or parent resize. */ readonly contentResize = output<ElementDimensions>(); protected readonly isScrollMode = computed(() => this.mode() === 'scroll'); protected readonly isXs = signal(false); protected readonly isSm = signal(false); protected readonly isMd = signal(true); protected readonly isLg = signal(false); protected readonly isXl = signal(false); protected readonly isCollapsed = signal(false); protected readonly ready = signal(false); protected readonly isHidden = signal(false); protected readonly showTempContent = signal(false); private readonly panelElement = viewChild.required<ElementRef>('sidePanel'); private readonly contentElement = viewChild.required<ElementRef>('content'); private readonly portalOutlet = viewChild.required<CdkPortalOutlet, CdkPortalOutlet>( 'portalOutlet', { read: CdkPortalOutlet } ); private readonly tmpPortalOutlet = viewChild.required<CdkPortalOutlet, CdkPortalOutlet>( 'tmpPortalOutlet', { read: CdkPortalOutlet } ); /** * The $rpanel-transition-duration in the style is 0.5 seconds. * Sending the resize event after resize need to wait until resize is done. */ private readonly resizeEventDelay = 500; private resizeEvent = new Subject<void>(); private openingOrClosing = false; private previousContentDimensions: ElementDimensions = { width: 0, height: 0 }; private isCollapsedInternal = false; // same as the other one, except w/o timeout for animation private readonly isBrowser = isPlatformBrowser(inject(PLATFORM_ID)); private readonly element = inject(ElementRef); private readonly resizeObserver = inject(ResizeObserverService); private readonly service = inject(SiSidePanelService); private readonly cdRef = inject(ChangeDetectorRef); private readonly destroyRef = inject(DestroyRef); private readonly document = inject(DOCUMENT); constructor() { if (this.isBrowser) { this.resizeEvent .asObservable() .pipe(takeUntilDestroyed(this.destroyRef), debounceTime(this.resizeEventDelay)) .subscribe(() => { this.openingOrClosing = false; this.emitResizeOutputs(); if (this.isCollapsedInternal && !this.collapsible()) { this.isHidden.set(true); } }); } } ngOnChanges(changes: SimpleChanges): void { if (changes.collapsed) { if (this.collapsed()) { this.service.close(); } else { this.service.open(); } } else Iif (changes.enableMobile) { this.service.enableMobile.set(this.enableMobile()); } } ngOnInit(): void { // handle initial state to avoid flicker const collapsed = this.collapsed() ?? !this.service.isOpen(); this.isCollapsedInternal = collapsed; this.isHidden.set(collapsed); this.isCollapsed.set(collapsed); this.resizeObserver .observe(this.element.nativeElement, 100, true) .pipe(takeUntilDestroyed(this.destroyRef)) .subscribe(dim => { this.setBreakpoints(dim.width, dim.height); if (!this.ready()) { // delay because the initial sizing needs to settle setTimeout(() => { this.ready.set(true); }, 100); } if (!this.openingOrClosing) { this.emitResizeOutputs(); } }); this.service.content$ .pipe(takeUntilDestroyed(this.destroyRef)) .subscribe(portal => this.attachContent(portal)); this.service.tempContent$ .pipe(takeUntilDestroyed(this.destroyRef)) .subscribe(portal => this.attachTempContent(portal)); this.service.isOpen$ .pipe(takeUntilDestroyed(this.destroyRef)) .subscribe(state => this.openClose(state)); } ngOnDestroy(): void { this.portalOutlet().detach(); } /** * Toggle whether the side panel is expanded or not. */ toggleSidePanel(): void { if (this.collapsible()) { this.service.toggle(); } else { this.service.close(); } } private emitResizeOutputs(): void { const contentDimensions = this.getContentDimensions(); if ( contentDimensions.width !== this.previousContentDimensions.width || contentDimensions.height !== this.previousContentDimensions.height ) { this.previousContentDimensions = contentDimensions; this.contentResize.emit(contentDimensions); } } private getContentDimensions(): ElementDimensions { if (!this.isCollapsedInternal && this.isXs()) { return { width: 0, height: 0 }; } const rect = this.contentElement().nativeElement.getBoundingClientRect(); return { width: rect.width, height: rect.height }; } private setBreakpoints(width: number, height: number): void { Iif (!width && !height) { // element is not visible, no point in changing anything return; } const breakpoints = this.breakpoints() ?? BOOTSTRAP_BREAKPOINTS; this.isXs.set(width < breakpoints.smMinimum); this.isSm.set(width >= breakpoints.smMinimum && width < breakpoints.mdMinimum); this.isMd.set(width >= breakpoints.mdMinimum && width < breakpoints.lgMinimum); this.isLg.set(width >= breakpoints.lgMinimum && width < breakpoints.xlMinimum); this.isXl.set(width >= breakpoints.xlMinimum); } private sendResize(): void { if (this.isScrollMode() || this.element.nativeElement.style.paddingRight !== '0') { this.openingOrClosing = true; this.resizeEvent.next(); } } private attachContent(portal?: Portal<any>): void { const portalOutlet = this.portalOutlet(); portalOutlet.detach(); if (portal) { portalOutlet.attach(portal); } this.cdRef.markForCheck(); } private attachTempContent(portal: Portal<any> | undefined): void { const tmpPortalOutlet = this.tmpPortalOutlet(); tmpPortalOutlet.detach(); if (portal) { tmpPortalOutlet.attach(portal); this.showTempContent.set(true); this.openClose(true, true); } else if (this.showTempContent()) { this.showTempContent.set(false); this.openClose(this.service.isOpen(), true); } } private openClose(open: boolean, regainFocus = false): void { if (open !== this.isCollapsedInternal) { this.moveFocusInside(open && regainFocus); return; } this.isCollapsedInternal = !open; Iif (open) { this.isHidden.set(false); } setTimeout(() => this.doOpenClose(open)); } private doOpenClose(open: boolean): void { this.moveFocusInside(open); this.isCollapsed.set(!open); this.collapsed.set(this.isCollapsed()); if (this.isCollapsedInternal) { this.closed.emit(); } this.sendResize(); } private moveFocusInside(open: boolean): void { if ( open && !this.document.activeElement?.parentElement?.classList.contains( 'side-panel-collapse-toggle' ) && !this.document.activeElement?.classList.contains('side-panel-collapse-toggle') ) { // moves the keyboard focus inside the panel so that the next tab is somewhere useful this.panelElement().nativeElement.focus(); queueMicrotask(() => this.panelElement().nativeElement.blur()); } } } |