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

Adds shadowRootOptions feature #1148

Merged
merged 1 commit into from
Feb 4, 2021
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
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,9 @@ and this project adheres to [Semantic Versioning](http://semver.org/).

## Unreleased

### Added
* Adds a `static shadowRootOptions` property for specifying shadow root options. This is a slightly simpler alternative to implementing a custom `createRenderRoot` method [#1147](https://github.com/Polymer/lit-element/issues/1147).

### Fixed
* Fixes an issue with `queryAssignedNodes` when applying a selector on a slot that included text nodes on older browsers not supporting Element.matches [#1088](https://github.com/Polymer/lit-element/issues/1088).

Expand Down
5 changes: 4 additions & 1 deletion src/lit-element.ts
Original file line number Diff line number Diff line change
Expand Up @@ -132,6 +132,8 @@ export class LitElement extends UpdatingElement {
*/
static styles?: CSSResultOrNative|CSSResultArray;

static shadowRootOptions: ShadowRootInit = { mode: "open" };

private static _styles: Array<CSSResultOrNative|CSSResult>|undefined;

/**
Expand Down Expand Up @@ -236,7 +238,8 @@ export class LitElement extends UpdatingElement {
* @returns {Element|DocumentFragment} Returns a node into which to render.
*/
protected createRenderRoot(): Element|ShadowRoot {
return this.attachShadow({mode: 'open'});
return this.attachShadow(
(this.constructor as typeof LitElement).shadowRootOptions);
}

/**
Expand Down
14 changes: 14 additions & 0 deletions src/test/lit-element_test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -289,4 +289,18 @@ suite('LitElement', () => {
a,
'testDom should be a child of the component');
});

(window.ShadyDOM && window.ShadyDOM.inUse ? test.skip : test)(
"can customize shadowRootOptions",
async () => {
class A extends LitElement {
static shadowRootOptions: ShadowRootInit = { mode: "closed" };
}
customElements.define(generateElementName(), A);
const a = new A();
container.appendChild(a);
await a.updateComplete;
assert.equal(a.shadowRoot, undefined);
}
);
});