---
name: happy-dom
description: Documents verified Happy DOM shadow DOM event behavior and guides agents away from unnecessary re-entrancy guards.
title: happy-dom
canonical_url: https://skilld.dev/gh/corey-alix/dwp-hours/happy-dom
last_updated: 2026-10-07T01:30:59.000Z
---

> Index of every page on this site: [llms.txt](https://skilld.dev/llms.txt)

> **Skill from skilld.dev.** Follow the user's requested action.
> If the user asked to fork this Skill, follow the fork workflow below. Do not execute the Skill instructions while copying it.
> Otherwise, follow the instructions below for this session. You do not need to install anything.
>
> If the user asked to install this Skill, run `npx skilld install corey-alix/dwp-hours/happy-dom`. Install writes the Skill files into the project, so every session loads them.
>
> ## Fork workflow
>
> A fork creates an editable local Skill with its original author and licence. The request authorizes copying and local installation.
> 1. Check `./skills/happy-dom`, the project lockfile, and selected Agent targets together. If the local directory or installed Skill exists, stop. Never overwrite an existing directory or Agent target.
> 2. Read [source metadata](https://skilld.dev/api/v1/skills/corey-alix/dwp-hours/happy-dom) once. Use sourceUrl, sourceCommit, skillPath, sourceGone, and license. If the source is gone or its path is missing, stop. If license is null, read licence files at the source commit.
> 3. Fetch only the source commit into a temporary Git repository. Do not clone full history. Derive repository_url from sourceUrl, including repository renames. If sourceCommit is absent, resolve the sourceUrl ref once. Set source_commit to that actual commit. Run these commands in one shell call:
>
> ```sh
> git init --quiet "$temporary_dir"
> git -C "$temporary_dir" fetch --quiet --depth=1 "$repository_url" "$source_commit"
> git -C "$temporary_dir" checkout --quiet --detach FETCH_HEAD
> ```
>
> Read applicable licence declarations and notices at that commit. If copying is not permitted, report the restriction and stop.
> 4. Inspect source entries together, then copy the directory containing skillPath into `./skills/happy-dom`. Use the user's path if selected. Keep the original SKILL.md, relative links, scripts, binary assets, and executable modes. Exclude .git metadata. Reject symlinks and paths outside the Skill directory. After checking entries, use cp -a where available. A regular source directory needs no custom copy script. Do not save this page wrapper as SKILL.md.
> Preserve author credit, notices, and applicable licence files from repository or parent directories. Add PROVENANCE.md with the Skill page, source URL, actual commit, original path, and licence. Retain any existing PROVENANCE.md and record new provenance separately. Batch source inspection, copying, and provenance work where practical.
> 5. In the project root, run `skilld install ./skills/happy-dom --mode copy --plain`. If skilld is unavailable, use `npx skilld install ./skills/happy-dom --mode copy --plain`. This known command needs no help lookup. Install does not support --json. Use detected Agent targets, or add --agent for the targets the user selected. Install the local path, never the upstream selector. If installation fails, preserve the local copy and report the exact failure.
> 6. Confirm the local lockfile source and installed Agent copies once. Report the local path, actual commit, and Agent targets. After edits, reinstall the same local path. Upstream updates must not replace it. Do not publish or push unless the user asks.

# Happy DOM Event Handling

## Description

Documents verified Happy DOM shadow DOM event behavior and guides agents away from unnecessary re-entrancy guards. Happy DOM correctly enforces shadow DOM event boundaries — events dispatched on a host element do **not** re-enter its shadow tree. When duplicate handler calls occur in tests, the cause is application-level bugs (typically listener accumulation), not Happy DOM defects.

## Trigger

- Adding or reviewing re-entrancy guards in web component event handlers
- Debugging duplicate event handling in Happy DOM unit tests
- Investigating whether Happy DOM leaks events across shadow boundaries
- Proxy/facade pattern: re-dispatching an event from shadowRoot listener onto the host

## Response Pattern

1. **Assume Happy DOM is correct** — Happy DOM (v20.6.1+) correctly enforces shadow DOM event boundaries. Events dispatched on the host element (`this`) do not propagate back into `this.shadowRoot` listeners. This was verified in the AdminPanel investigation (see [issue-endless-loop-employee-acknowledge.md](../../../TASKS/issue-endless-loop-employee-acknowledge.md)).
2. **Look for listener accumulation** — The most common cause of duplicate handler calls is listeners being registered multiple times. Check whether `setupEventDelegation()` overrides guard against re-registration across re-renders.
3. **Check for feedback loops** — If the handler dispatches an event of the same type, verify the listener target (`this` vs `this.shadowRoot`) prevents self-triggering.
4. **Never add a re-entrancy guard to paper over duplicates** — A `_handlingEvent` flag silently drops duplicate calls, masking the real bug (e.g., N listeners where only 1 should exist). Fix the root cause instead.

## Examples

- "My handler is called twice — is Happy DOM broken?" → No. Check for duplicate listener registration.
- "Should I add a `_handlingEvent` guard?" → No. Find and fix the source of re-entrancy.
- "Does `composed: true` cause events to loop back into the shadow tree?" → No. Happy DOM correctly scopes dispatch.
- "setupEventDelegation is called on every render — is that safe?" → Only if custom listeners are guarded against re-registration.

## Additional Context

### Verified Happy DOM Behavior (v20.6.1)

These behaviors were **verified by test** in this project, not assumed from documentation:

- `shadowRoot.addEventListener("x", handler)` — handler fires when event dispatched inside the shadow tree
- `this.dispatchEvent(new CustomEvent("x", { bubbles: true }))` on the host — does **NOT** trigger `shadowRoot` listeners for "x"
- `composed: true` allows events to cross shadow boundaries **outward** (child → host → parent), not **inward** (host → its own shadow tree)
- `stopPropagation()` on the original event plus re-dispatch on host is a safe proxy pattern — no cycle occurs

### The Listener Accumulation Pattern (Confirmed Root Cause)

The AdminPanel "infinite loop" was caused by **listener accumulation**, not Happy DOM:

1. `BaseComponent.connectedCallback()` calls `setupEventDelegation()` → adds custom listeners on `shadowRoot`
2. Then calls `update()` → `renderTemplate()` → `setupEventDelegation()` again → adds **duplicate** listeners
3. Each `requestUpdate()` → `renderTemplate()` → another `setupEventDelegation()` → more duplicates
4. `shadowRoot` listeners survive `innerHTML` replacement (they're on the root, not on child elements)
5. Result: N listeners fire for 1 event → handler called N times → looks like re-entrancy

**Fix**: Add a `_customEventsSetup` boolean guard in `setupEventDelegation()` overrides, same pattern `BaseComponent` uses with `isEventDelegationSetup`.

```ts
protected setupEventDelegation() {
  super.setupEventDelegation();
  if (this._customEventsSetup) return;  // Prevent duplicate listeners
  this._customEventsSetup = true;

  this.shadowRoot.addEventListener("my-event", (e) => {
    e.stopPropagation();
    this.handleCustomEvent(e as CustomEvent);
  });
}
```

### Why Re-entrancy Guards Are Wrong Here

```ts
// BAD: Masks the real bug (duplicate listeners)
private _handlingEvent = false;
handleCustomEvent(e: Event): void {
  if (this._handlingEvent) return;  // Silently drops calls 2..N
  this._handlingEvent = true;
  try { /* … */ } finally { this._handlingEvent = false; }
}
```

This "works" but:

- Silently drops legitimate duplicate calls that indicate a bug
- Tests can only assert `callCount ≤ 2` instead of `callCount === 1`
- The guard remains forever, obscuring whether the underlying bug is fixed

### Rules for Agents

- **Happy DOM shadow DOM event boundaries are correct** — do not claim otherwise without a minimal repro
- **Never add `_handlingEvent` guards** — find and fix the source of duplicate calls
- **Check `setupEventDelegation` overrides** for missing re-registration guards whenever duplicate handler calls are observed
- **Assert exact call counts in tests** (`toBe(1)`, not `toBeLessThanOrEqual(2)`) to catch listener accumulation regressions
