# When and how should you use lwc:dom="manual" for programmatic DOM manipulation?

**Topic:** LWC · **Difficulty:** Medium · **Tags:** LWC, DOM Manipulation, Manual DOM

Read it on the site: https://www.forcechallenges.com/interview-questions/when-and-how-should-you-use-lwc-dom-manual-for-programmatic-dom-manipulation

## Answer

**Basics:** LWC owns its DOM and normally forbids manual DOM edits inside the template, because they would conflict with its diffing. When a library such as a charting or map tool wants to draw into a container, you mark the container as manually managed so the engine stays out of it.

**How it works:**
1. Add `<div lwc:dom="manual" class="host"></div>` in the template.
2. In JS, after render (usually in a guarded `renderedCallback`), get it with `this.template.querySelector('.host')`.
3. Insert nodes: `el.appendChild(node)` or set `el.innerHTML`, or pass the element to a third-party library.
4. The engine ignores the children you add and does not remove or diff them.

**Advanced / deep dive:** The directive is only valid on native elements (for example `div`, `span`), not on custom or base components and not on `<template>`. Elements added manually are not scoped to the component's CSS in synthetic shadow, so style them inline, via the library, or with global/static resource CSS; this is a frequent surprise. Inserting HTML strings creates an XSS risk: only insert trusted or sanitized content, and note that Lightning Web Security or Locker may restrict or sanitize some operations. For displaying rich text from fields use `lightning-formatted-rich-text`, which sanitizes. You are responsible for cleanup: remove nodes and destroy library instances in `disconnectedCallback` to avoid leaks. Because the engine will not re-render the manual region, you must update it yourself when data changes, usually in `renderedCallback` guarded by a flag or when a setter runs. Prefer native LWC templates whenever possible; this is an escape hatch.

**Code / config example:**
```html
<template>
    <div class="chart-host" lwc:dom="manual"></div>
</template>
```
```javascript
import { LightningElement } from 'lwc';
import { loadScript } from 'lightning/platformResourceLoader';
import CHARTLIB from '@salesforce/resourceUrl/chartlib';

export default class ManualChart extends LightningElement {
    ready = false;
    async renderedCallback() {
        if (this.ready) return;
        this.ready = true;
        await loadScript(this, CHARTLIB);
        const host = this.template.querySelector('.chart-host');
        const canvas = document.createElement('canvas');
        host.appendChild(canvas);          // manual DOM is allowed here
        // new Chart(canvas, {...}); // library draws into canvas
    }
    disconnectedCallback() { /* destroy chart instance here */ }
}
```

**Hidden gotchas & must-remember points:**
- It works only on native HTML elements, never on `<template>` or custom components.
- Manually inserted nodes do not get the component's scoped CSS in synthetic shadow.
- `innerHTML` with untrusted data is an XSS hole; use sanitizing components.
- Without the directive, manual DOM changes throw or are ignored.
- Always guard `renderedCallback` and clean up in `disconnectedCallback`.
- Lightning Web Security can block certain libraries, so test in a real org.

**Best practice:** Treat `lwc:dom="manual"` as a last resort for third-party integration, with trusted content and explicit cleanup.

---
Source: ForceChallenges · last updated 2026-11-02
