# useOpenIndex (/docs/hooks/use-open-index)





A collection of media (a [`Gallery`](/docs/gallery)'s tiles, a [`Carousel`](/docs/carousel)'s slides) never imports what shows an item larger. It reads the **open-index seam** instead: an `OpenIndexProvider` above it, usually a [`Lightbox`](/docs/lightbox), says "open item N". With no provider, the items stay plain images and nothing is focusable.

Two hooks expose the seam:

* `useOpenIndexTrigger(index)` turns an element of your own into an item: a list row, a table cell, a map pin. Reach for it when your own layout should open in a Lightbox.
* `useOpenIndex()` reads the provider itself, for anything the trigger doesn't cover.

```tsx
import { useOpenIndexTrigger } from '@42-central/ui-react';

function PhotoRow({ photo, index }) {
  const trigger = useOpenIndexTrigger(index);
  if (!trigger) return <div>{photo.alt}</div>;
  return <button {...trigger}>{photo.alt}</button>;
}
```

## Usage [#usage]

A list of your own inside a `Lightbox`. Each row opens the lightbox at its own index; navigate to another photo and close, and focus lands on that photo's row, not the one you opened from.

<Preview code="&#x22;function PhotoRow({ photo, index }) {\n  const trigger = useOpenIndexTrigger(index);\n  if (!trigger) return <div>{photo.alt}</div>;\n  return (\n    <button {...trigger}>\n      <Image src={photo.src} alt=\&#x22;\&#x22; ratio={1} />\n      {photo.alt}\n    </button>\n  );\n}\n\n<Lightbox data={photos}>\n  <ul>\n    {photos.map((photo, index) => (\n      <li key={photo.key}><PhotoRow photo={photo} index={index} /></li>\n    ))}\n  </ul>\n</Lightbox>&#x22;">
  <OpenIndexTriggerDemo />
</Preview>

To provide the seam yourself, to open a route or a side panel instead of a lightbox, see [Gallery › Opening a photo](/docs/gallery#opening-a-photo).

## API [#api]

### `useOpenIndexTrigger(index, reveal?)` [#useopenindextriggerindex-reveal]

<TypeTable
  type="{
  index: { type: 'number', required: true, description: &#x22;The item's position in the viewer's data.&#x22; },
  reveal: {
    type: '() => void',
    description: 'For an item that can be mounted but out of view (a scrolled-away carousel slide): brings it into view at once. The viewer calls it before measuring or focusing the element on close. The latest function is called, so a new one each render does not re-register.',
  },
}"
/>

Returns `undefined` with no provider, so you render no button at all. Under a provider, spread the props it returns onto a `<button>`:

<TypeTable
  type="{
  ref: { type: 'RefObject<HTMLButtonElement | null>', required: true, description: &#x22;Registers the element under its index while it's mounted, in a layout effect, and again when the seam or the index changes.&#x22; },
  type: { type: '&#x22;button&#x22;', required: true, description: 'So the item never submits a form.' },
  'aria-haspopup': { type: '&#x22;dialog&#x22;', required: true, description: 'Announces that activating it opens a dialog.' },
  onClick: { type: '(event: MouseEvent<HTMLElement>) => void', required: true, description: 'Calls open(index, event.currentTarget).' },
}"
/>

The button needs an accessible name: visible text, or the image's `alt`.

### `useOpenIndex()` [#useopenindex]

Returns the nearest provider's `OpenIndex`, or `undefined` when items should stay inert.

<TypeTable
  type="{
  open: { type: '(index: number, from: HTMLElement) => void', required: true, description: 'Item index was activated. from is its element, for focus return and a morph origin.' },
  register: {
    type: '(index: number, element: HTMLElement, reveal?: () => void) => () => void',
    required: true,
    description: &#x22;Called while item index is mounted; returns its unregister. It's how a viewer finds the right element to return to on close.&#x22;,
  },
}"
/>

`OpenIndexProvider` takes that object as `value`. Keep it referentially stable (`useMemo`): every item registers again when it changes.
