Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/old-insects-care.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@keystar/ui': patch
---

Fix comboboxes sometimes only opening on second click
15 changes: 2 additions & 13 deletions design-system/pkg/src/combobox/Combobox.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ import React, {
RefObject,
useCallback,
useEffect,
useMemo,
useRef,
useState,
} from 'react';
Expand Down Expand Up @@ -83,7 +82,7 @@ const ComboboxBase = React.forwardRef(function ComboboxBase<T extends object>(
let buttonRef = useRef<HTMLButtonElement>(null);
let inputRef = useRef<HTMLInputElement>(null);
let listBoxRef = useRef<HTMLDivElement>(null);
let [popoverRefLikeValue, popoverRef] = useStatefulRef<HTMLDivElement>();
let popoverRef = useRef<HTMLDivElement>(null);
let fieldRef = useObjectRef(forwardedRef);

let { contains } = useFilter({ sensitivity: 'base' });
Expand All @@ -106,7 +105,7 @@ const ComboboxBase = React.forwardRef(function ComboboxBase<T extends object>(
...props,
layoutDelegate: layout,
buttonRef,
popoverRef: popoverRefLikeValue,
popoverRef,
listBoxRef,
inputRef,
menuTrigger,
Expand Down Expand Up @@ -220,16 +219,6 @@ export function usePopoverStyles(props: {
};
}

// FIXME: this is a hack to work around a requirement of react-aria. object refs
// never have the value early enough, so we need to use a stateful ref to force
// a re-render.
export function useStatefulRef<T extends HTMLElement>() {
let [current, statefulRef] = useState<T | null>(null);
return useMemo(() => {
return [{ current }, statefulRef] as const;
}, [current, statefulRef]);
}

interface ComboboxInputProps<T> extends ComboboxProps<T> {
inputProps: InputHTMLAttributes<HTMLInputElement>;
inputRef: RefObject<HTMLInputElement | HTMLTextAreaElement | null>;
Expand Down
5 changes: 2 additions & 3 deletions design-system/pkg/src/combobox/ComboboxMulti.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ import {
ComboboxEmptyState,
ComboboxInput,
usePopoverStyles,
useStatefulRef,
} from './Combobox';
import { MobileComboboxMulti } from './MobileComboboxMulti';
import { ComboboxMultiProps } from './types';
Expand Down Expand Up @@ -56,7 +55,7 @@ const ComboboxMultiBase = React.forwardRef(function ComboboxMultiBase<
let buttonRef = useRef<HTMLButtonElement>(null);
let inputRef = useRef<HTMLInputElement>(null);
let listBoxRef = useRef<HTMLDivElement>(null);
let [popoverRefLikeValue, popoverRef] = useStatefulRef<HTMLDivElement>();
let popoverRef = useRef<HTMLDivElement>(null);
let fieldRef = useObjectRef(forwardedRef);

let layoutDelegate = useListBoxLayout();
Expand All @@ -75,7 +74,7 @@ const ComboboxMultiBase = React.forwardRef(function ComboboxMultiBase<
inputRef,
layoutDelegate,
listBoxRef,
popoverRef: popoverRefLikeValue,
popoverRef,
},
state
);
Expand Down
Loading