2025-04-18 23:06:36 -04:00
|
|
|
<script lang="ts">
|
|
|
|
import { t } from 'svelte-i18n';
|
|
|
|
import { onMount } from 'svelte';
|
|
|
|
|
|
|
|
export let selectedTimezone: string = Intl.DateTimeFormat().resolvedOptions().timeZone;
|
2025-05-10 10:47:00 -04:00
|
|
|
// Generate a unique ID for this component instance
|
|
|
|
const instanceId = `tz-selector-${crypto.randomUUID().substring(0, 8)}`;
|
2025-04-18 23:06:36 -04:00
|
|
|
|
|
|
|
let dropdownOpen = false;
|
|
|
|
let searchQuery = '';
|
2025-05-10 10:47:00 -04:00
|
|
|
let searchInput: HTMLInputElement | null = null;
|
2025-04-18 23:06:36 -04:00
|
|
|
const timezones = Intl.supportedValuesOf('timeZone');
|
|
|
|
|
|
|
|
// Filter timezones based on search query
|
|
|
|
$: filteredTimezones = searchQuery
|
|
|
|
? timezones.filter((tz) => tz.toLowerCase().includes(searchQuery.toLowerCase()))
|
|
|
|
: timezones;
|
|
|
|
|
|
|
|
function selectTimezone(tz: string) {
|
|
|
|
selectedTimezone = tz;
|
|
|
|
dropdownOpen = false;
|
|
|
|
searchQuery = '';
|
|
|
|
}
|
|
|
|
|
2025-05-10 10:47:00 -04:00
|
|
|
// Focus search input when dropdown opens - with proper null check
|
2025-05-09 10:24:29 -04:00
|
|
|
$: if (dropdownOpen && searchInput) {
|
|
|
|
// Use setTimeout to delay focus until after the element is rendered
|
2025-05-10 10:47:00 -04:00
|
|
|
setTimeout(() => {
|
|
|
|
if (searchInput) searchInput.focus();
|
|
|
|
}, 0);
|
2025-05-09 10:24:29 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
function handleKeydown(event: KeyboardEvent, tz?: string) {
|
|
|
|
if (event.key === 'Enter' || event.key === ' ') {
|
|
|
|
event.preventDefault();
|
|
|
|
if (tz) selectTimezone(tz);
|
|
|
|
else dropdownOpen = !dropdownOpen;
|
|
|
|
} else if (event.key === 'Escape') {
|
|
|
|
event.preventDefault();
|
|
|
|
dropdownOpen = false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2025-04-18 23:06:36 -04:00
|
|
|
// Close dropdown if clicked outside
|
|
|
|
onMount(() => {
|
|
|
|
const handleClickOutside = (e: MouseEvent) => {
|
2025-05-10 10:47:00 -04:00
|
|
|
const dropdown = document.getElementById(instanceId);
|
2025-04-18 23:06:36 -04:00
|
|
|
if (dropdown && !dropdown.contains(e.target as Node)) dropdownOpen = false;
|
|
|
|
};
|
|
|
|
document.addEventListener('click', handleClickOutside);
|
|
|
|
return () => document.removeEventListener('click', handleClickOutside);
|
|
|
|
});
|
|
|
|
</script>
|
|
|
|
|
2025-05-10 10:47:00 -04:00
|
|
|
<div class="form-control w-full max-w-xs relative" id={instanceId}>
|
|
|
|
<label class="label" for={`timezone-display-${instanceId}`}>
|
2025-05-09 15:59:48 -04:00
|
|
|
<span class="label-text">{$t('adventures.timezone')}</span>
|
2025-04-18 23:06:36 -04:00
|
|
|
</label>
|
|
|
|
|
|
|
|
<!-- Trigger -->
|
|
|
|
<div
|
2025-05-10 10:47:00 -04:00
|
|
|
id={`timezone-display-${instanceId}`}
|
2025-04-18 23:06:36 -04:00
|
|
|
tabindex="0"
|
|
|
|
role="button"
|
2025-05-09 10:24:29 -04:00
|
|
|
aria-haspopup="listbox"
|
|
|
|
aria-expanded={dropdownOpen}
|
2025-04-18 23:06:36 -04:00
|
|
|
class="input input-bordered flex justify-between items-center cursor-pointer"
|
|
|
|
on:click={() => (dropdownOpen = !dropdownOpen)}
|
2025-05-09 10:24:29 -04:00
|
|
|
on:keydown={handleKeydown}
|
2025-04-18 23:06:36 -04:00
|
|
|
>
|
|
|
|
<span class="truncate">{selectedTimezone}</span>
|
|
|
|
<svg
|
|
|
|
xmlns="http://www.w3.org/2000/svg"
|
|
|
|
class="w-4 h-4"
|
|
|
|
fill="none"
|
|
|
|
viewBox="0 0 24 24"
|
|
|
|
stroke="currentColor"
|
2025-05-09 10:24:29 -04:00
|
|
|
aria-hidden="true"
|
2025-04-18 23:06:36 -04:00
|
|
|
>
|
|
|
|
<path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M19 9l-7 7-7-7" />
|
|
|
|
</svg>
|
|
|
|
</div>
|
|
|
|
|
|
|
|
<!-- Dropdown -->
|
|
|
|
{#if dropdownOpen}
|
|
|
|
<div
|
|
|
|
class="absolute mt-1 z-10 bg-base-100 shadow-lg rounded-box w-full max-h-60 overflow-y-auto"
|
2025-05-09 10:24:29 -04:00
|
|
|
role="listbox"
|
2025-05-10 10:47:00 -04:00
|
|
|
aria-labelledby={`timezone-display-${instanceId}`}
|
2025-04-18 23:06:36 -04:00
|
|
|
>
|
|
|
|
<!-- Search -->
|
|
|
|
<div class="sticky top-0 bg-base-100 p-2 border-b">
|
|
|
|
<input
|
|
|
|
type="text"
|
|
|
|
placeholder="Search timezone"
|
|
|
|
class="input input-sm input-bordered w-full"
|
|
|
|
bind:value={searchQuery}
|
2025-05-09 10:24:29 -04:00
|
|
|
bind:this={searchInput}
|
2025-04-18 23:06:36 -04:00
|
|
|
/>
|
|
|
|
</div>
|
|
|
|
|
|
|
|
<!-- Timezone list -->
|
|
|
|
{#if filteredTimezones.length > 0}
|
|
|
|
<ul class="menu p-2 space-y-1">
|
|
|
|
{#each filteredTimezones as tz}
|
|
|
|
<li>
|
2025-05-09 10:24:29 -04:00
|
|
|
<button
|
|
|
|
type="button"
|
|
|
|
class={`w-full text-left truncate ${tz === selectedTimezone ? 'active font-bold' : ''}`}
|
|
|
|
on:click={() => selectTimezone(tz)}
|
|
|
|
on:keydown={(e) => handleKeydown(e, tz)}
|
|
|
|
role="option"
|
|
|
|
aria-selected={tz === selectedTimezone}
|
2025-04-18 23:06:36 -04:00
|
|
|
>
|
|
|
|
{tz}
|
2025-05-09 10:24:29 -04:00
|
|
|
</button>
|
2025-04-18 23:06:36 -04:00
|
|
|
</li>
|
|
|
|
{/each}
|
|
|
|
</ul>
|
|
|
|
{:else}
|
|
|
|
<div class="p-2 text-sm text-center opacity-60">No timezones found</div>
|
|
|
|
{/if}
|
|
|
|
</div>
|
|
|
|
{/if}
|
|
|
|
</div>
|