v-show-case
Version:
conditional logic for vue's v-show directive
3 lines (2 loc) • 1.77 kB
JavaScript
(function(o,i){typeof exports=="object"&&typeof module<"u"?i(exports,require("vue")):typeof define=="function"&&define.amd?define(["exports","vue"],i):(o=typeof globalThis<"u"?globalThis:o||self,i(o.vShowCase={},o.Vue))})(this,function(o,i){"use strict";function d(e,{vShowWhen:n,vShowElse:t}){var s,f;const r=(s=e.dirs)==null?void 0:s.find(a=>a.dir===n);return r?r.value:!!((f=e.dirs)!=null&&f.find(a=>a.dir===t))}const p={name:"VShowCase",render(){const e=i.resolveDirective("showWhen"),n=i.resolveDirective("showElse");let t=!1,r;return this.$slots.default().map(s=>(t?r=!1:(r=d(s,{vShowWhen:e,vShowElse:n}),r&&(t=!0)),i.withDirectives(s,[[i.vShow,r]])))}},l="0",w="1",v="2",u="3",S=`^(${w}${u})+(${v}${u})?$`;function E(e){return new RegExp(S).test(m(e))}function m(e){let n=[];return Array.isArray(e)?(e.filter(t=>t.type!==3).forEach(t=>{t.type!==1&&h("v-show-case children must be an element or component",t.loc);let r="";t.props.find(c=>c.name==="for"||c.name==="if")&&h("v-show-case children cannot contain v-if or v-for directives",t.loc);const f=t.props.find(c=>c.name==="show-when");f&&(f.exp||h("v-show-when directive requires an argument",t.loc),r+=w);const a=t.props.find(c=>c.name==="show-else");a&&(a.exp&&h("v-show-else directive does not take any arguments",t.loc),r+=v),r===""&&(r+=l),r+=u,n.push(r)}),n.length||n.push(l)):n.push(l),n.join("")}function h(e,n){throw new Error(`${e} (Line: ${n.start.line})
${" ".repeat(n.start.column)}${n.source}`)}function R(e){e.tag==="v-show-case"&&(E(e.children)||h("Invalid syntax for v-show-case",e.loc))}const g={install(e){e.component("v-show-case",p),e.directive("showWhen",{}),e.directive("showElse",{})}};o.vShowCase=g,o.validateVShowCase=R,Object.defineProperty(o,Symbol.toStringTag,{value:"Module"})});