From 444d320dbc146399eb937c219ce983d427675425 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D5=A1=D3=84=D5=A1?= Date: Sat, 6 Jul 2024 16:04:56 +0800 Subject: [PATCH] fix(popover): close popover on scroll (#3414) * fix(popover): close popover on scroll * feat(popover): add "should close popover on scroll" test * feat(changeset): add changeset * feat(select): add ScrollableContainerTemplate --- .changeset/sweet-flowers-dress.md | 5 ++ .../popover/__tests__/popover.test.tsx | 33 +++++++++ .../popover/src/use-aria-popover.ts | 2 +- .../select/stories/select.stories.tsx | 73 +++++++++++++++++++ 4 files changed, 112 insertions(+), 1 deletion(-) create mode 100644 .changeset/sweet-flowers-dress.md diff --git a/.changeset/sweet-flowers-dress.md b/.changeset/sweet-flowers-dress.md new file mode 100644 index 0000000000..9aa55f3e95 --- /dev/null +++ b/.changeset/sweet-flowers-dress.md @@ -0,0 +1,5 @@ +--- +"@nextui-org/popover": patch +--- + +close popover on scroll (#3265) diff --git a/packages/components/popover/__tests__/popover.test.tsx b/packages/components/popover/__tests__/popover.test.tsx index 53d463a699..a73021d666 100644 --- a/packages/components/popover/__tests__/popover.test.tsx +++ b/packages/components/popover/__tests__/popover.test.tsx @@ -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( + + + + + + + + , + ); + + 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"); + }); }); diff --git a/packages/components/popover/src/use-aria-popover.ts b/packages/components/popover/src/use-aria-popover.ts index f9f323f5b1..d8f1c78067 100644 --- a/packages/components/popover/src/use-aria-popover.ts +++ b/packages/components/popover/src/use-aria-popover.ts @@ -102,7 +102,7 @@ export function useReactAriaPopover( containerPadding, placement: toReactAriaPlacement(placementProp), offset: showArrow ? offset + 3 : offset, - onClose: () => {}, + onClose: isNonModal ? state.close : () => {}, }); useSafeLayoutEffect(() => { diff --git a/packages/components/select/stories/select.stories.tsx b/packages/components/select/stories/select.stories.tsx index a30224cf3e..0a58ad10a3 100644 --- a/packages/components/select/stories/select.stories.tsx +++ b/packages/components/select/stories/select.stories.tsx @@ -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 ( + <> +
+
+
+
+ +
+
+
+
+ + ); +}; + export const Default = { render: MirrorTemplate, @@ -839,6 +904,14 @@ export const WithReactHookForm = { }, }; +export const WithScrollableContainer = { + render: ScrollableContainerTemplate, + + args: { + ...defaultProps, + }, +}; + export const Controlled = { render: ControlledTemplate,