Composed events and component public contracts
Quick answer
Events in a shadow tree can expose a component’s public actions without exposing internal elements. Bubbling and crossing a shadow boundary are separate event options.
How it works
For an intentional public notification dispatched from inside a root, construct new CustomEvent("quantity-change", {detail: value, bubbles: true, composed: true}). composed permits crossing shadow boundaries; bubbles enables ordinary ancestor bubbling. CustomEvent defaults are false for both. Outside listeners may see the host as event.target because of retargeting. composedPath returns the observable event path; closed shadow internals are hidden from outside listeners. Publish a small validated detail contract and do not send private data. Use native controls inside components rather than simulating keyboard behavior with div elements.
Local examples
Run in a fresh browser document with local synthetic data. Actual ByteMind editor support is unverified.
(async () => {const host=document.createElement("div");const root=host.attachShadow({mode:"open"});const button=document.createElement("button");root.append(button);document.body.append(host);const log=[];document.body.addEventListener("lesson-change",e=>log.push(e.target===host));for(const composed of [false,true])button.dispatchEvent(new CustomEvent("lesson-change",{bubbles:true,composed}));console.log(JSON.stringify(log));})()Expected console output:
[true]Only the composed event reaches the outer listener; its target is retargeted to the host.
Run in a fresh browser document with local synthetic data. Actual ByteMind editor support is unverified.
(async () => {const host=document.createElement("div");const root=host.attachShadow({mode:"open"});const button=document.createElement("button");root.append(button);document.body.append(host);host.addEventListener("lesson-signal",e=>console.log(e.target===host,e.composedPath()[0]===button));button.dispatchEvent(new CustomEvent("lesson-signal",{bubbles:true,composed:true}));})()Expected console output:
true trueThe external target is the host, but the open-root path includes the original button.
Common mistakes
Do not assume bubbles alone crosses a shadow boundary or depend on the exact internal node in an external event handler.
Practice
Emit a public quantity change from a native button inside an open shadow root. Compare listener results with composed false and true.
Check Your Understanding
Solidify your learning with the interactive assessment directly below this lesson.
Concept Quiz & Questions
Composed events and component public contracts — Knowledge Check
Apply the concept and reason about local example output.
Which option permits a custom event to cross a shadow boundary?
Why can an outer event listener see the host as target?
What should an intentional public component event contain?
What is the expected console output of Composed events and component public contracts example 1?
(async () => {const host=document.createElement("div");const root=host.attachShadow({mode:"open"});const button=document.createElement("button");root.append(button);document.body.append(host);const log=[];document.body.addEventListener("lesson-change",e=>log.push(e.target===host));for(const composed of [false,true])button.dispatchEvent(new CustomEvent("lesson-change",{bubbles:true,composed}));console.log(JSON.stringify(log));})()What is the expected console output of Composed events and component public contracts example 2?
(async () => {const host=document.createElement("div");const root=host.attachShadow({mode:"open"});const button=document.createElement("button");root.append(button);document.body.append(host);host.addEventListener("lesson-signal",e=>console.log(e.target===host,e.composedPath()[0]===button));button.dispatchEvent(new CustomEvent("lesson-signal",{bubbles:true,composed:true}));})()