Press n or j to go to the next uncovered block, b, p or k for the previous block.
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 | 126x 200x 126x 126x 126x 126x 126x 126x 126x 126x 126x 126x 126x 126x 126x 126x 126x 126x 126x 126x 126x 126x 126x 126x 126x 126x 126x 126x 126x 126x 126x 126x 126x 126x 25x 25x 25x 25x 25x 25x 25x 25x 25x 25x 25x 25x 25x 25x 25x 25x 25x 25x 25x 25x 25x 25x 25x 25x 25x 25x 25x 25x 25x 25x 25x 25x 25x 25x 25x 25x 25x 25x 25x 25x 25x 25x 25x 25x 25x 25x 25x 25x 25x 25x 25x 25x 25x 25x 25x 25x 25x 25x 25x 25x 25x 25x 25x 25x 25x 25x 25x 25x 25x 25x 25x 25x 25x 25x 25x 25x 25x 25x 25x 25x 25x 25x 25x 25x 25x 25x 25x 25x 25x 25x 25x 25x 25x 25x 25x 25x 25x 25x 25x 25x 25x 25x 25x 25x 25x 25x 25x 25x 25x 25x 25x 25x 25x 25x 25x 25x 25x 25x 25x 25x 25x 25x 25x 25x 25x 25x 25x 25x 25x 25x 25x 25x 25x 25x 25x 25x 25x 25x 25x 25x 25x 25x 25x 25x 25x 25x 25x 25x 25x 25x 25x 25x 25x 25x 25x 25x 25x 25x 25x 25x 25x 25x 25x 25x 25x 25x 25x 25x 25x 25x 25x 25x 25x 25x 25x 25x 25x 25x 25x 25x 25x 25x 25x 25x 25x 25x 25x 25x 25x 25x 25x 25x 25x 25x 25x 25x 25x 25x 25x 25x 25x 25x 25x 25x 25x 25x 25x 25x 25x 25x 25x 25x 25x 25x 25x 25x 25x 25x 25x 25x 25x 25x 25x 25x 25x 25x 25x 25x 25x 25x 25x 25x 25x 25x 25x 25x 25x 25x 25x 25x 4x 4x 25x 21x 21x 25x 25x 25x 25x 25x 25x 25x 25x 25x 25x 25x 25x 25x 25x 25x 25x 2x 25x 126x 126x 126x 126x 126x 126x 126x 53x 53x 53x 53x 53x 53x 53x 53x 53x 53x 4x 53x 53x 3x 3x 3x 3x 53x 53x 126x 126x 126x 126x 126x 126x 126x 126x 126x 126x 126x 126x 126x 217x 28x 28x 28x 28x 28x 126x 310x 310x 242x 242x 242x 242x 310x 1x 1x 1x 1x 150x 124x 310x 1x 1x 1x 14x 150x 14x 1x 1x 1x 14x 150x 150x 2x 2x 2x 2x 2x 2x 8x 150x 3x 3x 3x 3x 3x 310x 310x 53x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 53x 53x 53x 53x 53x 53x 53x 310x 310x 310x 310x 310x 310x 310x 199x 199x 199x 199x 199x 199x 199x 199x 199x 199x 199x 199x 126x 310x 310x 310x 29x 29x 29x 29x 29x 29x 29x 29x 29x 29x 29x 29x 29x 29x 29x 29x 29x 29x 29x 29x 29x 310x 310x 310x 310x 126x 126x 126x 126x 126x 126x 126x 126x 126x 126x 126x 126x 126x 126x 126x 126x 126x 126x 126x 126x 126x 126x 126x 126x 126x 126x 126x 126x 126x 126x 126x 126x 126x 126x 126x 126x 126x 126x 126x 126x 126x 126x 126x 126x 126x 126x 126x 126x 126x 126x 126x 126x 126x 126x 126x 126x 126x 126x 126x 126x 126x 126x 126x 126x 126x 126x 126x 126x 126x 126x 126x 126x 126x 126x 126x 126x 126x 126x 126x 126x 126x 126x 126x 126x 126x 126x 126x 126x 126x 126x 126x 126x 126x 126x 126x 126x 126x 126x 126x 126x 126x 126x 126x 126x 126x 126x 126x | import { html, css, CSSResultGroup, TemplateResult } from 'lit';
import { property, state, query } from 'lit/decorators.js';
import { styleMap } from 'lit/directives/style-map.js';
import { Icon } from '../Icons';
import { RapidElement } from '../RapidElement';
export interface ModalSearchResult {
// The label shown in the result's badge (e.g. "Send Message", a contact name)
typeName: string;
// Color for the badge background
color: string;
// Optional border color for the badge
borderColor?: string;
// Optional text color override for the badge
textColor?: string;
// The full text that was searched
fullText: string;
// The index of the match start in fullText
matchStart: number;
// The length of the match (0 renders the text as an unhighlighted preview)
matchLength: number;
}
/**
* Base class for modal search windows — a centered overlay with a search
* input, keyboard-navigable result list and match highlighting. Subclasses
* implement performSearch() to supply results, either synchronously as the
* user types (the default) or asynchronously when the user hits Enter (set
* searchOnEnter for searches that hit an endpoint).
*/
export abstract class SearchModal<
T extends ModalSearchResult = ModalSearchResult
> extends RapidElement {
static styles: CSSResultGroup = css`
:host {
position: fixed;
top: 0;
left: 0;
right: 0;
bottom: 0;
z-index: 100000;
display: none;
font-family: var(--font-family, sans-serif);
}
:host([open]) {
display: flex;
align-items: flex-start;
justify-content: center;
}
.backdrop {
position: absolute;
top: 0;
left: 0;
right: 0;
bottom: 0;
background: rgba(0, 0, 0, 0.3);
}
.search-container {
position: relative;
margin-top: 80px;
width: 560px;
max-height: calc(100vh - 160px);
background: white;
border-radius: 14px;
box-shadow:
0 24px 80px rgba(0, 0, 0, 0.2),
0 0 0 1px rgba(0, 0, 0, 0.05);
display: flex;
flex-direction: column;
overflow: hidden;
animation: searchSlideIn 0.15s ease-out;
}
@keyframes searchSlideIn {
from {
opacity: 0;
transform: scale(0.98) translateY(-8px);
}
to {
opacity: 1;
transform: scale(1) translateY(0);
}
}
.search-input-row {
display: flex;
align-items: center;
padding: 14px 18px;
border-bottom: 1px solid #e5e7eb;
gap: 10px;
}
.search-icon {
color: #9ca3af;
flex-shrink: 0;
}
.search-icon temba-icon {
--icon-color: #9ca3af;
display: block;
}
input {
flex: 1;
border: none;
outline: none;
font-size: 16px;
background: transparent;
color: #111;
font-family: inherit;
}
input::placeholder {
color: #9ca3af;
}
.results {
overflow-y: auto;
max-height: 400px;
}
.result-item {
display: flex;
align-items: center;
padding: 10px 18px;
gap: 12px;
cursor: pointer;
border-bottom: 1px solid #f3f4f6;
transition: background 0.08s;
}
.result-item:last-child {
border-bottom: none;
}
.result-item:hover,
.result-item.highlighted {
background: #f0f4ff;
}
.result-type-badge {
flex-shrink: 0;
font-size: 11px;
font-weight: 600;
color: white;
padding: 2px 10px;
border-radius: 8px;
border: 2px solid transparent;
white-space: nowrap;
text-align: center;
box-sizing: border-box;
}
.result-text {
min-width: 0;
font-size: 14px;
color: #374151;
line-height: 1.4;
white-space: nowrap;
overflow: hidden;
text-overflow: ellipsis;
}
.result-text mark {
background: #fef08a;
color: inherit;
border-radius: 2px;
padding: 0 1px;
}
.no-results {
padding: 24px 18px;
text-align: center;
color: #9ca3af;
font-size: 14px;
}
.loading {
display: flex;
justify-content: center;
padding: 24px 18px;
}
.result-count {
padding: 8px 18px;
font-size: 12px;
color: #9ca3af;
border-top: 1px solid #e5e7eb;
text-align: right;
flex-shrink: 0;
}
.hint {
padding: 10px 18px;
font-size: 12px;
color: #9ca3af;
text-align: center;
}
kbd {
background: #f3f4f6;
border: 1px solid #d1d5db;
border-radius: 4px;
padding: 1px 5px;
font-size: 11px;
font-family: inherit;
}
`;
@property({ type: Boolean, reflect: true })
open = false;
// When set, searching waits for Enter instead of running as the user types.
// Use for searches that hit an endpoint and shouldn't fire on every keystroke.
@property({ type: Boolean })
searchOnEnter = false;
@state()
protected searchQuery = '';
@state()
protected results: T[] = [];
@state()
protected highlightedIndex = 0;
@state()
protected loading = false;
// In searchOnEnter mode, whether the query has changed since the last search
@state()
protected dirty = false;
// Set when an async performSearch rejects, so a failed lookup reads as a
// failure rather than as an empty result set
@state()
protected error = false;
// Bumped on each search (and on query edits in searchOnEnter mode) so
// in-flight async results that are no longer wanted get dropped
private searchGeneration = 0;
@query('input')
private inputEl!: HTMLInputElement;
/**
* Produce results for the given query. Synchronous implementations run on
* every keystroke; async implementations should be paired with searchOnEnter.
*/
protected abstract performSearch(query: string): T[] | Promise<T[]>;
/**
* The label used for the dialog, placeholder and aria attributes.
*/
protected abstract getSearchLabel(): string;
/**
* Called when a search is abandoned - the modal closed, or the query
* edited out from under it. Subclasses with an in-flight request should
* cancel it here.
*/
protected cancelSearch(): void {
// nothing to do for synchronous searches
}
public show(): void {
this.open = true;
this.searchQuery = '';
this.results = [];
this.highlightedIndex = 0;
this.loading = false;
this.dirty = false;
this.error = false;
this.searchGeneration++;
this.updateComplete.then(() => {
this.inputEl?.focus();
this.inputEl?.select();
});
}
public hide(): void {
this.cancelSearch();
this.open = false;
this.searchQuery = '';
this.results = [];
this.loading = false;
this.dirty = false;
this.error = false;
this.searchGeneration++;
}
private handleInput(e: InputEvent): void {
const input = e.target as HTMLInputElement;
this.searchQuery = input.value;
this.highlightedIndex = 0;
this.error = false;
// whatever was in flight was for the old query
this.cancelSearch();
if (this.searchOnEnter) {
// invalidate any in-flight search and wait for Enter
this.searchGeneration++;
this.results = [];
this.loading = false;
this.dirty = true;
} else {
this.runSearch();
}
}
private handleKeyDown(e: KeyboardEvent): void {
if (e.key === 'Escape') {
e.preventDefault();
this.hide();
return;
}
if (e.key === 'ArrowDown' || (e.ctrlKey && e.key === 'n')) {
e.preventDefault();
if (this.results.length > 0) {
this.highlightedIndex =
(this.highlightedIndex + 1) % this.results.length;
}
return;
}
if (e.key === 'ArrowUp' || (e.ctrlKey && e.key === 'p')) {
e.preventDefault();
if (this.results.length > 0) {
this.highlightedIndex =
(this.highlightedIndex - 1 + this.results.length) %
this.results.length;
}
return;
}
if (e.key === 'Enter') {
e.preventDefault();
if (this.searchOnEnter && this.dirty) {
this.runSearch();
return;
}
if (this.results.length > 0) {
this.selectResult(this.results[this.highlightedIndex]);
}
return;
}
}
private runSearch(): void {
const generation = ++this.searchGeneration;
this.dirty = false;
this.highlightedIndex = 0;
this.error = false;
if (!this.searchQuery.trim()) {
this.results = [];
this.loading = false;
return;
}
const outcome = this.performSearch(this.searchQuery);
if (outcome instanceof Promise) {
this.results = [];
this.loading = true;
outcome
.then((results) => {
if (generation === this.searchGeneration) {
this.results = results;
this.loading = false;
}
})
.catch((error) => {
// a superseded search has already been replaced by whatever
// superseded it - that includes searches we aborted ourselves,
// so those never surface as failures
if (generation === this.searchGeneration) {
console.error(
'search failed',
error instanceof Response
? `${error.status} ${error.url || ''}`.trim()
: error
);
this.results = [];
this.loading = false;
this.error = true;
// the query is still the one that failed, so leaving it dirty
// lets Enter run it again
this.dirty = this.searchOnEnter;
}
});
} else {
this.results = outcome;
}
}
private handleBackdropClick(): void {
this.hide();
}
protected selectResult(result: T): void {
this.hide();
this.dispatchEvent(
new CustomEvent('temba-search-result-selected', {
detail: result,
bubbles: true,
composed: true
})
);
}
protected renderMatchText(result: T): TemplateResult {
const { matchStart, matchLength } = result;
const matchEnd = matchStart + matchLength;
// Replace newlines with spaces for single-line display
const text = result.fullText.replace(/\n/g, ' ');
// Zero-length matches show an unhighlighted content preview
if (matchLength === 0) {
return html`${text}`;
}
// Show leading context before the match, then the match, then trailing.
// CSS text-overflow:ellipsis clips the trailing text, so we keep the match
// near the start. When the match is near the end of the text, show more
// leading context since there's less trailing text to display.
const afterMatch = text.length - matchEnd;
const contextBefore = afterMatch < 30 ? 50 : 20;
const start = Math.max(0, matchStart - contextBefore);
const prefix = start > 0 ? '…' : '';
const before = text.slice(start, matchStart);
const match = text.slice(matchStart, matchEnd);
const after = text.slice(matchEnd);
return html`${prefix}${before}<mark>${match}</mark>${after}`;
}
/**
* The content of a single result row. Subclasses can override to customize.
*/
protected renderResultContent(result: T): TemplateResult {
const badgeStyles: { [key: string]: string } = {
background: result.color,
borderColor: result.borderColor || result.color
};
if (result.textColor) {
badgeStyles.color = result.textColor;
}
return html`
<div class="result-type-badge" style=${styleMap(badgeStyles)}>
${result.typeName}
</div>
<div class="result-text">${this.renderMatchText(result)}</div>
`;
}
/**
* The message shown when a completed search produced no results. Subclasses
* can override to explain why the result set came back empty.
*/
protected getNoResultsMessage(): string {
return 'No matches found';
}
/**
* The hint shown before any query has been entered.
*/
protected renderHint(): TemplateResult {
return html`<div class="hint">
<kbd>↑</kbd> <kbd>↓</kbd> to navigate <kbd>Enter</kbd> to open
<kbd>Esc</kbd> to close
</div>`;
}
private renderBody(): TemplateResult {
if (!this.searchQuery.trim()) {
return this.renderHint();
}
// a failed search leaves the query dirty so Enter retries it, so the
// failure has to be reported ahead of the "Enter to search" hint
if (this.error) {
return html`<div class="no-results">Search failed - try again</div>`;
}
if (this.searchOnEnter && this.dirty) {
return html`<div class="hint"><kbd>Enter</kbd> to search</div>`;
}
if (this.loading) {
return html`<div class="loading">
<temba-loading units="6" size="8"></temba-loading>
</div>`;
}
if (this.results.length === 0) {
return html`<div class="no-results">${this.getNoResultsMessage()}</div>`;
}
return html`
<div class="results">
${this.results.map(
(result, index) => html`
<div
class="result-item ${index === this.highlightedIndex
? 'highlighted'
: ''}"
@click=${() => this.selectResult(result)}
@mouseenter=${() => {
this.highlightedIndex = index;
}}
>
${this.renderResultContent(result)}
</div>
`
)}
</div>
<div class="result-count">
${this.results.length} result${this.results.length !== 1 ? 's' : ''}
</div>
`;
}
updated(changedProps: Map<string, unknown>): void {
if (changedProps.has('highlightedIndex')) {
const highlighted = this.shadowRoot?.querySelector(
'.result-item.highlighted'
);
highlighted?.scrollIntoView({ block: 'nearest' });
}
}
render(): TemplateResult {
const searchLabel = this.getSearchLabel();
return html`
<div class="backdrop" @click=${this.handleBackdropClick}></div>
<div
class="search-container"
role="dialog"
aria-modal="true"
aria-label=${searchLabel}
@click=${(e: Event) => e.stopPropagation()}
>
<div class="search-input-row">
<div class="search-icon">
<temba-icon name=${Icon.search} size="1.5"></temba-icon>
</div>
<input
type="text"
placeholder="${searchLabel}..."
aria-label=${searchLabel}
.value=${this.searchQuery}
@input=${this.handleInput}
@keydown=${this.handleKeyDown}
/>
</div>
${this.renderBody()}
</div>
`;
}
}
|