Skip to content
New issue

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

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

Already on GitHub? # to your account

Add new CloseButton component and useClose hook #3096

Merged
merged 6 commits into from
Apr 12, 2024

Conversation

RobinMalfait
Copy link
Member

@RobinMalfait RobinMalfait commented Apr 12, 2024

This PR adds a new CloseButton component to Headless UI as well as a new useClose hook.

These can be used inside a Dialog, Disclosure or Popover component and will close it when used.

Both the button and the hook are forgiving, meaning if you use them outside of a "closable" component they will do nothing.

CloseButton component

Use the CloseButton component within a "closable" component to automatically close it when the button is clicked:

import { CloseButton } from '@headlessui/react'

<MyDialog>
  /* ... */
  <CloseButton>
    Close
  </CloseButton>
<MyDialog>

This component also supports the as prop allowing you to wrap an existing component with it:

import { CloseButton } from '@headlessui/react'
import { MyButton, MyDialog } from './components'

<MyDialog>
  /* ... */
  <CloseButton as={MyButton}>
    Close
  </CloseButton>
<MyDialog>

useClose Hook

The useClose hook is useful in situations where you need more control over when the "closable" component is closed. For example, maybe you want to close a dialog when you submit a form within it:

import { Dialog, useClose } from '@headlessui/react'

export function MySearchForm() {
  let close = useClose()

  return (
    <form
      onSubmit={(event) => {
        event.preventDefault()
        /* perform search... */          
        close()
      }}
    >
      <input type="search" />
    </form>
    /* ... */
  )
}

export function MyDialog() {
  return (
    <Dialog>
      <MySearchForm />
      /* ... */
    </Dialog>
  )
}

Copy link

vercel bot commented Apr 12, 2024

The latest updates on your projects. Learn more about Vercel for Git ↗︎

Name Status Preview Comments Updated (UTC)
headlessui-react ✅ Ready (Inspect) Visit Preview 💬 Add feedback Apr 12, 2024 9:15am
headlessui-vue ✅ Ready (Inspect) Visit Preview 💬 Add feedback Apr 12, 2024 9:15am

Copy link
Member

@reinink reinink left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This looks good to me @RobinMalfait! 👍

@Elieserlaguerre
Copy link

I'm trying to use the CloseButton component in my Disclosure panel, next.js app, but for some reason, I keep getting an error message when I try to import the component from @headlessui/react. the error message says: Element type is invalid: expected a string (for built-in components) or a class/function (for composite components) but got: undefined.

@reinink
Copy link
Member

reinink commented Nov 1, 2024

I'm trying to use the CloseButton component in my Disclosure panel, next.js app, but for some reason, I keep getting an error message when I try to import the component from @headlessui/react. the error message says: Element type is invalid: expected a string (for built-in components) or a class/function (for composite components) but got: undefined.

@Elieserlaguerre Hey! Are you on the latest versions of @headlessui/react? You'll need v2, as that's when the CloseButton component was introduced.

If yes, maybe try dropping your .next folder and rebuilding — you can sometimes run into stale caches with Next.js.

Hope that helps 👍

@Elieserlaguerre
Copy link

Elieserlaguerre commented Nov 3, 2024 via email

# for free to join this conversation on GitHub. Already have an account? # to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants