UNPKG

@bothub-chat/ui

Version:

Bothub UI Components

2 lines (1 loc) 861 B
import{jsxs as e,jsx as t}from"react/jsx-runtime";import{useState as n,useRef as r,useEffect as i}from"react";import{SwitcherContainer as o,SwitcherSlider as l,SwitcherOption as c}from"./styled.js";const s=({options:s,defaultOption:u=0,onChange:d,className:f})=>{const[m,a]=n(u),[h,p]=n(0),[w,g]=n(0),C=r(null),v=r(null),j=r(null);i(()=>{k()},[m]),i(()=>{const e=()=>{k()};return window.addEventListener("resize",e),()=>window.removeEventListener("resize",e)},[m]);const k=()=>{const e=[v,j][m];if(e.current&&C.current){const t=C.current.getBoundingClientRect(),n=e.current.getBoundingClientRect(),r=n.left-t.left,{width:i}=n;p(r),g(i)}},x=()=>{a(e=>0===e?1:0),null==d||d(0===m?1:0)};return e(o,{ref:C,className:f,children:[t(l,{style:{left:h,width:w}}),t(c,{ref:v,onClick:x,children:s[0]}),t(c,{ref:j,onClick:x,children:s[1]})]})};export{s as SwitcherSelect};