|
4 | 4 | import Search from "$lib/components/ui/Search.svelte"; // Base component
|
5 | 5 | import "accessible-autocomplete/dist/accessible-autocomplete.min.css";
|
6 | 6 | import { browser } from "$app/environment";
|
| 7 | + import suggestionIconUrl from "$lib/assets/govuk_publishing_components/images/icon-autocomplete-search-suggestion.svg?url"; |
| 8 | + import closeIconUrl from "$lib/assets/govuk_publishing_components/images/icon-close.svg?url"; // Import for the cancel button |
| 9 | + |
7 | 10 |
|
8 | 11 | // SSR-safe HTML sanitizer: no-op on server
|
9 | 12 | let sanitize: (html: string) => string = (html) => html;
|
|
341 | 344 | // autocompleteInputElement,
|
342 | 345 | // ); // Updated log
|
343 | 346 |
|
344 |
| - // Post-initialisation tweaks |
345 |
| - if (autocompleteInputElement) { |
346 |
| - // Post-init: dynamically show a 'too-short' warning when the user types fewer than minLength characters |
347 |
| - // Find the dropdown menu <ul> generated by accessible-autocomplete |
348 |
| - const suggestionsMenu = containerElement.querySelector<HTMLUListElement>( |
349 |
| - ".gem-c-search-with-autocomplete__menu", |
350 |
| - ); |
351 |
| - // Listen for input changes on the autocomplete field |
352 |
| - autocompleteInputElement.addEventListener("input", () => { |
353 |
| - const val = autocompleteInputElement.value; |
354 |
| - // Remove any existing 'too-short' warning before adding a new one to ensure we don't accumulate multiple warning items. |
355 |
| - suggestionsMenu |
356 |
| - ?.querySelector(".gem-c-search-with-autocomplete__option--too-short") |
357 |
| - ?.remove(); |
358 |
| - if (val.length < minLength) { |
359 |
| - // Force the menu open so the warning is shown |
360 |
| - autocompleteInputElement.setAttribute("aria-expanded", "true"); |
361 |
| - suggestionsMenu?.classList.add( |
362 |
| - "gem-c-search-with-autocomplete__menu--visible", |
| 347 | + // Post-initialisation tweaks |
| 348 | + if (autocompleteInputElement) { |
| 349 | + // Post-init: dynamically show a 'too-short' warning when the user types fewer than minLength characters |
| 350 | + // Find the dropdown menu <ul> generated by accessible-autocomplete |
| 351 | + const suggestionsMenu = |
| 352 | + containerElement.querySelector<HTMLUListElement>( |
| 353 | + ".gem-c-search-with-autocomplete__menu", |
363 | 354 | );
|
364 |
| - suggestionsMenu?.classList.remove( |
365 |
| - "gem-c-search-with-autocomplete__menu--hidden", |
366 |
| - ); |
367 |
| - // Create a new <li> element with the warning text. Classes match the library's own suggestion items (so it looks and behaves consistently). Set role="option" and aria-disabled="true" for accessibility |
368 |
| - const li = document.createElement("li"); |
369 |
| - li.className = |
370 |
| - "gem-c-search-with-autocomplete__option gem-c-search-with-autocomplete__option--too-short"; |
371 |
| - li.setAttribute("role", "option"); |
372 |
| - li.setAttribute("aria-disabled", "true"); |
373 |
| - li.textContent = tooShort(minLength); |
374 |
| - // Insert the warning at the top of the suggestions list menu |
375 |
| - suggestionsMenu?.prepend(li); |
376 |
| - } |
377 |
| - }); |
378 |
| - autocompleteInputElement.setAttribute("type", "search"); // Ensure input type is search |
379 |
| - // autocompleteInputElement.classList.add("autocomplete__input"); // Add specific class if needed |
| 355 | + // Listen for input changes on the autocomplete field |
| 356 | + autocompleteInputElement.addEventListener("input", () => { |
| 357 | + const val = autocompleteInputElement.value; |
| 358 | + // Remove any existing 'too-short' warning before adding a new one to ensure we don't accumulate multiple warning items. |
| 359 | + suggestionsMenu |
| 360 | + ?.querySelector( |
| 361 | + ".gem-c-search-with-autocomplete__option--too-short", |
| 362 | + ) |
| 363 | + ?.remove(); |
| 364 | + if (val.length < minLength) { |
| 365 | + // Force the menu open so the warning is shown |
| 366 | + autocompleteInputElement.setAttribute("aria-expanded", "true"); |
| 367 | + suggestionsMenu?.classList.add( |
| 368 | + "gem-c-search-with-autocomplete__menu--visible", |
| 369 | + ); |
| 370 | + suggestionsMenu?.classList.remove( |
| 371 | + "gem-c-search-with-autocomplete__menu--hidden", |
| 372 | + ); |
| 373 | + // Create a new <li> element with the warning text. Classes match the library's own suggestion items (so it looks and behaves consistently). Set role="option" and aria-disabled="true" for accessibility |
| 374 | + const li = document.createElement("li"); |
| 375 | + li.className = |
| 376 | + "gem-c-search-with-autocomplete__option gem-c-search-with-autocomplete__option--too-short"; |
| 377 | + li.setAttribute("role", "option"); |
| 378 | + li.setAttribute("aria-disabled", "true"); |
| 379 | + li.textContent = tooShort(minLength); |
| 380 | + // Insert the warning at the top of the suggestions list menu |
| 381 | + suggestionsMenu?.prepend(li); |
| 382 | + } |
| 383 | + }); |
| 384 | + autocompleteInputElement.setAttribute("type", "search"); // Ensure input type is search |
| 385 | + // autocompleteInputElement.classList.add("autocomplete__input"); // Add specific class if needed |
380 | 386 |
|
381 | 387 | // Add Enter key workaround from original JS
|
382 | 388 | autocompleteInputElement.addEventListener("keydown", (e) => {
|
|
426 | 432 | data-source-key={source_key}
|
427 | 433 | data-source-property={source_property}
|
428 | 434 | {...outerDataAttributes}
|
| 435 | + style={`--suggestion-icon: url("${suggestionIconUrl}"); --cancel-icon: url("${closeIconUrl}")`} |
429 | 436 | >
|
430 | 437 | <!-- Render the base Search component initially -->
|
431 | 438 | <!-- accessible-autocomplete will enhance the input inside it -->
|
|
546 | 553 | height: 40px;
|
547 | 554 | margin-right: 10px;
|
548 | 555 | flex: none;
|
549 |
| - /* Use relative paths so Vite bundles the asset */ |
550 |
| - mask-image: url(../../assets/govuk_publishing_components/images/icon-autocomplete-search-suggestion.svg); |
551 |
| - -webkit-mask-image: url(../../assets/govuk_publishing_components/images/icon-autocomplete-search-suggestion.svg); |
| 556 | + mask-image: var(--suggestion-icon); |
| 557 | + -webkit-mask-image: var(--suggestion-icon); |
552 | 558 | background-color: #505a5f;
|
553 | 559 | }
|
554 | 560 |
|
|
0 commit comments