Taggor

All components · Category: Visual

Before and after slider

Drag a handle to compare two images.

A slider you can drag with a mouse, a finger or the arrow keys, over two drawn scenes or the two images you give it. The labels and the direction are props.

01

Try it

<before-after-slider>
View

Props

Accent
Corners
before-after-slider: Drag a handle to compare two images. A picture of the real component, drawn from its own code. The controls change the picture and the code beside it; nothing here is sent or stored.

Generated from the component's manifest and the values above.

Code for

index.html

<before-after-slider locale="en"></before-after-slider>

<script>
  const element = document.querySelector("before-after-slider");
  element.addEventListener("before-after-slider:change", (event) => {
    console.log(event.detail);
  });
</script>
  • Add the site's Taggor script once; the component appears when it loads.

taggor-component.js

const element = document.createElement("before-after-slider");
element.setAttribute("locale", "en");
element.addEventListener("before-after-slider:change", (event) => {
  console.log(event.detail);
});
document.body.append(element);
  • Add the site's Taggor script once; the component appears when it loads.

BeforeAfterSlider.tsx

import { useEffect, useRef } from "react";

export function BeforeAfterSlider() {
  const ref = useRef<HTMLElement>(null);

  useEffect(() => {
    const element = ref.current;
    if (!element) return;
    const onChange = (event: Event) => {
      console.log((event as CustomEvent).detail);
    };
    element.addEventListener("before-after-slider:change", onChange);
    return () => {
      element.removeEventListener("before-after-slider:change", onChange);
    };
  }, []);

  return <before-after-slider ref={ref} locale="en" />;
}
  • Add the site's Taggor script once; the component appears when it loads.
  • React 18 sends every prop of a custom element as a text attribute.
  • The event is heard with addEventListener, inside an effect.

BeforeAfterSlider.tsx

import { useEffect, useRef } from "react";

export function BeforeAfterSlider() {
  const ref = useRef<HTMLElement>(null);

  useEffect(() => {
    const element = ref.current;
    if (!element) return;
    const onChange = (event: Event) => {
      console.log((event as CustomEvent).detail);
    };
    element.addEventListener("before-after-slider:change", onChange);
    return () => {
      element.removeEventListener("before-after-slider:change", onChange);
    };
  }, []);

  return <before-after-slider ref={ref} locale="en" />;
}
  • Add the site's Taggor script once; the component appears when it loads.
  • React 19 sets a property when the element already has it, an attribute otherwise.
  • The event is heard with addEventListener, inside an effect.

src/BeforeAfterSlider.vue

<script setup lang="ts">
function onChange(event: Event) {
  console.log((event as CustomEvent).detail);
}
</script>

<template>
  <before-after-slider locale="en" @before-after-slider:change="onChange" />
</template>

vite.config.ts

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

export default defineConfig({
  plugins: [
    vue({
      template: {
        compilerOptions: {
          isCustomElement: (tag) => tag === "before-after-slider",
        },
      },
    }),
  ],
});
  • Add the site's Taggor script once; the component appears when it loads.
  • vite.config.ts tells Vue that this tag is not a Vue component.

src/app/before-after-slider.component.ts

import { afterNextRender, Component, CUSTOM_ELEMENTS_SCHEMA, DestroyRef, ElementRef, inject } from "@angular/core";

@Component({
  selector: "app-before-after-slider",
  schemas: [CUSTOM_ELEMENTS_SCHEMA],
  template: `
    <before-after-slider locale="en"></before-after-slider>
  `,
})
export class BeforeAfterSliderComponent {
  constructor() {
    const host = inject<ElementRef<HTMLElement>>(ElementRef);
    const element = () => host.nativeElement.querySelector("before-after-slider");
    const onChange = (event: Event) => {
      console.log((event as CustomEvent).detail);
    };
    afterNextRender(() => element()?.addEventListener("before-after-slider:change", onChange));
    inject(DestroyRef).onDestroy(() => element()?.removeEventListener("before-after-slider:change", onChange));
  }
}
  • Add the site's Taggor script once; the component appears when it loads.
  • The component declares CUSTOM_ELEMENTS_SCHEMA.
  • The event is heard with addEventListener, inside an effect.

src/TaggorComponent.svelte

<script>
  function onChange(event) {
    console.log(event.detail);
  }
</script>

<before-after-slider locale="en" onbefore-after-slider:change={onChange}></before-after-slider>
  • Add the site's Taggor script once; the component appears when it loads.

How an agent gets a component like this onto a site:

taggor checktaggor publish

The CSS variables the component exposes, set the way a customer's page would.

styles.css

before-after-slider {
  --ba-radius: 12px;
}

02

Props

Props of the component, read from its manifest
PropTypeDefaultWhat it does
scenecity | dashboard | productcityThe drawing used when an image is not set.
beforeImageurlnonehttps URL of the before image; draws the scene when empty.
afterImageurlnonehttps URL of the after image; draws the scene when empty.
beforeLabelstringnoneReplaces the word Before.
afterLabelstringnoneReplaces the word After.
descriptionstringnoneWhat is being compared; it names the slider for screen readers.
positionnumber 0 to 10050Where the divider starts, in percent.
stepnumber 1 to 255How far an arrow key moves the divider, in percent.
orientationhorizontal | verticalhorizontal–
ratio16:9 | 4:3 | 1:116:9Shape of the frame.
hideLabelsbooleanfalseHides the Before and After tags.
accentcolornoneHandle color; wins over the --ba-accent variable.
localept-BR | enen–

Prop names and descriptions are the component's own, in English, as its author wrote them.

03

Events

Events of the component, read from its manifest
EventWhen it fires
changeThe visitor finished a move of the divider.

04

Build one with your agent

Paste this into your agent. It asks you before it signs in, pays or goes further than checking and publishing.

Build me a Taggor component like "Before and after slider" (before-after-slider): Drag a handle to compare two images. Fetch https://taggor.dev/agent-setup/prompt.md and follow its instructions to set me up for Taggor. Ask me before you sign in, create an account, accept terms, pay for anything or type a secret. Check it with `taggor check` and publish it with `taggor publish`, and stop there. Do not promote it to production, attach it to a site, roll back anything or use --yes, --force or --force-drop-invalid unless I ask for exactly that in this conversation.

05

Related components