Patterns

Custom content

Besides images, a slide can hold a video, an iframe, or your own content. Custom content is an item with type: "custom" and render: "note". The viewer looks up the note renderer and draws it into the slide.

Loading the live example…

renderers.tsx
import { createContext, useContext } from "react";
import { itemLabel } from "@ogdakke/fluidbox";
import { Fluidbox } from "@ogdakke/fluidbox/react";
import { media, theme, thumbnailSrc } from "./data";

const ThemeContext = createContext("light");

/** Custom content renders inside the app, so it reads the app's context. */
function Note() {
  const theme = useContext(ThemeContext);
  return <article className="pattern-note">Theme: {theme}</article>;
}

export function Renderers() {
  return (
    <ThemeContext.Provider value={theme}>
      {/* Items of type "custom" whose render is "note" render <Note />. */}
      <Fluidbox.Root items={media} renderers={{ note: () => <Note /> }}>
        <div className="pattern-grid">
          {media.map((item) => (
            <Fluidbox.Trigger
              key={item.id}
              item={item}
              className="pattern-thumb"
            >
              <img src={thumbnailSrc(item)} alt={itemLabel(item)} />
            </Fluidbox.Trigger>
          ))}
        </div>
      </Fluidbox.Root>
    </ThemeContext.Provider>
  );
}
  • Framework components keep their context. React and Vue render custom slides through portals inside Root, and Solid renders them under Root's owner, so context, provide/inject and stores work. Angular renders an ng-template, which keeps the injector of the place you declare it.
  • Plain renderers are functions that get { item, index, target, signal }, append to target, and may return a function that releases their own resources. The viewer empties target itself.
  • A YouTube embed is an iframe item. YouTube won't play an embed that sends no referrer, so the example data sets referrerPolicy: "strict-origin-when-cross-origin" on it.
  • Markup. On custom elements, any element other than an image, video or iframe in a <fluidbox-item>'s content template is custom content. With gallery.items, set gallery.renderers.
  • Every type needs width and height so the viewer can size the slide before anything loads. Videos show their poster until they play.

See Media types for every field.