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

Feature/Add proper stylesheet imports #4

Merged
merged 4 commits into from
May 15, 2023
Merged
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 package.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
"eslint-config-galex": "^3.6.3",
"tailwindcss": "^3.0.18",
"typescript": "^4.5.5",
"vite": "^2.7.13"
"vite": "^3.0.9"
},
"dependencies": {
"@adobe/lit-mobx": "^2.0.0",
Expand Down
1 change: 1 addition & 0 deletions src/@types/vite.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
/// <reference types="vite/client" />
35 changes: 29 additions & 6 deletions src/util/TailwindMixin.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,33 @@
import css from "../styles/main.css?inline";

const supportsAdoptedCSS = Array.isArray(document.adoptedStyleSheets);

export const TW = <T extends LitMixin>(superClass: T): T =>
class extends superClass {
connectedCallback() {
super.connectedCallback();
class Mixin extends superClass {
static styleElement: HTMLStyleElement;

static stylesheet: CSSStyleSheet;

// Disable no-explicit-any because Mixin constructor parameters must have an any[] type
// eslint-disable-next-line @typescript-eslint/no-explicit-any
constructor(...args: any[]) {
super(...args);
void this.createStylesheet();
}

document.head.querySelectorAll("link[rel='stylesheet']").forEach((link) => {
this.shadowRoot.append(link.cloneNode());
});
async createStylesheet() {
if (supportsAdoptedCSS) {
if (!Mixin.stylesheet) {
Mixin.stylesheet = new CSSStyleSheet();
await Mixin.stylesheet.replace(css);
}
this.shadowRoot.adoptedStyleSheets.push(Mixin.stylesheet);
} else {
if (!Mixin.styleElement) {
Mixin.styleElement = document.createElement("style");
Mixin.styleElement.textContent = css;
}
this.shadowRoot.append(Mixin.styleElement.cloneNode());
}
}
};
Loading