|
| 1 | +import { msg, str } from '@lit/localize'; |
| 2 | +import { css, html, nothing } from 'lit'; |
| 3 | +import { customElement, property } from 'lit/decorators.js'; |
| 4 | +import type { Ticket } from '../../models/ticket'; |
| 5 | +import { getLocale } from '../../utils/localization'; |
| 6 | +import { ThemedComponent } from '../themed-component'; |
| 7 | +import '../ui/hb-button'; |
| 8 | +import '../ui/hb-sticker'; |
| 9 | + |
| 10 | +/** |
| 11 | + * A ticket-shaped card: the name, price, dates and info, stickers when it is sold out, popular or |
| 12 | + * cheaper than full price, and a tear-off stub with the link to buy. |
| 13 | + */ |
| 14 | +@customElement('ticket-card') |
| 15 | +export class TicketCard extends ThemedComponent { |
| 16 | + static override styles = css` |
| 17 | + :host { |
| 18 | + display: block; |
| 19 | + block-size: 100%; |
| 20 | + } |
| 21 | +
|
| 22 | + .ticket { |
| 23 | + position: relative; |
| 24 | + display: flex; |
| 25 | + flex-direction: column; |
| 26 | + block-size: 100%; |
| 27 | + border: var(--hb-border-width) solid var(--hb-border-color); |
| 28 | + border-radius: var(--hb-radius-l); |
| 29 | + background-color: var(--hb-panel-background); |
| 30 | + backdrop-filter: var(--hb-backdrop-filter); |
| 31 | + color: var(--hb-color-on-surface); |
| 32 | + box-shadow: var(--hb-shadow-card); |
| 33 | + } |
| 34 | +
|
| 35 | + .ticket.in-demand { |
| 36 | + background-color: var(--hb-color-primary-container); |
| 37 | + color: var(--hb-color-on-primary-container); |
| 38 | + } |
| 39 | +
|
| 40 | + .stickers { |
| 41 | + position: absolute; |
| 42 | + inset-block-start: calc(var(--hb-space-3) * -1); |
| 43 | + inset-inline-end: var(--hb-space-4); |
| 44 | + display: flex; |
| 45 | + gap: var(--hb-space-1); |
| 46 | + } |
| 47 | +
|
| 48 | + .main { |
| 49 | + flex: 1; |
| 50 | + padding: var(--hb-space-6) var(--hb-space-5) var(--hb-space-5); |
| 51 | + } |
| 52 | +
|
| 53 | + .name { |
| 54 | + margin: 0; |
| 55 | + padding: 0; |
| 56 | + font: 700 var(--hb-text-lg) / 1.2 var(--hb-font-body); |
| 57 | + } |
| 58 | +
|
| 59 | + .price { |
| 60 | + margin: var(--hb-space-3) 0 0; |
| 61 | + font: 800 var(--hb-text-4xl) / 1 var(--hb-font-display); |
| 62 | + overflow-wrap: anywhere; |
| 63 | + } |
| 64 | +
|
| 65 | + .sold-out .price { |
| 66 | + text-decoration: line-through; |
| 67 | + } |
| 68 | +
|
| 69 | + .dates { |
| 70 | + margin: var(--hb-space-3) 0 0; |
| 71 | + font: 500 var(--hb-text-sm) / 1.4 var(--hb-font-mono); |
| 72 | + } |
| 73 | +
|
| 74 | + .info { |
| 75 | + margin: var(--hb-space-2) 0 0; |
| 76 | + font-size: var(--hb-text-sm); |
| 77 | + } |
| 78 | +
|
| 79 | + /* The tear-off line, with a notch at each end in the band's color. */ |
| 80 | + .stub { |
| 81 | + position: relative; |
| 82 | + padding: var(--hb-space-4) var(--hb-space-5) var(--hb-space-5); |
| 83 | + border-block-start: var(--hb-border-width) dashed currentColor; |
| 84 | + } |
| 85 | +
|
| 86 | + .stub::before, |
| 87 | + .stub::after { |
| 88 | + content: ''; |
| 89 | + position: absolute; |
| 90 | + inset-block-start: 0; |
| 91 | + inline-size: 24px; |
| 92 | + block-size: 24px; |
| 93 | + border: var(--hb-border-width) solid var(--hb-border-color); |
| 94 | + border-radius: 50%; |
| 95 | + background-color: var(--hb-band-background, var(--hb-color-surface)); |
| 96 | + translate: 0 -50%; |
| 97 | + } |
| 98 | +
|
| 99 | + /* Half circles that cover the card's edge. */ |
| 100 | + .stub::before { |
| 101 | + inset-inline-start: -14px; |
| 102 | + clip-path: inset(0 0 0 50%); |
| 103 | + } |
| 104 | +
|
| 105 | + .stub::after { |
| 106 | + inset-inline-end: -14px; |
| 107 | + clip-path: inset(0 50% 0 0); |
| 108 | + } |
| 109 | +
|
| 110 | + .stub hb-button { |
| 111 | + inline-size: 100%; |
| 112 | + } |
| 113 | +
|
| 114 | + @media (forced-colors: active) { |
| 115 | + .ticket { |
| 116 | + border-color: CanvasText; |
| 117 | + } |
| 118 | + } |
| 119 | + `; |
| 120 | + |
| 121 | + @property({ attribute: false }) |
| 122 | + accessor ticket: Ticket | undefined; |
| 123 | + /** The primary ticket's price, which a regular ticket shows its saving against. */ |
| 124 | + @property({ type: Number }) |
| 125 | + accessor fullPrice: number | undefined; |
| 126 | + |
| 127 | + override render() { |
| 128 | + const ticket = this.ticket; |
| 129 | + if (!ticket) return nothing; |
| 130 | + const discount = this.discount(ticket); |
| 131 | + const classes = [ticket.soldOut && 'sold-out', ticket.inDemand && 'in-demand'].filter(Boolean); |
| 132 | + return html` |
| 133 | + <article class="ticket ${classes.join(' ')}"> |
| 134 | + <div class="stickers"> |
| 135 | + ${ |
| 136 | + ticket.soldOut |
| 137 | + ? html`<hb-sticker accent="2" tilt="4" |
| 138 | + >${msg('Sold out', { id: 'home.tickets-block.sold-out-sticker' })}</hb-sticker |
| 139 | + >` |
| 140 | + : nothing |
| 141 | + } |
| 142 | + ${ |
| 143 | + !ticket.soldOut && ticket.inDemand |
| 144 | + ? html`<hb-sticker tilt="4" |
| 145 | + >${msg('Popular', { id: 'home.tickets-block.in-demand' })}</hb-sticker |
| 146 | + >` |
| 147 | + : nothing |
| 148 | + } |
| 149 | + ${discount ? html`<hb-sticker accent="4" tilt="-3">${discount}</hb-sticker>` : nothing} |
| 150 | + </div> |
| 151 | + <div class="main"> |
| 152 | + <h3 class="name">${ticket.name}</h3> |
| 153 | + <p class="price">${ticket.currency}${ticket.price}</p> |
| 154 | + ${ticket.starts ? html`<p class="dates">${ticket.starts} – ${ticket.ends}</p>` : nothing} |
| 155 | + <p class="info">${ticket.info}</p> |
| 156 | + </div> |
| 157 | + <div class="stub"> |
| 158 | + <hb-button |
| 159 | + variant="cta" |
| 160 | + href="${ticket.url}" |
| 161 | + target="_blank" |
| 162 | + ?disabled="${ticket.soldOut || !ticket.available}" |
| 163 | + > |
| 164 | + ${this.buttonLabel(ticket)} |
| 165 | + </hb-button> |
| 166 | + </div> |
| 167 | + </article> |
| 168 | + `; |
| 169 | + } |
| 170 | + |
| 171 | + private buttonLabel(ticket: Ticket) { |
| 172 | + if (ticket.soldOut) { |
| 173 | + return msg('You missed it!', { |
| 174 | + id: 'home.tickets-block.sold-out', |
| 175 | + desc: 'Shown on a sold-out ticket.', |
| 176 | + }); |
| 177 | + } |
| 178 | + return ticket.available |
| 179 | + ? msg('Buy ticket', { id: 'common.buy-ticket' }) |
| 180 | + : msg('Not available yet', { id: 'home.tickets-block.not-available' }); |
| 181 | + } |
| 182 | + |
| 183 | + private discount(ticket: Ticket) { |
| 184 | + const fullPrice = this.fullPrice; |
| 185 | + if (!ticket.regular || ticket.primary || ticket.soldOut || !fullPrice) { |
| 186 | + return ''; |
| 187 | + } |
| 188 | + const discount = new Intl.NumberFormat(getLocale(), { style: 'percent' }).format( |
| 189 | + Math.round(100 - (ticket.price * 100) / fullPrice) / 100, |
| 190 | + ); |
| 191 | + return msg(str`Save ${discount} today`, { id: 'home.tickets-block.save' }); |
| 192 | + } |
| 193 | +} |
| 194 | + |
| 195 | +declare global { |
| 196 | + interface HTMLElementTagNameMap { |
| 197 | + 'ticket-card': TicketCard; |
| 198 | + } |
| 199 | +} |
0 commit comments