Skip to content

Commit

Permalink
add caret to combobox
Browse files Browse the repository at this point in the history
  • Loading branch information
Chris Kruining committed Jan 7, 2025
1 parent 1ae0e14 commit 7f71f83
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 0 deletions.
17 changes: 17 additions & 0 deletions src/components/combobox/index.module.css
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
}

.button {
position: relative;
display: grid;
grid-template-columns: inherit;
place-items: center start;
Expand All @@ -23,6 +24,22 @@
font-size: 1rem;

cursor: pointer;

&:hover {
background-color: var(--surface-700);
}

&:has(> .caret) {
padding-inline-end: calc(1em + (2 * var(--padding-m)));
}

& > .caret {
position: absolute;
inset-inline-end: var(--padding-m);
inset-block-start: 50%;
translate: 0 -50%;
inline-size: 1em;
}
}

.dialog {
Expand Down
8 changes: 8 additions & 0 deletions src/components/combobox/index.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { createMemo, createSignal, For, JSX, Setter, createEffect, Show } from "solid-js";
import css from './index.module.css';
import { FaSolidAngleDown } from "solid-icons/fa";

interface ComboBoxProps<T, K extends string> {
id: string;
Expand All @@ -8,6 +9,7 @@ interface ComboBoxProps<T, K extends string> {
setValue?: Setter<K>;
values: Record<K, T>;
open?: boolean;
showCaret?: boolean;
children: (key: K, value: T) => JSX.Element;
filter?: (query: string, key: K, value: T) => boolean;
}
Expand All @@ -30,6 +32,8 @@ export function ComboBox<T, K extends string>(props: ComboBoxProps<T, K>) {
return entries;
});

const showCaret = createMemo(() => props.showCaret ?? true);

createEffect(() => {
props.setValue?.(() => value());
});
Expand All @@ -41,6 +45,10 @@ export function ComboBox<T, K extends string>(props: ComboBoxProps<T, K>) {
return <section class={`${css.box} ${props.class}`}>
<button id={`${props.id}_button`} popoverTarget={`${props.id}_dialog`} class={css.button}>
{props.children(value(), props.values[value()])}

<Show when={showCaret()}>
<FaSolidAngleDown class={css.caret} />
</Show>
</button>

<dialog ref={setDialog} id={`${props.id}_dialog`} anchor={`${props.id}_button`} popover class={css.dialog} onToggle={e => setOpen(e.newState === 'open')}>
Expand Down
1 change: 1 addition & 0 deletions src/features/i18n/picker.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ export const LocalePicker: Component<LocalePickerProps> = (props) => {
value={locale()}
setValue={setLocale}
values={locales}
showCaret={false}
>
{(locale, { flag, label }) => <Dynamic component={flag} lang={locale} aria-label={label} class={css.flag} />}
</ComboBox>
Expand Down

0 comments on commit 7f71f83

Please sign in to comment.