Portal
import { useState } from 'react';
const css = `
@keyframes toggle-portal-go {
0% { transform: translateX(0) scale(1); }
40% { transform: translateX(-15px) scale(0); }
41% { transform: translateX(51px) scale(0); }
100% { transform: translateX(36px) scale(1); }
}
@keyframes toggle-portal-back {
0% { transform: translateX(36px) scale(1); }
40% { transform: translateX(51px) scale(0); }
41% { transform: translateX(-15px) scale(0); }
100% { transform: translateX(0) scale(1); }
}
@keyframes toggle-portal-flare-a { 0%, 100% { box-shadow: 0 0 0 0 transparent; } 40% { box-shadow: 0 0 10px 3px color-mix(in oklab, light-dark(#373a40, #f1f2f3) 60%, transparent); } }
@keyframes toggle-portal-flare-b { 0%, 100% { box-shadow: 0 0 0 0 transparent; } 40% { box-shadow: 0 0 10px 3px color-mix(in oklab, light-dark(#373a40, #f1f2f3) 60%, transparent); } }
.toggle-portal { position: relative; display: inline-flex; align-items: center; gap: 12px; color: light-dark(#42454c, #e5e6e9); font-size: 14px; cursor: pointer; user-select: none; }
.toggle-portal__input { position: absolute; width: 1px; height: 1px; margin: 0; opacity: 0; }
.toggle-portal__track {
position: relative;
width: 84px;
height: 32px;
border-radius: 999px;
background: light-dark(#f3f4f7, #1a1c1f);
box-shadow: inset 0 0 0 1px light-dark(#dbdde1, #2e3035);
}
.toggle-portal__input:focus-visible + .toggle-portal__track { outline: 2px solid light-dark(#5f626a, #a7abb2); outline-offset: 3px; }
.toggle-portal__gate {
position: absolute;
top: 4px;
bottom: 4px;
width: 10px;
border-radius: 50%;
border: 2px solid light-dark(#6d7179, #7d8087);
background: radial-gradient(ellipse, light-dark(#fbfcfd, #121417) 40%, light-dark(#ebedf0, #232529));
transition: border-color 0.3s;
}
.toggle-portal__gate--a { left: 4px; }
.toggle-portal__gate--b { right: 4px; }
.toggle-portal__ball {
position: absolute;
left: 14px;
top: 6px;
width: 20px;
height: 20px;
border-radius: 50%;
background: radial-gradient(circle at 35% 35%, light-dark(#373a40, #f1f2f3), light-dark(#5f626a, #a7abb2));
}
.toggle-portal__input:checked + .toggle-portal__track .toggle-portal__ball { transform: translateX(36px); }
.toggle-portal__input:checked + .toggle-portal__track .toggle-portal__gate--b { border-color: light-dark(#42454c, #e5e6e9); }
.toggle-portal__input:not(:checked) + .toggle-portal__track .toggle-portal__gate--a { border-color: light-dark(#42454c, #e5e6e9); }
/* Only after the first press, so nothing plays on page load. */
.toggle-portal[data-armed] .toggle-portal__input:checked + .toggle-portal__track .toggle-portal__ball { animation: toggle-portal-go 0.7s cubic-bezier(0.5, 0, 0.5, 1); }
.toggle-portal[data-armed] .toggle-portal__input:not(:checked) + .toggle-portal__track .toggle-portal__ball { animation: toggle-portal-back 0.7s cubic-bezier(0.5, 0, 0.5, 1); }
.toggle-portal[data-armed] .toggle-portal__input:checked + .toggle-portal__track .toggle-portal__gate { animation: toggle-portal-flare-a 0.7s ease; }
.toggle-portal[data-armed] .toggle-portal__input:not(:checked) + .toggle-portal__track .toggle-portal__gate { animation: toggle-portal-flare-b 0.7s ease; }
@media (prefers-reduced-motion: reduce) {
.toggle-portal[data-armed] .toggle-portal__track * { animation: none !important; }
}
`;
export function PortalToggle() {
const [armed, setArmed] = useState(false);
return (
<>
<style>
{css}
</style>
<label className="toggle-portal" data-armed={armed || undefined}>
<input className="toggle-portal__input" type="checkbox" role="switch" onChange={() => setArmed(true)} />
<span className="toggle-portal__track" aria-hidden="true">
<span className="toggle-portal__gate toggle-portal__gate--a" />
<span className="toggle-portal__gate toggle-portal__gate--b" />
<span className="toggle-portal__ball" />
</span>
Teleport
</label>
</>
);
}Markup where the component goes, CSS in your stylesheet, script before </body>. In Vue, Svelte or Angular, call initTogglePortal(el) on the mounted element instead.
<label class="toggle-portal"><input class="toggle-portal__input" type="checkbox" role="switch"/><span class="toggle-portal__track" aria-hidden="true"><span class="toggle-portal__gate toggle-portal__gate--a"></span><span class="toggle-portal__gate toggle-portal__gate--b"></span><span class="toggle-portal__ball"></span></span>Teleport</label>@keyframes toggle-portal-go {
0% { transform: translateX(0) scale(1); }
40% { transform: translateX(-15px) scale(0); }
41% { transform: translateX(51px) scale(0); }
100% { transform: translateX(36px) scale(1); }
}
@keyframes toggle-portal-back {
0% { transform: translateX(36px) scale(1); }
40% { transform: translateX(51px) scale(0); }
41% { transform: translateX(-15px) scale(0); }
100% { transform: translateX(0) scale(1); }
}
@keyframes toggle-portal-flare-a { 0%, 100% { box-shadow: 0 0 0 0 transparent; } 40% { box-shadow: 0 0 10px 3px color-mix(in oklab, light-dark(#373a40, #f1f2f3) 60%, transparent); } }
@keyframes toggle-portal-flare-b { 0%, 100% { box-shadow: 0 0 0 0 transparent; } 40% { box-shadow: 0 0 10px 3px color-mix(in oklab, light-dark(#373a40, #f1f2f3) 60%, transparent); } }
.toggle-portal { position: relative; display: inline-flex; align-items: center; gap: 12px; color: light-dark(#42454c, #e5e6e9); font-size: 14px; cursor: pointer; user-select: none; }
.toggle-portal__input { position: absolute; width: 1px; height: 1px; margin: 0; opacity: 0; }
.toggle-portal__track {
position: relative;
width: 84px;
height: 32px;
border-radius: 999px;
background: light-dark(#f3f4f7, #1a1c1f);
box-shadow: inset 0 0 0 1px light-dark(#dbdde1, #2e3035);
}
.toggle-portal__input:focus-visible + .toggle-portal__track { outline: 2px solid light-dark(#5f626a, #a7abb2); outline-offset: 3px; }
.toggle-portal__gate {
position: absolute;
top: 4px;
bottom: 4px;
width: 10px;
border-radius: 50%;
border: 2px solid light-dark(#6d7179, #7d8087);
background: radial-gradient(ellipse, light-dark(#fbfcfd, #121417) 40%, light-dark(#ebedf0, #232529));
transition: border-color 0.3s;
}
.toggle-portal__gate--a { left: 4px; }
.toggle-portal__gate--b { right: 4px; }
.toggle-portal__ball {
position: absolute;
left: 14px;
top: 6px;
width: 20px;
height: 20px;
border-radius: 50%;
background: radial-gradient(circle at 35% 35%, light-dark(#373a40, #f1f2f3), light-dark(#5f626a, #a7abb2));
}
.toggle-portal__input:checked + .toggle-portal__track .toggle-portal__ball { transform: translateX(36px); }
.toggle-portal__input:checked + .toggle-portal__track .toggle-portal__gate--b { border-color: light-dark(#42454c, #e5e6e9); }
.toggle-portal__input:not(:checked) + .toggle-portal__track .toggle-portal__gate--a { border-color: light-dark(#42454c, #e5e6e9); }
/* Only after the first press, so nothing plays on page load. */
.toggle-portal[data-armed] .toggle-portal__input:checked + .toggle-portal__track .toggle-portal__ball { animation: toggle-portal-go 0.7s cubic-bezier(0.5, 0, 0.5, 1); }
.toggle-portal[data-armed] .toggle-portal__input:not(:checked) + .toggle-portal__track .toggle-portal__ball { animation: toggle-portal-back 0.7s cubic-bezier(0.5, 0, 0.5, 1); }
.toggle-portal[data-armed] .toggle-portal__input:checked + .toggle-portal__track .toggle-portal__gate { animation: toggle-portal-flare-a 0.7s ease; }
.toggle-portal[data-armed] .toggle-portal__input:not(:checked) + .toggle-portal__track .toggle-portal__gate { animation: toggle-portal-flare-b 0.7s ease; }
@media (prefers-reduced-motion: reduce) {
.toggle-portal[data-armed] .toggle-portal__track * { animation: none !important; }
}function initTogglePortal(root) {
var input = root.querySelector('input');
if (!input) return;
// Animate only after the first change, so nothing plays on page load.
function arm() { root.setAttribute('data-armed', ''); }
input.addEventListener('change', arm);
return function () { input.removeEventListener('change', arm); };
}
document.querySelectorAll('.toggle-portal').forEach(initTogglePortal);Goal: Create a "Portal" toggle.
Visual behavior: A teleport switch: the ball dives into a portal at one end of the track, vanishes, and pops out of the portal at the other end; each gate flares as it passes through.
Technique:
- A real native checkbox with role="switch" (visually hidden: absolute, 1x1, opacity 0) inside a <label>; every visual is aria-hidden and styled from the input with :checked + sibling selectors.
- The move is a keyframe on translateX and scale: shrink to nothing into the near gate (40%), jump to the far gate while invisible (41%), grow out to the rest position.
- The keyframes only apply once a data-armed flag is set on the first change, so nothing plays on page load; checked and unchecked use differently named keyframes, so each change restarts the right one.
- Each gate is an ellipse that flares with a box-shadow keyframe; the gate on the ball's side is brighter.
Accessibility: A native checkbox with role="switch" inside a <label>, so Space toggles it and screen readers announce a switch; the visible label names it and :focus-visible on the input outlines the visual. Under prefers-reduced-motion it simply changes sides.
My stack: {{USER_STACK}}
My styling: {{USER_STYLING}}
My constraints: {{USER_CONSTRAINTS}}
My colors: neutral only. Black, white and grays, no hues. Use the color values above as written, or my project's closest neutral tokens.
Return a single self-contained component. No dependencies.Goal: Create a "Portal" toggle.
Visual behavior: A teleport switch: the ball dives into a portal at one end of the track, vanishes, and pops out of the portal at the other end; each gate flares as it passes through.
Technique:
- A real native checkbox with role="switch" (visually hidden: absolute, 1x1, opacity 0) inside a <label>; every visual is aria-hidden and styled from the input with :checked + sibling selectors.
- The move is a keyframe on translateX and scale: shrink to nothing into the near gate (40%), jump to the far gate while invisible (41%), grow out to the rest position.
- The keyframes only apply once a data-armed flag is set on the first change, so nothing plays on page load; checked and unchecked use differently named keyframes, so each change restarts the right one.
- Each gate is an ellipse that flares with a box-shadow keyframe; the gate on the ball's side is brighter.
Accessibility: A native checkbox with role="switch" inside a <label>, so Space toggles it and screen readers announce a switch; the visible label names it and :focus-visible on the input outlines the visual. Under prefers-reduced-motion it simply changes sides.
My stack: {{USER_STACK}}
My styling: {{USER_STYLING}}
My constraints: {{USER_CONSTRAINTS}}
My colors: use my project's existing color tokens. The color values above are grayscale placeholders. Map each one by its role (surface, text, border, accent, glow) to my theme, and keep their relative lightness so contrast and the effect still read.
Return a single self-contained component. No dependencies.Paste into Claude, ChatGPT or Cursor after filling in {{USER_STACK}}, {{USER_STYLING}} and {{USER_CONSTRAINTS}}. Neutral keeps the greys you see here; Match my project asks the model to map them onto your own colour tokens.
Free to use in any project, commercial included. MIT-0, no attribution required. Licence
About this component
A teleport switch: the ball dives into a portal at one end of the track, vanishes, and pops out of the portal at the other end; each gate flares as it passes through.
- Dependencies
- None
- Difficulty
- Intermediate
- Triggers
- click, state change, glow
- Techniques
- keyframes, state machine, radial gradient
- Reduced motion
- Respected
- Theme
- Light and dark (follows your page's color-scheme)
A native checkbox with role="switch" inside a <label>, so Space toggles it and screen readers announce a switch; the visible label names it and :focus-visible on the input outlines the visual. Under prefers-reduced-motion it simply changes sides.