Recently, I've been experimenting with
Web Components
and seeing how they can be used in modern web development. One of the libraries
I've been using is Lit, a light framework on top of Web
Components. While its a departure from my functional programming style, I've
found it to be a great way to build reusable components. But one thing that I've
really come to love is using Tailwind CSS for
styling.
In this guide, I'll show how I've set up a Lit project with Tailwind CSS. This
setup allows me to use the utility-first CSS framework to style my components
and build a consistent design system.
1. Create a vite project
To create a vite project, run the following command:
Make sure to select the lit framework when prompted, and use TypeScript.
Once setup, navigate to the project directory and install the required
dependencies:
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.
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.
And modify the index.html file to point to the lib/my-element.ts file.
Finally, modify the tsconfig.json file to point to the lib directory.
3. Create a vite.config.ts file
First lets install the required dependencies:
At the root of the project, create a vite.config.ts file and add the
following:
You can now test building the project by running the following command:
And you should see the output in the dist directory. (make sure to add the
dist directory to the .gitignore file)
Finally, update the package.json file to point to the dist/ files.
4. Add Tailwind CSS
First install the required dependencies:
Then run the following command to generate the tailwind.config.js and
postcss.config.js files:
Now update the tailwind.config.js file to generate content from the lib
directory:
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.
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.
Now you can run the project and see the tailwind styles applied to the
component.
BONUS: make tailwind more useful
Up to this point, you have successfully integrated Tailwind CSS with your Lit
and Vite project. However, you can make it more useful by adding some utilities
and extending the theme similar to how its done using shadcn/ui.
First, install the required dependencies:
Then lets create a utils.ts file in the lib/shared directory and add the
following:
Next we want to update out vscode settings to use Tailwind CSS IntelliSense and
add the classRegex to the settings.
Now we can extend the theme in the tailwind.config.js file similar to how its
done in shadcn/ui:
Next we can update the tailwind.global.css file to use the
pseudo-private properties
that can be overridden by user defined css variables:
This setup allows you to use the cva function to apply variants that combine
tailwind classes into more meaningful classes and then control the theme using
css variables.