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