mirror of
https://github.com/open-webui/open-webui.git
synced 2026-11-23 22:11:28 -05:00
195 lines
5.1 KiB
Svelte
195 lines
5.1 KiB
Svelte
<script lang="ts">
|
|
import { flyAndScale } from '$lib/utils/transitions';
|
|
import { tick } from 'svelte';
|
|
import { mobile } from '$lib/stores';
|
|
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`;
|
|
|
|
const contentHeight = contentEl.offsetHeight || 0;
|
|
const spaceBelow = window.innerHeight - rect.bottom - 4;
|
|
const spaceAbove = rect.top - 4;
|
|
|
|
let openAbove = side === 'top';
|
|
if (side === 'bottom' && spaceBelow < contentHeight && spaceAbove > spaceBelow) {
|
|
openAbove = true;
|
|
} else if (side === 'top' && spaceAbove < contentHeight && spaceBelow > spaceAbove) {
|
|
openAbove = false;
|
|
}
|
|
|
|
if (openAbove) {
|
|
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(focusContent = false) {
|
|
open = !open;
|
|
if (open) {
|
|
await tick();
|
|
positionContent();
|
|
if (focusContent) {
|
|
contentEl?.querySelector<HTMLElement>('button:not([disabled]), a[href], input')?.focus();
|
|
}
|
|
}
|
|
}
|
|
|
|
function handleTriggerKeydown(event) {
|
|
if (event.target === triggerEl && (event.key === 'Enter' || event.key === ' ')) {
|
|
event.preventDefault();
|
|
toggleOpen(true);
|
|
}
|
|
}
|
|
|
|
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) {
|
|
const restoreFocus = contentEl?.contains(document.activeElement);
|
|
open = false;
|
|
onClose();
|
|
if (restoreFocus) triggerEl?.focus();
|
|
}
|
|
}
|
|
|
|
export function selectItem(item) {
|
|
const restoreFocus = contentEl?.contains(document.activeElement);
|
|
value = item.value;
|
|
open = false;
|
|
onChange(value);
|
|
if (restoreFocus) triggerEl?.focus();
|
|
}
|
|
</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(!$mobile)}
|
|
on:keydown={handleTriggerKeydown}
|
|
>
|
|
<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"
|
|
aria-pressed={value === item.value}
|
|
on:click={() => selectItem(item)}
|
|
>
|
|
<slot name="item" {item} selected={value === item.value}>
|
|
{item.label}
|
|
</slot>
|
|
</button>
|
|
{/each}
|
|
</slot>
|
|
</DropdownMenu>
|
|
</div>
|
|
{/if}
|