|
| 1 | +var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) { |
| 2 | + var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d; |
| 3 | + if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc); |
| 4 | + else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r; |
| 5 | + return c > 3 && r && Object.defineProperty(target, key, r), r; |
| 6 | +}; |
| 7 | +import { LitElement } from "lit"; |
| 8 | +import tailwindCss from '../styles/tailwind.min.css?raw'; |
| 9 | +import { property } from "lit/decorators.js"; |
| 10 | +import { booleanStringFalseConverter } from "../utils/converters"; |
| 11 | +/** |
| 12 | + * TailwindElement is an abstract class that extends the LitElement base class. |
| 13 | + * It integrates Tailwind CSS support into custom web components. This class |
| 14 | + * handles the application of Tailwind CSS styles within the shadow DOM and |
| 15 | + * offers basic functionality for debugging and dynamic styling purposes. |
| 16 | + * |
| 17 | + * The class ensures Tailwind CSS styles are properly applied, even within the |
| 18 | + * encapsulated shadow DOM of a web component. |
| 19 | + */ |
| 20 | +export default class TailwindElement extends LitElement { |
| 21 | + constructor() { |
| 22 | + super(...arguments); |
| 23 | + /** |
| 24 | + * A boolean variable that indicates whether debugging mode is enabled. |
| 25 | + * When set to true, debugging features or additional log outputs may be activated. |
| 26 | + * When set to false, the debugging mode is disabled, and normal operation occurs. |
| 27 | + */ |
| 28 | + this.debug = false; |
| 29 | + /** |
| 30 | + * A boolean variable that indicates whether the current mode or feature |
| 31 | + * operates in a retro or vintage style. |
| 32 | + * |
| 33 | + * When set to `true`, the system or feature is expected to behave in a manner |
| 34 | + * resembling older or classic functionality. When set to `false`, it operates |
| 35 | + * in standard or modern mode. |
| 36 | + */ |
| 37 | + this.retroDesign = false; |
| 38 | + } |
| 39 | + /** |
| 40 | + * This method is called after the first update of the component's properties. |
| 41 | + * It ensures that TailwindCSS is applied correctly within the shadow DOM. |
| 42 | + * |
| 43 | + * @param {PropertyValues} _changedProperties - A map of properties that were changed |
| 44 | + * during the first update cycle. |
| 45 | + * @return {void} This method does not return a value. |
| 46 | + */ |
| 47 | + firstUpdated(_changedProperties) { |
| 48 | + super.firstUpdated(_changedProperties); |
| 49 | + this.ensureTailwindInShadow(); |
| 50 | + } |
| 51 | + /** |
| 52 | + * Processes a collection of class values, including strings, numbers, arrays, or objects, |
| 53 | + * and returns a concatenated string of valid class names. |
| 54 | + * |
| 55 | + * @param {...ClassValue[]} values - A list of class values that can include strings, numbers, |
| 56 | + * arrays, or objects. Arrays and objects are processed recursively. |
| 57 | + * Strings and numbers are trimmed before inclusion, and object keys are included if associated values evaluate to true. |
| 58 | + * @return {string} A single string containing all valid class names, separated by a space. |
| 59 | + */ |
| 60 | + cn(...values) { |
| 61 | + const out = []; |
| 62 | + const push = (v) => { |
| 63 | + if (!v) |
| 64 | + return; |
| 65 | + if (typeof v === 'string' || typeof v === 'number') { |
| 66 | + const s = String(v).trim(); |
| 67 | + if (s) |
| 68 | + out.push(s); |
| 69 | + return; |
| 70 | + } |
| 71 | + if (Array.isArray(v)) { |
| 72 | + for (const item of v) |
| 73 | + push(item); |
| 74 | + return; |
| 75 | + } |
| 76 | + if (typeof v === 'object') { |
| 77 | + for (const [key, val] of Object.entries(v)) { |
| 78 | + if (val) |
| 79 | + out.push(key.trim()); |
| 80 | + } |
| 81 | + } |
| 82 | + }; |
| 83 | + for (const v of values) |
| 84 | + push(v); |
| 85 | + return out.join(' '); |
| 86 | + } |
| 87 | + /** |
| 88 | + * Ensures that Tailwind CSS styles are applied within the shadow DOM of the current component. |
| 89 | + * |
| 90 | + * This method checks if the Tailwind CSS styles have been initialized on the constructor level. |
| 91 | + * If not, it assigns the Tailwind CSS stylesheet to the constructor. The method then creates |
| 92 | + * a `<style>` element containing the Tailwind CSS styles and prepends it to the shadow root of |
| 93 | + * the component. |
| 94 | + * |
| 95 | + * If an error occurs during the operation, it logs a message to the console. |
| 96 | + * |
| 97 | + * @return {void} This method does not return any value. |
| 98 | + */ |
| 99 | + ensureTailwindInShadow() { |
| 100 | + try { |
| 101 | + const ctor = this.constructor; |
| 102 | + if (!ctor.tailwindCssText) { |
| 103 | + ctor.tailwindCssText = tailwindCss; |
| 104 | + } |
| 105 | + const styleEl = document.createElement('style'); |
| 106 | + styleEl.textContent = ctor.tailwindCssText; |
| 107 | + this.shadowRoot?.prepend(styleEl); |
| 108 | + } |
| 109 | + catch (err) { |
| 110 | + console.error('Tailwind CSS konnte nicht geladen werden:', err); |
| 111 | + } |
| 112 | + } |
| 113 | +} |
| 114 | +__decorate([ |
| 115 | + property({ type: Boolean }) |
| 116 | +], TailwindElement.prototype, "debug", void 0); |
| 117 | +__decorate([ |
| 118 | + property({ type: Boolean, converter: booleanStringFalseConverter, attribute: 'retro-design' }) |
| 119 | +], TailwindElement.prototype, "retroDesign", void 0); |
0 commit comments