|
| 1 | +import { css, html, LitElement } from 'lit'; |
| 2 | +import { customElement, queryAssignedElements, state } from 'lit/decorators.js'; |
| 3 | +import '../../shared/components/button'; |
| 4 | +import '../../shared/components/code-icon'; |
| 5 | + |
| 6 | +declare global { |
| 7 | + interface HTMLElementTagNameMap { |
| 8 | + 'gl-features-carousel': GlFeaturesCarousel; |
| 9 | + 'gl-feature-card': GlFeatureCard; |
| 10 | + } |
| 11 | +} |
| 12 | + |
| 13 | +@customElement('gl-features-carousel') |
| 14 | +export class GlFeaturesCarousel extends LitElement { |
| 15 | + static override styles = [ |
| 16 | + css` |
| 17 | + :host { |
| 18 | + display: block; |
| 19 | + width: 100%; |
| 20 | + } |
| 21 | +
|
| 22 | + .carousel { |
| 23 | + display: flex; |
| 24 | + align-items: center; |
| 25 | + gap: 1rem; |
| 26 | + justify-content: center; |
| 27 | + } |
| 28 | +
|
| 29 | + .content { |
| 30 | + flex: 1; |
| 31 | + max-width: 520px; |
| 32 | + min-height: 160px; |
| 33 | + display: flex; |
| 34 | + align-items: center; |
| 35 | + justify-content: center; |
| 36 | + } |
| 37 | +
|
| 38 | + .button { |
| 39 | + flex-shrink: 0; |
| 40 | + } |
| 41 | +
|
| 42 | + ::slotted(*) { |
| 43 | + display: none; |
| 44 | + } |
| 45 | +
|
| 46 | + ::slotted([data-active]) { |
| 47 | + display: flex; |
| 48 | + width: 100%; |
| 49 | + } |
| 50 | + `, |
| 51 | + ]; |
| 52 | + |
| 53 | + @queryAssignedElements({ flatten: true }) |
| 54 | + private cards!: HTMLElement[]; |
| 55 | + |
| 56 | + @state() |
| 57 | + private currentIndex = 0; |
| 58 | + |
| 59 | + override firstUpdated(): void { |
| 60 | + this.updateActiveCard(); |
| 61 | + } |
| 62 | + |
| 63 | + private updateActiveCard(): void { |
| 64 | + this.cards.forEach((card, index) => { |
| 65 | + if (index === this.currentIndex) { |
| 66 | + card.setAttribute('data-active', ''); |
| 67 | + } else { |
| 68 | + card.removeAttribute('data-active'); |
| 69 | + } |
| 70 | + }); |
| 71 | + } |
| 72 | + |
| 73 | + private handlePrevious(): void { |
| 74 | + if (this.cards.length === 0) return; |
| 75 | + this.currentIndex = (this.currentIndex - 1 + this.cards.length) % this.cards.length; |
| 76 | + this.updateActiveCard(); |
| 77 | + } |
| 78 | + |
| 79 | + private handleNext(): void { |
| 80 | + if (this.cards.length === 0) return; |
| 81 | + this.currentIndex = (this.currentIndex + 1) % this.cards.length; |
| 82 | + this.updateActiveCard(); |
| 83 | + } |
| 84 | + |
| 85 | + private handleSlotChange(): void { |
| 86 | + this.currentIndex = 0; |
| 87 | + this.updateActiveCard(); |
| 88 | + } |
| 89 | + |
| 90 | + override render(): unknown { |
| 91 | + return html` |
| 92 | + <div class="carousel"> |
| 93 | + <gl-button |
| 94 | + class="button" |
| 95 | + appearance="input" |
| 96 | + @click=${this.handlePrevious} |
| 97 | + aria-label="Previous feature" |
| 98 | + > |
| 99 | + <code-icon icon="chevron-left" size="20"></code-icon> |
| 100 | + </gl-button> |
| 101 | +
|
| 102 | + <div class="content"> |
| 103 | + <slot @slotchange=${this.handleSlotChange}></slot> |
| 104 | + </div> |
| 105 | +
|
| 106 | + <gl-button class="button" appearance="input" @click=${this.handleNext} aria-label="Next feature"> |
| 107 | + <code-icon icon="chevron-right" size="20"></code-icon> |
| 108 | + </gl-button> |
| 109 | + </div> |
| 110 | + `; |
| 111 | + } |
| 112 | +} |
| 113 | + |
| 114 | +@customElement('gl-feature-card') |
| 115 | +export class GlFeatureCard extends LitElement { |
| 116 | + static override styles = [ |
| 117 | + css` |
| 118 | + :host { |
| 119 | + display: flex; |
| 120 | + } |
| 121 | +
|
| 122 | + .image { |
| 123 | + } |
| 124 | + .content { |
| 125 | + } |
| 126 | + ::slotted(img) { |
| 127 | + } |
| 128 | +
|
| 129 | + ::slotted(h1) { |
| 130 | + } |
| 131 | +
|
| 132 | + ::slotted(p) { |
| 133 | + } |
| 134 | + `, |
| 135 | + ]; |
| 136 | + |
| 137 | + override render(): unknown { |
| 138 | + return html` |
| 139 | + <div class="image"> |
| 140 | + <slot name="image"></slot> |
| 141 | + </div> |
| 142 | + <div class="content"> |
| 143 | + <slot></slot> |
| 144 | + </div> |
| 145 | + `; |
| 146 | + } |
| 147 | +} |
0 commit comments