DEV Community

UI Motion AI
UI Motion AI

Posted on

Building a offset snap code without a framework

The empty cell has a ghost outline that snaps in.

A code field where an empty cell has an offset outline that snaps onto it when you type, and a right code snaps a frame around the row.

demo

The trick

The offset outline moves with a spring translate on each digit.

A correct code animates an outer frame onto the whole row.

Everything runs through Web Animations with reduced motion collapsing delays.

const go=(el,kf,o={})=>el.animate(kf,{duration:300,easing:'cubic-bezier(.2,.8,.2,1)',fill:'forwards',...o,...(RM?{duration:1,delay:0}:{})}).finished.catch(()=>{});
const wipe=(...els)=>els.flat(3).forEach(e=>e&&e.getAnimations&&e.getAnimations().forEach(a=>a.cancel()));
const ping=el=>el.dispatchEvent(new CustomEvent('digit',{bubbles:true}));
/* digit cells: one char per cell, paste, arrows, backspace, onFull(isRight,value) */
function otp(row,n,code,onFull){
 const ins=[...row.querySelectorAll('input')];
 const get=()=>ins.map(i=>i.value).join('');
 const fire=()=>{const v=get();if(v.length===n)onFull(v===code,v)};
 ins.forEach((el,i)=>{
Enter fullscreen mode Exit fullscreen mode

Where I'd use it

Verification code screens

The ghost outline hints at where the next digit goes without any label.

The prompt for this one is free: uimotion.app.

Top comments (0)