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
Installing and configuring Tailwind CSS and Tailwind Elements
Step 1
Enable built-in Tailwind CSS support in Remix. Set the
unstable_tailwind
feature flag in your
remix.config.js
file. Eventually this will become stable and
won't be necessary.
future: {
unstable_tailwind: true,
},
Step 2
Install Tailwind CSS
npm install -D tailwindcss postcss autoprefixer
npx tailwindcss init -p
Step 3
Add the paths to all of your template files in your
tailwind.config.cjs
file.
/** @type {import('tailwindcss').Config} */
module.exports = {
content: [
"./app/**/*.{js,jsx,ts,tsx}"
"./node_modules/tw-elements/dist/js/**/*.js"
],
theme: {
extend: {},
},
darkMode: "class",
plugins: [require("tw-elements/dist/plugin.cjs")]
}
Step 3
Create a ./app/tailwind.css
file and add the
@tailwind
directives for each of Tailwind’s layers.
@tailwind base;
@tailwind components;
@tailwind utilities;
Step 4
Import the newly-created ./app/tailwind.css
file in your
./app/root.jsx
file.
Also attach the Roboto
font.
import stylesheet from "~/tailwind.css";
export const links: LinksFunction = () => [
{ rel: "stylesheet", href: stylesheet },
{ rel: "stylesheet", href: "https://fonts.googleapis.com/css2?family=Roboto:wght@400;500;700&display=swap" }
];
Step 5
Install Tailwind Elements.
npm install tw-elements
Step 6
Create standalone file with name of your component (for example
MyDatepicker.client.jsx
(it shows framework that should not
render this component in a server-side mode) in
app/components
directory) and import TE components which are
you intend to use. Also include necessary function initTE
.
Initialize initTE
in a lifecycle method.
import { useEffect } from "react";
import { Datepicker, Input, initTE } from "tw-elements";
const MyDatepicker = () => {
useEffect(() => {
initTE({ Datepicker, Input });
}, []);
return (
<div
class="relative mb-3"
data-te-datepicker-init
data-te-input-wrapper-init>
<input
type="text"
className="peer block min-h-[auto] w-full rounded border-0 bg-transparent px-3 py-[0.32rem] leading-[1.6] outline-none transition-all duration-200 ease-linear focus:placeholder:opacity-100 peer-focus:text-primary data-[te-input-state-active]:placeholder:opacity-100 motion-reduce:transition-none dark:text-neutral-200 dark:placeholder:text-neutral-200 dark:peer-focus:text-primary [&:not([data-te-input-placeholder-active])]:placeholder:opacity-0"
placeholder="Select a date" />
<label
htmlFor="floatingInput"
className="pointer-events-none absolute left-3 top-0 mb-0 max-w-[90%] origin-[0_0] truncate pt-[0.37rem] leading-[1.6] text-neutral-500 transition-all duration-200 ease-out peer-focus:-translate-y-[0.9rem] peer-focus:scale-[0.8] peer-focus:text-primary peer-data-[te-input-state-active]:-translate-y-[0.9rem] peer-data-[te-input-state-active]:scale-[0.8] motion-reduce:transition-none dark:text-neutral-200 dark:peer-focus:text-primary">
Select a date
</label>
</div>
); }; export default MyDatepicker;
Step 7
Install remix-utils
.
npm i remix-utils
Step 8
Import (for example in index.js
) newly created component
wrapping it into ClientOnly
element.
import { ClientOnly } from "remix-utils";
import MyDatepicker from "./components/MyDatepicker.client";
...
export default function App() {
return (
...
<ClientOnly fallback="{null}">{() => <MyDatepicker />}</ClientOnly>
); }
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 { Datepicker, Input } from "tw-elements";
const MyDatepicker = () => {
useEffect(() => {
const myInput = new Input(document.getElementById("myDatepicker"));
const options = {
format: "dd-mm-yyyy",
};
const myDatepicker = new Datepicker(
document.getElementById("myDatepicker"),
options
);
}, []);
return (
<div className="relative mb-3" id="myDatepicker">
<input
type="text"
className="peer block min-h-[auto] w-full rounded border-0 bg-transparent px-3 py-[0.32rem] leading-[1.6] outline-none transition-all duration-200 ease-linear focus:placeholder:opacity-100 peer-focus:text-primary data-[te-input-state-active]:placeholder:opacity-100 motion-reduce:transition-none dark:text-neutral-200 dark:placeholder:text-neutral-200 dark:peer-focus:text-primary [&:not([data-te-input-placeholder-active])]:placeholder:opacity-0"
placeholder="Select a date" />
<label
htmlFor="floatingInput"
className="pointer-events-none absolute left-3 top-0 mb-0 max-w-[90%] origin-[0_0] truncate pt-[0.37rem] leading-[1.6] text-neutral-500 transition-all duration-200 ease-out peer-focus:-translate-y-[0.9rem] peer-focus:scale-[0.8] peer-focus:text-primary peer-data-[te-input-state-active]:-translate-y-[0.9rem] peer-data-[te-input-state-active]:scale-[0.8] motion-reduce:transition-none dark:text-neutral-200 dark:peer-focus:text-primary">
Select a date
</label>
</div>
); }; export default MyDatepicker;