Prerequisites
Before starting the project make sure to install the following utilities:
Creating a new Remix application
Let's create a fresh Remix application so that we can go through all the steps together.
Step 1
Create new project
npx create-remix@latest my-project
cd my-project
Step 2
Run the development server.
npm run dev
Installing and configuring Tailwind CSS and TW Elements
Step 1
Install Tailwind CSS.
npm install -D tailwindcss postcss autoprefixer
npx tailwindcss init -p
File structure
If you have followed all instructions mentioned earlier, your file structure should look like this:
my-project/
├── app/
│ ├── routes/
│ │ └── _index.tsx
│ ├── entry.client.tsx
│ ├── entry.server.tsx
│ └── root.tsx
├── build/
├── node_modules/
├── public/
├── ...
├── package-lock.json
├── package.json
├── postcss.config.js
├── README.md
├── remix.config.js
├── ...
├── tailwind.config.js
└── tsconfig.json
Step 2
Add the paths to all of your template files in your
tailwind.config.js
file.
/** @type {import('tailwindcss').Config} */
module.exports = {
content: [
"./app/**/*.{js,jsx,ts,tsx}",
"./node_modules/tw-elements/js/**/*.js",
],
theme: {
extend: {},
},
darkMode: "class",
plugins: [require("tw-elements/plugin.cjs")],
};
Step 3
Create a styles.css
file in your app folder, and make sure to
include the necessary @tailwind
directives for each of
Tailwind's layers.
@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
Import created styles.css
file in your
root.tsx
file.
...
import stylesheet from "~/styles.css";
export const links: LinksFunction = () => [
{ rel: "stylesheet", href: stylesheet },
];
...
Step 5
Install TW Elements.
npm install tw-elements
Step 6
Create MyTooltip.client.tsx
file (it shows framework that
should not render this component in a server-side mode) in the
app/components
directory. Then, import the TWE components you
intend to use and include the necessary function initTWE
.
Initialize initTWE
within a lifecycle method.
import { useEffect } from "react";
import { Tooltip, initTWE } from "tw-elements";
const MyTooltip = () => {
useEffect(() => {
initTWE({ Tooltip });
}, []);
return (
<div className="mt-16 flex justify-center">
<p className="text-lg">
Hover the link to see the
<a
href="#"
className="text-primary ps-1 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>
);
};
export default MyTooltip;
Step 7
Install remix-utils
.
npm i remix-utils
Step 8
Import in _index.tsx
your component and wrap it into
ClientOnly
element.
import type { MetaFunction } from "@remix-run/node";
import { ClientOnly } from "remix-utils/client-only";
import MyTooltip from "../components/MyTooltip.client";
export const meta: MetaFunction = () => {
return [
{ title: "TW Elements Remix integration" },
{ name: "description", content: "Welcome to TWE!" },
];
};
export default function Index() {
return <ClientOnly fallback="{null}">{() => <MyTooltip />}</ClientOnly>;
}
Step 9
Start the app and see if everything's fine. Awesome! You're all set to dive into using TW Elements for your Remix project. Have fun!
npm run dev
Initializing via JS
By default all components have auto init 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 iniitialize them in lifecycle hook.
import { useEffect } from "react";
import { Tooltip } from "tw-elements";
const MyTooltip = () => {
useEffect(() => {
new Tooltip(document.getElementById("my-tooltip"));
}, []);
return (
<div className="mt-16 flex justify-center">
<p className="text-lg">
Hover the link to see the
<a
id="my-tooltip"
href="#"
className="text-primary ps-1 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>
);
};
export default MyTooltip;