UNPKG

primevue

Version:

[![License: MIT](https://img.shields.io/badge/License-MIT-yellow.svg)](https://opensource.org/licenses/MIT) [![npm version](https://badge.fury.io/js/primevue.svg)](https://badge.fury.io/js/primevue) [![Discord Chat](https://img.shields.io/discord/55794023

2 lines (1 loc) 2.96 kB
import e from"primevue/ripple";import{resolveDirective as t,withDirectives as i,openBlock as l,createElementBlock as n,normalizeClass as s,createElementVNode as o,mergeProps as a,createCommentVNode as u,toDisplayString as c}from"vue";var d={name:"ToggleButton",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,default:null},inputStyle:{type:null,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.onIcon&&this.onIcon.length>0&&this.offIcon&&this.offIcon.length>0},label(){return this.hasLabel?this.modelValue?this.onLabel:this.offLabel:" "}},directives:{ripple:e}};const r={class:"p-hidden-accessible"},h=["id","checked","value","aria-labelledby","aria-label"],p={class:"p-button-label"};d.render=function(e,d,b,f,m,L){const g=t("ripple");return i((l(),n("div",{ref:"container",class:s(L.buttonClass),onClick:d[2]||(d[2]=e=>L.onClick(e))},[o("span",r,[o("input",a({id:b.inputId,type:"checkbox",role:"switch",class:b.inputClass,style:b.inputStyle,checked:b.modelValue,value:b.modelValue,"aria-labelledby":e.ariaLabelledby,"aria-label":e.ariaLabel,onFocus:d[0]||(d[0]=e=>L.onFocus(e)),onBlur:d[1]||(d[1]=e=>L.onBlur(e))},b.inputProps),null,16,h)]),L.hasIcon?(l(),n("span",{key:0,class:s(L.iconClass)},null,2)):u("",!0),o("span",p,c(L.label),1)],2)),[[g]])};export{d as default};