Slight cleanup

This commit is contained in:
Gregory Schier
2025-01-13 16:59:39 -08:00
parent 49f5e980de
commit db64b54c79
2 changed files with 24 additions and 31 deletions
+16 -22
View File
@@ -12,11 +12,11 @@ import type {
SetStateAction, SetStateAction,
} from 'react'; } from 'react';
import React, { import React, {
useEffect,
Children, Children,
cloneElement, cloneElement,
forwardRef, forwardRef,
useCallback, useCallback,
useEffect,
useImperativeHandle, useImperativeHandle,
useMemo, useMemo,
useRef, useRef,
@@ -101,35 +101,29 @@ export const Dropdown = forwardRef<DropdownRef, DropdownProps>(function Dropdown
setOpenId((prevId) => { setOpenId((prevId) => {
const prevIsOpen = prevId === id; const prevIsOpen = prevId === id;
const newIsOpen = typeof o === 'function' ? o(prevIsOpen) : o; const newIsOpen = typeof o === 'function' ? o(prevIsOpen) : o;
return newIsOpen ? id : null; // Set global atom to current ID to signify open state
});
},
[id, setOpenId],
);
if (newIsOpen) { useEffect(() => {
onOpen?.(); if (isOpen) {
// Persist background color of button until we close the dropdown // Persist background color of button until we close the dropdown
buttonRef.current!.style.backgroundColor = window buttonRef.current!.style.backgroundColor = window
.getComputedStyle(buttonRef.current!) .getComputedStyle(buttonRef.current!)
.getPropertyValue('background-color'); .getPropertyValue('background-color');
onOpen?.();
} else { } else {
onClose?.(); onClose?.();
buttonRef.current?.focus(); // Focus button
// Reset background color of button buttonRef.current!.style.backgroundColor = ''; // Clear persisted BG
buttonRef.current!.style.backgroundColor = '';
} }
// Set to different value when opened and closed to force it to update. This is to force // Set to different value when opened and closed to force it to update. This is to force
// <Menu/> to reset its selected-index state, which it does when this prop changes // <Menu/> to reset its selected-index state, which it does when this prop changes
setDefaultSelectedIndex(newIsOpen ? -1 : null); setDefaultSelectedIndex(isOpen ? -1 : null);
}, [isOpen, onClose, onOpen]);
return newIsOpen ? id : null; // Set global atom to current ID to signify open state
});
},
[id, onClose, onOpen, setOpenId],
);
const handleClose = useCallback(() => {
setIsOpen(false);
buttonRef.current?.focus();
}, [setIsOpen]);
// Pull into variable so linter forces us to add it as a hook dep to useImperativeHandle. If we don't, // Pull into variable so linter forces us to add it as a hook dep to useImperativeHandle. If we don't,
// the ref will not update when menuRef updates, causing stale callback state to be used. // the ref will not update when menuRef updates, causing stale callback state to be used.
@@ -148,10 +142,10 @@ export const Dropdown = forwardRef<DropdownRef, DropdownProps>(function Dropdown
setIsOpen(true); setIsOpen(true);
}, },
close() { close() {
handleClose(); setIsOpen(false);
}, },
}), }),
[handleClose, isOpen, setIsOpen, menuRefCurrent], [isOpen, setIsOpen, menuRefCurrent],
); );
useHotKey(hotKeyAction ?? null, () => { useHotKey(hotKeyAction ?? null, () => {
@@ -199,7 +193,7 @@ export const Dropdown = forwardRef<DropdownRef, DropdownProps>(function Dropdown
defaultSelectedIndex={defaultSelectedIndex} defaultSelectedIndex={defaultSelectedIndex}
items={items} items={items}
triggerShape={triggerRect ?? null} triggerShape={triggerRect ?? null}
onClose={handleClose} onClose={() => setIsOpen(false)}
isOpen={isOpen} isOpen={isOpen}
/> />
</> </>
+2 -3
View File
@@ -44,9 +44,8 @@ export function SegmentedControl<T extends string>({ value, onChange, options, n
return ( return (
<IconButton <IconButton
size="xs" size="xs"
variant={isActive ? "border" : "solid"} variant="solid"
// color={isActive ? "secondary": "default"} color={isActive ? "secondary" : "default"}
color={isActive ? "default" : "default"}
role="radio" role="radio"
event={{ id: name, value: String(o.value) }} event={{ id: name, value: String(o.value) }}
tabIndex={isSelected ? 0 : -1} tabIndex={isSelected ? 0 : -1}