Skip to content

Commit fa79717

Browse files
author
Steven Orvell
committed
Adds shadowRootOptions feature
This is a slightly simpler alternative for customizing shadow root options than implementing `createRenderRoot`. Fixes #1147.
1 parent d97341d commit fa79717

File tree

3 files changed

+21
-1
lines changed

3 files changed

+21
-1
lines changed

CHANGELOG.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,9 @@ and this project adheres to [Semantic Versioning](http://semver.org/).
1919

2020
## Unreleased
2121

22+
### Added
23+
* 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).
24+
2225
### Fixed
2326
* 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).
2427

src/lit-element.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -132,6 +132,8 @@ export class LitElement extends UpdatingElement {
132132
*/
133133
static styles?: CSSResultOrNative|CSSResultArray;
134134

135+
static shadowRootOptions: ShadowRootInit = { mode: "open" };
136+
135137
private static _styles: Array<CSSResultOrNative|CSSResult>|undefined;
136138

137139
/**
@@ -236,7 +238,8 @@ export class LitElement extends UpdatingElement {
236238
* @returns {Element|DocumentFragment} Returns a node into which to render.
237239
*/
238240
protected createRenderRoot(): Element|ShadowRoot {
239-
return this.attachShadow({mode: 'open'});
241+
return this.attachShadow(
242+
(this.constructor as typeof LitElement).shadowRootOptions);
240243
}
241244

242245
/**

src/test/lit-element_test.ts

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -289,4 +289,18 @@ suite('LitElement', () => {
289289
a,
290290
'testDom should be a child of the component');
291291
});
292+
293+
(window.ShadyDOM && window.ShadyDOM.inUse ? test.skip : test)(
294+
"can customize shadowRootOptions",
295+
async () => {
296+
class A extends LitElement {
297+
static shadowRootOptions: ShadowRootInit = { mode: "closed" };
298+
}
299+
customElements.define(generateElementName(), A);
300+
const a = new A();
301+
container.appendChild(a);
302+
await a.updateComplete;
303+
assert.equal(a.shadowRoot, undefined);
304+
}
305+
);
292306
});

0 commit comments

Comments
 (0)