Components
Disclosure

Disclosure

Installation

To get started, install Headless UI via npm:

npm install headlessui-react-native

Basic example

Modals are built using the Disclosure, DisclosureButton, and DisclosurePanel components. The button will automatically open/close the panel when clicked.

import {
  CloseButton,
  Disclosure,
  DisclosureButton,
  DisclosurePanel,
} from "headlessui-react-native";
import React from "react";
import { View, Text, ViewStyle, TextStyle } from "react-native";
 
export function DisclosureExample() {
  return (
    <View>
      <Disclosure style={disclosureStyle}>
        <DisclosureButton style={disclosureButtonStyle}>
          {({ open }) => (
            <>
              <Text style={disclosureButtonTextStyle}>
                What is your refund policy?
              </Text>
              <Arrow rotate={open} />
            </>
          )}
        </DisclosureButton>
        <DisclosurePanel>
          <Text style={disclosurePannelTextStyle}>
            If you're unhappy with your purchase, we'll refund you in full.
          </Text>
        </DisclosurePanel>
      </Disclosure>
      <Disclosure style={disclosureStyle}>
        <DisclosureButton style={disclosureButtonStyle}>
          {({ open }) => (
            <>
              <Text style={disclosureButtonTextStyle}>
                Do you offer technical support?
              </Text>
              <Arrow rotate={open} />
            </>
          )}
        </DisclosureButton>
        <DisclosurePanel>
          <Text style={disclosurePannelTextStyle}>No.</Text>
        </DisclosurePanel>
      </Disclosure>
    </View>
  );
}
 
const disclosureStyle: ViewStyle = {
  width: 300,
  padding: 16,
  backgroundColor: "rgb(33, 150, 243)",
};
const disclosureButtonStyle: ViewStyle = {
  flexDirection: "row",
  alignItems: "center",
  justifyContent: "space-between",
};
const disclosureButtonTextStyle: TextStyle = { color: "white" };
const disclosurePannelTextStyle: TextStyle = {
  color: "rgba(0, 0, 0, 0.6)",
};
 
const Arrow = ({ rotate }: { rotate: boolean }) => (
  <Text
    style={{
      color: "white",
      transform: [{ rotate: rotate ? "180deg" : "0deg" }],
    }}
  >

  </Text>
);

Examples

Closing disclosures manually

To close a disclosure manually when clicking a child of its panel, render that child as a CloseButton.

import {
  CloseButton,
  Disclosure,
  DisclosureButton,
  DisclosurePanel,
} from "headlessui-react-native";
import React from "react";
import { Text } from "react-native";
 
function Example() {
  return (
    <Disclosure defaultOpen>
      <DisclosureButton>Open mobile menu</DisclosureButton>
      <DisclosurePanel>
        <CloseButton>
          <Text>Home</Text>
        </CloseButton>
      </DisclosurePanel>
    </Disclosure>
  );
}

Component API

Disclosure

The main disclosure component.

PropDefaultDescription
asFragmentString The element the disclosure should render as.
defaultOpenfalseBoolean Whether or not the Disclosure component should be open by default.
Render PropDescription
openBoolean Whether the disclosure is open or not.
close() => void Closes the disclosure.

DisclosureButton

The trigger component that toggles a Disclosure.

PropDefaultDescription
asPressableString The element the disclosure button should render as.
Render PropDescription
openBoolean Whether the disclosure is open or not.

DisclosurePanel

This component contains the contents of your disclosure.

PropDefaultDescription
asViewString The element the disclosure panel should render as.
Render PropDescription
openBoolean Whether the disclosure is open or not.
close() => void Closes the disclosure.

CloseButton

This button will close the nearest Disclosure ancestor when clicked.

PropDefaultDescription
asPressableString The element the close button should render as.