From c09208e9588f038a4dfc7ab5283f388dc143ba15 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Kamil=20My=C5=9Bliwiec?= Date: Thu, 17 Oct 2024 14:25:23 +0200 Subject: [PATCH] feat: hide carbon when it overlaps with toc --- src/app/homepage/homepage.component.ts | 33 +++++++++++++++++++ .../shared/components/toc/toc.component.ts | 6 ++-- 2 files changed, 37 insertions(+), 2 deletions(-) diff --git a/src/app/homepage/homepage.component.ts b/src/app/homepage/homepage.component.ts index 2fe610a482..70f4a37ec0 100644 --- a/src/app/homepage/homepage.component.ts +++ b/src/app/homepage/homepage.component.ts @@ -16,6 +16,8 @@ import { debounceTime, filter } from 'rxjs/operators'; import { environment } from '../../environments/environment'; import { BasePageComponent } from './pages/page/page.component'; +const CARBON_WIDTH_BREAKPOINT = 1200; + @Component({ selector: 'app-homepage', templateUrl: './homepage.component.html', @@ -133,16 +135,26 @@ export class HomepageComponent implements OnInit, OnDestroy, AfterViewInit { if (!nativeElement) { return; } + this.contentRef = nativeElement.querySelector('.content'); if (this.contentRef && !this.contentRef.querySelector('.carbon-wrapper')) { const scriptTag = this.createCarbonScriptTag(); const carbonWrapper = document.createElement('div'); carbonWrapper.classList.add('carbon-wrapper'); + + if (window.innerWidth > CARBON_WIDTH_BREAKPOINT) { + carbonWrapper.classList.add('hide'); + } carbonWrapper.prepend(scriptTag); this.contentRef.prepend(carbonWrapper); } + this.cd.markForCheck(); + + // Schedule check as TOC might not be rendered yet + const adOverlapCheckDelay = 300; + setTimeout(() => this.hideAdIfTocOverflow(), adOverlapCheckDelay); } createCarbonScriptTag(): HTMLScriptElement { @@ -170,4 +182,25 @@ export class HomepageComponent implements OnInit, OnDestroy, AfterViewInit { }; return scriptTag; } + + hideAdIfTocOverflow() { + const carbonHeight = 160; + const offset = 200; + const viewportHeight = window.innerHeight; + const maxTocHeight = viewportHeight - carbonHeight - offset; + + const tocElRef = document.querySelector('.toc-wrapper ul'); + if (!tocElRef) { + return; + } + + if ( + tocElRef.clientHeight > maxTocHeight && + window.innerWidth > CARBON_WIDTH_BREAKPOINT + ) { + this.contentRef.querySelector('.carbon-wrapper').classList.add('hide'); + } else { + this.contentRef.querySelector('.carbon-wrapper').classList.remove('hide'); + } + } } diff --git a/src/app/shared/components/toc/toc.component.ts b/src/app/shared/components/toc/toc.component.ts index 9ab239b7f0..c4647a81c8 100644 --- a/src/app/shared/components/toc/toc.component.ts +++ b/src/app/shared/components/toc/toc.component.ts @@ -1,8 +1,10 @@ +import { DOCUMENT } from '@angular/common'; import { ChangeDetectionStrategy, ChangeDetectorRef, Component, ElementRef, + Inject, Input, OnChanges, OnDestroy, @@ -52,7 +54,7 @@ export class TocComponent implements OnInit, OnDestroy, OnChanges { ngOnInit() { this.scrollSubscription = fromEvent(window, 'scroll') .pipe(debounceTime(this.scrollDebounceTime)) - .subscribe(_ => { + .subscribe((_) => { this.findCurrentHeading(); this.checkViewportBoundaries(); }); @@ -164,7 +166,7 @@ export class TocComponent implements OnInit, OnDestroy, OnChanges { } } - trackById(index, item) { + trackById(index: number, item: { id: number }) { return item.id; } }