Skip to content
Open
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
2 changes: 1 addition & 1 deletion src/app/app.component.html
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
<mat-sidenav mode="side">
<rxjs-docs-sidenav></rxjs-docs-sidenav>
</mat-sidenav>
<mat-sidenav-content>
<mat-sidenav-content cdkScrollable>
<router-outlet></router-outlet>
</mat-sidenav-content>
</mat-sidenav-container>
Expand Down
2 changes: 2 additions & 0 deletions src/app/app.module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import { MarkdownModule } from 'ngx-markdown';
import { SharedModule } from './shared/shared.module';
import { HttpClientModule, HttpClient } from '@angular/common/http';
import { FontAwesomeModule } from '@fortawesome/angular-fontawesome';
import { ScrollingModule } from '@angular/cdk/scrolling';

@NgModule({
declarations: [AppComponent],
Expand All @@ -21,6 +22,7 @@ import { FontAwesomeModule } from '@fortawesome/angular-fontawesome';
SharedModule,
MarkdownModule.forRoot({ loader: HttpClient }),
FontAwesomeModule,
ScrollingModule,
],
providers: [],
bootstrap: [AppComponent],
Expand Down
40 changes: 40 additions & 0 deletions src/app/shared/components/scroll-spy/scroll-spy.component.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
.root {
padding: 4px;
padding-left: 8px;
display: inline-table;
position: sticky;
top: 20px;
border-left: 1px solid gray;
display: inline-table;
}

.section {
display: flex;
align-items: center;
margin-bottom: 0.6em;
cursor: pointer;
transition: all 0.3s;
}

.actual-section-indicator {
width: 0;
height: 10px;
margin-left: -17px;
margin-right: 15px;
border-radius: 100px;
background-color: white;
border: 1px solid white;
flex: none;
display: inline-block;
transition: all 0.3s;
}

.section:hover {
color: rgb(148, 7, 78);
}

.actual-section-indicator.active {
width: 16px;
margin-right: 5px;
border: 1px solid black;
}
13 changes: 13 additions & 0 deletions src/app/shared/components/scroll-spy/scroll-spy.component.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<div class="root">
<a
class="section"
*ngFor="let section of sections"
(click)="scrollTo(section.id)"
>
<span
class="actual-section-indicator"
[class.active]="isActualSection(section.id)"
></span>
<span>{{ section.name }}</span>
</a>
</div>
25 changes: 25 additions & 0 deletions src/app/shared/components/scroll-spy/scroll-spy.component.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
import { async, ComponentFixture, TestBed } from '@angular/core/testing';

import { ScrollSpyComponent } from './scroll-spy.component';

describe('ScrollSpyComponent', () => {
let component: ScrollSpyComponent;
let fixture: ComponentFixture<ScrollSpyComponent>;

beforeEach(async(() => {
TestBed.configureTestingModule({
declarations: [ ScrollSpyComponent ]
})
.compileComponents();
}));

beforeEach(() => {
fixture = TestBed.createComponent(ScrollSpyComponent);
component = fixture.componentInstance;
fixture.detectChanges();
});

it('should create', () => {
expect(component).toBeTruthy();
});
});
88 changes: 88 additions & 0 deletions src/app/shared/components/scroll-spy/scroll-spy.component.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
import {
Component,
AfterContentChecked,
ChangeDetectorRef,
OnInit,
} from '@angular/core';
import { debounce, throttle, debounceTime } from 'rxjs/operators';
import { ScrollDispatcher, CdkScrollable } from '@angular/cdk/overlay';
import { interval } from 'rxjs';

@Component({
selector: 'rxjs-scroll-spy',
templateUrl: './scroll-spy.component.html',
styleUrls: ['./scroll-spy.component.css'],
})
export class ScrollSpyComponent implements AfterContentChecked, OnInit {
currentSection = '';
sections = [];
private sectionsHeader: NodeListOf<HTMLHeadingElement>;

constructor(
private scroll: ScrollDispatcher,
private changeDetector: ChangeDetectorRef
) {}

ngOnInit() {
this.scroll
.scrolled()
.pipe(throttle((ev) => interval(100)))
.subscribe((scroll: CdkScrollable) => this.onScroll(scroll));
}

ngAfterContentChecked(): void {
this.loadSections();
}

scrollTo(sectionId) {
document.querySelector('#' + sectionId).scrollIntoView();
}

isActualSection(sectionId: string) {
return sectionId === this.currentSection;
}

private loadSections() {
this.sectionsHeader = document.querySelectorAll('h2');
this.sections = [];
this.sectionsHeader.forEach((header: HTMLHeadingElement) => {
this.sections.push({
name: header.textContent,
id: header.id,
});
});
}

private onScroll(scroll: CdkScrollable) {
const scrollTop = scroll.getElementRef().nativeElement.scrollTop || 0;
const parentOffset = scroll.getElementRef().nativeElement.offsetTop;
const currentSection = this.getCurrentSection({ scrollTop, parentOffset });

if (!currentSection || currentSection.id === this.currentSection) {
return;
}

this.currentSection = currentSection.id;
this.changeDetector.detectChanges();
}

private getCurrentSection({ scrollTop, parentOffset }) {
const sectionsCount = this.sectionsHeader.length;
let actualHeader;
for (let i = 0; i < sectionsCount; i++) {
const header = this.sectionsHeader[i];
if (header.offsetTop - parentOffset <= scrollTop) {
actualHeader = header;
}
}

return actualHeader;

/* For some reason, this (and similar) DONT work. Only get first element
return [].find.call(
this.sectionsHeader,
(header: HTMLHeadingElement) =>
header.offsetTop - parentOffset <= scrollTop
); */
}
}
10 changes: 8 additions & 2 deletions src/app/shared/shared.module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,16 @@ import { NgModule } from '@angular/core';
import { MatDividerModule } from '@angular/material/divider';
import { SidenavComponent } from './components/sidenav/sidenav.component';
import { RouterModule } from '@angular/router';
import { ScrollSpyComponent } from './components/scroll-spy/scroll-spy.component';

const CORE_MODULES = [CommonModule, FormsModule, ReactiveFormsModule];

const COMPONENTS = [FooterComponent, HeaderComponent, SidenavComponent];
const COMPONENTS = [
FooterComponent,
HeaderComponent,
SidenavComponent,
ScrollSpyComponent,
];

const MATERIAL_MODULES = [
MatInputModule,
Expand All @@ -49,7 +55,7 @@ const MATERIAL_MODULES = [
];

@NgModule({
declarations: COMPONENTS,
declarations: [COMPONENTS, ScrollSpyComponent],
imports: [MATERIAL_MODULES, CORE_MODULES, FlexLayoutModule],
exports: [COMPONENTS, MATERIAL_MODULES, CORE_MODULES, FlexLayoutModule],
})
Expand Down
20 changes: 20 additions & 0 deletions src/app/views/home/content/content.component.css
Original file line number Diff line number Diff line change
@@ -1,3 +1,12 @@
.md-container {
display: flex;
}

.md-container > * {
margin: 0 24px;
min-width: 0; /* Fix flex horizontal overflow */
}

details {
border-top: 2px solid #e3e7e7;
margin: 50px 0 0;
Expand Down Expand Up @@ -65,6 +74,10 @@ th {
padding: 25px;
}

.scrollspy-container {
max-width: 450px;
}

.page-heading {
display: flex;
justify-content: space-between;
Expand Down Expand Up @@ -114,3 +127,10 @@ summary:hover {
padding: 40px;
}
}

@media only screen and (max-width: 700px) {
.scrollspy-container {
display: none;
}
}

7 changes: 6 additions & 1 deletion src/app/views/home/content/content.component.html
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
<div class="md-container">
<scully-content></scully-content>
<div class="scully-content-container">
<scully-content></scully-content>
</div>
<div class="scrollspy-container">
<rxjs-scroll-spy></rxjs-scroll-spy>
</div>
</div>