(function(s,i){typeof exports=="object"&&typeof module<"u"?i(exports,require("react/jsx-runtime"),require("react")):typeof define=="function"&&define.amd?define(["exports","react/jsx-runtime","react"],i):(s=typeof globalThis<"u"?globalThis:s||self,i(s.ReactFancySwitch={},s["react/jsx-runtime"],s.React))})(this,function(s,i,B){"use strict";function L(n){const u=Object.create(null,{[Symbol.toStringTag]:{value:"Module"}});if(n){for(const d in n)if(d!=="default"){const f=Object.getOwnPropertyDescriptor(n,d);Object.defineProperty(u,d,f.get?f:{enumerable:!0,get:()=>n[d]})}}return u.default=n,Object.freeze(u)}const r=L(B);function D({options:n,valueKey:u="value",labelKey:d="label",disabledKey:f="disabled",value:b,onChange:g,radioClassName:x,highlighterClassName:$,highlighterIncludeMargin:j=!1,highlighterStyle:E,disabledOptions:R=[],renderOption:O,...p}){var P;const w=r.useRef(null),m=r.useRef([]),y=r.useCallback(e=>typeof e!="object"?e:e[u],[u]),T=r.useCallback(e=>String(typeof e!="object"?e:e[d]),[d]),C=r.useCallback(e=>{const t=y(e);return R.includes(t)?!0:typeof e=="object"&&f in e?!!e[f]:!1},[R,y,f]),o=r.useMemo(()=>n.map(e=>({...typeof e=="object"?e:{},label:T(e),value:y(e),disabled:C(e)})),[n,y,T,C]),[l,I]=r.useState(()=>{if(b===void 0)return 0;const e=o.findIndex(t=>t.value===b);return e===-1?(console.warn(`FancySwitch: No option found for value "${b}". Defaulting to first option.`),0):e}),[N,W]=r.useState({height:0,width:0,transform:"translate(0, 0)"}),S=r.useCallback(()=>{const e=m.current[l],t=w.current;if(e&&t){const a=t.getBoundingClientRect(),c=e.getBoundingClientRect(),v=window.getComputedStyle(t),k=window.getComputedStyle(e),z={left:parseFloat(v.paddingLeft),top:parseFloat(v.paddingTop)},A={left:parseFloat(v.borderLeftWidth),top:parseFloat(v.borderTopWidth)},F={left:parseFloat(k.marginLeft),right:parseFloat(k.marginRight),top:parseFloat(k.marginTop),bottom:parseFloat(k.marginBottom)},q=c.left-a.left-z.left-A.left-(j?F.left:0),M=c.top-a.top-z.top-A.top-F.top;W({height:c.height,width:c.width+(j?F.left+F.right:0),transform:`translate(${q}px, ${M}px)`})}},[l,j]),h=r.useCallback(e=>{var t;o[e].disabled||((t=m.current[e])==null||t.focus(),I(e),g==null||g(o[e].value))},[o,g]),_=r.useCallback((e,t)=>{const a=t===l;return O?O({option:e,isSelected:a,getOptionProps:()=>({ref:c=>m.current[t]=c,role:"radio","aria-checked":a,tabIndex:a&&!e.disabled?0:-1,onClick:()=>h(t),className:x,...a?{"data-checked":!0}:{},...e.disabled?{"aria-disabled":!0,"data-disabled":!0}:{},"aria-label":`${e.label} option`})}):i.jsx("div",{ref:c=>m.current[t]=c,role:"radio","aria-checked":a,tabIndex:a&&!e.disabled?0:-1,onClick:()=>h(t),className:x,...a?{"data-checked":!0}:{},...e.disabled?{"aria-disabled":!0,"data-disabled":!0}:{},"aria-label":`${e.label} option`,children:e.label})},[l,O,x,h]);return r.useEffect(()=>{S()},[S]),r.useEffect(()=>{const e=new ResizeObserver(S);return w.current&&e.observe(w.current),()=>e.disconnect()},[S]),r.useEffect(()=>{const e=o.findIndex(t=>t.value===b);e!==-1&&e!==l&&I(e)},[b,o,l]),i.jsxs("div",{role:"radiogroup","aria-label":"Fancy switch options",ref:w,onKeyDown:e=>{var t;if((t=p.onKeyDown)==null||t.call(p,e),!e.defaultPrevented)switch(e.key){case"ArrowDown":case"ArrowRight":e.preventDefault();const a=(l+1)%n.length;h(a);break;case"ArrowUp":case"ArrowLeft":e.preventDefault();const c=(l-1+n.length)%n.length;h(c);break}},...p,children:[i.jsx("div",{className:$,style:{position:"absolute",transitionProperty:"all",transitionTimingFunction:"cubic-bezier(0.4, 0, 0.2, 1)",transitionDuration:"300ms",...N,...E},"aria-hidden":"true","data-highlighter":!0}),o.map((e,t)=>i.jsx(r.Fragment,{children:_(e,t)},e.value.toString())),i.jsxs("div",{"aria-live":"polite",style:{position:"absolute",width:"1px",height:"1px",padding:0,margin:"-1px",overflow:"hidden",clip:"rect(0, 0, 0, 0)",whiteSpace:"nowrap",borderWidth:0},children:[(P=o[l])==null?void 0:P.label," selected"]})]})}D.displayName="FancySwitch",s.FancySwitch=D,Object.defineProperty(s,Symbol.toStringTag,{value:"Module"})}); //# sourceMappingURL=react-fancy-switch.umd.cjs.map