nextui icon indicating copy to clipboard operation
nextui copied to clipboard

[BUG] Popover not working open controll When using backdrop option on touch devie

Open rom-sky-blue opened this issue 2 years ago • 3 comments

NextUI Version

2.2.9

Describe the bug

I found a bug regarding popover not working open controll When using backdrop option on touch devie. I wanted to place a close button inside the popover and tried to implement it using useState. It worked well on a PC, but did not work properly on a touch device. This only happens when the Backdrop option is set.

Your Example Website or App

No response

Steps to Reproduce the Bug or Issue

A sample code is shown below.

import React from 'react'
import {
  Popover,
  PopoverTrigger,
  PopoverContent,
  Button,
} from '@nextui-org/react'

export default function App() {
  const [isOpen, setIsOpen] = React.useState(false)

  return (
    <div className='flex flex-col gap-2'>
      <Popover
        isOpen={isOpen}
        onOpenChange={(open) => {
          console.log('onOpenChange argument is', open)
          setIsOpen(open)
        }}
        backdrop='opaque'
      >
        <PopoverTrigger>
          <Button>OpenState: {isOpen ? 'Open' : 'Close'}</Button>
        </PopoverTrigger>
        <PopoverContent>
          <div className='px-1 py-2'>
            <div className='text-small font-bold'>Popover Content</div>
            <Button
              color='danger'
              variant='light'
              aria-label='Close'
              onPress={() => {
                setIsOpen(false)
              }}
            >
              Close
            </Button>
          </div>
        </PopoverContent>
      </Popover>
    </div>
  )
}

Expected behavior

When processing the close button with the content in the popover the next time the popover is opened, it is closed immediately. When I check in the console, I see that onOpenChange is called twice.

Screenshots or Videos

No response

Operating System Version

macOS

Browser

Chrome

rom-sky-blue avatar Jan 20 '24 02:01 rom-sky-blue

@rom-sky-blue While it may not align with the creator's intention, I was able to achieve the desired functionality temporarily using the following method:

import {
  Button,
  Popover,
  PopoverContent,
  PopoverTrigger,
} from "@nextui-org/react";
import ClosedButton from "./closedButton";

export default function App() {
  return (
    <div className="flex flex-col gap-2">
      <Popover backdrop="opaque">
        <PopoverTrigger>
          <Button>Open</Button>
        </PopoverTrigger>
        <PopoverContent>
          <div className="px-1 py-2">
            <div className="text-small font-bold">Popover Content</div>
            <ClosedButton />
          </div>
        </PopoverContent>
      </Popover>
    </div>
  );
}
// closedButton.tsx
import { Button, usePopoverContext } from "@nextui-org/react";

export default function ClosedButton() {
  const { getBackdropProps } = usePopoverContext();
  return (
    <Button
      color="danger"
      variant="light"
      aria-label="Close"
      onClick={(e) => getBackdropProps()?.onClick?.(e)}
    >
      Close
    </Button>
  );
}

Upon reviewing the source code, it seems that the isOpen flag doesn't correctly close the popover. Additionally, the close process is quite complex. Therefore, since there is a method within the internal React.Context used by the component that correctly handles the close process, calling this method from a custom close button allows for proper closing of the popover.

※ Please consider that this uses internal functions not documented, and future updates might cause this solution to no longer work correctly.

kght6123 avatar Jan 20 '24 16:01 kght6123

There is exactly the same issue with Dropdown (version 2.2.10).

rkrajewski avatar Apr 02 '24 14:04 rkrajewski

Closing in favor of v3. If the issue is reproducible in v3, please create a new one with a minimal reproducible example.

wingkwong avatar Apr 03 '26 13:04 wingkwong