Skip to content

Commit

Permalink
Merge pull request #97 from Travelopia/feat/dynamic-nav-items
Browse files Browse the repository at this point in the history
Dynamic lightbox navigation injection
  • Loading branch information
junaidbhura authored Dec 9, 2024
2 parents b8cbb41 + 36debf4 commit 0be72d2
Show file tree
Hide file tree
Showing 2 changed files with 57 additions and 4 deletions.
10 changes: 6 additions & 4 deletions src/lightbox/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -25,10 +25,12 @@
<tp-lightbox-content></tp-lightbox-content>
<tp-lightbox-count format="$current / $total"></tp-lightbox-count>
<tp-lightbox-nav>
<tp-lightbox-nav-item><button>1</button></tp-lightbox-nav-item>
<tp-lightbox-nav-item><button>2</button></tp-lightbox-nav-item>
<tp-lightbox-nav-item><button>3</button></tp-lightbox-nav-item>
<tp-lightbox-nav-item><button>4</button></tp-lightbox-nav-item>
<template>
<tp-lightbox-nav-item>
<button>
</button>
</tp-lightbox-nav-item>
</template>
</tp-lightbox-nav>
</dialog>
</tp-lightbox>
Expand Down
51 changes: 51 additions & 0 deletions src/lightbox/tp-lightbox-nav.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,56 @@
/**
* Internal dependencies.
*/
import { TPLightboxElement } from './tp-lightbox';

/**
* TP Lightbox Nav.
*/
export class TPLightboxNavElement extends HTMLElement {
/**
* Properties.
*/
protected template: HTMLTemplateElement | null = null;
protected lightbox : TPLightboxElement | null;

/**
* Constructor.
*/
constructor() {
// Initialize parent.
super();

// Initialize properties.
this.template = this.querySelector( 'template' );
this.lightbox = this.closest( 'tp-lightbox' );

// Add event listener.
this.lightbox?.addEventListener( 'template-set', this.setTemplate.bind( this ) );
}

/**
* Set the template.
*/
setTemplate(): void {
// Bail if no template.
if ( ! this.template ) {
// Exit.
return;
}

// Total slides.
const totalSlides = Number( this.lightbox?.getAttribute( 'total' ) ?? 0 );

// Clear the navigation.
this.innerHTML = '';

// Append the navigation items.
for ( let i = 0; i < totalSlides; i++ ) {
// Clone the template.
const navItem = this.template.content.cloneNode( true );

// Append the clone.
this.appendChild( navItem );
}
}
}

0 comments on commit 0be72d2

Please sign in to comment.