# Composition (/docs/bottom-sheet/composition)

A sheet is a root and the parts placed inside it. The root does all of the arithmetic — the
snap points, the keyboard lift, where the footer sits — and publishes it through three contexts. A part
reads those and renders one thing, and every part accepts `style` and `ref`.

```tsx
import { BottomSheet } from "@delacour/react-native-bottom-sheet";

<BottomSheet>
  <BottomSheet.Trigger />
  <BottomSheet.Portal>
    <BottomSheet.Overlay />
    <BottomSheet.Container>
      <BottomSheet.Background />
      <BottomSheet.Handle />
      <BottomSheet.Content>
        <BottomSheet.Title />
        <BottomSheet.Description />
        <BottomSheet.TextInput />
        <BottomSheet.Close />
      </BottomSheet.Content>
      <BottomSheet.Footer />
    </BottomSheet.Container>
  </BottomSheet.Portal>
</BottomSheet>;
```

## The render tree [#the-render-tree]

```txt
BottomSheet              the root — state, geometry, animation, the pans, the ref; renders nothing
  Trigger                anywhere in the app's tree; opens the sheet
  Portal                 the frame — teleported to the nearest host, or in place with `inline`
    Overlay              the scrim, written BEFORE the panel so hit-testing gives the panel its taps
    Container            the panel — the one view that moves
      Background         an absolute fill under everything; where the surface colour and radius go
      Handle             the grabber row; a pan detector and the sheet's one adjustable element
      Content            the static body; a pan detector; clamped to the area above the footer
        Title            labels the panel for assistive technology
        Description      supporting copy
        TextInput        a field the sheet owns the keyboard for
        Close            dismisses
      Footer             sticky over the body, riding the keyboard; or a plain view that scrolls
```

The same parts, as they are laid out on the screen:

```txt
  +-- Portal: the frame ---------------------+
  |                                          |
  |   Overlay: fills the frame, behind       |
  |                                          |
  | +-- Container: the panel, the one -----+ |
  | |   view that moves                    | |
  | | +----------------------------------+ | |
  | | | Handle          ====             | | |
  | | +----------------------------------+ | |
  | | | Content                          | | |   Background fills the panel
  | | |   Title                          | | |   under all three
  | | |   Description                    | | |
  | | +----------------------------------+ | |
  | | | Footer: absolute, over the body  | | |
  | | +----------------------------------+ | |
  +-+--------------------------------------+-+
```

Exactly one body: `Content`, or one of the [scrollables](/docs/bottom-sheet/scrollables), or
[`Steps`](/docs/bottom-sheet/steps). `Footer` is written **beside** the body, not inside it, so it
can stay put while the body scrolls under it.

`Container` is the moving panel and `Content` is the static body. That is the naming the skinned
library uses too, so a sheet written against one reads against the other.

## The overlay is written first [#the-overlay-is-written-first]

Native hit-testing walks siblings back to front, so a touch on the panel never reaches the overlay
and a touch beside it does. The overlay is a React Native `Pressable`, never a Gesture Handler tap:
a full-screen tap gesture behind the sheet wins a touch on a `TextInput`, which has no gesture of
its own, and the field never focuses. Omit `Overlay` and the sheet has no scrim, and the app behind
it stays interactive.

## Context reaches every part [#context-reaches-every-part]

The three contexts are provided once, by the root. A teleported portal moves the **native** view
and leaves the React tree where it was, so they reach every part without being re-provided — and
so does every context your app provides around the trigger: a theme, a form, a query client.

```tsx
import { useBottomSheet, useBottomSheetAnimated } from "@delacour/react-native-bottom-sheet";
import Animated, { useAnimatedStyle } from "react-native-reanimated";

function Chevron() {
  const { index, snapToIndex } = useBottomSheet();
  const { animatedIndex } = useBottomSheetAnimated();
  const style = useAnimatedStyle(() => ({ transform: [{ rotate: `${animatedIndex.value * 180}deg` }] }));
  return <Animated.View onTouchEnd={() => snapToIndex(index === 0 ? 1 : 0)} style={style} />;
}
```

| Hook | Returns | Thread |
| --- | --- | --- |
| `useBottomSheet()` | `isOpen`, `setOpen`, `index`, `snapPointCount`, and every ref method | JS — React state, updated on settle |
| `useBottomSheetAnimated()` | `animatedIndex`, `animatedPosition`, `animatedHeight`, `containerHeight`, `handleHeight`, `contentHeight`, `footerHeight`, `keyboardLift`, `snapPoints`, `closedHeight` | UI — shared values, for `useAnimatedStyle` |
| `useBottomSheetInternal()` | what the parts share, for a part written outside the package | |

Each throws outside a `<BottomSheet>`; the `useOptional…` form returns `null` instead.

## The ref [#the-ref]

```tsx
const sheet = useRef<BottomSheetRef>(null);

sheet.current?.snapToIndex(1);
sheet.current?.snapToPosition("50%");
sheet.current?.expand();
sheet.current?.collapse();
sheet.current?.open();
sheet.current?.close();
sheet.current?.dismiss(); // close, under the name the library this replaces used
sheet.current?.forceClose(); // no animation
```

Every method is an intent: none animates from the JS thread, and each is a no-op when it makes no
sense — `close` on a closed sheet, `snapToIndex(4)` with two snap points. `snapToPosition` clamps to
the available height and never adds a snap point. `open` and `close` are the same state a controlled
`isOpen` drives.

## API Reference [#api-reference]

### BottomSheet [#bottomsheet]

<TypeTable
  type={{
    isOpen: { type: "boolean", description: "Controlled open state. Leave it off and the sheet holds its own." },
    defaultOpen: { type: "boolean", description: "Initial open state while uncontrolled." },
    onOpenChange: {
      type: "(isOpen: boolean) => void",
      description: "Every open and close — trigger, swipe, overlay, Close, the ref, or a controlled isOpen.",
    },
    index: { type: "number", description: "Controlled snap point index. Changing it snaps an open sheet." },
    initialIndex: { type: "number", default: "0", description: "The snap point `open` lands on." },
    onIndexChange: {
      type: "(index: number, height: number, source: AnimationSource) => void",
      description: "The settled index changed; -1 on close.",
    },
    onAnimate: {
      type: "(fromIndex, toIndex, fromHeight, toHeight, source) => void",
      description: "An animation started.",
    },
    onClose: { type: "() => void", description: "The sheet reached its closed height. onOpenChange(false) follows." },
    snapPoints: {
      type: "readonly SnapPointSpec[]",
      default: "[]",
      description: "Heights above the resting bottom, or % of the available height.",
    },
    dynamicSizing: { type: "boolean", default: "true", description: "Adds the content's own height as a snap point." },
    maxDynamicContentSize: { type: "number", description: "Caps the dynamic snap point." },
    topInset: { type: "number", default: "0", description: "Pixels the frame leaves clear at the top." },
    bottomInset: {
      type: "number",
      default: "0",
      description: "The safe-area inset an attached sheet reserves under its content.",
    },
    detached: {
      type: "boolean | { horizontalMargin?: number; bottomOffset?: number }",
      description: "A floating card. true is { horizontalMargin: 16, bottomOffset: 16 }.",
    },
    enablePanDownToClose: { type: "boolean", default: "true" },
    enableHandlePanningGesture: { type: "boolean", default: "true" },
    enableContentPanningGesture: { type: "boolean", default: "true" },
    enableOverDrag: { type: "boolean", default: "true", description: "Rubber-band past the snap points." },
    overDragResistanceFactor: { type: "number", default: "2.5", description: "Larger allows more travel." },
    keyboardBehavior: { type: '"interactive" | "extend" | "fillParent" | "none"', default: '"interactive"' },
    keyboardBlurBehavior: { type: '"none" | "restore"', default: '"restore"' },
    keyboardLiftLimit: {
      type: '"container" | "maxDynamicContentSize" | "highestSnapPoint"',
      default: '"container"',
      description: "Where the keyboard's lift stops: the container's top, maxDynamicContentSize, or the highest snap point.",
    },
    keyboardScope: { type: '"registered" | "inside" | "always"', default: '"inside"' },
    enableBlurKeyboardOnGesture: { type: "boolean", default: "true" },
    animation: { type: "SheetAnimation", description: "A spring or a timing. Platform default when omitted." },
    overrideReduceMotion: { type: '"system" | "always" | "never"', description: "Beats the config's own reduceMotion." },
    animateOnMount: { type: "boolean", default: "true", description: "Animate the first open, or land on it." },
    keepMounted: { type: "boolean", default: "false", description: "Keep the portal's children mounted while closed." },
    stackBehavior: {
      type: '"push" | "replace"',
      default: '"push"',
      description: "What opening does to the other sheets open in the same host.",
    },
    closeOnBack: {
      type: "boolean",
      default: "true",
      description: "Android's back button closes the sheet while it is the top one in its host.",
    },
    onSnapPointHaptic: { type: "() => void", description: "A worklet. A drag crossed a snap point." },
    onCloseHaptic: { type: "() => void", description: "A worklet. A drag let go on a close." },
    onOverDragHaptic: { type: "() => void", description: "A worklet. A drag entered the rubber band." },
    ref: { type: "Ref<BottomSheetRef>" },
  }}
/>

### Trigger [#trigger]

Opens the sheet. A React Native `Pressable` around its children, or with `asChild` the child
itself with `onPress` donated to it.

<TypeTable
  type={{
    asChild: { type: "boolean", description: "Hand onPress to the child rather than rendering a Pressable around it." },
    "...PressableProps": { type: "PressableProps" },
  }}
/>

### Portal [#portal]

The frame the sheet measures itself against. Teleported to the nearest host under a provider; in
place with `inline` or without one. See [Portal and host](/docs/bottom-sheet/portal-and-host).

<TypeTable
  type={{
    inline: {
      type: "boolean",
      description: "Render where written — an absolute fill of the nearest positioned ancestor — rather than teleporting.",
    },
    hostName: { type: "string", description: "The BottomSheet.Host to teleport to. Default: the nearest one." },
    unmountOnClose: {
      type: "boolean",
      description: "Unmount the children once a close has settled. Default true when teleported, false when inline.",
    },
    style: { type: "ViewStyle" },
  }}
/>

### Overlay [#overlay]

The scrim. Its opacity follows the keyboard-free index on the UI thread, so a keyboard lift never
dims the app and a higher snap point is no darker.

<TypeTable
  type={{
    appearsOnIndex: { type: "number", default: "0", description: "The index the scrim is fully in by." },
    disappearsOnIndex: { type: "number", default: "-1", description: "The index the scrim is gone by." },
    opacity: { type: "number", default: "1", description: "Opacity at full appearance." },
    pressBehavior: {
      type: '"close" | "collapse" | "none" | number',
      default: '"close"',
      description: "What a press does: close, collapse to the first snap point, snap to an index, or nothing.",
    },
    enableTouchThrough: { type: "boolean", default: "false", description: "Let touches reach the app behind the scrim." },
    accessibilityLabel: { type: "string", default: '"Close"' },
    onPress: { type: "() => void" },
    style: { type: "ViewStyle", description: "Where the scrim's colour goes." },
  }}
/>

### Container [#container]

The panel — the one view that moves. `accessibilityViewIsModal` while open under an overlay, its
descendants hidden from assistive technology while closed, labelled by the `Title`.

<TypeTable type={{ "...ViewProps": { type: "ViewProps", description: "style, testID and the rest." } }} />

### Background [#background]

An absolute fill under everything in the panel, `pointerEvents: none`. Where the surface colour and
the corner radii go. Exactly the sheet's height tall when detached, so a floating card's bottom
corners are real.

<TypeTable type={{ "...ViewProps": { type: "ViewProps" } }} />

### Handle [#handle]

The grabber's row: a pan detector, and the sheet's one `adjustable` accessibility element —
increment and decrement move a snap point, escape closes, and its value reads *Snap point i of n*. Put the
pill inside it.

<TypeTable
  type={{
    enablePanningGesture: { type: "boolean", description: "Overrides the root's enableHandlePanningGesture for this handle." },
    "...ViewProps": { type: "ViewProps" },
  }}
/>

### Content [#content]

The static body: a pan detector, clamped to the area above the footer and the keyboard, with an
inner box that measures the dynamic snap point.

<TypeTable
  type={{
    footerGap: { type: "number", default: "0", description: "Extra space between the content and a sticky footer." },
    "...ViewProps": { type: "ViewProps" },
  }}
/>

### Footer [#footer]

Sticky over the body by default, riding the keyboard by transform. See [Footer](/docs/bottom-sheet/footer).

<TypeTable
  type={{
    sticky: {
      type: "boolean",
      default: "true",
      description: "Stay put over the body. Off, a plain View for a footer that scrolls with the content.",
    },
    padding: { type: "number", description: "Padding on the footer's view, merged with style. It counts in the sheet's height." },
    "...ViewProps": { type: "ViewProps" },
  }}
/>

### TextInput [#textinput]

React Native's `TextInput` with `useBottomSheetTextInput()` already spread on it. See
[Keyboard](/docs/bottom-sheet/keyboard).

<TypeTable type={{ "...TextInputProps": { type: "TextInputProps" } }} />

### Close [#close]

Dismisses. A `Pressable`, or with `asChild` the child with `onPress` donated.

<TypeTable
  type={{
    asChild: { type: "boolean" },
    "...PressableProps": { type: "PressableProps" },
  }}
/>

### Title and Description [#title-and-description]

A `Text` each. `Title` publishes the `nativeID` the panel is labelled by; `Description` publishes
one of its own. Both take `asChild` to hand those to a text of yours.

<TypeTable
  type={{
    asChild: { type: "boolean" },
    "...TextProps": { type: "TextProps" },
  }}
/>

### Host and Provider [#host-and-provider]

`BottomSheet.Host` and `BottomSheet.Provider` are named on the compound from `src/portal`; they
are documented on [Portal and host](/docs/bottom-sheet/portal-and-host). `ScrollView`, `FlatList`
and `SectionList` are on [Scrollables](/docs/bottom-sheet/scrollables); `Steps` and `Step` on
[Steps](/docs/bottom-sheet/steps).
