From 9d943c118230737d9defb7532ad4e1e76c186b6d Mon Sep 17 00:00:00 2001 From: Chris Kruining Date: Tue, 7 Jan 2025 14:04:02 +0100 Subject: [PATCH] refactor menu to use dropdown component --- src/components/dropdown/index.tsx | 4 +- src/components/select/index.tsx | 3 +- src/features/menu/index.module.css | 24 ++-------- src/features/menu/index.tsx | 73 ++++++++---------------------- 4 files changed, 25 insertions(+), 79 deletions(-) diff --git a/src/components/dropdown/index.tsx b/src/components/dropdown/index.tsx index 4179a03..1cf50a8 100644 --- a/src/components/dropdown/index.tsx +++ b/src/components/dropdown/index.tsx @@ -21,8 +21,6 @@ export function Dropdown(props: DropdownProps) { const [dialog, setDialog] = createSignal(); const [open, setOpen] = createSignal(props.open ?? false); - const showCaret = createMemo(() => props.showCaret ?? true); - createEffect(() => { dialog()?.[open() ? 'showPopover' : 'hidePopover'](); }); @@ -42,7 +40,7 @@ export function Dropdown(props: DropdownProps) { diff --git a/src/components/select/index.tsx b/src/components/select/index.tsx index d22b4fa..6a2caea 100644 --- a/src/components/select/index.tsx +++ b/src/components/select/index.tsx @@ -19,6 +19,7 @@ export function Select(props: SelectProps) { const [key, setKey] = createSignal(props.value); const [query, setQuery] = createSignal(''); + const showCaret = createMemo(() => props.showCaret ?? true); const values = createMemo(() => { let entries = Object.entries(props.values) as [K, T][]; const filter = props.filter; @@ -43,7 +44,7 @@ export function Select(props: SelectProps) { } } - return + return
setQuery(e.target.value)} /> diff --git a/src/features/menu/index.module.css b/src/features/menu/index.module.css index bf19523..0286ef8 100644 --- a/src/features/menu/index.module.css +++ b/src/features/menu/index.module.css @@ -1,4 +1,6 @@ .root { + display: grid; + grid-auto-flow: column; margin: 0; padding: 0; @@ -7,7 +9,7 @@ } } -.item { +:is(.item, .child > button) { padding: var(--padding-m) var(--padding-l); background-color: inherit; @@ -22,26 +24,12 @@ } } -.child { - position: fixed; - inset-inline-start: anchor(self-start); - inset-block-start: anchor(end); - +.child > dialog { grid-template-columns: auto auto; place-content: start; gap: var(--padding-m); padding: var(--padding-m) 0; - inline-size: max-content; - - background-color: var(--surface-500); - border: 1px solid var(--surface-300); - border-block-start-width: 0; - margin: unset; - - &:popover-open { - display: grid; - } & > .separator { grid-column: span 2; @@ -62,8 +50,4 @@ background-color: var(--surface-600); } } -} - -:popover-open + .item { - background-color: var(--surface-500); } \ No newline at end of file diff --git a/src/features/menu/index.tsx b/src/features/menu/index.tsx index 176b0f3..0ad9e3c 100644 --- a/src/features/menu/index.tsx +++ b/src/features/menu/index.tsx @@ -3,6 +3,7 @@ import { Portal } from "solid-js/web"; import { createStore } from "solid-js/store"; import { CommandType, Command, useCommands } from "../command"; import css from "./index.module.css"; +import { Dropdown, DropdownApi } from "~/components/dropdown"; export interface MenuContextType { ref: Accessor; @@ -102,77 +103,39 @@ const Separator: Component = (props) => { const Root: ParentComponent<{}> = (props) => { const menuContext = useMenu(); const commandContext = useCommands(); - const [current, setCurrent] = createSignal(); const items = children(() => props.children).toArray() as unknown as (Item | ItemWithChildren)[]; - menuContext.addItems(items) - - const close = () => { - const el = current(); - - if (el) { - el.hidePopover(); - - setCurrent(undefined); - } - }; - - const onExecute = (command?: CommandType) => { - return command - ? (e: Event) => { - close(); - - return commandContext?.execute(command, e); - } - : () => { } - }; - - const Child: Component<{ command: CommandType }> = (props) => { - return - }; + menuContext.addItems(items); return { item => { - item => <> -
{ - if (e.newState === 'open' && e.target !== null) { - return setCurrent(e.target as HTMLElement); - } - }} - > + item => { + const [dropdown, setDropdown] = createSignal(); + + return { child => { - item => + item => }
-
- }
-
- - - +
}
+ ; + } } { - item => + item => } }