Skip to content

Add support for css file styles injection #18

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

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
29 changes: 19 additions & 10 deletions src/dev/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,12 @@ require('@webcomponents/custom-elements');
module.exports = {
/**
* @param {JSX.Element} app
* @param {string} tagName - The name of the web component. Has to be minus "-" delimited.
* @param {boolean} useShadowDom - If the value is set to "true" the web component will use the `shadowDom`. The default value is true.
* @param {string} tagName - The name of the web component. Has to be minus '-' delimited.
* @param {Object} config - The config object used to generate the web component.
* @param {string} config.useShadowDom - If the value is set to 'true' the web component will use the `shadowDom`. The default value is true.
* @param {string} config.cssFile - The css file location in the distribution server. Optional.
*/
create: (app, tagName, useShadowDom = true) => {
create: (app, tagName, config = { useShadowDom: true }) => {
let appInstance;

const lifeCycleHooks = {
Expand Down Expand Up @@ -44,7 +46,7 @@ module.exports = {
const webComponentInstance = this;
let mountPoint = webComponentInstance;

if (useShadowDom) {
if (config.useShadowDom) {
// Re-assign the webComponentInstance (this) to the newly created shadowRoot
const shadowRoot = webComponentInstance.attachShadow({ mode: 'open' });
// Re-assign the mountPoint to the newly created "div" element
Expand All @@ -63,14 +65,21 @@ module.exports = {
}

ReactDOM.render(React.cloneElement(app, extractAttributes(webComponentInstance)) , mountPoint, function () {
appInstance = this;
appInstance = this;

callConstructorHook(webComponentInstance);
callLifeCycleHook('connectedCallback');
});
callConstructorHook(webComponentInstance);
callLifeCycleHook('connectedCallback');
});

if (config.cssFile) {
mountPoint.insertAdjacentHTML(
'afterbegin',
`<link rel='stylesheet' type='text/css' href='${config.cssFile}'/>`
);
}
}
disconnectedCallback () {
callLifeCycleHook('disconnectedCallback');
callLifeCycleHook('disconnectedCallback');
}
attributeChangedCallback (attributeName, oldValue, newValue, namespace) {
callLifeCycleHook('attributeChangedCallback', [attributeName, oldValue, newValue, namespace]);
Expand All @@ -81,5 +90,5 @@ module.exports = {
}

customElements.define(tagName, proto);
},
}
};