UNPKG

@popeindustries/lit-html

Version:

Seamlessly and efficiently use @popeindustries/lit-html-server rendered HTML to hydrate lit-html templates in the browser

6 lines (5 loc) 1.23 kB
/** * @license * Copyright 2018 Google LLC * SPDX-License-Identifier: BSD-3-Clause */import{noChange as l}from"../lit-html.js";import{directive as o,Directive as d,PartType as u}from"../directive.js";class h extends d{constructor(i){var e;if(super(i),i.type!==u.ATTRIBUTE||i.name!=="class"||((e=i.strings)===null||e===void 0?void 0:e.length)>2)throw new Error("`classMap()` can only be used in the `class` attribute and must be the only part in the attribute.")}render(i){return" "+Object.keys(i).filter(e=>i[e]).join(" ")+" "}update(i,[e]){var t,r;if(this._previousClasses===void 0){this._previousClasses=new Set,i.strings!==void 0&&(this._staticClasses=new Set(i.strings.join(" ").split(/\s/).filter(s=>s!=="")));for(const s in e)e[s]&&!(!((t=this._staticClasses)===null||t===void 0)&&t.has(s))&&this._previousClasses.add(s);return this.render(e)}const a=i.element.classList;this._previousClasses.forEach(s=>{s in e||(a.remove(s),this._previousClasses.delete(s))});for(const s in e){const n=!!e[s];n!==this._previousClasses.has(s)&&!(!((r=this._staticClasses)===null||r===void 0)&&r.has(s))&&(n?(a.add(s),this._previousClasses.add(s)):(a.remove(s),this._previousClasses.delete(s)))}return l}}const _=o(h);export{_ as classMap};