2023-10-19 04:46:06 +02:00
|
|
|
<script lang="ts" context="module">
|
|
|
|
// Necessary for eslint
|
|
|
|
/* eslint-disable @typescript-eslint/no-explicit-any */
|
|
|
|
type T = any;
|
|
|
|
</script>
|
|
|
|
|
|
|
|
<script lang="ts" generics="T">
|
2023-10-25 09:48:25 -04:00
|
|
|
import Icon from './icon.svelte';
|
|
|
|
|
|
|
|
import { mdiCheck } from '@mdi/js';
|
|
|
|
|
2024-02-07 23:27:54 -08:00
|
|
|
import { isEqual } from 'lodash-es';
|
2023-07-01 00:50:47 -04:00
|
|
|
import LinkButton from './buttons/link-button.svelte';
|
|
|
|
import { clickOutside } from '$lib/utils/click-outside';
|
|
|
|
import { fly } from 'svelte/transition';
|
2023-09-24 15:22:46 +02:00
|
|
|
import { createEventDispatcher } from 'svelte';
|
2023-06-21 08:05:59 -05:00
|
|
|
|
2023-12-05 13:16:37 -06:00
|
|
|
let className = '';
|
|
|
|
export { className as class };
|
|
|
|
|
2023-09-24 15:22:46 +02:00
|
|
|
const dispatch = createEventDispatcher<{
|
2023-10-19 04:46:06 +02:00
|
|
|
select: T;
|
2023-12-05 13:16:37 -06:00
|
|
|
'click-outside': void;
|
2023-09-24 15:22:46 +02:00
|
|
|
}>();
|
2023-10-19 04:46:06 +02:00
|
|
|
|
|
|
|
export let options: T[];
|
|
|
|
export let selectedOption = options[0];
|
|
|
|
|
2024-02-02 04:18:00 +01:00
|
|
|
export let render: (item: T) => string | RenderedOption = String;
|
2023-10-19 04:46:06 +02:00
|
|
|
|
|
|
|
type RenderedOption = {
|
|
|
|
title: string;
|
2023-10-25 09:48:25 -04:00
|
|
|
icon?: string;
|
2023-10-19 04:46:06 +02:00
|
|
|
};
|
2023-06-21 08:05:59 -05:00
|
|
|
|
2023-11-30 04:52:28 +01:00
|
|
|
export let showMenu = false;
|
|
|
|
export let controlable = false;
|
2023-06-21 08:05:59 -05:00
|
|
|
|
2023-07-01 00:50:47 -04:00
|
|
|
const handleClickOutside = () => {
|
2023-11-30 04:52:28 +01:00
|
|
|
if (!controlable) {
|
|
|
|
showMenu = false;
|
|
|
|
}
|
2023-12-05 13:16:37 -06:00
|
|
|
|
|
|
|
dispatch('click-outside');
|
2023-07-01 00:50:47 -04:00
|
|
|
};
|
2023-06-21 08:05:59 -05:00
|
|
|
|
2023-10-19 04:46:06 +02:00
|
|
|
const handleSelectOption = (option: T) => {
|
|
|
|
dispatch('select', option);
|
|
|
|
selectedOption = option;
|
2023-09-24 15:22:46 +02:00
|
|
|
|
2023-07-01 00:50:47 -04:00
|
|
|
showMenu = false;
|
|
|
|
};
|
2023-08-17 15:46:39 +02:00
|
|
|
|
2023-10-19 04:46:06 +02:00
|
|
|
const renderOption = (option: T): RenderedOption => {
|
|
|
|
const renderedOption = render(option);
|
|
|
|
switch (typeof renderedOption) {
|
2024-02-02 04:18:00 +01:00
|
|
|
case 'string': {
|
2023-10-19 04:46:06 +02:00
|
|
|
return { title: renderedOption };
|
2024-02-02 04:18:00 +01:00
|
|
|
}
|
|
|
|
default: {
|
2023-10-19 04:46:06 +02:00
|
|
|
return {
|
|
|
|
title: renderedOption.title,
|
|
|
|
icon: renderedOption.icon,
|
|
|
|
};
|
2024-02-02 04:18:00 +01:00
|
|
|
}
|
2023-10-19 04:46:06 +02:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
$: renderedSelectedOption = renderOption(selectedOption);
|
2023-06-21 08:05:59 -05:00
|
|
|
</script>
|
|
|
|
|
2023-09-26 04:53:26 +02:00
|
|
|
<div id="dropdown-button" use:clickOutside on:outclick={handleClickOutside} on:escape={handleClickOutside}>
|
2023-07-01 00:50:47 -04:00
|
|
|
<!-- BUTTON TITLE -->
|
2023-11-30 04:52:28 +01:00
|
|
|
<LinkButton on:click={() => (showMenu = true)} fullwidth>
|
2023-07-01 00:50:47 -04:00
|
|
|
<div class="flex place-items-center gap-2 text-sm">
|
2023-10-19 04:46:06 +02:00
|
|
|
{#if renderedSelectedOption?.icon}
|
2023-10-25 09:48:25 -04:00
|
|
|
<Icon path={renderedSelectedOption.icon} size="18" />
|
2023-08-17 15:46:39 +02:00
|
|
|
{/if}
|
2023-10-19 04:46:06 +02:00
|
|
|
<p class="hidden sm:block">{renderedSelectedOption.title}</p>
|
2023-07-01 00:50:47 -04:00
|
|
|
</div>
|
|
|
|
</LinkButton>
|
2023-06-21 08:05:59 -05:00
|
|
|
|
2023-07-01 00:50:47 -04:00
|
|
|
<!-- DROP DOWN MENU -->
|
|
|
|
{#if showMenu}
|
|
|
|
<div
|
2023-11-30 04:52:28 +01:00
|
|
|
transition:fly={{ y: -30, x: 30, duration: 100 }}
|
2024-02-19 02:57:56 +00:00
|
|
|
class="text-md fixed z-50 flex min-w-[250px] max-h-[70vh] overflow-y-auto immich-scrollbar flex-col rounded-2xl bg-gray-100 py-2 text-black shadow-lg dark:bg-gray-700 dark:text-white {className}"
|
2023-07-01 00:50:47 -04:00
|
|
|
>
|
2023-10-19 04:46:06 +02:00
|
|
|
{#each options as option (option)}
|
|
|
|
{@const renderedOption = renderOption(option)}
|
2023-07-01 00:50:47 -04:00
|
|
|
<button
|
2023-11-30 04:52:28 +01:00
|
|
|
class="grid grid-cols-[20px,1fr] place-items-center p-2 transition-all hover:bg-gray-300 dark:hover:bg-gray-800"
|
2023-10-19 04:46:06 +02:00
|
|
|
on:click={() => handleSelectOption(option)}
|
2023-07-01 00:50:47 -04:00
|
|
|
>
|
2024-02-07 23:27:54 -08:00
|
|
|
{#if isEqual(selectedOption, option)}
|
2023-10-19 04:46:06 +02:00
|
|
|
<div class="text-immich-primary dark:text-immich-dark-primary">
|
2023-10-25 09:48:25 -04:00
|
|
|
<Icon path={mdiCheck} size="18" />
|
2023-07-01 00:50:47 -04:00
|
|
|
</div>
|
2023-10-19 04:46:06 +02:00
|
|
|
<p class="justify-self-start text-immich-primary dark:text-immich-dark-primary">
|
|
|
|
{renderedOption.title}
|
2023-07-01 00:50:47 -04:00
|
|
|
</p>
|
|
|
|
{:else}
|
|
|
|
<div />
|
|
|
|
<p class="justify-self-start">
|
2023-10-19 04:46:06 +02:00
|
|
|
{renderedOption.title}
|
2023-07-01 00:50:47 -04:00
|
|
|
</p>
|
|
|
|
{/if}
|
|
|
|
</button>
|
|
|
|
{/each}
|
|
|
|
</div>
|
|
|
|
{/if}
|
2023-06-21 08:05:59 -05:00
|
|
|
</div>
|