Files
veridian/app/composables/useDropdown.ts
T
zoeissleeping 59bb7fbc12 Performance enhancements galore! New themining system
This is once again a huge commit, but its mostly performance
improvements along with some bug fixes and refactoring. It also includes
changes to the theming systems. I'm still not 100% happy with the
theming system, but its better than before.

Model fetching has been dramatically improved! Nearly all the important
computation and pre-processing has been moved to the server. This has
also somehow fixed the way model details are loaded, which was causing
many models to be missing their details despite models.dev having them.

The markdown renderer has once again been changed, but I'm mostly
certain that this is the last time major changes will be made to it. The
renderer is not spamming components, bloating memory usage, and its not
using a bug prone custom written chunking system.

There's also a lot more that I haven't mentioned and honestly forgot. I
need to get better commit hygiene tbh.
2026-02-20 00:20:50 -06:00

78 lines
2.3 KiB
TypeScript

import type { DropdownItem } from "~/types/dropdown"
interface DropdownOptions {
placement?: 'right' | 'left' | 'center';
verticality?: 'ascending' | 'descending';
width?: string | number;
minWidth?: string;
maxWidth?: string;
}
interface DropdownState {
open: boolean;
x: number;
y: number;
itemsFactory: (() => DropdownItem[]) | null;
options: DropdownOptions | null;
}
const dropdownState = reactive<DropdownState>({
open: false,
x: 0,
y: 0,
itemsFactory: null,
options: null,
})
export const useDropdown = () => {
const openDropdown = (e: MouseEvent, itemsFactory: () => DropdownItem[], options?: DropdownOptions) => {
// TODO: take in placement logic and prefered verticality, and measure the space
// available in the direction of the prefered verticality, and if it doesnt fit
// in the direction of the placement, flip the placement
const rect = (e.target as HTMLElement).getBoundingClientRect()
dropdownState.x = rect.left
dropdownState.y = rect.bottom
if (options) {
switch (options.placement) {
case 'right':
dropdownState.x = rect.right
dropdownState.y = rect.bottom
break;
case 'left':
dropdownState.x = rect.left
dropdownState.y = rect.bottom
break;
case 'center':
dropdownState.x = rect.left + rect.width / 2
dropdownState.y = rect.bottom
break;
}
switch (options.verticality) {
case 'ascending':
dropdownState.y = rect.top;
break;
case 'descending':
dropdownState.y = rect.bottom;
break;
}
}
dropdownState.itemsFactory = itemsFactory
dropdownState.options = options ?? null
dropdownState.open = true
}
const closeDropdown = () => {
dropdownState.open = false
dropdownState.itemsFactory = null
dropdownState.options = null
}
return {
dropdownState,
openDropdown,
closeDropdown,
}
}