mirror of
https://github.com/open-webui/open-webui.git
synced 2026-09-16 23:43:03 +00:00
161 lines
4.1 KiB
Svelte
161 lines
4.1 KiB
Svelte
<script lang="ts">
|
|
import { flyAndScale } from '$lib/utils/transitions';
|
|
import { tick } from 'svelte';
|
|
import DropdownMenu from '$lib/components/common/DropdownMenu.svelte';
|
|
|
|
/** Currently selected value */
|
|
export let value = '';
|
|
|
|
/** Items array: { value: string, label: string }[] */
|
|
export let items = [];
|
|
|
|
/** Placeholder text when no value is selected */
|
|
export let placeholder = '';
|
|
|
|
/** Callback when value changes */
|
|
export let onChange: (value: string) => void = () => {};
|
|
|
|
/** CSS classes for the trigger button */
|
|
export let triggerClass = '';
|
|
|
|
/** CSS classes for the label inside the trigger */
|
|
export let labelClass = '';
|
|
|
|
/** CSS classes for the dropdown content container */
|
|
export let contentClass = 'min-w-[10.625rem]';
|
|
|
|
/** Max height for the dropdown content */
|
|
export let maxHeight = '18rem';
|
|
|
|
/** CSS classes for each item button */
|
|
export let itemClass =
|
|
'flex h-[1.6875rem] w-full cursor-pointer items-center gap-2 rounded-xl bg-transparent px-2 text-[0.8125rem] hover:bg-gray-50/40 hover:text-gray-900 dark:hover:bg-gray-800/40 dark:hover:text-gray-100';
|
|
|
|
/** Alignment of the dropdown: 'start' | 'end' */
|
|
export let align = 'start';
|
|
|
|
/** Side to open on: 'bottom' | 'top' */
|
|
export let side = 'bottom';
|
|
|
|
/** Callback when dropdown closes */
|
|
export let onClose: () => void = () => {};
|
|
|
|
export let open = false;
|
|
|
|
let triggerEl;
|
|
let contentEl;
|
|
|
|
$: selectedLabel = items.find((i) => i.value === value)?.label ?? placeholder;
|
|
|
|
/** Svelte action: moves the node to document.body (portal) */
|
|
function portal(node) {
|
|
document.body.appendChild(node);
|
|
return {
|
|
destroy() {
|
|
if (node.parentNode) {
|
|
node.parentNode.removeChild(node);
|
|
}
|
|
}
|
|
};
|
|
}
|
|
|
|
function positionContent() {
|
|
if (!triggerEl || !contentEl) return;
|
|
const rect = triggerEl.getBoundingClientRect();
|
|
|
|
contentEl.style.position = 'fixed';
|
|
contentEl.style.zIndex = '9999';
|
|
contentEl.style.minWidth = `${rect.width}px`;
|
|
if (side === 'top') {
|
|
contentEl.style.bottom = `${window.innerHeight - rect.top + 4}px`;
|
|
contentEl.style.top = 'auto';
|
|
} else {
|
|
contentEl.style.top = `${rect.bottom + 4}px`;
|
|
contentEl.style.bottom = 'auto';
|
|
}
|
|
|
|
const contentWidth = contentEl.offsetWidth || 0;
|
|
|
|
if (align === 'end') {
|
|
let right = window.innerWidth - rect.right;
|
|
if (right + contentWidth > window.innerWidth) {
|
|
right = window.innerWidth - contentWidth - 16;
|
|
}
|
|
contentEl.style.right = `${Math.max(16, right)}px`;
|
|
contentEl.style.left = 'auto';
|
|
} else {
|
|
let left = rect.left;
|
|
if (left + contentWidth + 16 > window.innerWidth) {
|
|
left = window.innerWidth - contentWidth - 16;
|
|
}
|
|
contentEl.style.left = `${Math.max(16, left)}px`;
|
|
contentEl.style.right = 'auto';
|
|
}
|
|
}
|
|
|
|
async function toggleOpen() {
|
|
open = !open;
|
|
if (open) {
|
|
await tick();
|
|
positionContent();
|
|
}
|
|
}
|
|
|
|
function handleWindowClick(event) {
|
|
if (!open) return;
|
|
if (triggerEl?.contains(event.target)) return;
|
|
if (contentEl?.contains(event.target)) return;
|
|
open = false;
|
|
onClose();
|
|
}
|
|
|
|
function handleKeydown(event) {
|
|
if (event.key === 'Escape' && open) {
|
|
open = false;
|
|
onClose();
|
|
}
|
|
}
|
|
|
|
export function selectItem(item) {
|
|
value = item.value;
|
|
open = false;
|
|
onChange(value);
|
|
}
|
|
</script>
|
|
|
|
<svelte:window
|
|
on:click={handleWindowClick}
|
|
on:keydown={handleKeydown}
|
|
on:scroll|capture={positionContent}
|
|
on:resize={positionContent}
|
|
/>
|
|
|
|
<button
|
|
bind:this={triggerEl}
|
|
class="focus-ring {triggerClass}"
|
|
type="button"
|
|
aria-expanded={open}
|
|
on:click={toggleOpen}
|
|
>
|
|
<slot name="trigger" {selectedLabel} {open}>
|
|
<span class={labelClass}>
|
|
{selectedLabel}
|
|
</span>
|
|
</slot>
|
|
</button>
|
|
|
|
{#if open}
|
|
<div use:portal bind:this={contentEl} transition:flyAndScale>
|
|
<DropdownMenu className={contentClass} style={`max-height: ${maxHeight}; overflow-y: auto;`}>
|
|
<slot {open} {selectItem}>
|
|
{#each items as item}
|
|
<button class="focus-ring {itemClass}" type="button" on:click={() => selectItem(item)}>
|
|
<slot name="item" {item} selected={value === item.value}>
|
|
{item.label}
|
|
</slot>
|
|
</button>
|
|
{/each}
|
|
</slot>
|
|
</DropdownMenu>
|
|
</div>
|
|
{/if}
|