UNPKG

react-custom-switcher

Version:

Multi-option Switch/Toggle component for React with dragging, snap and customizable UI

2 lines (1 loc) 8.69 kB
!function(e,o){"object"==typeof exports&&"undefined"!=typeof module?o(exports,require("react")):"function"==typeof define&&define.amd?define(["exports","react"],o):o((e="undefined"!=typeof globalThis?globalThis:e||self)["react-custom-switcher"]={},e.React)}(this,(function(e,o){"use strict";const t="#666",i="#cacaca",r=(e,o)=>o?i:e||t,a=(e,o,t)=>e<0?0:e+t>o?o-t:e,n=(e,o)=>{if(e){const e=document.body.style.overflow;o({height:document.body.style.height,overflow:e}),document.body.style.overflow="hidden",document.body.style.height="100%"}},s=(e,o)=>{e&&(document.body.style.overflow=o.overflow,document.body.style.height=o.height)};e.CustomSwitcher=({options:e,value:d,containerWidth:l,variant:c="primary",switchSize:u,dragEnabled:b=!0,scaleWhileDrag:v=1.2,disabled:m=!1,cssOverrides:p={},callback:h})=>{const y=o.useMemo((()=>((e,o)=>e||("secondary"===o?46:36))(u,c)),[u,c]),f=o.useMemo((()=>(()=>{const e=navigator.userAgent||navigator.vendor;return!(!/(android|bb\d+|meego).+mobile|avantgo|bada\/|blackberry|blazer|compal|elaine|fennec|hiptop|iemobile|ip(hone|od)|iris|kindle|lge |maemo|midp|mmp|mobile.+firefox|netfront|opera m(ob|in)i|palm( os)?|phone|p(ixi|re)\/|plucker|pocket|psp|series(4|6)0|symbian|treo|up\.(browser|link)|vodafone|wap|windows ce|xda|xiino|android|ipad|playbook|silk/i.test(e)&&!/1207|6310|6590|3gso|4thp|50[1-6]i|770s|802s|a wa|abac|ac(er|oo|s-)|ai(ko|rn)|al(av|ca|co)|amoi|an(ex|ny|yw)|aptu|ar(ch|go)|as(te|us)|attw|au(di|-m|r |s )|avan|be(ck|ll|nq)|bi(lb|rd)|bl(ac|az)|br(e|v)w|bumb|bw-(n|u)|c55\/|capi|ccwa|cdm-|cell|chtm|cldc|cmd-|co(mp|nd)|craw|da(it|ll|ng)|dbte|dc-s|devi|dica|dmob|do(c|p)o|ds(12|-d)|el(49|ai)|em(l2|ul)|er(ic|k0)|esl8|ez([4-7]0|os|wa|ze)|fetc|fly(-|_)|g1 u|g560|gene|gf-5|g-mo|go(\.w|od)|gr(ad|un)|haie|hcit|hd-(m|p|t)|hei-|hi(pt|ta)|hp( i|ip)|hs-c|ht(c(-| |_|a|g|p|s|t)|tp)|hu(aw|tc)|i-(20|go|ma)|i230|iac( |-|\/)|ibro|idea|ig01|ikom|im1k|inno|ipaq|iris|ja(t|v)a|jbro|jemu|jigs|kddi|keji|kgt( |\/)|klon|kpt |kwc-|kyo(c|k)|le(no|xi)|lg( g|\/(k|l|u)|50|54|-[a-w])|libw|lynx|m1-w|m3ga|m50\/|ma(te|ui|xo)|mc(01|21|ca)|m-cr|me(rc|ri)|mi(o8|oa|ts)|mmef|mo(01|02|bi|de|do|t(-| |o|v)|zz)|mt(50|p1|v )|mwbp|mywa|n10[0-2]|n20[2-3]|n30(0|2)|n50(0|2|5)|n7(0(0|1)|10)|ne((c|m)-|on|tf|wf|wg|wt)|nok(6|i)|nzph|o2im|op(ti|wv)|oran|owg1|p800|pan(a|d|t)|pdxg|pg(13|-([1-8]|c))|phil|pire|pl(ay|uc)|pn-2|po(ck|rt|se)|prox|psio|pt-g|qa-a|qc(07|12|21|32|60|-[2-7]|i-)|qtek|r380|r600|raks|rim9|ro(ve|zo)|s55\/|sa(ge|ma|mm|ms|ny|va)|sc(01|h-|oo|p-)|sdk\/|se(c(-|0|1)|47|mc|nd|ri)|sgh-|shar|sie(-|m)|sk-0|sl(45|id)|sm(al|ar|b3|it|t5)|so(ft|ny)|sp(01|h-|v-|v )|sy(01|mb)|t2(18|50)|t6(00|10|18)|ta(gt|lk)|tcl-|tdg-|tel(i|m)|tim-|t-mo|to(pl|sh)|ts(70|m-|m3|m5)|tx-9|up(\.b|g1|si)|utst|v400|v750|veri|vi(rg|te)|vk(40|5[0-3]|-v)|vm40|voda|vulc|vx(52|53|60|61|70|80|81|83|85|98)|w3c(-| )|webc|whit|wi(g |nc|nw)|wmlb|wonu|x700|yas-|your|zeto|zte-/i.test(e.substring(0,4)))})()),[]),g=o.useMemo((()=>(({containerWidth:e,switchSize:o,cssOverrides:r})=>({root:{color:"#777"},container:{width:e,height:o,position:"relative",display:"flex",alignItems:"center",WebkitTapHighlightColor:"transparent",WebkitTouchCallout:"none",userSelect:"none"},switch:{width:o,height:o,borderRadius:"50%",cursor:r.cursorGrab?r.cursorGrab:"grab",opacity:1,transition:"all 0.4s",pointerEvents:"all",boxSizing:"border-box"},draggable:{position:"absolute",zIndex:1},switchOverride:{...r.switch},switchPrimary:{backgroundColor:t},switchSecondary:{border:`5px solid ${t}`},switchDisabled:{...r.switchDisabled},switchDisabledPrimary:{backgroundColor:i},switchDisabledSecondary:{borderColor:i},grabbing:{cursor:r.cursorGrabbing?r.cursorGrabbing:"grabbing"},transition:{transition:"transform 200ms ease-out"},divisionsWrap:{position:"absolute",width:e-o,height:"100%",top:"50%",left:"50%",transform:"translate3d(-50%,-50%,0)",display:"flex",alignItems:"center"},divWrap:{position:"absolute",top:"50%",width:o,height:o,display:"flex",justifyContent:"center",alignItems:"center"},division:{boxSizing:"border-box",position:"absolute",width:20,height:20,cursor:"pointer"},divisionPrimary:{borderRadius:"50%",backgroundColor:i,border:`5px solid ${i}`},divisionSecondary:{},divisionOverride:{...r.division},divLine:{height:5,width:e-o,borderRadius:2.5,backgroundColor:i},divLineSecondary:{backgroundColor:"transparent"},divLineOverride:{...r.divisionLine},label:{position:"absolute",cursor:"pointer"},labelPrimary:{top:"130%",fontSize:13},labelSecondary:{top:"50%",transform:"translateY(-50%)",fontSize:14},labelDisabled:{opacity:.6},labelOverride:{...r.label},defaultCursor:{cursor:r.cursorDefault?r.cursorDefault:"pointer"},disabledCursor:{cursor:r.cursorDisabled},defaultDisabledCursor:{cursor:"default !important"},stopScrolling:{height:"100%",overflow:"hidden"}}))({containerWidth:l,switchSize:y,cssOverrides:p})),[l,y,p]),w=o.useRef(null),k=o.useRef(null),[E,D]=o.useState(d),[S,x]=o.useState(!1),[C,L]=o.useState(!1),[I,O]=o.useState(),[z,_]=o.useState(0),[W,T]=o.useState(0),[P,R]=o.useState({height:document.body.style.height,overflow:document.body.style.overflow}),A=(l-y)/(e.length-1),j=o.useCallback((o=>{E!==e[o].value&&(D(e[o].value),h(e[o].value)),x(!0),L(!1)}),[h,e,E]),M=o.useCallback((()=>{L(!0),x(!1)}),[]),$=(o,t)=>{t.stopPropagation(),x(!0),E!==e[o].value&&(D(e[o].value),h(e[o].value))};o.useEffect((()=>{const o=e.findIndex((e=>e.value===E));o>=0&&(_(A*o),T(A*o))}),[E,e,A]),o.useEffect((()=>{D(d)}),[d]),o.useEffect((()=>{const e=e=>{if(null==e.relatedTarget){s(f,P);const e=Math.abs(Math.round(z/A));T(e*A),j(e),L(!1)}};return b&&document.addEventListener("pointerout",e),()=>document.removeEventListener("pointerout",e)}),[z,A,j,b,f,P]),o.useEffect((()=>{const e=()=>{const e=Math.abs(Math.round(z/A));_(e*A),s(f,P),T(e*A),j(e),L(!1)};return b&&(f?document.body.addEventListener("touchend",e):document.body.addEventListener("pointerup",e)),()=>{document.body.removeEventListener("touchend",e),document.body.removeEventListener("pointerup",e)}}),[z,A,j,b,f,P]),o.useEffect((()=>{const e=e=>{if(e.preventDefault(),M(),k.current&&w.current){const o=W+(e.touches[0].clientX-I);_(a(o,w.current.offsetWidth,k.current.offsetWidth))}},o=e=>{if(e.preventDefault(),M(),k.current&&w.current){const o=W+(e.clientX-I);_(a(o,w.current.offsetWidth,k.current.offsetWidth))}};return b&&C&&(f?document.body.addEventListener("touchmove",e):document.body.addEventListener("pointermove",o)),()=>{document.body.removeEventListener("touchmove",e),document.body.removeEventListener("pointermove",o)}}),[C,w,k,b,M,W,I,f]);const q=(e,o)=>{const t=o.find((o=>o.value===e))?.color;return t};return o.createElement("div",{style:g.root},o.createElement("div",{style:g.container,ref:w},o.createElement("div",{style:{...g.draggable,...S?{...g.transition}:void 0,transform:`translateX(${z}px)`},ref:k,onPointerDown:f||m||!b?void 0:e=>{n(f,R),L(!0),O(e.clientX)},onTouchStart:f&&!m&&b?e=>{n(f,R),L(!0),O(e.touches[0].clientX)}:void 0},o.createElement("div",{style:{...g.switch,..."primary"===c?{...g.switchPrimary}:void 0,..."secondary"===c?{...g.switchSecondary}:void 0,...C?{...g.grabbing}:void 0,...m&&"primary"===c?{...g.switchDisabledPrimary}:void 0,...m&&"secondary"===c?{...g.switchDisabledSecondary}:void 0,...m&&p.switchDisabled?{...g.switchDisabled}:void 0,...m||!b?{...g.defaultDisabledCursor}:void 0,transform:C?`scale(${F=v,"number"==typeof F?F:"boolean"==typeof F&&F?1.2:1})`:"scale(1)",..."primary"===c?{backgroundColor:r(q(E,e),m)}:void 0,..."secondary"===c?{borderColor:r(q(E,e),m)}:void 0,...g.switchOverride}})),o.createElement("div",{style:g.divisionsWrap},o.createElement("div",{style:{...g.divLine,..."secondary"===c?{...g.divLineSecondary}:void 0,...g.divLineOverride}}),e.map(((e,t)=>o.createElement("div",{key:`key-${e.value}-${t}`,style:{...g.divWrap,...g.defaultCursor,transform:`translate3d(calc(${t*A}px - 50%), -50%, 0)`},onPointerDown:m||e.value===E?void 0:e=>$(t,e)},o.createElement("div",{style:{...g.division,..."primary"===c?{...g.divisionPrimary}:void 0,...m&&!p.cursorDisabled?{...g.defaultDisabledCursor}:void 0,...g.divisionOverride,...m&&p.cursorDisabled?{...g.disabledCursor}:void 0}}),e.label&&o.createElement("div",{style:{...g.label,..."primary"===c?{...g.labelPrimary}:void 0,..."secondary"===c?{...g.labelSecondary}:void 0,...g.defaultCursor,...g.labelOverride,...m?{...g.labelDisabled}:void 0,...p.cursorDisabled||!m&&E!==e.value?void 0:{...g.defaultDisabledCursor},...p.cursorDisabled&&(m||E===e.value)?{...g.disabledCursor}:void 0},onPointerDown:m?void 0:e=>$(t,e)},e.label)))))));var F},e.DEFAULT_BORDER_SIZE=5,e.DEFAULT_DIVISION_COLOR=i,e.DEFAULT_DIVISION_LINE_HEIGHT=5,e.DEFAULT_DIVISION_POINT_SIZE=20,e.DEFAULT_SCALE_WHILE_DRAG=1.2,e.DEFAULT_SWITCH_COLOR=t}));