-
Notifications
You must be signed in to change notification settings - Fork 23
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
f404d1c
commit bca7de8
Showing
3 changed files
with
137 additions
and
55 deletions.
There are no files selected for viewing
39 changes: 39 additions & 0 deletions
39
libs/extensions/angular/image-banner/src/image-banner-resize.directive.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
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. | ||
*/ | ||
@Directive({ | ||
selector: `kirby-x-image-banner[kirbyImageBannerResize]`, | ||
standalone: true, | ||
}) | ||
export class ImageBannerResizeDirective implements OnInit, OnDestroy { | ||
private host = inject(ElementRef); | ||
private renderer = inject(Renderer2); | ||
private resizeObserverService = inject(ResizeObserverService); | ||
|
||
private readonly CONTAINER_BREAKPOINT = 600; // pixels | ||
private readonly CONTAINER_MORE_THAN_CSS_CLASS_NAME = 'container-more-than-width'; | ||
private readonly CONTAINER_LESS_THAN_CSS_CLASS_NAME = 'container-less-than-width'; | ||
|
||
ngOnInit() { | ||
this.resizeObserverService.observe(this.host.nativeElement, (entry) => | ||
this.handleHostResize(entry) | ||
); | ||
} | ||
|
||
ngOnDestroy() { | ||
this.resizeObserverService.unobserve(this.host.nativeElement); | ||
} | ||
|
||
private handleHostResize(entry: ResizeObserverEntry) { | ||
if (entry.contentRect.width < this.CONTAINER_BREAKPOINT) { | ||
this.renderer.removeClass(this.host.nativeElement, this.CONTAINER_MORE_THAN_CSS_CLASS_NAME); | ||
this.renderer.addClass(this.host.nativeElement, this.CONTAINER_LESS_THAN_CSS_CLASS_NAME); | ||
} else { | ||
this.renderer.removeClass(this.host.nativeElement, this.CONTAINER_LESS_THAN_CSS_CLASS_NAME); | ||
this.renderer.addClass(this.host.nativeElement, this.CONTAINER_MORE_THAN_CSS_CLASS_NAME); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters