# How to: Setup Lit with Tailwind v4
## 1. Create a vite project
To create a vite project, run the following command:
```bash
bun create vite@latest
```
Make sure to select the `lit` framework when prompted, and use TypeScript.
Once setup, navigate to the project directory and install the required
dependencies:
```bash
cd my-vite-project
bun install
```
## 2. Structure the project
currently, the project structure should have a `public` and `src` directory.
Vite provides you with and example my-element.ts file in the `src` directory and
an `index.html` file in the root directory.
First move the index.html file to the `src` directory.
```diff
.
├── 📁 public
├── 📂 src
│ ├── 📁 assets
│ ├── index.css
+│ ├── index.html
│ ├── my-element.ts
│ └── vite-env.d.ts
├── .gitignore
-├── index.html
├── package.json
└── tsconfig.json
```
Now create a `lib` directory and move the `assets`. Now create a components
folder inside lib and move the `my-element.ts` file to the `lib` directory.
Finally create a `main.ts` file in the `lib` directory and export the
`my-element.ts` file.
```diff
.
+├── 📂 lib
+│ ├── 📁 assets
+│ ├── 📂 components
+│ │ └── my-element.ts
+│ └── main.ts
├── 📁 public
├── 📂 src
-│ ├── 📁 assets
│ ├── index.css
│ ├── index.html
-│ ├── my-element.ts
│ └── vite-env.d.ts
├── .gitignore
├── package.json
└── tsconfig.json
```
And modify the `index.html` file to point to the `lib/my-element.ts` file.
```diff
Vite + Lit + TS
+
```
Finally, modify the `tsconfig.json` file to point to the `lib` directory.
```diff
{
"compilerOptions": {
"target": "ES2020",
"experimentalDecorators": true,
"useDefineForClassFields": false,
"module": "ESNext",
"lib": ["ES2020", "DOM", "DOM.Iterable"],
"skipLibCheck": true,
/* Bundler mode */
"moduleResolution": "bundler",
"allowImportingTsExtensions": true,
"isolatedModules": true,
"moduleDetection": "force",
"noEmit": true,
/* Linting */
"strict": true,
"noUnusedLocals": true,
"noUnusedParameters": true,
"noFallthroughCasesInSwitch": true
},
+ "include": ["src", "lib"]
}
```
## 3. Create a vite.config.ts file
First lets install the required dependencies:
```bash
bun add -D vite-plugin-dts vite-tsconfig-paths
```
At the root of the project, create a `vite.config.ts` file and add the
following:
```typescript
import { defineConfig } from "vite";
import { resolve } from "path";
import dts from "vite-plugin-dts";
import tsconfigPaths from "vite-tsconfig-paths";
export default defineConfig({
plugins: [tsconfigPaths(), dts({ rollupTypes: true })],
build: {
copyPublicDir: false,
lib: {
entry: resolve(__dirname, "lib/main.ts"),
name: "MyElement",
fileName: "my-element",
},
rollupOptions: {
external: ["react", "react-dom", "react/jsx-runtime"],
output: {
globals: {
react: "React",
"react-dom": "ReactDOM",
"react/jsx-runtime": "react/jsx-runtime",
},
},
},
},
server: {
open: "/src/index.html",
},
});
```
You can now test building the project by running the following command:
```bash
bun run build
```
And you should see the output in the `dist` directory. (make sure to add the
dist directory to the `.gitignore` file)
```diff
.
+├── 📂 dist
+│ ├── my-element.d.ts
+│ ├── my-element.js
+│ └── my-element.umd.cjs
├── 📁 lib
├── 📁 public
├── 📁 src
├── .gitignore
├── package.json
└── tsconfig.json
```
Finally, update the `package.json` file to point to the `dist/` files.
```diff
{
"name": "my-lit-element",
- "private": true,
+ "version": "0.0.1",
"type": "module",
+ "files": [
+ "dist"
+ ],
+ "main": "./dist/my-element.umd.cjs",
+ "module": "./dist/my-element.js",
+ "exports": {
+ ".": {
+ "import": "./dist/my-element.js",
+ "require": "./dist/my-element.umd.cjs"
+ }
+ },
"scripts": {
"dev": "vite ",
"build": "tsc && vite build",
"preview": "vite preview"
},
"dependencies": {
"lit": "^3.2.0"
},
"devDependencies": {
"typescript": "^5.5.3",
"vite": "^5.4.1",
"vite-plugin-dts": "^4.1.0",
"vite-tsconfig-paths": "^5.0.1"
}
}
```
## 4. Add Tailwind CSS
First install the required dependencies:
```bash
bun add -D tailwindcss @tailwindcss/vite
```
Add the tailwind plugin to the `vite.config.ts` file:
```diff
import { defineConfig } from "vite";
import { resolve } from "path";
import dts from "vite-plugin-dts";
import tsconfigPaths from "vite-tsconfig-paths";
+import tailwindcss from "@tailwindcss/vite";
export default defineConfig({
+ plugins: [tsconfigPaths(), dts({ rollupTypes: true }), tailwindcss()],
// ...rest of config
});
```
In the `lib` directory, create a `shared` folder and add two files:
`tailwindMixin.ts` and `tailwindMixin.d.ts`. and create a `styles` folder and
add a new file `tailwind.global.css`.
```diff
.
├── 📂 lib
│ ├── 📁 assets
│ ├── 📂 components
│ │ └── my-element.ts
+│ ├── 📂 shared
+│ │ ├── tailwindMixin.d.ts
+│ │ └── tailwindMixin.ts
+│ ├── 📂 styles
+│ │ └── tailwind.global.css
│ └── main.ts
├── 📁 public
├── 📁 src
├── .gitignore
├── package.json
└── tsconfig.json
```
### tailwindMixin.ts
```typescript
import { adoptStyles, type LitElement, unsafeCSS } from "lit";
import tailwindCss from "../styles/tailwind.global.css?inline";
declare global {
// biome-ignore lint/suspicious/noExplicitAny: Required for mixin pattern compatibility
export type LitMixin = new (...args: any[]) => T & LitElement;
}
export const tailwind = unsafeCSS(tailwindCss);
// https://github.com/tailwindlabs/tailwindcss/issues/15005
// Set all @property values from tailwind on the document
// And only do this once (check if there is already a stylesheet with the same content)
if (
tailwind.styleSheet &&
document?.adoptedStyleSheets &&
!document.adoptedStyleSheets.some(
(sheet) =>
sheet.cssRules[0]?.cssText === tailwind.styleSheet?.cssRules[0].cssText
)
) {
const propertiesSheet = new CSSStyleSheet();
let code = tailwind.cssText;
code = code
.replaceAll("inherits: false", "inherits: true")
.substring(code.indexOf("@property"));
propertiesSheet.replaceSync(code);
document.adoptedStyleSheets.push(propertiesSheet);
}
export const TW = (superClass: T): T =>
class extends superClass {
connectedCallback() {
super.connectedCallback();
if (this.shadowRoot) adoptStyles(this.shadowRoot, [tailwind]);
}
};
```
### tailwindMixin.d.ts
```typescript
import { type LitElement } from "lit";
declare global {
export type LitMixin = new (...args: any[]) => T & LitElement;
}
export declare const TW: (superClass: T) => T;
```
### tailwind.global.css
```css
@import "tailwindcss";
```
## 5. Using Tailwind CSS in your components
With the mixins in place, you can now import the `TW` mixin in the
`my-element.ts` file and use it in the class definition and replace the
`static styles` property with the tailwind classes.
```diff
import { LitElement, css, html } from "lit";
import { customElement, property } from "lit/decorators.js";
+import { TW } from "../shared/tailwindMixin";
+const TwLitElement = TW(LitElement);
@customElement("my-element")
+export class MyElement extends TwLitElement {
@property() docsHint = "Click on the Vite and Lit logos to learn more";
@property({ type: Number }) count = 0;
render() {
return html`
+