-
Notifications
You must be signed in to change notification settings - Fork 12
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Improve style sheet adoption ergonomics.
One common pattern for element authors (now that import attributes enable folks to import css files directly) is to adopt imported style sheets into a shadow root at custom element initialization time. This adds one static getter — `styles`. It’s still possible to do something more custom in `createRenderRoot`, but this adds a simple, declarative interface for the task of adding styles to a shadow root. Closes #52.
- Loading branch information
1 parent
ce67bc9
commit 3661bb0
Showing
9 changed files
with
166 additions
and
16 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
// TODO: Replace with actual css file when ESLint accepts import attributes. | ||
const css = `\ | ||
:host { | ||
display: block; | ||
background-color: coral; | ||
width: 100px; | ||
height: 100px; | ||
} | ||
`; | ||
const styleSheet = new CSSStyleSheet(); | ||
styleSheet.replaceSync(css); | ||
export default styleSheet; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
<!doctype html> | ||
<html> | ||
<body> | ||
<meta charset="UTF-8"> | ||
<script type="module" src="test-styles.js"></script> | ||
<h3>Test Styles</h3> | ||
</body> | ||
</html> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,79 @@ | ||
import { assert, it } from './x-test.js'; | ||
import styleSheet from './test-styles.css.js'; | ||
import XElement from '../x-element.js'; | ||
|
||
class TestElement1 extends XElement { | ||
static count = 0; | ||
static get styles() { | ||
TestElement1.count++; | ||
return [styleSheet]; | ||
} | ||
static template(html) { | ||
return () => { | ||
return html``; | ||
}; | ||
} | ||
} | ||
customElements.define('test-element-1', TestElement1); | ||
|
||
it('provided style sheets are adopted', () => { | ||
const el = document.createElement('test-element-1'); | ||
document.body.append(el); | ||
const boundingClientRect = el.getBoundingClientRect(); | ||
assert(boundingClientRect.width === 100); | ||
assert(boundingClientRect.height === 100); | ||
el.remove(); | ||
}); | ||
|
||
it('should only get styles _once_ per constructor', () => { | ||
for (let iii = 0; iii < 10; iii++) { | ||
// No matter how many times you do this, styles must only be accessed once. | ||
const el = document.createElement('test-element-1'); | ||
document.body.append(el); | ||
const boundingClientRect = el.getBoundingClientRect(); | ||
assert(boundingClientRect.width === 100); | ||
assert(boundingClientRect.height === 100); | ||
el.remove(); | ||
assert(TestElement1.count === 1); | ||
} | ||
}); | ||
|
||
it('errors are thrown when providing styles without a shadow root', () => { | ||
class BadElement extends XElement { | ||
static get styles() { return [styleSheet]; } | ||
static createRenderRoot(host) { return host; } | ||
} | ||
customElements.define('test-element-2', BadElement); | ||
let passed = false; | ||
let message = 'no error was thrown'; | ||
try { | ||
new BadElement(); | ||
} catch (error) { | ||
const expected = 'Unexpected "styles" declared without a shadow root.'; | ||
message = error.message; | ||
passed = error.message === expected; | ||
} | ||
assert(passed, message); | ||
}); | ||
|
||
it('errors are thrown when styles already exist on shadow root.', () => { | ||
class BadElement extends XElement { | ||
static get styles() { return [styleSheet]; } | ||
static createRenderRoot(host) { | ||
host.attachShadow({ mode: 'open' }); | ||
host.shadowRoot.adoptedStyleSheets = [styleSheet]; | ||
return host.shadowRoot; | ||
} | ||
} | ||
customElements.define('test-element-3', BadElement); | ||
let passed = false; | ||
let message = 'no error was thrown'; | ||
try { | ||
new BadElement(); | ||
} catch (error) { | ||
const expected = 'Unexpected "styles" declared when preexisting "adoptedStyleSheets" exist.'; | ||
message = error.message; | ||
passed = error.message === expected; | ||
} | ||
assert(passed, message); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters