TootFilterButton: show the menu in the click pos

This commit is contained in:
thislight 2024-11-03 18:24:10 +08:00
parent c69d54e171
commit 06e988e0e5
No known key found for this signature in database
GPG key ID: FCFE5192241CCD4E
2 changed files with 18 additions and 21 deletions

View file

@ -13,10 +13,12 @@ import {
animateShrinkToTopRight, animateShrinkToTopRight,
} from "../platform/anim"; } from "../platform/anim";
type Anchor = Pick<DOMRect, "top" | "left" | "right">
type Props = { type Props = {
open?: boolean; open?: boolean;
onClose?: JSX.EventHandlerUnion<HTMLDialogElement, Event>; onClose?: JSX.EventHandlerUnion<HTMLDialogElement, Event>;
anchor: () => DOMRect; anchor: () => Anchor;
}; };
function px(n?: number) { function px(n?: number) {

View file

@ -1,15 +1,5 @@
import { import { Button, MenuItem, Checkbox, ListItemText } from "@suid/material";
Button, import { createMemo, createSignal, createUniqueId, For } from "solid-js";
MenuItem,
Checkbox,
ListItemText,
} from "@suid/material";
import {
createMemo,
createSignal,
createUniqueId,
For,
} from "solid-js";
import Menu from "../material/Menu"; import Menu from "../material/Menu";
import { FilterList, FilterListOff } from "@suid/icons-material"; import { FilterList, FilterListOff } from "@suid/icons-material";
@ -68,9 +58,20 @@ function TootFilterButton<F extends Record<string, string>>(props: Props<F>) {
); );
}; };
let anchor: { left: number; top: number; right: number };
const onClick = (event: MouseEvent) => {
anchor = {
left: event.clientX,
right: event.clientX,
top: event.clientY,
};
setOpen(true);
};
return ( return (
<> <>
<Button size="large" onClick={[setOpen, true]} id={buttonId}> <Button size="large" onClick={onClick} id={buttonId}>
{appliedKeys().length === optionKeys().length ? ( {appliedKeys().length === optionKeys().length ? (
<FilterListOff /> <FilterListOff />
) : ( ) : (
@ -79,13 +80,7 @@ function TootFilterButton<F extends Record<string, string>>(props: Props<F>) {
<span style={{ "margin-left": "0.5em" }}>{text()}</span> <span style={{ "margin-left": "0.5em" }}>{text()}</span>
</Button> </Button>
<Menu <Menu open={open()} onClose={[setOpen, false]} anchor={() => anchor}>
open={open()}
onClose={[setOpen, false]}
anchor={() =>
document.getElementById(buttonId)!.getBoundingClientRect()
}
>
<For each={Object.keys(props.options)}> <For each={Object.keys(props.options)}>
{(item, idx) => ( {(item, idx) => (
<> <>