Skip to main content
independoc
Language: English

Custom Web Components Guide#

With independoc, you can embed interactive team-specific UI components directly inside Markdown without adding React or any JavaScript bundler.

Add a Component in 3 Steps#

  1. Place a vanilla JS Custom Element in static/

    Custom element tag names must contain a hyphen (-) per the HTML specification.

    static/hello-counter.js
    class HelloCounter extends HTMLElement {
      connectedCallback() {
        if (this._initialized) return;
        this._initialized = true;
    
        const initial = Number(this.getAttribute('start') || 0);
        let count = Number.isFinite(initial) ? initial : 0;
        const label = this.getAttribute('label') || 'Clicks';
        const resetLabel = this.getAttribute('reset-label') || 'Reset';
    
        this.innerHTML = '';
    
        const wrap = document.createElement('div');
        wrap.className = 'hc-wrap';
    
        const btn = document.createElement('button');
        btn.type = 'button';
        btn.className = 'nd-icon-btn hc-btn';
    
        const resetBtn = document.createElement('button');
        resetBtn.type = 'button';
        resetBtn.className = 'nd-icon-btn hc-reset';
        resetBtn.textContent = resetLabel;
    
        const update = () => {
          btn.textContent = `${label}: ${count}`;
        };
    
        btn.addEventListener('click', () => {
          count += 1;
          update();
        });
        resetBtn.addEventListener('click', () => {
          count = Number.isFinite(initial) ? initial : 0;
          update();
        });
    
        update();
        wrap.append(btn, resetBtn);
        this.append(wrap);
      }
    }
    
    if (!customElements.get('hello-counter')) {
      customElements.define('hello-counter', HelloCounter);
    }
  2. Register it in independoc.config.json

    independoc.config.json
    {
      "customScripts": ["/hello-counter.js"]
    }
  3. Use it directly in Markdown

    markdown
    <hello-counter start="1" label="Clicks" reset-label="Reset">
      <p>When JavaScript is disabled, this fallback text is displayed as-is.</p>
    </hello-counter>

Live Demo (<hello-counter>)#

This documentation site loads static/hello-counter.js and renders the interactive element below. Click the buttons to increment or reset the counter.

When JavaScript is disabled, this fallback text is displayed as-is.

Edit this pageLast updated:
Built with independoc itself — 0 npm dependencies.