Skip to content

Commit 3634b03

Browse files
Merge pull request #94 from Travelopia/feat/lightbox-navigation-bullets
Add Lightbox nav items
2 parents eebfb43 + 5896727 commit 3634b03

File tree

6 files changed

+130
-1
lines changed

6 files changed

+130
-1
lines changed

src/lightbox/README.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,8 @@ Next, we need to trigger the lightbox with and give it some content. Any content
6262
|----------------|-------------------------------------------------------------|
6363
| change | When any attribute has changed |
6464
| template-set | When a template is set, before content has actually updated |
65-
| content-update | When the content has updated inside the lightbox |
65+
| content-change | When the content has updated inside the lightbox |
66+
| slide-set | When a slide in the lightbox is set |
6667

6768
## Methods
6869

src/lightbox/index.html

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,12 @@
2424
</tp-lightbox-next>
2525
<tp-lightbox-content></tp-lightbox-content>
2626
<tp-lightbox-count format="$current / $total"></tp-lightbox-count>
27+
<tp-lightbox-nav>
28+
<tp-lightbox-nav-item><button>1</button></tp-lightbox-nav-item>
29+
<tp-lightbox-nav-item><button>2</button></tp-lightbox-nav-item>
30+
<tp-lightbox-nav-item><button>3</button></tp-lightbox-nav-item>
31+
<tp-lightbox-nav-item><button>4</button></tp-lightbox-nav-item>
32+
</tp-lightbox-nav>
2733
</dialog>
2834
</tp-lightbox>
2935

@@ -34,6 +40,20 @@
3440
</template>
3541
</tp-lightbox-trigger>
3642

43+
<tp-lightbox-trigger lightbox="my-lightbox" group="group-1">
44+
<button>Open Lightbox: Group 1</button>
45+
<template>
46+
<img src="https://picsum.photos/id/77/600/300" width="600" height="300" alt="">
47+
</template>
48+
</tp-lightbox-trigger>
49+
50+
<tp-lightbox-trigger lightbox="my-lightbox" group="group-1">
51+
<button>Open Lightbox: Group 1</button>
52+
<template>
53+
<img src="https://picsum.photos/id/78/600/300" width="600" height="300" alt="">
54+
</template>
55+
</tp-lightbox-trigger>
56+
3757
<tp-lightbox-trigger lightbox="my-lightbox" group="group-1">
3858
<button>Open Lightbox: Group 1</button>
3959
<template>

src/lightbox/index.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,8 @@ import { TPLightboxPreviousElement } from './tp-lightbox-previous';
1313
import { TPLightboxNextElement } from './tp-lightbox-next';
1414
import { TPLightboxCountElement } from './tp-lightbox-count';
1515
import { TPLightboxTriggerElement } from './tp-lightbox-trigger';
16+
import { TPLightboxNavElement } from './tp-lightbox-nav';
17+
import { TPLightboxNavItemElement } from './tp-lightbox-nav-item';
1618

1719
/**
1820
* Register Components.
@@ -24,3 +26,5 @@ customElements.define( 'tp-lightbox-previous', TPLightboxPreviousElement );
2426
customElements.define( 'tp-lightbox-next', TPLightboxNextElement );
2527
customElements.define( 'tp-lightbox-count', TPLightboxCountElement );
2628
customElements.define( 'tp-lightbox-trigger', TPLightboxTriggerElement );
29+
customElements.define( 'tp-lightbox-nav', TPLightboxNavElement );
30+
customElements.define( 'tp-lightbox-nav-item', TPLightboxNavItemElement );
Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
/**
2+
* Internal dependencies.
3+
*/
4+
import { TPLightboxElement } from './tp-lightbox';
5+
import { TPLightboxNavElement } from './tp-lightbox-nav';
6+
7+
/**
8+
* TP Lightbox Nav Item.
9+
*/
10+
export class TPLightboxNavItemElement extends HTMLElement {
11+
/**
12+
* Properties.
13+
*/
14+
protected lightbox : TPLightboxElement | null;
15+
16+
/**
17+
* Constructor.
18+
*/
19+
constructor() {
20+
// Initialize parent.
21+
super();
22+
this.lightbox = this.closest( 'tp-lightbox' );
23+
24+
// Get the nav-item button.
25+
this.querySelector( 'button' )?.addEventListener( 'click', this.handleClick.bind( this ) );
26+
}
27+
28+
/**
29+
* Handle when the button is clicked.
30+
*/
31+
handleClick(): void {
32+
// Check if lightbox exists.
33+
if ( ! this.lightbox ) {
34+
// No its not! Terminate.
35+
return;
36+
}
37+
38+
// Set current slide.
39+
this.lightbox.currentIndex = Number( this.getIndex() ) ?? 1;
40+
41+
// Update navigation current item.
42+
this.lightbox.updateNavCurrentItem();
43+
}
44+
45+
/**
46+
* Get index of this item inside the navigation.
47+
*
48+
* @return {number} Index.
49+
*/
50+
getIndex(): number {
51+
// Bail if no lightbox.
52+
if ( ! this.lightbox ) {
53+
// Exit.
54+
return 0;
55+
}
56+
57+
// No, find it in the navigation.
58+
const lightboxNav: TPLightboxNavElement | null = this.closest( 'tp-lightbox-nav' );
59+
60+
// Return index of this element considering the step value.
61+
return ( Array.from( lightboxNav?.children ?? [] ).indexOf( this ) ) + 1;
62+
}
63+
}

src/lightbox/tp-lightbox-nav.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
/**
2+
* TP Lightbox Nav.
3+
*/
4+
export class TPLightboxNavElement extends HTMLElement {
5+
}

src/lightbox/tp-lightbox.ts

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import { TPLightboxPreviousElement } from './tp-lightbox-previous';
66
import { TPLightboxNextElement } from './tp-lightbox-next';
77
import { TPLightboxTriggerElement } from './tp-lightbox-trigger';
88
import { TPLightboxCountElement } from './tp-lightbox-count';
9+
import { TPLightboxNavItemElement } from './tp-lightbox-nav-item';
910

1011
/**
1112
* TP Lightbox.
@@ -21,6 +22,7 @@ export class TPLightboxElement extends HTMLElement {
2122
protected touchStartY: number = 0;
2223
protected swipeThreshold: number = 200;
2324
protected dialogElement: HTMLDialogElement | null;
25+
protected lightboxNavItems: NodeListOf<TPLightboxNavItemElement> | null;
2426

2527
/**
2628
* Constructor.
@@ -31,6 +33,7 @@ export class TPLightboxElement extends HTMLElement {
3133

3234
// Initialize
3335
this.dialogElement = this.querySelector( 'dialog' );
36+
this.lightboxNavItems = this.querySelectorAll( 'tp-lightbox-nav-item' );
3437

3538
// Event listeners.
3639
this.dialogElement?.addEventListener( 'click', this.handleDialogClick.bind( this ) );
@@ -69,6 +72,11 @@ export class TPLightboxElement extends HTMLElement {
6972
if ( 'index' === name ) {
7073
this.triggerCurrentIndexTarget();
7174
}
75+
76+
// Trigger navigation update if open or index has changed.
77+
if ( 'open' === name || 'index' === name ) {
78+
this.updateNavCurrentItem();
79+
}
7280
}
7381

7482
/**
@@ -159,6 +167,13 @@ export class TPLightboxElement extends HTMLElement {
159167

160168
// Setting this attributes triggers a re-trigger.
161169
this.setAttribute( 'index', index.toString() );
170+
171+
// dispatch slide-set event.
172+
this.dispatchEvent( new CustomEvent( 'slide-set', {
173+
detail: {
174+
slideIndex: index,
175+
},
176+
} ) );
162177
}
163178

164179
/**
@@ -482,4 +497,25 @@ export class TPLightboxElement extends HTMLElement {
482497
}
483498
}
484499
}
500+
501+
/**
502+
* Update current item in navigation.
503+
*/
504+
updateNavCurrentItem(): void {
505+
// Bail if we don't have nav items.
506+
if ( ! this.lightboxNavItems ) {
507+
// Exit.
508+
return;
509+
}
510+
511+
// Update current item.
512+
this.lightboxNavItems.forEach( ( navItem: TPLightboxNavItemElement, index: number ): void => {
513+
// Update current attribute.
514+
if ( this.currentIndex - 1 === index ) {
515+
navItem.setAttribute( 'current', 'yes' );
516+
} else {
517+
navItem.removeAttribute( 'current' );
518+
}
519+
} );
520+
}
485521
}

0 commit comments

Comments
 (0)