Skip to content

Commit c09208e

Browse files
feat: hide carbon when it overlaps with toc
1 parent bdbced9 commit c09208e

File tree

2 files changed

+37
-2
lines changed

2 files changed

+37
-2
lines changed

src/app/homepage/homepage.component.ts

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,8 @@ import { debounceTime, filter } from 'rxjs/operators';
1616
import { environment } from '../../environments/environment';
1717
import { BasePageComponent } from './pages/page/page.component';
1818

19+
const CARBON_WIDTH_BREAKPOINT = 1200;
20+
1921
@Component({
2022
selector: 'app-homepage',
2123
templateUrl: './homepage.component.html',
@@ -133,16 +135,26 @@ export class HomepageComponent implements OnInit, OnDestroy, AfterViewInit {
133135
if (!nativeElement) {
134136
return;
135137
}
138+
136139
this.contentRef = nativeElement.querySelector('.content');
137140
if (this.contentRef && !this.contentRef.querySelector('.carbon-wrapper')) {
138141
const scriptTag = this.createCarbonScriptTag();
139142
const carbonWrapper = document.createElement('div');
140143
carbonWrapper.classList.add('carbon-wrapper');
144+
145+
if (window.innerWidth > CARBON_WIDTH_BREAKPOINT) {
146+
carbonWrapper.classList.add('hide');
147+
}
141148
carbonWrapper.prepend(scriptTag);
142149

143150
this.contentRef.prepend(carbonWrapper);
144151
}
152+
145153
this.cd.markForCheck();
154+
155+
// Schedule check as TOC might not be rendered yet
156+
const adOverlapCheckDelay = 300;
157+
setTimeout(() => this.hideAdIfTocOverflow(), adOverlapCheckDelay);
146158
}
147159

148160
createCarbonScriptTag(): HTMLScriptElement {
@@ -170,4 +182,25 @@ export class HomepageComponent implements OnInit, OnDestroy, AfterViewInit {
170182
};
171183
return scriptTag;
172184
}
185+
186+
hideAdIfTocOverflow() {
187+
const carbonHeight = 160;
188+
const offset = 200;
189+
const viewportHeight = window.innerHeight;
190+
const maxTocHeight = viewportHeight - carbonHeight - offset;
191+
192+
const tocElRef = document.querySelector('.toc-wrapper ul');
193+
if (!tocElRef) {
194+
return;
195+
}
196+
197+
if (
198+
tocElRef.clientHeight > maxTocHeight &&
199+
window.innerWidth > CARBON_WIDTH_BREAKPOINT
200+
) {
201+
this.contentRef.querySelector('.carbon-wrapper').classList.add('hide');
202+
} else {
203+
this.contentRef.querySelector('.carbon-wrapper').classList.remove('hide');
204+
}
205+
}
173206
}

src/app/shared/components/toc/toc.component.ts

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,10 @@
1+
import { DOCUMENT } from '@angular/common';
12
import {
23
ChangeDetectionStrategy,
34
ChangeDetectorRef,
45
Component,
56
ElementRef,
7+
Inject,
68
Input,
79
OnChanges,
810
OnDestroy,
@@ -52,7 +54,7 @@ export class TocComponent implements OnInit, OnDestroy, OnChanges {
5254
ngOnInit() {
5355
this.scrollSubscription = fromEvent(window, 'scroll')
5456
.pipe(debounceTime(this.scrollDebounceTime))
55-
.subscribe(_ => {
57+
.subscribe((_) => {
5658
this.findCurrentHeading();
5759
this.checkViewportBoundaries();
5860
});
@@ -164,7 +166,7 @@ export class TocComponent implements OnInit, OnDestroy, OnChanges {
164166
}
165167
}
166168

167-
trackById(index, item) {
169+
trackById(index: number, item: { id: number }) {
168170
return item.id;
169171
}
170172
}

0 commit comments

Comments
 (0)