Prerequisites
Before starting the project make sure to install the following utilities:
Creating a new Qwik application
Let's create a fresh Qwik application so that we can go through all the
steps together. New Qwik application can be created with use of the Vite by
selecting one of the two templates: qwik
or
qwik-ts
. We are going to use the qwik-ts
template.
Step 1
Create a new project
npm create vite@latest my-project -- --template qwik-ts
cd my-project
This will create a new project in the my-project
directory.
Installing and configuring Tailwind CSS and TW Elements
Step 1
Install Tailwind CSS in the newly created my-project
directory.
npm install -D tailwindcss postcss autoprefixer
npx tailwindcss init -p
File structure
After running the above commands, your project should look something like this:
my-project/
├── index.html
├── package.json
├── postcss.config.js
├── public/
├── src/
│ ├── app.css
│ ├── app.tsx
│ ├── assets/
│ ├── index.css
│ ├── main.tsx
│ └── vite-env.d.ts
├── tailwind.config.js
├── tsconfig.json
├── ...
└── vite.config.ts
Step 2
Add the paths to all of your template files in your
tailwind.config.js
file.
/** @type {import('tailwindcss').Config} */
module.exports = {
content: [
"./src/**/*.{js,ts,jsx,tsx,mdx}",
"./node_modules/tw-elements/js/**/*.js",
],
theme: {
extend: {},
},
darkMode: "class",
plugins: [require("tw-elements/plugin.cjs")],
};
Step 3
Add the @tailwind
directives for each of Tailwind’s layers to
your index.css
file. Remove the styles from
app.css
so that the default styles won't override our new
styles.
@import url("https://fonts.googleapis.com/css?family=Roboto:300,400,500,700,900&display=swap");
@tailwind base;
@layer base {
html {
@apply text-surface;
@apply bg-white;
}
html.dark {
@apply text-neutral-50;
@apply bg-body-dark;
}
}
@tailwind components;
@tailwind utilities;
p {
@apply leading-[1.6];
}
Step 4
Install TW Elements.
npm install tw-elements
Step 5
Import components which you intend to use and function
initTWE
that would allow us to automaticaly initialize used
components. Call the initTWE
inside the
useVisibleTask$
lifecycle method. Keep in mind that
useVisibleTask$
executes the code on the client side.
import { component$, useVisibleTask$ } from "@builder.io/qwik";
import { Tooltip, initTWE } from "tw-elements";
export const App = component$(() => {
useVisibleTask$(() => {
initTWE({ Tooltip });
});
return (
<div class="mt-16 flex justify-center">
<p class="text-lg">
Hover the link to see the
<a
href="#"
class="ps-1 text-primary transition duration-150 ease-in-out hover:text-primary-600 focus:text-primary-600 active:text-primary-700 dark:text-primary-400 dark:hover:text-primary-500 dark:focus:text-primary-500 dark:active:text-primary-600"
data-twe-toggle="tooltip"
title="Hi! I'm tooltip">
tooltip
</a>
</p>
</div>
); });
Step 6
Start the app and see if everything's fine. Awesome! You're all set to dive into using TW Elements for your Qwik project. Have fun!
npm run dev
Initializing via JS
By default all components have autoinit which means they are initialized by data attributes. But if you want to make init by JavaScript - there is also possibility to do that.
Step 1
Import components which are you intend to use and initialize components in lifecycle hook.
import { component$, useVisibleTask$ } from "@builder.io/qwik";
import { Tooltip } from "tw-elements";
export const App = component$(() => {
useVisibleTask$(() => {
new Tooltip(document.getElementById("my-tooltip"));
});
return (
<div class="mt-16 flex justify-center">
<p class="text-lg">
Hover the link to see the
<a
id="my-tooltip"
href="#"
class="ps-1 text-primary transition duration-150 ease-in-out hover:text-primary-600 focus:text-primary-600 active:text-primary-700 dark:text-primary-400 dark:hover:text-primary-500 dark:focus:text-primary-500 dark:active:text-primary-600"
title="Hi! I'm tooltip">
tooltip
</a>
</p>
</div>
); });