Skip to content
Snippets Groups Projects
Commit 66ec13da authored by squidfunk's avatar squidfunk
Browse files

Fixed handling of missing targets for navigation blur

parent 70cb57e1
No related branches found
No related tags found
No related merge requests found
...@@ -43,7 +43,7 @@ import * as viewport from "../device/viewport" ...@@ -43,7 +43,7 @@ import * as viewport from "../device/viewport"
*/ */
export interface NavBlurState extends ComponentState { export interface NavBlurState extends ComponentState {
anchors: HTMLAnchorElement[] /* Anchor elements */ anchors: HTMLAnchorElement[] /* Anchor elements */
targets: HTMLElement[] /* Target elements */ targets: Array<HTMLElement | null> /* Target elements */
index: number /* Current index */ index: number /* Current index */
offset: number /* Current offset */ offset: number /* Current offset */
direction: boolean /* Current scroll direction */ direction: boolean /* Current scroll direction */
...@@ -54,7 +54,7 @@ export interface NavBlurState extends ComponentState { ...@@ -54,7 +54,7 @@ export interface NavBlurState extends ComponentState {
*/ */
export interface NavBlur { export interface NavBlur {
anchor: HTMLAnchorElement /* Active anchor */ anchor: HTMLAnchorElement /* Active anchor */
target: HTMLElement /* Active target */ target: HTMLElement | null /* Active target */
} }
/* ---------------------------------------------------------------------------- /* ----------------------------------------------------------------------------
...@@ -105,7 +105,8 @@ export function update(state: NavBlurState) { ...@@ -105,7 +105,8 @@ export function update(state: NavBlurState) {
/* Scroll direction is down */ /* Scroll direction is down */
if (state.offset <= y) { if (state.offset <= y) {
for (let i = state.index + 1; i < anchors.length; i++) { for (let i = state.index + 1; i < anchors.length; i++) {
if (targets[i].offsetTop - (56 + 24) <= y) { // TODO: Refactor value determination const target = targets[i]
if (target && target.offsetTop - (48 + 12) <= y) {
if (i > 0) if (i > 0)
anchors[i - 1].dataset.mdState = "blur" anchors[i - 1].dataset.mdState = "blur"
state.index = i state.index = i
...@@ -117,7 +118,8 @@ export function update(state: NavBlurState) { ...@@ -117,7 +118,8 @@ export function update(state: NavBlurState) {
/* Scroll direction is up */ /* Scroll direction is up */
} else { } else {
for (let i = state.index; i >= 0; i--) { for (let i = state.index; i >= 0; i--) {
if (targets[i].offsetTop - (56 + 24) > y) { const target = targets[i]
if (target && target.offsetTop - (48 + 12) > y) {
if (i > 0) if (i > 0)
anchors[i - 1].dataset.mdState = "" anchors[i - 1].dataset.mdState = ""
} else { } else {
...@@ -141,6 +143,10 @@ export function update(state: NavBlurState) { ...@@ -141,6 +143,10 @@ export function update(state: NavBlurState) {
}) })
} }
/* ----------------------------------------------------------------------------
* Functions
* ------------------------------------------------------------------------- */
/** /**
* Component factory method * Component factory method
* *
...@@ -152,9 +158,9 @@ export function factory( ...@@ -152,9 +158,9 @@ export function factory(
els: ArrayLike<HTMLAnchorElement> els: ArrayLike<HTMLAnchorElement>
): Component<NavBlurState, NavBlur> { ): Component<NavBlurState, NavBlur> {
const anchors = Array.from(els) const anchors = Array.from(els)
const targets: HTMLElement[] = anchors.reduce((result, anchor) => [ const targets = anchors.map(anchor => {
...result, document.getElementById(anchor.hash.substring(1)) || [] return document.getElementById(anchor.hash.substring(1))
], []) })
return { return {
initial: { ...initial, anchors, targets }, create, destroy, update initial: { ...initial, anchors, targets }, create, destroy, update
} }
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment