# Photo gallery

Source: https://help.ruuster.com/idx/recipe-gallery

> The full-screen photo gallery of the listing page, with the Photos and Street view tabs, the photo mosaic, the lightbox, the sidebar and the mobile action bar, from FullGallery and Lightbox.

Every photo of a listing opens the full-screen gallery: a view over the listing page with a Photos
tab, a Street view tab when the home can show one, and the price, the contact card and the tour card
beside the photos on wide screens. A photo in the gallery opens the lightbox, which shows one photo
at a time. `ListingDetail` mounts the gallery for you, so a listing page built from the
[listing detail recipe](https://help.ruuster.com/idx/recipe-listing-detail) has it already. This page
explains how it works and how to use it on a layout of your own.

## What the visitor sees

<figure class="idx-shot"><img src="/idx-docs/screens/ours/full-gallery-1440.png" alt="The full gallery dialog, closed, on its Photos tab, on a desktop (1440 px wide)" width="1440" height="900" loading="lazy" decoding="async"><figcaption>The gallery, opened from the photo grid.</figcaption></figure>

<figure class="idx-shot"><img src="/idx-docs/screens/ours/full-gallery-375.png" alt="The full gallery dialog, closed, on its Photos tab, on a phone (375 px wide)" width="375" height="812" loading="lazy" decoding="async"><figcaption>The gallery, opened from the photo grid.</figcaption></figure>

The markup is `static/html/full-gallery.html` in `@ruuster/idx-recipes`. The [HTML and CSS recipe](https://help.ruuster.com/idx/recipe-html-css) shows how to fill it and which stylesheet and script to load.

<figure class="idx-shot"><img src="/idx-docs/screens/ours/mobile-gallery-pinned.png" alt="The gallery on a phone scrolled down, with the price, address and facts in the header in place of the tabs" width="390" height="844" loading="lazy" decoding="async"><figcaption>Once the visitor scrolls, the header shows the price, the address and the facts.</figcaption></figure>

## How it opens

Every button that opens the gallery carries `data-rw-open="gallery"`, and the gallery listens for
clicks on those buttons inside the listing page's root element (`data-rw-detail`). On the listing
page these buttons open it:

| Button | Opens |
|---|---|
| A photo in the photo grid | The Photos tab, scrolled to that photo |
| The "N photos" badge | The Photos tab at the first photo |
| The "Street view" pill | The Street view tab |

A button can name the tab and the photo with `data-rw-open-tab="photos"` or `"street"` and
`data-rw-open-index="3"`. A request for a photo the listing does not have opens the top of the
Photos tab, and a request for Street view on a home with no Street view tab opens the Photos tab.

While the gallery is open the page behind it stays still. The X button and the Escape key close it,
and the keyboard focus goes back to the button that opened it.

## The Photos tab

The photos sit in a mosaic with rounded corners:

- Under 768 pixels, one column of photos at 4:3.
- From 768 pixels, the first photo and every third photo after it fill the width at 16:9, and the
  others sit two to a row at 4:3. When one photo would be left alone on its row, it fills the width.

Photos load as the visitor scrolls, at the size the screen needs. Each photo is a button named
"Open photo 3 of 24", which opens the lightbox at that photo.

## The lightbox

The lightbox shows one photo at a time with "3 of 24" under it. The arrow buttons, the left and right
arrow keys, and a swipe of 50 pixels on a touch screen move to the next or the previous photo, and
both ends wrap around. The X button and the Escape key close it, and the focus goes back to the photo
that opened it.

## The Street view tab

The Street view tab appears when you pass your Google Maps key, the listing has coordinates, and its
address is shown. It loads Google Street View at the nearest outdoor view within 50 meters of the
home. While the view loads the tab reads "Street view is loading." When Google has no imagery for the
address, it reads "There is no street view imagery for this address. The Photos tab has every photo
of this home." The sidebar steps aside on this tab, so the view has the full width.

## The header, the sidebar and the mobile bar

- **The header** stays at the top of the gallery. It holds the tabs and, from 768 pixels, a dark
  "Share" button when the page has a share link, then the X button. Once the price block in the
  sidebar scrolls out of view (on a phone, after 50 pixels), the tabs give way to a summary with the
  price, the address, beds, baths and square feet.
- **The sidebar**, from 992 pixels, holds the price block and, below it, the contact and tour cards,
  which stay in view as the visitor scrolls.
- **On a phone**, the share button floats over the photos, and under 992 pixels the contact and tour
  buttons stay fixed at the bottom of the screen, as on the listing page.

## Use it on your own layout

`ListingDetail` passes the gallery its sidebar and its mobile bar. On a layout of your own, mount
`FullGallery` once inside the element that carries `data-rw-detail`, next to any buttons that open
it:

<p class="code-title"><code>packages/ruuster-idx-recipes/snippets/own-listing-layout.tsx</code></p>

```tsx
// Your own listing layout from the pieces ListingDetail is made of. The screens open from any button
// with data-rw-open inside the element that carries data-rw-detail, so mount each screen once inside
// that element and put your buttons anywhere in it.
import type { ListingDetail as ListingDetailData } from "@ruuster/idx";
import { ContactAgentModal, ContactCards, FullGallery, PhotoGrid, PriceBlock, RequestTourModal, ShareModal } from "@ruuster/idx-recipes";

const AGENT = { name: "Jordan Lee", phone: "(404) 555-0134", email: "jordan@peachrealty.example", brokerage: "Peach Realty" };
const CONSENT = "I agree that Peach Realty may call, text or email me about this home. I can opt out at any time.";

export function MyListingLayout({ listing, pageUrl }: { listing: ListingDetailData; pageUrl: string }) {
  return (
    <article className="rw-recipes my-listing" data-rw-detail={listing.id}>
      <PhotoGrid photos={listing.photos} fallbackAlt={listing.address.display} streetView={false} share />
      <div className="my-listing__columns">
        <PriceBlock listing={listing} />
        <aside>
          <ContactCards agent={AGENT} />
          {/* Any button of your own opens a screen the same way. */}
          <button type="button" className="my-button" data-rw-open="tour">
            Pick a tour time
          </button>
        </aside>
      </div>
      <FullGallery listing={listing} share />
      <ShareModal listing={listing} url={pageUrl} />
      <ContactAgentModal listing={listing} agent={AGENT} action="/api/leads/fub" consentText={CONSENT} />
      <RequestTourModal listing={listing} action="/api/leads/fub" consentText={CONSENT} />
    </article>
  );
}
```

### FullGallery

| Prop | Type | Required | What it does |
|---|---|---|---|
| `listing` | `ListingDetail` or `ListingCard` | Yes | The listing whose photos the gallery shows |
| `share` | `boolean` | Yes | Shows the Share button, which opens the share screen |
| `googleMapsKey` | `string \| null` | No | Your Google Maps key. With coordinates and a shown address it adds the Street view tab |
| `sidebar` | `ReactNode` | No | The content under the price block from 992 pixels, such as `ContactCards` |
| `mobileBar` | `ReactNode` | No | The bar fixed to the bottom under 992 pixels, such as `MobileActionBar` |

### Lightbox

`FullGallery` opens the lightbox itself. Use `Lightbox` on its own when your layout shows photos
without the gallery.

| Prop | Type | Required | What it does |
|---|---|---|---|
| `photos` | `Photo[]` | Yes | `listing.photos` |
| `fallbackAlt` | `string` | Yes | The alt text for a photo without its own |
| `index` | `number \| null` | Yes | The photo to show, or `null` when the lightbox is closed |
| `onIndex` | `(index: number \| null) => void` | Yes | Called with the next photo, or `null` when the visitor closes it |
| `returnFocusTo` | `HTMLElement \| null` | No | The element that gets the focus back on close |

## Rules this screen follows

**MUST**

- Keep every photo's alt text from the API. It never names the street of a hidden address.
- Show the Street view tab only with your own Google Maps key, restricted to your domains.

**NEVER**

- Show Street view or coordinates for a listing whose address is hidden. `FullGallery` shows the
  tab only when the address is shown.

## Without React

The static listing page in [Build without React](https://help.ruuster.com/idx/recipe-html-css) prints
the gallery closed, with every photo in it, and `ruuster-idx.js` opens it from the same
`data-rw-open` buttons. `static/HOOKS.md` in `@ruuster/idx-recipes` lists every hook the script reads.
