Skip to content

Commit

Permalink
Blur trigger when closing popover
Browse files Browse the repository at this point in the history
  • Loading branch information
kraenhansen committed Nov 13, 2024
1 parent 464e79d commit 6d19bbe
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@ type InteractivePopoverProps = {
containedElements?: string[];
containerClassName?: string;
closeButtonClassName?: string;
blurTriggerOnClose?: boolean;
} & Pick<
React.ComponentProps<typeof Popover>,
'align' | 'justify' | 'spacing' | 'popoverZIndex'
Expand All @@ -80,6 +81,7 @@ function InteractivePopover({
popoverZIndex,
containerClassName,
closeButtonClassName,
blurTriggerOnClose = false,
}: InteractivePopoverProps): React.ReactElement {
const darkMode = useDarkMode();
const triggerRef = useRef<HTMLButtonElement>(null);
Expand All @@ -91,9 +93,13 @@ function InteractivePopover({

// Return focus to the trigger when the popover is hidden.
setTimeout(() => {
triggerRef.current?.focus();
if (blurTriggerOnClose) {
triggerRef.current?.blur();
} else {
triggerRef.current?.focus();
}
});
}, [setOpen]);
}, [setOpen, blurTriggerOnClose]);

const onClickTrigger = useCallback(
(event) => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,8 @@ export default function ConnectionsFilterPopover({
[onFilterChange]
);

const excludeInactiveId = useId('Sort by');
const excludeInactiveToggleId = useId('exclude-inactive-toggle');
const excludeInactiveLabelId = useId('exclude-inactive-label');

// Add future filters to the boolean below
const isActivated = filter.excludeInactive;
Expand All @@ -76,13 +77,14 @@ export default function ConnectionsFilterPopover({
<InteractivePopover
open={open}
setOpen={setOpen}
blurTriggerOnClose
closeButtonClassName={closeButtonStyles}
containerClassName={containerStyles}
trigger={({ onClick, children, ref }) => (
<>
<Tooltip
align="right"
enabled={!open}
/* enabled={!open} */
trigger={
<IconButton
onClick={onClick}
Expand Down Expand Up @@ -116,12 +118,15 @@ export default function ConnectionsFilterPopover({
<Overline>Filter Options</Overline>
<div className={groupStyles}>
<Toggle
aria-labelledby={excludeInactiveId}
id={excludeInactiveToggleId}
aria-labelledby={excludeInactiveLabelId}
checked={filter.excludeInactive}
onChange={onExcludeInactiveChange}
size="small"
/>
<Label htmlFor={excludeInactiveId}>Show only active connections</Label>
<Label htmlFor={excludeInactiveToggleId} id={excludeInactiveLabelId}>
Show only active connections
</Label>
</div>
</InteractivePopover>
);
Expand Down

0 comments on commit 6d19bbe

Please sign in to comment.