Open the Share access menu above the dialog so both options can be picked.

The listbox was clipped inside the sheet, sat at 24px, and ignored arrow keys, so Anyone with the link could not be chosen by mouse or touch.
This commit is contained in:
adilallo committed 2026-11-21 11:41:05 -07:00
1 parent 2dfc365dbd
commit 3a264ac2df
9 files changed
+264 -56

No files matched your search

@@ -2,6 +2,7 @@
import React, {
Children,
type CSSProperties,
type ReactElement,
type ReactNode,
forwardRef,
@@ -12,11 +13,13 @@ import React, {
memo,
useImperativeHandle,
useEffect,
useLayoutEffect,
useMemo,
} from "react";
import { useClickOutside } from "../../../hooks";
import { useTranslation } from "../../../contexts/MessagesContext";
import { SelectInputView } from "./SelectInput.view";
import type { SelectInputProps } from "./SelectInput.types";
import type { SelectInputProps, SelectOptionData } from "./SelectInput.types";
/**
* Figma: "Control / SelectInput". Custom-styled select dropdown
@@ -70,11 +73,31 @@ const SelectInputContainer = forwardRef<HTMLButtonElement, SelectInputProps>(
const generatedId = useId();
const selectId = id || `select-input-${generatedId}`;
const labelId = `${selectId}-label`;
const listboxId = `${selectId}-listbox`;
const [isOpen, setIsOpen] = useState(false);
const [selectedValue, setSelectedValue] = useState(value || "");
const [highlightedIndex, setHighlightedIndex] = useState(0);
const [menuStyle, setMenuStyle] = useState<CSSProperties | null>(null);
const selectRef = useRef<HTMLButtonElement>(null);
const menuRef = useRef<HTMLDivElement>(null);
const optionItems = useMemo((): SelectOptionData[] => {
if (options && Array.isArray(options)) return options;
return Children.toArray(children).flatMap((child) => {
if (!React.isValidElement(child) || child.type !== "option") return [];
const optionProps = child.props as {
value: string;
children: ReactNode;
};
return [
{
value: optionProps.value,
label: String(optionProps.children),
},
];
});
}, [options, children]);
// Internal state management: track if focused and how (mouse vs keyboard)
const [isFocused, setIsFocused] = useState(false);
const [focusMethod, setFocusMethod] = useState<"mouse" | "keyboard" | null>(
@@ -99,14 +122,69 @@ const SelectInputContainer = forwardRef<HTMLButtonElement, SelectInputProps>(
() => selectRef.current as HTMLButtonElement | null,
);
const closeMenu = useCallback(() => {
setIsOpen(false);
setMenuStyle(null);
}, []);
// Handle click outside to close menu
useClickOutside([menuRef, selectRef], () => setIsOpen(false), isOpen);
useClickOutside([menuRef, selectRef], closeMenu, isOpen);
const indexForValue = useCallback(
(current: string) => {
const index = optionItems.findIndex((option) => option.value === current);
return index >= 0 ? index : 0;
},
[optionItems],
);
const openMenu = useCallback(() => {
const trigger = selectRef.current?.getBoundingClientRect();
setMenuStyle({
top: (trigger?.bottom ?? 0) + 4,
left: trigger?.left ?? 0,
width: Math.max(trigger?.width ?? 0, 200),
});
setHighlightedIndex(indexForValue(selectedValue));
setIsOpen(true);
}, [indexForValue, selectedValue]);
useLayoutEffect(() => {
if (!isOpen) {
setMenuStyle(null);
return;
}
const update = () => {
const trigger = selectRef.current;
if (!trigger) return;
const rect = trigger.getBoundingClientRect();
const menuHeight = menuRef.current?.offsetHeight ?? optionItems.length * 44 + 8;
const gap = 4;
const openUp =
rect.bottom + gap + menuHeight > window.innerHeight &&
rect.top > menuHeight + gap;
setMenuStyle({
top: openUp
? Math.max(8, rect.top - menuHeight - gap)
: rect.bottom + gap,
left: rect.left,
width: Math.max(rect.width, 200),
});
};
update();
window.addEventListener("resize", update);
window.addEventListener("scroll", update, true);
return () => {
window.removeEventListener("resize", update);
window.removeEventListener("scroll", update, true);
};
}, [isOpen, optionItems.length, highlightedIndex]);
// Handle option selection
const handleOptionSelect = useCallback(
(optionValue: string, optionText: string) => {
setSelectedValue(optionValue);
setIsOpen(false);
closeMenu();
if (onChange) {
onChange({ target: { value: optionValue, text: optionText } });
}
@@ -114,7 +192,7 @@ const SelectInputContainer = forwardRef<HTMLButtonElement, SelectInputProps>(
selectRef.current.focus();
}
},
[onChange],
[closeMenu, onChange],
);
// Handle mouse down to detect mouse clicks
@@ -126,24 +204,74 @@ const SelectInputContainer = forwardRef<HTMLButtonElement, SelectInputProps>(
// Handle select button click
const handleSelectClick = useCallback(() => {
if (!disabled) {
setIsOpen(!isOpen);
if (disabled) return;
if (isOpen) {
closeMenu();
return;
}
}, [disabled, isOpen]);
openMenu();
}, [closeMenu, disabled, isOpen, openMenu]);
// Handle keyboard navigation
const handleKeyDown = useCallback(
(e: React.KeyboardEvent<HTMLButtonElement>) => {
if (disabled) return;
if (e.key === "ArrowDown") {
e.preventDefault();
if (!isOpen) {
openMenu();
return;
}
setHighlightedIndex((index) =>
Math.min(index + 1, Math.max(optionItems.length - 1, 0)),
);
return;
}
if (e.key === "ArrowUp") {
e.preventDefault();
if (!isOpen) {
openMenu();
return;
}
setHighlightedIndex((index) => Math.max(index - 1, 0));
return;
}
if (e.key === "Home" && isOpen) {
e.preventDefault();
setHighlightedIndex(0);
return;
}
if (e.key === "End" && isOpen) {
e.preventDefault();
setHighlightedIndex(Math.max(optionItems.length - 1, 0));
return;
}
if (e.key === "Enter" || e.key === " ") {
e.preventDefault();
setIsOpen(!isOpen);
} else if (e.key === "Escape") {
setIsOpen(false);
if (!isOpen) {
openMenu();
return;
}
const option = optionItems[highlightedIndex];
if (option) {
handleOptionSelect(option.value, option.label);
}
return;
}
if (e.key === "Escape") {
closeMenu();
}
},
[disabled, isOpen],
[
closeMenu,
disabled,
handleOptionSelect,
highlightedIndex,
isOpen,
openMenu,
optionItems,
],
);
// Handle focus to detect mouse vs keyboard
@@ -237,6 +365,9 @@ const SelectInputContainer = forwardRef<HTMLButtonElement, SelectInputProps>(
onOptionClick={handleOptionSelect}
selectRef={selectRef}
menuRef={menuRef}
listboxId={listboxId}
highlightedIndex={highlightedIndex}
menuStyle={menuStyle}
ariaLabelledby={shouldShowLabel ? labelId : undefined}
ariaInvalid={error}
asterisk={asterisk}