primevue
Version:
[](https://opensource.org/licenses/MIT) [](https://badge.fury.io/js/primevue) [ • 3.19 kB
JavaScript
import e from"primevue/basecomponent";import t from"primevue/ripple";import{resolveDirective as i,withDirectives as l,openBlock as n,createElementBlock as s,mergeProps as o,createElementVNode as a,renderSlot as u,normalizeClass as c,createCommentVNode as d,toDisplayString as r}from"vue";var p={name:"ToggleButton",extends:e,emits:["update:modelValue","change","click","focus","blur"],props:{modelValue:Boolean,onIcon:String,offIcon:String,onLabel:{type:String,default:"Yes"},offLabel:{type:String,default:"No"},iconPos:{type:String,default:"left"},disabled:{type:Boolean,default:!1},tabindex:{type:Number,default:null},inputId:{type:String,default:null},inputClass:{type:[String,Object],default:null},inputStyle:{type:Object,default:null},inputProps:{type:null,default:null},"aria-labelledby":{type:String,default:null},"aria-label":{type:String,default:null}},outsideClickListener:null,data:()=>({focused:!1}),mounted(){this.bindOutsideClickListener()},beforeUnmount(){this.unbindOutsideClickListener()},methods:{onClick(e){this.disabled||(this.$emit("update:modelValue",!this.modelValue),this.$emit("change",e),this.$emit("click",e),this.focused=!0)},onFocus(e){this.focused=!0,this.$emit("focus",e)},onBlur(e){this.focused=!1,this.$emit("blur",e)},bindOutsideClickListener(){this.outsideClickListener||(this.outsideClickListener=e=>{this.focused&&!this.$refs.container.contains(e.target)&&(this.focused=!1)},document.addEventListener("click",this.outsideClickListener))},unbindOutsideClickListener(){this.outsideClickListener&&(document.removeEventListener("click",this.outsideClickListener),this.outsideClickListener=null)}},computed:{buttonClass(){return["p-button p-togglebutton p-component",{"p-focus":this.focused,"p-button-icon-only":this.hasIcon&&!this.hasLabel,"p-disabled":this.disabled,"p-highlight":!0===this.modelValue}]},iconClass(){return[this.modelValue?this.onIcon:this.offIcon,"p-button-icon",{"p-button-icon-left":"left"===this.iconPos&&this.label,"p-button-icon-right":"right"===this.iconPos&&this.label}]},hasLabel(){return this.onLabel&&this.onLabel.length>0&&this.offLabel&&this.offLabel.length>0},hasIcon(){return this.$slots.icon||this.onIcon&&this.offIcon},label(){return this.hasLabel?this.modelValue?this.onLabel:this.offLabel:" "}},directives:{ripple:t}};const h=["id","checked","value","aria-labelledby","aria-label"];p.render=function(e,t,p,b,f,m){const L=i("ripple");return l((n(),s("div",o({ref:"container",class:m.buttonClass,onClick:t[2]||(t[2]=e=>m.onClick(e))},e.ptm("root")),[a("span",o({class:"p-hidden-accessible"},e.ptm("hiddenInputWrapper")),[a("input",o({id:p.inputId,type:"checkbox",role:"switch",class:p.inputClass,style:p.inputStyle,checked:p.modelValue,value:p.modelValue,"aria-labelledby":e.ariaLabelledby,"aria-label":e.ariaLabel,onFocus:t[0]||(t[0]=e=>m.onFocus(e)),onBlur:t[1]||(t[1]=e=>m.onBlur(e))},{...p.inputProps,...e.ptm("hiddenInput")}),null,16,h)],16),u(e.$slots,"icon",{value:p.modelValue,class:c(m.iconClass)},(()=>[p.onIcon||p.offIcon?(n(),s("span",o({key:0,class:m.iconClass},e.ptm("icon")),null,16)):d("",!0)])),a("span",o({class:"p-button-label"},e.ptm("label")),r(m.label),17)],16)),[[L]])};export{p as default};