Skip to content

Commit ecbe347

Browse files
committed
feat: add support for serializable option in shadow DOM
1 parent 4711c23 commit ecbe347

File tree

3 files changed

+22
-1
lines changed

3 files changed

+22
-1
lines changed

src/index.d.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ type Options =
88
shadow: true;
99
mode?: 'open' | 'closed';
1010
adoptedStyleSheets?: CSSStyleSheet[];
11+
serializable?: boolean;
1112
};
1213

1314
/**

src/index.js

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,11 @@ export default function register(Component, tagName, propNames, options) {
1515
inst._vdomComponent = Component;
1616

1717
if (options && options.shadow) {
18-
inst._root = inst.attachShadow({ mode: options.mode || 'open' });
18+
const shadowOptions = { mode: options.mode || 'open' };
19+
if (options.serializable !== undefined) {
20+
shadowOptions.serializable = !!options.serializable;
21+
}
22+
inst._root = inst.attachShadow(shadowOptions);
1923

2024
if (options.adoptedStyleSheets) {
2125
inst._root.adoptedStyleSheets = options.adoptedStyleSheets;

test/index.test.jsx

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -421,4 +421,20 @@ describe('web components', () => {
421421
'<h1>Light DOM Children</h1><div><slot><p>Child 1</p><p>Child 2</p></slot></div>'
422422
);
423423
});
424+
425+
it('supports the `serializable` option', async () => {
426+
function SerializableComponent() {
427+
return <div className="serializable-child">Serializable Shadow DOM</div>;
428+
}
429+
430+
registerElement(SerializableComponent, 'x-serializable', [], {
431+
shadow: true,
432+
serializable: true,
433+
});
434+
435+
root.innerHTML = `<x-serializable></x-serializable>`;
436+
437+
const el = document.querySelector('x-serializable');
438+
assert.isTrue(el.shadowRoot.serializable);
439+
});
424440
});

0 commit comments

Comments
 (0)