nextui-org / nextui

🚀 Beautiful, fast and modern React UI library.
https://nextui.org
MIT License
22.19k stars 1.56k forks source link

[BUG] - Modal does not open on mobile when the trigger button is inside a Navbar #1796

Open mauriciabad opened 1 year ago

mauriciabad commented 1 year ago

NextUI Version

2.1.13

Describe the bug

Given a button to open a modal placed inside a Navbar and uses the onPress event, when taped from a mobile the modal doesn't open.

The onPress works as expected (you can put a console.log to test it). It's weird because if the onOpen function is placed inside the onClick event, then it works.

If the button is placed outside the Navbar it works just fine.

If the modal has the prop disableAnimation it works just fine. So it looks like it's a problem with AnimatePresence from framer motion.

If you do a long press, it works, it doesn't when it is a normal (quick) tap. And if you use the onPressStart event, the modal animation starts when you press, but if you release the button after the animation finished, it goes backwards and doesn't open the modal, but if you wait for the animation to end, it stays open.

I also noticed that when running the documentation's example in codesandbox.io, it doesn't work even without being inside the Navbar.

This is a minimal example: Open in CodeSandbox

'use client'

import React from "react"
import { Button } from '@nextui-org/button'
import {
  Modal,
  ModalBody,
  ModalContent,
  ModalHeader,
  useDisclosure,
} from '@nextui-org/modal'
import { Navbar, NavbarContent, NavbarItem } from '@nextui-org/navbar'

export default function App() {
  const { isOpen, onOpen, onOpenChange } = useDisclosure()
  return (
    <>
      <Navbar isBordered>
        <NavbarContent justify="end">
          <NavbarItem>
            <Button onPress={onOpen}>Not working on mobile</Button>
            <Modal isOpen={isOpen} onOpenChange={onOpenChange}>
              <ModalContent>
                {() => (
                  <>
                    <ModalHeader>Test modal</ModalHeader>
                    <ModalBody>Test modal</ModalBody>
                  </>
                )}
              </ModalContent>
            </Modal>
            <Button onClick={onOpen}>Working</Button>
          </NavbarItem>
        </NavbarContent>
      </Navbar>

      <Button onPress={onOpen}>Working</Button>
    </>
  )
}

Your Example Website or App

https://codesandbox.io/p/sandbox/hxgdz8?file=%2FApp.jsx%3A7%2C13&utm_medium=sandpack

Steps to Reproduce the Bug or Issue

Open the page from a mobile and tap the button inside the navbar.

Alternatively, you can open the devtools and emulate a touch device.

Expected behavior

Modal is opened.

Screenshots or Videos

https://github.com/nextui-org/nextui/assets/12821361/646b7bd6-ec18-4d40-8990-2a692a396690

Operating System Version

Android

Browser

Chrome

mauriciabad commented 1 year ago

If someone encounters the same problem, I found 2 workarounds.

  1. Instead of onPress use onClick in the <Button>.
  2. Add the disableAnimation to the <Modal>
timomedia commented 1 year ago

I also got the same error

myxtype commented 8 months ago

me too

ETOPS7 commented 8 months ago

The same problem on mobiles! instead of onPress helped onClick

mozart25 commented 5 months ago

me too. onclick working thx

dragos99 commented 1 month ago

Same here. However, With onClick the buttons does not support keyboard events and we need to add them manually.