Skip to content

Commit 52daae4

Browse files
add adoptedStyleSheets to the options (#95)
* add adoptefStyleSheets to the options * test: Add simple test case * refactor: Add types & format code * docs: Add ReadMe example --------- Co-authored-by: Ryan Christian <[email protected]>
1 parent af1991d commit 52daae4

File tree

3 files changed

+33
-4
lines changed

3 files changed

+33
-4
lines changed

README.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,9 +14,9 @@ const Greeting = ({ name = 'World' }) => (
1414
<p>Hello, {name}!</p>
1515
);
1616

17-
register(Greeting, 'x-greeting', ['name'], { shadow: true, mode: 'open' });
18-
// ^ ^ ^ ^ ^
19-
// | HTML tag name | use shadow-dom |
17+
register(Greeting, 'x-greeting', ['name'], { shadow: true, mode: 'open', adoptedStyleSheets: [] });
18+
// ^ ^ ^ ^ ^ ^
19+
// | HTML tag name | use shadow-dom | use adoptedStyleSheets
2020
// Component definition Observed attributes Encapsulation mode for the shadow DOM tree
2121
```
2222

src/index.js

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import { h, cloneElement, render, hydrate } from 'preact';
22

33
/**
44
* @typedef {import('preact').FunctionComponent<any> | import('preact').ComponentClass<any> | import('preact').FunctionalComponent<any> } ComponentDefinition
5-
* @typedef {{ shadow: false } | { shadow: true, mode?: 'open' | 'closed' }} Options
5+
* @typedef {{ shadow: false } | { shadow: true, mode?: 'open' | 'closed', adoptedStyleSheets?: CSSStyleSheet[] }} Options
66
* @typedef {HTMLElement & { _root: ShadowRoot | HTMLElement, _vdomComponent: ComponentDefinition, _vdom: ReturnType<typeof import("preact").h> | null }} PreactCustomElement
77
*/
88

@@ -47,6 +47,11 @@ export default function register(Component, tagName, propNames, options) {
4747
options && options.shadow
4848
? inst.attachShadow({ mode: options.mode || 'open' })
4949
: inst;
50+
51+
if (options && options.adoptedStyleSheets) {
52+
inst._root.adoptedStyleSheets = options.adoptedStyleSheets;
53+
}
54+
5055
return inst;
5156
}
5257
PreactElement.prototype = Object.create(HTMLElement.prototype);

src/index.test.jsx

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -309,4 +309,28 @@ describe('web components', () => {
309309
assert.equal(myForm.elements[0].tagName, 'X-FORM-ASSOCIATED-CLASS');
310310
assert.equal(myForm.elements[2].tagName, 'X-FORM-ASSOCIATED-FUNCTION');
311311
});
312+
313+
it('supports the `adoptedStyleSheets` option', async () => {
314+
function AdoptedStyleSheets() {
315+
return <div className="styled-child">Adopted Style Sheets</div>;
316+
}
317+
318+
const sheet = new CSSStyleSheet();
319+
sheet.replaceSync('.styled-child { color: red; }');
320+
321+
registerElement(AdoptedStyleSheets, 'x-adopted-style-sheets', [], {
322+
shadow: true,
323+
adoptedStyleSheets: [sheet],
324+
});
325+
326+
root.innerHTML = `<x-adopted-style-sheets></x-adopted-style-sheets>`;
327+
328+
const child = document
329+
.querySelector('x-adopted-style-sheets')
330+
.shadowRoot
331+
.querySelector('.styled-child');
332+
333+
const style = getComputedStyle(child);
334+
assert.equal(style.color, 'rgb(255, 0, 0)');
335+
});
312336
});

0 commit comments

Comments
 (0)