Skip to content

Commit

Permalink
refactor: improve dropdown state handling (external state) (#43)
Browse files Browse the repository at this point in the history
  • Loading branch information
hugolxt authored Dec 24, 2024
1 parent 604de55 commit e51a849
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 5 deletions.
2 changes: 0 additions & 2 deletions src/components/dapp/TransactionButton.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,6 @@ export default function TransactionButton({ tx, name, children, onExecute, ...pr
const { address: user, client, sendTransaction } = useWalletContext();

const execute = useCallback(async () => {
console.log("CLIENT", client);

if (!tx || !user || !client) return;

const hash = await sendTransaction?.([
Expand Down
17 changes: 14 additions & 3 deletions src/components/extenders/Dropdown.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,9 @@ export default function Dropdown({
if (!internalState || !onHover) return;
hideTimeout.current = setTimeout(() => {
setInternalState(false);
state?.[1]?.(false);
}, 100);
}, [internalState, onHover]);
}, [internalState, onHover, state]);

const handleMouseEnter = useCallback(() => {
if (!onHover) return;
Expand All @@ -44,7 +45,8 @@ export default function Dropdown({
hideTimeout.current = null;
}
setInternalState(true);
}, [onHover]);
state?.[1]?.(true);
}, [onHover, state]);

const cancelClose = useCallback(() => {
if (!onHover || !hideTimeout.current) return;
Expand All @@ -57,11 +59,20 @@ export default function Dropdown({
clearTimeout(hideTimeout.current);
}, [onHover]);

const toggle = useCallback(
() =>
blockEvent(() => {
setInternalState(!internalState);
state?.[1]?.(!state?.[0]);
}),
[internalState, state],
);

return (
<Popover.Root open={!state ? internalState : state?.[0]} onOpenChange={!state ? setInternalState : state?.[1]}>
<Popover.Trigger
className={className}
onClick={blockEvent(() => setInternalState(r => !r))}
onClick={toggle}
onMouseEnter={handleMouseEnter}
onMouseLeave={closeModalWithDelay}>
{children}
Expand Down

0 comments on commit e51a849

Please sign in to comment.