Embed

Put a live gradient on any page, in plain HTML, React, Vue or Svelte, with nothing to install.

In the editor, Export → Code copies a snippet for the current gradient. The HTML, React, Vue and Svelte tabs all load one hosted module, which defines a <gradients-element> custom element. There is nothing to install.

The examples below show params="…" where the editor writes your gradient's settings. Copy the real snippet from the editor.

Plain HTML

<script type="module" src="https://gradients.sh/embed.js?v=1"></script>
<gradients-element params="…"></gradients-element>

It works in static sites, Framer code blocks and any page that can load an ES module. Size it like any element, with class or style.

React

The React tab's component loads the module and adds the element from an effect, so TypeScript needs no typings for the custom tag:

GradientBackground.tsx
"use client";

import { useEffect, useRef } from "react";

const src = "https://gradients.sh/embed.js?v=1";
const params = "…";

export function GradientBackground({ className = "" }: { className?: string }) {
  const hostRef = useRef<HTMLDivElement>(null);
  useEffect(() => {
    const host = hostRef.current;
    if (!host) return;
    const element = document.createElement("gradients-element");
    element.setAttribute("params", params);
    element.style.cssText = "display: block; width: 100%; height: 100%";
    host.append(element);
    loadEmbed();
    return () => element.remove();
  }, []);
  return <div ref={hostRef} className={className} aria-hidden="true" />;
}

function loadEmbed() {
  if ([...document.scripts].some((script) => script.src === src)) return;
  const script = document.createElement("script");
  script.type = "module";
  script.src = src;
  document.head.append(script);
}

The gradient fills the component's box. Size it with className, for example className="absolute inset-0" behind a hero.

Vue

Vue treats unknown tags as components and warns when it can't resolve them. Tell it that gradients-element is a custom element in vite.config:

vite.config.js
import vue from "@vitejs/plugin-vue";
import { defineConfig } from "vite";

export default defineConfig({
  plugins: [
    vue({
      template: {
        compilerOptions: {
          isCustomElement: (tag) => tag === "gradients-element",
        },
      },
    }),
  ],
});

In Nuxt, the same function goes in nuxt.config as vue.compilerOptions.isCustomElement. Then add the component:

GradientBackground.vue
<script setup>
import { onMounted } from "vue";

const src = "https://gradients.sh/embed.js?v=1";
const params = "…";

onMounted(() => {
  if ([...document.scripts].some((script) => script.src === src)) return;
  const script = document.createElement("script");
  script.type = "module";
  script.src = src;
  document.head.append(script);
});
</script>

<template>
  <gradients-element
    :params="params"
    style="display: block; width: 100%; height: 100%"
    aria-hidden="true"
  />
</template>

The element fills its parent, so give the parent a size. onMounted only runs in the browser, so the module never loads during server rendering.

Svelte

Svelte renders custom elements as they are, so there is no config:

GradientBackground.svelte
<script>
  import { onMount } from "svelte";

  const src = "https://gradients.sh/embed.js?v=1";
  const params = "…";

  onMount(() => {
    if ([...document.scripts].some((script) => script.src === src)) return;
    const script = document.createElement("script");
    script.type = "module";
    script.src = src;
    document.head.append(script);
  });
</script>

<gradients-element
  {params}
  style="display: block; width: 100%; height: 100%"
  aria-hidden="true"
></gradients-element>

The element fills its parent, so give the parent a size. onMount only runs in the browser, which keeps it safe in SvelteKit.

Removing a gradient

Removing the element, or unmounting the component that rendered it, stops the animation and frees its GPU slot. The module loads once per page, however many gradients the page renders.

In a shadcn project

The editor's shadcn tab installs the gradient as a typed React component with props instead. See presets for the install command.