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

feat: inject template styles to shadow dom #58

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
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 .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ jobs:
- name: Install webkit deps
run: |
sudo apt-get update
sudo apt-get install libgstreamer1.0-0 libbrotli1 libopus0 libwoff1 libgstreamer-plugins-base1.0-0 libgstreamer-gl1.0-0 libgstreamer-plugins-bad1.0-0 libopenjp2-7 libwebpdemux2 libhyphen0 libgles2
sudo apt-get install libgstreamer1.0-0 libbrotli1 libopus0 libwoff1 libgstreamer-plugins-base1.0-0 libgstreamer-gl1.0-0 libgstreamer-plugins-bad1.0-0 libopenjp2-7 libwebpdemux2 libhyphen0 libgles2 libharfbuzz-icu0 libenchant1c2a
- name: Cache node modules
uses: actions/cache@v1
env:
Expand Down
21 changes: 17 additions & 4 deletions src/index.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,27 @@
import { h, cloneElement, render, hydrate } from 'preact';

export default function register(Component, tagName, propNames, options) {
const resolvedTagName =
tagName || Component.tagName || Component.displayName || Component.name;

function PreactElement() {
const inst = Reflect.construct(HTMLElement, [], PreactElement);
inst._vdomComponent = Component;
inst._root =
options && options.shadow ? inst.attachShadow({ mode: 'open' }) : inst;

let template = document.getElementById(`${resolvedTagName}-styles`);
if (!template && options && options.styles) {
template = document.createElement('template');
template.id = `${resolvedTagName}-styles`;
template.innerHTML = `<style id='${resolvedTagName}-styles-tag'>${options.styles}</style>`;
document.head.appendChild(template.cloneNode(true));
}

if (template) {
inst._root.appendChild(template.content.cloneNode(true));
}

return inst;
}
PreactElement.prototype = Object.create(HTMLElement.prototype);
Expand Down Expand Up @@ -49,10 +65,7 @@ export default function register(Component, tagName, propNames, options) {
});
});

return customElements.define(
tagName || Component.tagName || Component.displayName || Component.name,
PreactElement
);
return customElements.define(resolvedTagName, PreactElement);
}

function ContextProvider(props) {
Expand Down