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

Spike/emp 818 investigate shadow dom #305

Open
wants to merge 2 commits into
base: main
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
8 changes: 8 additions & 0 deletions rollup.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,5 +19,13 @@ export default createConfig({
},
plugins: {
// Modify plugins options here.
replace: {
// Replace X CSS injector by our custom one.
'addStyle(id, style);': 'window.xCSSInjector.addStyle(style);',
delimiters: ['', '']
},
styles: {
mode: ['inject', varname => `window.xCSSInjector.addStyle({ source: ${varname} });`]
}
}
});
1 change: 1 addition & 0 deletions src/main.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import './xCSSInjector';
import { XInstaller } from '@empathyco/x-components';
import Vue from 'vue';
import { installXOptions } from './x-components/plugin.options';
Expand Down
8 changes: 8 additions & 0 deletions src/shims-tsx.d.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
import Vue, { VNode } from 'vue';

export interface XCSSInjector {
addStyle: (styles: { source: string }) => void;
setHost: (el: Element | ShadowRoot) => void;
}

declare global {
namespace JSX {
interface Element extends VNode {}
Expand All @@ -8,4 +13,7 @@ declare global {
[elem: string]: any;
}
}
interface Window {
xCSSInjector: XCSSInjector;
}
}
20 changes: 20 additions & 0 deletions src/x-components/plugin.options.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,31 @@ import store from '../store';
import { adapter } from '../adapter/adapter';
import { useDevice } from '../composables/use-device.composable';

console.log(process.env.NODE_ENV);

const device = useDevice();

function getDomElement(): Element {
const domElement = document.createElement('div');
if (process.env.NODE_ENV === 'production' || true) {
const container = document.createElement('div');
const shadowRoot = container.attachShadow({ mode: 'open' });
shadowRoot.appendChild(domElement);
document.body.appendChild(container);
window.xCSSInjector.setHost(shadowRoot);
} else {
document.body.appendChild(domElement);
window.xCSSInjector.setHost(document.head);
}

return domElement;
}

export const installXOptions: InstallXOptions = {
adapter,
store,
app: App,
domElement: getDomElement,
xModules: {
facets: {
config: {
Expand Down
25 changes: 25 additions & 0 deletions src/xCSSInjector.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
import { XCSSInjector } from './shims-tsx';

class CssInjector implements XCSSInjector {
protected host!: Element | ShadowRoot;
protected stylesQueue: string[] = [];

addStyle(styles: { source: string }): void {
console.log(styles);
this.stylesQueue.push(styles.source);
if (this.host) {
this.stylesQueue.forEach(styles => {
const styleTag = document.createElement('style');
styleTag.textContent = styles;
this.host.appendChild(styleTag);
});
this.stylesQueue = [];
}
}

setHost(host: Element | ShadowRoot): void {
this.host = host;
}
}

window.xCSSInjector = new CssInjector();