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
Props
Generated from the component's manifest and the values above.
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 publishThe CSS variables the component exposes, set the way a customer's page would.
styles.css
before-after-slider {
--ba-radius: 12px;
}02
Props
| Prop | Type | Default | What it does |
|---|---|---|---|
| scene | city | dashboard | product | city | The drawing used when an image is not set. |
| beforeImage | url | none | https URL of the before image; draws the scene when empty. |
| afterImage | url | none | https URL of the after image; draws the scene when empty. |
| beforeLabel | string | none | Replaces the word Before. |
| afterLabel | string | none | Replaces the word After. |
| description | string | none | What is being compared; it names the slider for screen readers. |
| position | number 0 to 100 | 50 | Where the divider starts, in percent. |
| step | number 1 to 25 | 5 | How far an arrow key moves the divider, in percent. |
| orientation | horizontal | vertical | horizontal | – |
| ratio | 16:9 | 4:3 | 1:1 | 16:9 | Shape of the frame. |
| hideLabels | boolean | false | Hides the Before and After tags. |
| accent | color | none | Handle color; wins over the --ba-accent variable. |
| locale | pt-BR | en | en | – |
Prop names and descriptions are the component's own, in English, as its author wrote them.
03
Events
| Event | When it fires |
|---|---|
| change | The 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
- Card carousel<card-carousel>
- Launch countdown<launch-countdown>
- FAQ accordion<faq-accordion>