All files / web-components/src index.js

78% Statements 39/50
66.66% Branches 20/30
85.71% Functions 12/14
77.55% Lines 38/49

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152                                      11x   2x     9x 1x     8x 11x       1x       5x 5x 5x 5x 5x       5x   5x 2x       5x       1x   1x     1x         2x 1x 1x 1x               1x 1x 1x               1x       7x       7x   7x             7x 7x               7x 7x                               11x 11x             1x                 1x        
/**
 * Coherent.js Web Components Integration
 * Provides custom element and web component utilities
 */
 
import { render } from '@coherent.js/core';
 
/**
 * Define a Coherent.js component as a custom element
 *
 * @param {string} name - Custom element tag name (must contain a hyphen)
 * @param {Function|Object} component - Coherent.js component (function or object)
 * @param {Object} [options] - Configuration options
 * @param {boolean} [options.shadow] - Use Shadow DOM for style encapsulation
 * @param {string[]} [options.observedAttributes] - Attributes to watch for changes
 * @param {Object} [options.defaults] - Default property values
 * @returns {Function|Object} The custom element class, or a server-side placeholder
 */
export function defineComponent(name, component, options = {}) {
  if (typeof window === 'undefined') {
    // Server-side: return a placeholder for SSR
    return { name, component, options };
  }
 
  if (!window.customElements) {
    throw new Error('Custom Elements API not supported');
  }
 
  const observedAttrs = options.observedAttributes || [];
  const defaults = options.defaults || {};
 
  class CoherentElement extends HTMLElement {
    static get observedAttributes() {
      return observedAttrs;
    }
 
    constructor() {
      super();
      this._props = { ...defaults };
      this._connected = false;
      this.component = component;
      this.options = options;
    }
 
    connectedCallback() {
      this._connected = true;
      // Copy initial attributes to props
      for (const attr of observedAttrs) {
        Iif (this.hasAttribute(attr)) {
          this._props[attr] = this.getAttribute(attr);
        }
      }
      this._render();
    }
 
    disconnectedCallback() {
      this._connected = false;
      // Cleanup: remove event listeners and clear content
      Iif (options.shadow && this.shadowRoot) {
        this.shadowRoot.innerHTML = '';
      } else {
        this.innerHTML = '';
      }
    }
 
    attributeChangedCallback(attrName, oldValue, newValue) {
      if (oldValue === newValue) return;
      this._props[attrName] = newValue;
      Eif (this._connected) {
        this._render();
      }
    }
 
    /**
     * Set a property and re-render
     */
    setProperty(key, value) {
      this._props[key] = value;
      Eif (this._connected) {
        this._render();
      }
    }
 
    /**
     * Get current props
     */
    getProperties() {
      return { ...this._props };
    }
 
    _render() {
      const componentDef = typeof this.component === 'function'
        ? this.component(this._props)
        : this.component;
 
      const html = render(componentDef);
 
      Iif (this.options.shadow) {
        if (!this.shadowRoot) {
          this.attachShadow({ mode: 'open' });
        }
        this.shadowRoot.innerHTML = html;
        this._delegateEvents(this.shadowRoot);
      } else {
        this.innerHTML = html;
        this._delegateEvents(this);
      }
    }
 
    /**
     * Delegate data-action events to component handlers
     */
    _delegateEvents(root) {
      const actionElements = root.querySelectorAll('[data-action]');
      for (const el of actionElements) {
        const action = el.dataset.action;
        const [eventType, handlerName] = action.includes(':')
          ? action.split(':')
          : ['click', action];
 
        el.addEventListener(eventType, (event) => {
          this.dispatchEvent(new CustomEvent('coherent-action', {
            bubbles: true,
            detail: { action: handlerName, event, element: el }
          }));
        });
      }
    }
  }
 
  window.customElements.define(name, CoherentElement);
  return CoherentElement;
}
 
/**
 * Integration utilities for runtime environments
 */
export function integrateWithWebComponents(_runtime) {
  return {
    defineComponent: (name, component, options) => defineComponent(name, component, options)
  };
}
 
/**
 * Alias for defineComponent
 */
export function defineCoherentElement(name, component, options = {}) {
  return defineComponent(name, component, options);
}
 
export { defineComponent as default };