About
The glass drains for five seconds while you can still undo. Animated glass UI with editable scene colours, reversible states and an explicit offline demo. Connect onAction for real application operations.
Built with React and Tailwind. Copy the prompt and paste it into Claude Code, Cursor or Lovable: your agent adds Glass Undo Button to your project and fits it to your theme.
Code
How Glass Undo Button is used: the demo's code. The component's own source comes with a free account.
Usage.tsx
// @ts-nocheck
'use client';
import * as React from 'react';
import {GlassUndoButton} from '@/components/ui/glass-undo-button';
// The existing SL catalogue picker is retained inside a scoped demo toolbar.
// Studio's inferred knobs do not update CLI previews reliably; these controls do.
const CONTROL_CSS=":host{font:500 14px/1.4 'SL Glass Instrument',sans-serif;color:#34404e;--duration-short:160ms;--ease:cubic-bezier(.2,0,0,1)}*{box-sizing:border-box}button{font:inherit;color:inherit;border:0;cursor:pointer;padding:0;background:none}\n.palette-controls{position:absolute;right:10px;top:10px;display:flex;gap:2px;z-index:2;opacity:var(--card-surface,var(--chrome,1));border-radius:20px;background:#ffffff99;border:1px solid #ffffff75;padding:3px;backdrop-filter:blur(8px)}\n.palette-swatch{display:grid;place-items:center;width:25px;height:25px;padding:0;background:none;border-radius:50%;position:relative}\n.palette-swatch::before{content:'';width:12px;height:12px;border-radius:50%;background:var(--swatch);border:1px solid #24324820;transition:transform var(--duration-short) var(--ease)}\n.palette-swatch[aria-pressed=true]::before{transform:scale(1);box-shadow:inset 0 0 0 1px #22222225}\n.palette-swatch:focus-visible{outline:2px solid #6c7a8d;outline-offset:1px}\n.palette-swatch:is(:hover,:focus-visible)::before{transform:scale(1.18)}\n.palette-swatch[data-palette=black]{--swatch:#292d33}.palette-swatch[data-palette=white]{--swatch:#fafbfd}.palette-swatch[data-palette=blue]{--swatch:#55749a}.palette-swatch.palette-custom::before{background:conic-gradient(#d66f80,#d8bb73,#7cb6a0,#6b92cd,#a480bf,#d66f80);box-shadow:inset 0 0 0 1px #33415518}\n.palette-swatch.palette-custom[aria-pressed=true]::before{background:var(--custom-color,conic-gradient(#d66f80,#d8bb73,#7cb6a0,#6b92cd,#a480bf,#d66f80));box-shadow:inset 0 0 0 1px #33415545}\n.custom-color-panel{position:fixed;inset:auto;margin:0;width:264px;max-width:calc(100vw - 24px);padding:16px;box-sizing:border-box;border:1px solid #d6dfe9;border-radius:16px;background:#fafbfd;color:#34404e;box-shadow:0 12px 36px #26374926;font:14px/1.4 'SL Glass Instrument',sans-serif;user-select:auto}\n.custom-color-panel::backdrop{background:transparent}\n.custom-color-heading{display:flex;align-items:center;justify-content:space-between;margin-bottom:12px}\n.custom-color-heading button{display:grid;place-items:center;width:28px;height:28px;border:0;border-radius:8px;background:#edf1f6;color:#526277;font-size:21px;cursor:pointer}\n.color-well-label{display:grid;gap:8px;color:#58677a;font-size:12px}\n.color-well-label input{width:100%;height:68px;padding:3px;border:1px solid #d2dae5;border-radius:10px;background:#fff;cursor:pointer}\n.color-well-label input::-webkit-color-swatch-wrapper{padding:0}.color-well-label input::-webkit-color-swatch{border:0;border-radius:6px}\n.hex-row{display:flex;align-items:center;gap:12px;margin-top:12px;border:1px solid #d2dae5;border-radius:10px;background:#fff;padding:8px 10px}\n.hex-row span{color:#778394;font-size:11px;letter-spacing:.06em}\n.hex-row input{min-width:0;width:100%;font:14px/1.5 'SL Glass Instrument',sans-serif;color:#34404e;border:0;background:transparent;outline:none;user-select:text}\n.hex-row:focus-within{outline:2px solid #647e9c;outline-offset:2px}.color-hint{font-size:12px;color:#65758a;margin:12px 0 0}.color-error{display:block;font-size:12px;color:#9b3535}.color-error:empty{display:none}.custom-color-panel button:focus-visible,.color-well-label input:focus-visible{outline:2px solid #647e9c;outline-offset:2px}\n.custom-color-panel{opacity:0;transform:translateY(-5px) scale(.985);transform-origin:top right;max-height:calc(100svh - 24px);overflow:auto;transition:opacity 180ms ease,transform 220ms cubic-bezier(.05,.7,.1,1),display 220ms allow-discrete,overlay 220ms allow-discrete}\n.custom-color-panel:popover-open{opacity:1;transform:translateY(0) scale(1)}\n@starting-style{.custom-color-panel:popover-open{opacity:0;transform:translateY(-5px) scale(.985)}}\n.color-well-button{display:grid;width:100%;gap:8px;padding:0;border:0;background:transparent;text-align:left;font:inherit;font-size:12px;color:#58677a;cursor:pointer;border-radius:10px}\n.color-well{display:block;height:56px;background:var(--picked-color);border:1px solid #34404e2b;border-radius:10px;box-shadow:inset 0 0 0 3px #ffffff75}\n.color-editor{display:grid;grid-template-rows:0fr;opacity:0;transition:grid-template-rows 240ms cubic-bezier(.2,0,0,1),opacity 160ms ease}\n.color-editor.is-open{grid-template-rows:1fr;opacity:1}.color-editor-clip{min-height:0;overflow:hidden}.color-editor-content{padding:12px 2px 2px}\n.color-plane{position:relative;height:142px;margin:2px;border-radius:12px;background:linear-gradient(to top,#000,transparent),linear-gradient(to right,#fff,transparent),hsl(var(--picked-hue) 100% 50%);touch-action:none;cursor:crosshair;outline-offset:3px}\n.color-point{position:absolute;left:var(--picked-x);top:var(--picked-y);width:13px;height:13px;border:2px solid #fff;border-radius:50%;box-shadow:0 0 0 1px #0009;transform:translate(-50%,-50%);pointer-events:none}\n.hue-row{display:flex;align-items:center;gap:10px;margin-top:14px;color:#65758a;font-size:12px}\n.hue-row input{appearance:none;min-width:0;width:100%;height:12px;margin:0;border-radius:10px;background:linear-gradient(to right,#f00,#ff0,#0f0,#0ff,#00f,#f0f,#f00);cursor:pointer}\n.hue-row input::-webkit-slider-thumb{appearance:none;width:18px;height:18px;border:2px solid #fff;border-radius:50%;background:hsl(var(--picked-hue) 100% 50%);box-shadow:0 0 0 1px #34404e80}\n.hue-row input::-moz-range-thumb{width:14px;height:14px;border:2px solid #fff;border-radius:50%;background:hsl(var(--picked-hue) 100% 50%)}\n.color-plane:focus-visible,.hue-row input:focus-visible{outline:2px solid #526c8a;outline-offset:3px}\n@media(prefers-reduced-motion:reduce){.custom-color-panel,.color-editor,#action-variants button{transition:none}.custom-color-panel{transform:none}}\n.palette-swatch[data-palette=pastel]{--swatch:conic-gradient(from 200deg,#7ea4ff,#c09cff,#ffa678,#7ea4ff)}\n.palette-swatch[data-palette=vivid]{--swatch:conic-gradient(from 200deg,#2f5bff,#ff2d6f,#ffb000,#2f5bff)}\n.palette-swatch[data-palette=night]{--swatch:radial-gradient(circle at 35% 35%,#8a46ff 0 22%,#3b5bff 45%,#0f111b 75%)}\n.palette-swatch[data-palette=mono]{--swatch:linear-gradient(135deg,#fff,#d4d8e0)}\n.gstops{display:grid;gap:10px;margin:2px 0 12px}\n.gmode{display:grid;grid-template-columns:1fr 1fr;padding:3px;border-radius:10px;background:#eef1f5}\n.gmode button{height:30px;border:0;border-radius:8px;background:none;font:inherit;font-size:13px;color:#5b6573;cursor:pointer;transition:background .25s,color .25s,box-shadow .25s}\n.gmode button[aria-pressed=true]{background:#fff;color:#22303e;box-shadow:0 1px 3px rgb(30 40 60/.14)}\n.gchips{display:flex;gap:8px}\n.gchip{flex:1;height:30px;border:0;border-radius:8px;background:var(--s,#ccc);box-shadow:inset 0 0 0 1px rgb(40 55 80/.15);cursor:pointer;transition:box-shadow .2s,background .2s}\n.gchip[aria-pressed=true]{box-shadow:inset 0 0 0 1px rgb(40 55 80/.15),0 0 0 2px #fafbfd,0 0 0 3.5px #647e9c}\n.gstops.solid .gchip:not([data-i=\"0\"]){display:none}\n.gstops.solid .gchip[aria-pressed=true]{box-shadow:inset 0 0 0 1px rgb(40 55 80/.15)}.palette-controls{pointer-events:auto;gap:3px;top:12px;right:12px}.palette-swatch,.reset-demo{width:30px;height:30px}.palette-swatch::before{width:16px;height:16px}.reset-demo{display:grid;place-items:center;border-radius:50%;color:#526277}.reset-demo:hover{background:#ffffff70}.reset-demo svg{width:16px;height:16px}.reset-demo:focus-visible{outline:2px solid #647e9c;outline-offset:1px}@media(prefers-reduced-motion:reduce){*,*::before{transition:none!important;animation:none!important}}";
const rgb=hex=>[1,3,5].map(i=>parseInt(hex.slice(i,i+2),16));
const clamp=(v,min,max)=>Math.max(min,Math.min(max,v));
function toHsv(hex){const [r,g,b]=rgb(hex).map(v=>v/255),max=Math.max(r,g,b),min=Math.min(r,g,b),d=max-min;let h=0;if(d)h=max===r?((g-b)/d+6)%6:max===g?(b-r)/d+2:(r-g)/d+4;return {h:h*60,s:max?d/max:0,v:max};}
function fromHsv({h,s,v}){const c=v*s,x=c*(1-Math.abs((h/60)%2-1)),m=v-c;const values=h<60?[c,x,0]:h<120?[x,c,0]:h<180?[0,c,x]:h<240?[0,x,c]:h<300?[x,0,c]:[c,0,x];return '#'+values.map(n=>Math.round((n+m)*255).toString(16).padStart(2,'0')).join('');}
function attachCustomColor(group,frame,onChange,setPalette){
const button=document.createElement('button');button.type='button';button.className='palette-swatch palette-custom';button.dataset.palette='custom';button.setAttribute('aria-label','Custom color');button.title='Custom color';button.setAttribute('aria-pressed','false');button.setAttribute('aria-expanded','false');
const panel=document.createElement('div');panel.className='custom-color-panel';panel.setAttribute('popover','auto');
// Static trusted UI. User HEX values only ever pass the strict validator.
panel.innerHTML='<div class="custom-color-heading"><strong>Custom color</strong><button type="button" aria-label="Close color picker">×</button></div><button class="color-well-button" type="button" aria-expanded="false"><span>Choose any color</span><span class="color-well" aria-hidden="true"></span></button><div class="color-editor"><div class="color-editor-clip"><div class="color-editor-content" inert><div class="color-plane" role="group" tabindex="0" aria-label="Saturation and brightness. Use left and right arrows for saturation, up and down for brightness."><span class="color-point"></span></div><label class="hue-row"><span>Hue</span><input type="range" min="0" max="359" step="1" value="212" aria-label="Hue"></label></div></div></div><label class="hex-row"><span>HEX</span><input type="text" value="#55749a" maxlength="7" spellcheck="false" autocomplete="off" aria-label="HEX color"></label><p class="color-hint">Text contrast adjusts automatically.</p><span class="color-error" role="status"></span>';
const well=panel.querySelector('.color-well-button'),editor=panel.querySelector('.color-editor'),content=panel.querySelector('.color-editor-content'),plane=panel.querySelector('.color-plane'),hue=panel.querySelector('[type=range]'),hex=panel.querySelector('[type=text]'),error=panel.querySelector('.color-error');
let timer,paintFrame,last=frame.dataset.palette?.startsWith('#')?frame.dataset.palette:'#55749a',hsv=toHsv(last),dragging=false;
function paint(){panel.style.setProperty('--picked-color',last);panel.style.setProperty('--picked-hue',hsv.h);panel.style.setProperty('--picked-x',(hsv.s*100)+'%');panel.style.setProperty('--picked-y',((1-hsv.v)*100)+'%');hue.value=Math.round(hsv.h);hex.value=last.toUpperCase();plane.setAttribute('aria-label',`Saturation ${Math.round(hsv.s*100)}%, brightness ${Math.round(hsv.v*100)}%. Left/right changes saturation, up/down brightness.`);}
function position(){if(!panel.matches(':popover-open'))return;const rect=button.getBoundingClientRect(),height=panel.offsetHeight;panel.style.left=Math.max(12,Math.min(innerWidth-276,rect.right-264))+'px';panel.style.top=Math.max(12,Math.min(innerHeight-height-12,rect.bottom+8))+'px';}
const resize=new ResizeObserver(position);
frame.addEventListener('savepalettechange',event=>{if(event.detail.startsWith('#')){last=event.detail;button.dataset.palette=last;button.style.setProperty('--custom-color',last);}});
function flushPalette(){cancelAnimationFrame(paintFrame);paintFrame=undefined;if(frame.isConnected)setPalette(frame,last);}
function commit(){clearTimeout(timer);timer=undefined;if(!frame.isConnected)return;if(paintFrame)flushPalette();onChange(last);}
function apply(value,fromField=false){
if(!/^#[0-9a-f]{6}$/i.test(value))return;
last=value.toLowerCase();if(!fromField)hsv=toHsv(last);paint();hex.removeAttribute('aria-invalid');error.textContent='';button.dataset.palette=last;button.style.setProperty('--custom-color',last);
if(!paintFrame)paintFrame=requestAnimationFrame(flushPalette);for(const swatch of group.querySelectorAll('.palette-swatch'))swatch.setAttribute('aria-pressed',String(swatch===button));
const dialog=group.closest('dialog');for(const action of dialog?.querySelectorAll('#copy-source,#download-link')||[])action.disabled=true;
clearTimeout(timer);timer=setTimeout(commit,120);
}
well.addEventListener('click',()=>{const open=well.getAttribute('aria-expanded')!=='true';well.setAttribute('aria-expanded',String(open));editor.classList.toggle('is-open',open);content.inert=!open;});
function pick(event){const rect=plane.getBoundingClientRect();hsv.s=clamp((event.clientX-rect.left)/rect.width,0,1);hsv.v=1-clamp((event.clientY-rect.top)/rect.height,0,1);apply(fromHsv(hsv),true);}
plane.addEventListener('pointerdown',event=>{if(event.button!==0)return;event.preventDefault();plane.focus({preventScroll:true});dragging=true;plane.setPointerCapture(event.pointerId);pick(event);});
plane.addEventListener('pointermove',event=>{if(dragging)pick(event);});
plane.addEventListener('pointerup',()=>{dragging=false;commit();});plane.addEventListener('lostpointercapture',()=>{dragging=false;});
plane.addEventListener('keydown',event=>{const step=event.shiftKey?.1:.01;if(!['ArrowLeft','ArrowRight','ArrowUp','ArrowDown'].includes(event.key))return;event.preventDefault();if(event.key==='ArrowLeft')hsv.s=clamp(hsv.s-step,0,1);if(event.key==='ArrowRight')hsv.s=clamp(hsv.s+step,0,1);if(event.key==='ArrowUp')hsv.v=clamp(hsv.v+step,0,1);if(event.key==='ArrowDown')hsv.v=clamp(hsv.v-step,0,1);apply(fromHsv(hsv),true);});
hue.addEventListener('input',()=>{hsv.h=Number(hue.value);apply(fromHsv(hsv),true);});hue.addEventListener('change',commit);
hex.addEventListener('input',()=>{
let value=hex.value.trim();if(!value.startsWith('#'))value='#'+value;
if(/^#[0-9a-f]{6}$/i.test(value))apply(value);else{hex.setAttribute('aria-invalid','true');error.textContent='Enter a valid HEX color.';}
});
hex.addEventListener('keydown',event=>{if(event.key==='Enter'){event.preventDefault();event.stopPropagation();if(!hex.hasAttribute('aria-invalid')){commit();panel.hidePopover();button.focus();}}});
panel.querySelector('button').addEventListener('click',()=>{panel.hidePopover();button.focus();});
panel.addEventListener('keydown',event=>{if(event.key==='Escape'){event.preventDefault();event.stopPropagation();panel.hidePopover();button.focus();}});
panel.addEventListener('toggle',event=>{const open=event.newState==='open';button.setAttribute('aria-expanded',String(open));if(open)resize.observe(panel);else{resize.disconnect();if(timer)commit();}});
button.addEventListener('click',()=>{
if(panel.matches(':popover-open')){panel.hidePopover();return;}
const active=frame.dataset.palette;if(active?.startsWith('#'))last=active;
hsv=toHsv(last);paint();error.textContent='';hex.removeAttribute('aria-invalid');
panel.showPopover();position();
});
group.addEventListener('click',event=>{if(event.target.closest('.palette-swatch')!==button&&!panel.contains(event.target)){clearTimeout(timer);timer=undefined;cancelAnimationFrame(paintFrame);paintFrame=undefined;if(panel.matches(':popover-open'))panel.hidePopover();}});
if(frame.dataset.palette?.startsWith('#')){button.dataset.palette=last;button.style.setProperty('--custom-color',last);}
group.append(button,panel);
return ()=>{clearTimeout(timer);cancelAnimationFrame(paintFrame);resize.disconnect();if(panel.matches(':popover-open'))panel.hidePopover();panel.remove();button.remove();};
}
function mountControls(shadow,host,onScene,onReset){
const scenes=new Map(),customs=new Map(),id='glass-demo';
const SCENES=[['pastel','Pastel'],['night','Night'],['mono','Mono']];
const customOf=()=>{if(!customs.has(id))customs.set(id,{stops:['#7ea4ff','#ffa678','#c09cff'],solid:false,sel:0});return customs.get(id);};
const swatchFill=c=>c.solid?c.stops[0]:`conic-gradient(from 200deg,${c.stops[0]},${c.stops[1]},${c.stops[2]},${c.stops[0]})`;
const group=document.createElement('div');group.className='palette-controls glass-palette';group.setAttribute('role','group');group.setAttribute('aria-label','Preview background');
const refresh=()=>{
const scene=scenes.get(id)||'pastel',c=scene==='custom'?customOf():null;
host.dataset.palette=c?c.stops[c.sel]:scene;
onScene(c?{stops:[...c.stops],solid:c.solid}:scene);group._sync();
};
for(const [value,label]of SCENES){const b=document.createElement('button');b.type='button';b.className='palette-swatch';b.dataset.palette=value;b.setAttribute('aria-label',label+' background');b.title=label;b.addEventListener('click',()=>{scenes.set(id,value);refresh();});group.append(b);}
const releasePicker=attachCustomColor(group,host,()=>{},(_host,hex)=>{const c=customOf();c.stops[c.sel]=hex;scenes.set(id,'custom');refresh();});
const custom=group.querySelector('.palette-custom'),panel=group.querySelector('.custom-color-panel'),hexField=panel.querySelector('.hex-row input');
const box=document.createElement('div');box.className='gstops';
box.innerHTML='<div class="gmode" role="group" aria-label="Scene"><button type="button" data-mode="gradient">Gradient</button><button type="button" data-mode="solid">Solid</button></div><div class="gchips" role="group" aria-label="Gradient colours">'+[0,1,2].map(i=>`<button type="button" class="gchip" data-i="${i}" aria-label="Colour ${i+1}"></button>`).join('')+'</div>';
panel.querySelector('.color-well-button').before(box);
const pick=i=>{const c=customOf();c.sel=i;scenes.set(id,'custom');hexField.value=c.stops[i];hexField.dispatchEvent(new Event('input',{bubbles:true}));refresh();};
box.addEventListener('click',event=>{const chip=event.target.closest('.gchip'),mode=event.target.closest('[data-mode]');if(chip)pick(+chip.dataset.i);if(mode){const c=customOf();c.solid=mode.dataset.mode==='solid';if(c.solid)c.sel=0;pick(c.sel);}});
panel.addEventListener('toggle',event=>{if(event.newState==='open'){pick(customOf().sel);}});
group._sync=()=>{const scene=scenes.get(id)||'pastel',c=customs.get(id);for(const b of group.querySelectorAll('.palette-swatch'))b.setAttribute('aria-pressed',String(b.dataset.palette===scene||(b===custom&&scene==='custom')));if(c){if(scene==='custom')custom.style.setProperty('--custom-color',swatchFill(c));box.classList.toggle('solid',c.solid);for(const b of box.querySelectorAll('[data-mode]'))b.setAttribute('aria-pressed',String((b.dataset.mode==='solid')===c.solid));for(const chip of box.querySelectorAll('.gchip')){chip.style.setProperty('--s',c.stops[chip.dataset.i]);chip.setAttribute('aria-pressed',String(+chip.dataset.i===c.sel));}}};group._sync();
const reset=document.createElement('button');reset.type='button';reset.className='reset-demo';reset.setAttribute('aria-label','Reset demo');reset.title='Reset demo';reset.innerHTML='<svg viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="1.6" stroke-linecap="round" stroke-linejoin="round" aria-hidden="true"><path d="M4 10a8 8 0 1 1 1.5 7M4 4v6h6"/></svg>';reset.addEventListener('click',onReset);group.append(reset);shadow.append(group);
return ()=>{releasePicker();group.remove();};
}
function Toolbar({onScene,onReset}){
const host=React.useRef(null),actions=React.useRef({onScene,onReset});actions.current={onScene,onReset};
React.useEffect(()=>{
const el=host.current,shadow=el.shadowRoot||el.attachShadow({mode:'open'});
const sheet=new CSSStyleSheet();sheet.replaceSync(CONTROL_CSS);shadow.adoptedStyleSheets=[sheet];
const cleanup=mountControls(shadow,el,value=>actions.current.onScene(value),()=>actions.current.onReset());
return ()=>{cleanup();shadow.replaceChildren();};
},[]);
return <div ref={host} style={{position:'absolute',inset:0,pointerEvents:'none'}}/>;
}
const defaults={firstColor:'#7ea4ff',secondColor:'#ffa678',thirdColor:'#c09cff',solid:false,showBackdrop:true};
export default function Demo(props:Partial<typeof defaults>){
const [scene,setScene]=React.useState('pastel'),[reset,setReset]=React.useState(0);
React.useEffect(()=>{
if(Object.keys(props).some(key=>key!=='showBackdrop'))setScene({stops:[props.firstColor||defaults.firstColor,props.secondColor||defaults.secondColor,props.thirdColor||defaults.thirdColor],solid:!!props.solid});
},[props.firstColor,props.secondColor,props.thirdColor,props.solid]);
return <div className="w-full bg-background text-foreground" style={{padding:24,minWidth:280}}>
<div style={{width:'100%',maxWidth:620,margin:'0 auto',position:'relative'}}>
<GlassUndoButton demo scene={scene} height={360} showBackdrop={props.showBackdrop??true} resetKey={reset}/>
<Toolbar onScene={setScene} onReset={()=>setReset(value=>value+1)}/>
</div>
<p className="text-muted-foreground" style={{textAlign:'center',fontSize:12,margin:'12px 0 0'}}>Interactive demo · no data is sent or recorded</p>
</div>;
}















