Added navigation component

This commit is contained in:
squidfunk 2019-11-20 10:09:23 +01:00
parent 919fccd238
commit e457d901b2
4 changed files with 92 additions and 5 deletions

View File

@ -34,7 +34,7 @@ import { ViewportOffset, getElement } from "../../ui"
* Anchors * Anchors
*/ */
export interface Anchors { export interface Anchors {
done: HTMLAnchorElement[][] /* Read anchors */ done: HTMLAnchorElement[][] /* Done anchors */
next: HTMLAnchorElement[][] /* Next anchors */ next: HTMLAnchorElement[][] /* Next anchors */
} }
@ -158,7 +158,10 @@ export function watchAnchors(
/* Return partition */ /* Return partition */
return [done, next] return [done, next]
}, [[], [...table]]), }, [[], [...table]]),
distinctUntilChanged((a, b) => a[0] === b[0] && a[1] === b[1]) distinctUntilChanged((a, b) => {
return a[0] === b[0]
&& a[1] === b[1]
})
) )
/* Extract anchors and return hot observable */ /* Extract anchors and return hot observable */

View File

@ -23,4 +23,5 @@
export * from "./anchor" export * from "./anchor"
export * from "./container" export * from "./container"
export * from "./header" export * from "./header"
export * from "./navigation"
export * from "./sidebar" export * from "./sidebar"

View File

@ -0,0 +1,82 @@
/*
* Copyright (c) 2016-2019 Martin Donath <martin.donath@squidfunk.com>
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to
* deal in the Software without restriction, including without limitation the
* rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
* sell copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
* IN THE SOFTWARE.
*/
import { Observable, of } from "rxjs"
import { shareReplay } from "rxjs/operators"
import { getElement, getElements } from "../../ui"
/* ----------------------------------------------------------------------------
* Types
* ------------------------------------------------------------------------- */
/**
* Navigation index
*/
export type NavigationIndex = Map<HTMLInputElement, HTMLElement>
/* ----------------------------------------------------------------------------
* Functions
* ------------------------------------------------------------------------- */
/**
* Set navigation overflow scrolling
*
* @param nav - Navigation element
* @param active - Whether overflow scrolling is active
*/
export function setNavigationOverflowScrolling(
nav: HTMLElement, active: boolean
): void {
nav.style.webkitOverflowScrolling = active ? "touch" : ""
}
/* ------------------------------------------------------------------------- */
/**
* Create an observable to index all navigation elements
*
* @param nav - Top-level navigation element
*
* @return Navigation index observable
*/
export function watchNavigationIndex(
nav: HTMLElement
): Observable<NavigationIndex> {
const list = getElements("nav", nav)
/* Build index to map inputs to navigation lists */
const index = new Map<HTMLInputElement, HTMLElement>()
for (const item of list) {
const label = getElement<HTMLLabelElement>("label", item)!
if (typeof label !== "undefined") {
const input = getElement<HTMLInputElement>(`#${label.htmlFor}`)!
index.set(input, item)
}
}
/* Return navigation index */
return of(index)
.pipe(
shareReplay({ bufferSize: 1, refCount: true })
)
}

View File

@ -20,8 +20,9 @@
* IN THE SOFTWARE. * IN THE SOFTWARE.
*/ */
import { equals } from "ramda"
import { Observable, combineLatest } from "rxjs" import { Observable, combineLatest } from "rxjs"
import { map, shareReplay } from "rxjs/operators" import { distinctUntilChanged, map, shareReplay } from "rxjs/operators"
import { ViewportOffset } from "../../ui" import { ViewportOffset } from "../../ui"
import { Container } from "../container" import { Container } from "../container"
@ -112,8 +113,7 @@ export function watchSidebar(
const height$ = combineLatest(offset$, container$) const height$ = combineLatest(offset$, container$)
.pipe( .pipe(
map(([{ y }, { offset, height }]) => { map(([{ y }, { offset, height }]) => {
return height - adjust return height - adjust + Math.min(adjust, Math.max(0, y - offset))
+ Math.min(adjust, Math.max(0, y - offset))
}) })
) )
@ -127,6 +127,7 @@ export function watchSidebar(
return combineLatest(height$, lock$) return combineLatest(height$, lock$)
.pipe( .pipe(
map(([height, lock]) => ({ height, lock })), map(([height, lock]) => ({ height, lock })),
distinctUntilChanged<Sidebar>(equals),
shareReplay({ bufferSize: 1, refCount: true }) shareReplay({ bufferSize: 1, refCount: true })
) )
} }