Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(popover): close popover on scroll #3414

Merged
merged 4 commits into from
Jul 6, 2024
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/sweet-flowers-dress.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@nextui-org/popover": patch
---

close popover on scroll (#3265)
33 changes: 33 additions & 0 deletions packages/components/popover/__tests__/popover.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -314,4 +314,37 @@ describe("Popover", () => {
// assert that the popover is still open
expect(popover).toHaveAttribute("aria-expanded", "true");
});

it("should close popover on scroll", async () => {
const wrapper = render(
<Popover>
<PopoverTrigger>
<Button data-testid="popover">Open popover</Button>
</PopoverTrigger>
<PopoverContent>
<Select data-testid="select" label="Select country">
<SelectItem key="argentina">Argentina</SelectItem>
<SelectItem key="venezuela">Venezuela</SelectItem>
<SelectItem key="brazil">Brazil</SelectItem>
</Select>
</PopoverContent>
</Popover>,
);

const popover = wrapper.getByTestId("popover");

// open popover
await act(async () => {
await userEvent.click(popover);
});

// assert that the popover is open
expect(popover).toHaveAttribute("aria-expanded", "true");

// scroll it
fireEvent.scroll(document.body);

// assert that the popover is closed
expect(popover).toHaveAttribute("aria-expanded", "false");
});
});
2 changes: 1 addition & 1 deletion packages/components/popover/src/use-aria-popover.ts
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@ export function useReactAriaPopover(
containerPadding,
placement: toReactAriaPlacement(placementProp),
offset: showArrow ? offset + 3 : offset,
onClose: () => {},
onClose: isNonModal ? state.close : () => {},
});

useSafeLayoutEffect(() => {
Expand Down
73 changes: 73 additions & 0 deletions packages/components/select/stories/select.stories.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -627,6 +627,71 @@ const WithReactHookFormTemplate = (args: SelectProps) => {
);
};

const ScrollableContainerTemplate = (args: SelectProps) => {
const categories = [
{
target: "Animals",
items: [
{name: "Lion", emoji: "🦁"},
{name: "Tiger", emoji: "🐅"},
{name: "Elephant", emoji: "🐘"},
{name: "Kangaroo", emoji: "🦘"},
{name: "Panda", emoji: "🐼"},
{name: "Giraffe", emoji: "🦒"},
{name: "Zebra", emoji: "🦓"},
{name: "Cheetah", emoji: "🐆"},
],
},
{
target: "Birds",
items: [
{name: "Eagle", emoji: "🦅"},
{name: "Parrot", emoji: "🦜"},
{name: "Penguin", emoji: "🐧"},
{name: "Ostrich", emoji: "🦢"},
{name: "Peacock", emoji: "🦚"},
{name: "Swan", emoji: "🦢"},
{name: "Falcon", emoji: "🦅"},
{name: "Flamingo", emoji: "🦩"},
],
},
];
const DEFAULT_CATEGORY = "Animals";

return (
<>
<form className="h-full overflow-auto">
<div className="flex justify-between h-[1500px]">
<div className="flex items-center gap-2">
<div className="flex w-full flex-wrap gap-4 md:flex-nowrap">
<Select
aria-label="Favourite Animals"
className="w-52"
defaultSelectedKeys={[DEFAULT_CATEGORY]}
label="Category"
name="Category"
{...args}
>
{categories.map((category, idx, arr) => (
<SelectSection
key={category.target}
showDivider={idx !== arr.length - 1}
title={category.target}
>
{category.items.map((item) => (
<SelectItem key={item.name}>{`${item.emoji} ${item.name}`}</SelectItem>
))}
</SelectSection>
))}
</Select>
</div>
</div>
</div>
</form>
</>
);
};

export const Default = {
render: MirrorTemplate,

Expand Down Expand Up @@ -839,6 +904,14 @@ export const WithReactHookForm = {
},
};

export const WithScrollableContainer = {
render: ScrollableContainerTemplate,

args: {
...defaultProps,
},
};

export const Controlled = {
render: ControlledTemplate,

Expand Down