UNPKG

@stanfordspezi/spezi-web-design-system

Version:

Stanford Biodesign Digital Health Spezi Web Design System

2 lines (1 loc) 4.29 kB
"use strict";const s=require("react/jsx-runtime"),O=require("./index-DwV9MiDB.js"),M=require("react"),h=require("./index-CLdeiAAB.js"),P=require("./index-UhBwhhTY.js"),g=require("./index-14SsMENa.js"),y=require("./index-CrhDNEEl.js"),$=require("./index-BM0VQ-wq.js"),F=require("./index-DZvjbfOs.js"),L=require("./index-DYxWvftI.js"),z=require("./index-DM1mqyG5.js");function K(e){const t=Object.create(null,{[Symbol.toStringTag]:{value:"Module"}});if(e){for(const o in e)if(o!=="default"){const a=Object.getOwnPropertyDescriptor(e,o);Object.defineProperty(t,o,a.get?a:{enumerable:!0,get:()=>e[o]})}}return t.default=e,Object.freeze(t)}const d=K(M);var k="Radio",[T,S]=h.createContextScope(k),[V,H]=T(k),w=d.forwardRef((e,t)=>{const{__scopeRadio:o,name:a,checked:r=!1,required:n,disabled:i,value:p="on",onCheck:u,form:f,...m}=e,[l,R]=d.useState(null),c=P.useComposedRefs(t,x=>R(x)),v=d.useRef(!1),b=l?f||!!l.closest("form"):!0;return s.jsxs(V,{scope:o,checked:r,disabled:i,children:[s.jsx(g.Primitive.button,{type:"button",role:"radio","aria-checked":r,"data-state":_(r),"data-disabled":i?"":void 0,disabled:i,value:p,...m,ref:c,onClick:h.composeEventHandlers(e.onClick,x=>{r||u==null||u(),b&&(v.current=x.isPropagationStopped(),v.current||x.stopPropagation())})}),b&&s.jsx(U,{control:l,bubbles:!v.current,name:a,value:p,checked:r,required:n,disabled:i,form:f,style:{transform:"translateX(-100%)"}})]})});w.displayName=k;var E="RadioIndicator",G=d.forwardRef((e,t)=>{const{__scopeRadio:o,forceMount:a,...r}=e,n=H(E,o);return s.jsx(z.Presence,{present:a||n.checked,children:s.jsx(g.Primitive.span,{"data-state":_(n.checked),"data-disabled":n.disabled?"":void 0,...r,ref:t})})});G.displayName=E;var U=e=>{const{control:t,checked:o,bubbles:a=!0,...r}=e,n=d.useRef(null),i=L.usePrevious(o),p=F.useSize(t);return d.useEffect(()=>{const u=n.current,f=window.HTMLInputElement.prototype,l=Object.getOwnPropertyDescriptor(f,"checked").set;if(i!==o&&l){const R=new Event("click",{bubbles:a});l.call(u,o),u.dispatchEvent(R)}},[i,o,a]),s.jsx("input",{type:"radio","aria-hidden":!0,defaultChecked:o,...r,tabIndex:-1,ref:n,style:{...e.style,...p,position:"absolute",pointerEvents:"none",opacity:0,margin:0}})};function _(e){return e?"checked":"unchecked"}var B=["ArrowUp","ArrowDown","ArrowLeft","ArrowRight"],C="RadioGroup",[W,te]=h.createContextScope(C,[y.createRovingFocusGroupScope,S]),j=y.createRovingFocusGroupScope(),I=S(),[X,Y]=W(C),q=d.forwardRef((e,t)=>{const{__scopeRadioGroup:o,name:a,defaultValue:r,value:n,required:i=!1,disabled:p=!1,orientation:u,dir:f,loop:m=!0,onValueChange:l,...R}=e,c=j(o),v=$.useDirection(f),[b,x]=h.useControllableState({prop:n,defaultProp:r,onChange:l});return s.jsx(X,{scope:o,name:a,required:i,disabled:p,value:b,onValueChange:x,children:s.jsx(y.Root,{asChild:!0,...c,orientation:u,dir:v,loop:m,children:s.jsx(g.Primitive.div,{role:"radiogroup","aria-required":i,"aria-orientation":u,"data-disabled":p?"":void 0,dir:v,...R,ref:t})})})});q.displayName=C;var N="RadioGroupItem",A=d.forwardRef((e,t)=>{const{__scopeRadioGroup:o,disabled:a,...r}=e,n=Y(N,o),i=n.disabled||a,p=j(o),u=I(o),f=d.useRef(null),m=P.useComposedRefs(t,f),l=n.value===r.value,R=d.useRef(!1);return d.useEffect(()=>{const c=b=>{B.includes(b.key)&&(R.current=!0)},v=()=>R.current=!1;return document.addEventListener("keydown",c),document.addEventListener("keyup",v),()=>{document.removeEventListener("keydown",c),document.removeEventListener("keyup",v)}},[]),s.jsx(y.Item,{asChild:!0,...p,focusable:!i,active:l,children:s.jsx(w,{disabled:i,required:n.required,checked:l,...u,...r,name:n.name,ref:m,onCheck:()=>n.onValueChange(r.value),onKeyDown:h.composeEventHandlers(c=>{c.key==="Enter"&&c.preventDefault()}),onFocus:h.composeEventHandlers(r.onFocus,()=>{var c;R.current&&((c=f.current)==null||c.click())})})})});A.displayName=N;var J="RadioGroupIndicator",D=d.forwardRef((e,t)=>{const{__scopeRadioGroup:o,...a}=e,r=I(o);return s.jsx(G,{...r,...a,ref:t})});D.displayName=J;var Q=q,Z=A,ee=D;const oe=Q,re=({className:e,...t})=>s.jsx(Z,{className:O.cn("focus-ring flex-center border-input size-4 shrink-0 rounded-full border disabled:cursor-not-allowed disabled:opacity-50",e),...t,children:s.jsx(ee,{className:"flex-center bg-primary size-2 rounded-full"})});exports.Radio=re;exports.RadioGroup=oe;