Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for shadow DOM closed mode #75

Merged
merged 2 commits into from
Aug 16, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ export default function register(Component, tagName, propNames, options) {
const inst = Reflect.construct(HTMLElement, [], PreactElement);
inst._vdomComponent = Component;
inst._root =
options && options.shadow ? inst.attachShadow({ mode: 'open' }) : inst;
options && options.shadow ? inst.attachShadow({ mode: options.mode || 'open' }) : inst;
return inst;
}
PreactElement.prototype = Object.create(HTMLElement.prototype);
Expand Down
34 changes: 34 additions & 0 deletions src/index.test.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -245,4 +245,38 @@ describe('web components', () => {
});
assert.equal(getShadowHTML(), '<p>Active theme: sunny</p>');
});

it('renders element in shadow dom open mode', async () => {
function ShadowDomOpen() {
return <div className="shadow-child">Shadow DOM Open</div>;
}

registerElement(ShadowDomOpen, 'x-shadowdom-open', [], {
shadow: true,
mode: 'open',
});

const el = document.createElement('x-shadowdom-open');
root.appendChild(el);
const shadowRoot = el.shadowRoot;
assert.isTrue(!!shadowRoot);
const child = shadowRoot.querySelector('.shadow-child');
assert.isTrue(!!child);
assert.equal(child.textContent, 'Shadow DOM Open');
});

it('renders element in shadow dom closed mode', async () => {
function ShadowDomClosed() {
return <div className="shadow-child">Shadow DOM Closed</div>;
}

registerElement(ShadowDomClosed, 'x-shadowdom-closed', [], {
shadow: true,
mode: 'closed',
});

const el = document.createElement('x-shadowdom-closed');
root.appendChild(el);
assert.isTrue(el.shadowRoot === null);
});
});