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#
Place a vanilla JS Custom Element in
static/Custom element tag names must contain a hyphen (
-) per the HTML specification.static/hello-counter.jsclass 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); }Register it in
independoc.config.jsonindependoc.config.json{ "customScripts": ["/hello-counter.js"] }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.