Skip to content

Commit

Permalink
fix(esl-anchornav): fix ESLAnchornavRender signature
Browse files Browse the repository at this point in the history
  • Loading branch information
dshovchko committed Aug 21, 2024
1 parent 9eba4ef commit 3d43ba1
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 21 deletions.
4 changes: 2 additions & 2 deletions site/src/esl-anchornav/esl-anchornav.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,11 @@ import {ESLAnchor, ESLAnchornav, ESLAnchornavSticked} from '@exadel/esl/modules/

import type {ESLAnchornavRender, ESLAnchorData} from '@exadel/esl/modules/esl-anchornav/core';

const demoRenderer: ESLAnchornavRender = (data: ESLAnchorData): Element => {
const demoRenderer: ESLAnchornavRender = (data: ESLAnchorData, index: number): Element => {
const a = document.createElement('a');
a.href = `#${data.id}`;
a.className = 'esl-anchornav-item';
a.dataset.index = `${data.index + 1}`;
a.dataset.index = `${index + 1}`;
a.textContent = data.title;
return a;
};
Expand Down
2 changes: 0 additions & 2 deletions src/modules/esl-anchornav/core.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
export type * from './core/esl-anchornav-types';

export * from './core/esl-anchornav';
export * from './core/esl-anchornav-sticked';
export * from './core/esl-anchor';
10 changes: 0 additions & 10 deletions src/modules/esl-anchornav/core/esl-anchornav-types.ts
Original file line number Diff line number Diff line change
@@ -1,10 +0,0 @@
/** {@link ESLAnchornav} item renderer */
export type ESLAnchornavRender = (data: ESLAnchorData) => string | Element;

/** {@link ESLAnchornav} anchor data interface */
export interface ESLAnchorData {
id: string;
title: string;
index: number; // order number in the anchor list
$anchor: HTMLElement;
}
34 changes: 27 additions & 7 deletions src/modules/esl-anchornav/core/esl-anchornav.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,16 @@ import {ESLEventUtils, ESLIntersectionTarget} from '../../esl-event-listener/cor
import {ESLAnchor} from './esl-anchor';

import type {DelegatedEvent, ESLIntersectionEvent} from '../../esl-event-listener/core';
import type {ESLAnchorData, ESLAnchornavRender} from './esl-anchornav-types';

/** {@link ESLAnchornav} item renderer */
export type ESLAnchornavRender = (data: ESLAnchorData, index: number, anchrnav: ESLAnchornav) => string | Element;

/** {@link ESLAnchornav} anchor data interface */
export interface ESLAnchorData {
id: string;
title: string;
$anchor: HTMLElement;
}

/**
* ESLAnchornav
Expand Down Expand Up @@ -52,7 +61,7 @@ export class ESLAnchornav extends ESLBaseElement {
public set active(value: ESLAnchorData) {
if (this._active === value) return;
this._active = value;
this._onActiveChange(value);
this._onActiveChange();
}

/** Anchors list */
Expand Down Expand Up @@ -88,6 +97,16 @@ export class ESLAnchornav extends ESLBaseElement {
return getViewportForEl(this);
}

/** Data for prepend anchor */
protected get prependData(): ESLAnchorData[] {
return [];
}

/** Data for append anchor */
protected get appependData(): ESLAnchorData[] {
return [];
}

@ready
protected override connectedCallback(): void {
super.connectedCallback();
Expand Down Expand Up @@ -120,8 +139,8 @@ export class ESLAnchornav extends ESLBaseElement {
protected renderAnchors(): Element[] {
const itemRenderer = ESLAnchornav.getRenderer(this.rendererName);
this._items.clear();
return itemRenderer ? this._anchors.map((anchor) => {
let item = itemRenderer(anchor);
return itemRenderer ? this._anchors.map((anchor, index) => {
let item = itemRenderer(anchor, index, this);
if (typeof item === 'string') item = this.htmlToElement(item);
this._items.set(anchor.id, item);
return item;
Expand All @@ -133,7 +152,6 @@ export class ESLAnchornav extends ESLBaseElement {
return {
id: $anchor.id,
title: $anchor.title,
index,
$anchor
};
}
Expand All @@ -160,8 +178,8 @@ export class ESLAnchornav extends ESLBaseElement {

/** Handles changing the active anchor */
@decorate(microtask)
protected _onActiveChange(active: ESLAnchorData): void {
const detail = {id: active.id};
protected _onActiveChange(): void {
const detail = {id: this.active.id};
ESLEventUtils.dispatch(this, this.ACTIVECHANGED_EVENT, {detail});
}

Expand All @@ -177,6 +195,8 @@ export class ESLAnchornav extends ESLBaseElement {
})
protected _onAnchornavRequest(): void {
this._anchors = [...document.querySelectorAll<HTMLElement>(this.ANCHOR_SELECTOR)].map(this.getDataFrom);
this._anchors.unshift(...this.prependData);
this._anchors.push(...this.appependData);
this.update();
}

Expand Down

0 comments on commit 3d43ba1

Please sign in to comment.