---
image: https://remotion.media/docs/generated/articles-docs-elements-libraries.png
title: 'Building Element Libraries'
---

# Building Element Libraries

An Element Library is a website that offers a collection of reusable video components, such as titles, backgrounds, and caption styles. Users can preview these [Elements](/docs/elements) and install them into their Remotion projects, where they can edit the source code.

Users can visit your library directly or add it to [Browse Elements in Studio](/docs/studio/browse-elements). See [Element Libraries](/elements/libraries) for examples.

We encourage you to publish your Elements in your own library rather than contributing them to ours.

For guidance on building the components themselves, see [Element best practices](/docs/elements/creating).

## Template with best practices

The [Element Library template](https://github.com/remotion-dev/template-element-library) is a working library website for previewing, installing, and authoring Elements.

The rest of this page is for building a library from scratch or adding Studio support to an existing one.

## 1. Share the Element source

Import the component file twice. Render the component in a [`<Player>`](/docs/player) so users can see it before installing, and pass the file's source code as a string to [`createElementPayload()`](/docs/studio-protocol/create-element-payload). The preview and the installed Element then run the same code. Use the same dimensions and duration in both.

Studio receives only the payload. Before creating it, your website can combine or modify the source code, or let users edit it interactively.

### Vite

Append [`?raw`](https://vite.dev/guide/assets#importing-asset-as-string) to an import to get the file content as a string:

```tsx title="LowerThirdGalleryItem.tsx"
import {Player} from '@remotion/player';
import {InstallActions} from './InstallActions';
import {LowerThirdElement, lowerThirdElementDefinition} from './LowerThirdElement';
import lowerThirdSourceCode from './LowerThirdElement.tsx?raw';

export const LowerThirdGalleryItem = () => {
  return (
    <>
      <Player component={LowerThirdElement} compositionWidth={lowerThirdElementDefinition.dimensions.width} compositionHeight={lowerThirdElementDefinition.dimensions.height} durationInFrames={lowerThirdElementDefinition.durationInFrames} fps={lowerThirdElementDefinition.fps} />
      <InstallActions definition={lowerThirdElementDefinition} sourceCode={lowerThirdSourceCode} />
    </>
  );
};
```

### Next.js

How you get the source code string depends on the bundler:

- With Webpack, add an [`asset/source` rule](https://webpack.js.org/guides/asset-modules/#source-assets) to support `?raw` imports.
- With Turbopack, write a custom script that generates a module exporting the component source as a string.

## 2. Handle assets<AvailableFrom v="4.0.528" /> {#asset-handling}

Pass each image, audio, and video URL to the component as a prop named after its content, such as `logoUrl` or `audioUrl`, instead of hardcoding it:

```tsx twoslash title="PodcastIntro.tsx"
import {Audio} from '@remotion/media';

export const PodcastIntro = ({audioUrl}: {audioUrl: string}) => {
  return <Audio src={audioUrl} />;
};
```

The preview passes the URL through the `<Player>`'s `inputProps`. On install, Studio can download each asset into the user's project or leave it remote.

### Let Studio download assets (recommended)

Declare the asset in the payload's [`assets`](/docs/studio-protocol/create-element-payload#assets) and reference its path in `initialProps` with [`staticFileRef()`](/docs/studio-protocol/static-file-ref):

```ts twoslash title="element-payload.ts"
import {createElementPayload, staticFileRef} from '@remotion/studio-protocol';
const sourceCode = '';
// ---cut---
const music = {
  path: 'podcast-intro/music.mp3',
  type: 'url',
  url: 'https://cdn.example.com/music.mp3',
} as const;

const payload = createElementPayload({
  displayName: 'Podcast Intro',
  slug: 'podcast-intro',
  sourceCode,
  dependencies: [],
  dimensions: {width: 1920, height: 1080},
  durationInFrames: 150,
  assets: [music],
  initialProps: {
    audioUrl: staticFileRef(music.path),
  },
});
```

On install, Studio saves the file to `public/podcast-intro/music.mp3` and inserts the Element with [`staticFile()`](/docs/staticfile), so it plays offline:

```tsx title="Generated component invocation"
<PodcastIntro audioUrl={staticFile('podcast-intro/music.mp3')} />
```

### Keep assets remote

Put the URL in `initialProps`, for example `{audioUrl: 'https://cdn.example.com/music.mp3'}`. The installed Element loads the asset from that URL.

## 3. Add install actions

Create one payload and use it for both actions in the browser:

- An Install in Studio button that calls [`installInStudio()`](/docs/studio-protocol/install-in-studio).
- Dragging the component into Studio with [`setStudioDragData()`](/docs/studio-protocol/set-studio-drag-data).

When Studio embeds your library in Browse Elements, use [`isInsideStudio()`](/docs/studio-protocol/is-inside-studio) to hide actions that are redundant inside the Studio iframe.

### Fallbacks

If no Studio receives the request, `installInStudio()` resolves with `success: false` and a [`code`](/docs/studio-protocol/install-in-studio#code). Show a message that tells the user what to do, for example:

- `no-compatible-studio`: Start Remotion Studio.
- `no-installable-target`: Focus the Remotion Studio tab to install into, then try again.
- `studio-upgrade-required`: [Upgrade Remotion](/docs/upgrading).

Also offer a way around the failure:

- A Try again button that is also draggable into Studio.
- A Copy source button, so the user can paste the code into a new `.tsx` file.

## 4. Embed the payload<AvailableFrom v="4.0.542" />

Give each Element its own page, and put its payload in a `<script>` tag in that page's HTML. Users and agents can then install the Element from the page's URL with [`npx remotion elements use`](/docs/cli/elements/use). Use [`serializeElementPayloadForHtml()`](/docs/studio-protocol/serialize-element-payload-for-html) to create the script's content:

```tsx title="ElementPage.tsx"
const payload = createElementPayload({...});

<script
  type={ELEMENT_PAYLOAD_MIME_TYPE}
  dangerouslySetInnerHTML={{__html: serializeElementPayloadForHtml(payload)}}
/>
<InstallButton payload={payload} />
```

Do not embed payloads on gallery or list pages, because each payload contains the Element's full source code. Link gallery entries to the Element's page instead.

See [`serializeElementPayloadForHtml()`](/docs/studio-protocol/serialize-element-payload-for-html#example) for Astro and other frameworks.

The script must be in the HTML that your server sends. Render the page on the server or prerender it at build time. If your page renders only in the browser, [link to a JSON payload](/docs/studio-protocol/serialize-element-payload-for-html#pages-rendered-in-the-browser) instead.

## 5. Test the integration

<Step>1</Step> Run your website in dev mode and install an Element into Studio with both the install button and drag-and-drop.<br />
<Step>2</Step> Check that the Element's assets and dependencies are in the project.<br />
<Step>3</Step> Run [`npx remotion elements use`](/docs/cli/elements/use) with the URL of an Element page.

In production, serve your website over HTTPS. HTTP works only on `localhost` and `127.0.0.1`.

## See also

- [Element Libraries](/elements/libraries)
- [Element best practices](/docs/elements/creating)
- [`<Player>`](/docs/player)
- [`createElementPayload()`](/docs/studio-protocol/create-element-payload)
- [`staticFileRef()`](/docs/studio-protocol/static-file-ref)
- [`setStudioDragData()`](/docs/studio-protocol/set-studio-drag-data)
- [`installInStudio()`](/docs/studio-protocol/install-in-studio)
- [`serializeElementPayloadForHtml()`](/docs/studio-protocol/serialize-element-payload-for-html)
- [`npx remotion elements use`](/docs/cli/elements/use)
- [`isInsideStudio()`](/docs/studio-protocol/is-inside-studio)
- [Studio Protocol security](/docs/studio-protocol/security)
