Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Image Banner: set height explicitly to preserve scroll state in safari #3804

Merged
merged 13 commits into from
Feb 27, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
import { Directive, ElementRef, inject, OnDestroy, OnInit, Renderer2 } from '@angular/core';
import { ResizeObserverService } from '@kirbydesign/designsystem/shared';

/**
* @Description Temporary directive to ensure correct scroll position behavior on Safari.
*
* When navigating between stacked pages, scroll position is not correctly restored on Safari,
* when the nested kirby-card element uses containment and the host element does not have an explicit height.
*/
@Directive({
selector: `[kirbyImageBannerResize]`,
})
export class ImageBannerHeightDirective implements OnInit, OnDestroy {
private currentHeight: number = 0;
private host = inject(ElementRef);
private resizeObserverService = inject(ResizeObserverService);
private renderer = inject(Renderer2);

ngOnInit() {
this.resizeObserverService.observe(this.host, (entry) => this.setCardHeightOnHost(entry));
}

ngOnDestroy() {
this.resizeObserverService.unobserve(this.host);
}

private setCardHeightOnHost(entry: ResizeObserverEntry) {
const hostElement = entry.target as HTMLElement;
const card = hostElement.querySelector('kirby-card');
const cardHeight = card?.getBoundingClientRect().height;

if (!hostElement || !cardHeight) return;
if (cardHeight === this.currentHeight) return;

this.currentHeight = cardHeight;
this.renderer.setStyle(hostElement, 'min-height', `${cardHeight}px`);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,6 @@ $container-query-breakpoint: 600px;

:host {
display: block;
container-name: banner;
container-type: inline-size;
}

:host(.none) {
Expand Down Expand Up @@ -71,6 +69,8 @@ $container-query-breakpoint: 600px;
}

kirby-card {
container-name: banner;
container-type: inline-size;
height: 100%;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,12 @@ import { CardModule } from '@kirbydesign/designsystem/card';
import { ButtonComponent } from '@kirbydesign/designsystem/button';
import { IconModule } from '@kirbydesign/designsystem/icon';
import { TranslationService } from '@kirbydesign/designsystem/shared';
import { ImageBannerHeightDirective } from './image-banner-height.directive';

@Component({
selector: 'kirby-x-image-banner',
imports: [CardModule, ButtonComponent, IconModule, CommonModule],
hostDirectives: [ImageBannerHeightDirective],
templateUrl: './image-banner.component.html',
styleUrl: './image-banner.component.scss',
})
Expand Down
1 change: 1 addition & 0 deletions libs/extensions/angular/image-banner/src/index.ts
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
export { ImageBannerComponent } from './image-banner.component';
export { ImageBannerHeightDirective } from './image-banner-height.directive';
Loading