Styling Remix using Tailwind and PostCSS

Table of Contents

TL;DR: Source and Demo
Introduction
Dependencies
Add scripts
Styles Presets
PostCSS and Tailwind config
Remix Integration
Tailwind sample usage
VSCode Plugins
Conclusion

TL;DR: Source and Demo

Here’s a live d…


This content originally appeared on DEV Community and was authored by Carlo Gino Catapang

Table of Contents

TL;DR: Source and Demo

Here's a live demo
Link to the source code
Link to step by step commits

Introduction

In my last blog post, I discussed how to style a Remix app using Vanilla CSS. This blog will show how to integrate Tailwind and PostCSS into our Remix app.

Dependencies

Installation

npm install -D autoprefixer postcss postcss-cli postcss-import tailwindcss cssnano

OR if you prefer yarn

yarn add -D autoprefixer postcss postcss-cli postcss-import tailwindcss cssnano

Add scripts to package.json

Add Script for CSS generation

// package.json
"scripts": {
  // ...
  "css:watch": "npm run css:build -- --watch",
  "css:build": "postcss styles/**/*.css --dir app/styles",
  "css:prod": "npm run css:build -- --env production",
  // ...
},

Replace npm run with yarn if you prefer to use yarn

I don't want to commit those generated CSS files to the repo, so I'll be adding them to .gitignore

app/styles/*.css

Add Script for cleaning up build files

// package.json
"scripts": {
  // ...
  "build": "npm run css:prod && remix build",
  "prebuild": "rimraf ./public/build \"./app/styles/**/*.css\""
  // ...
},

Running the scripts

  • Development

Run npm run css:watch in one terminal and remix dev in another

npm run css:watch
npm run dev

DISCLAIMER: Don't expect it will work immediately. We still need to configure a few things with Tailwind and PostCSS.

OPTIONAL: Run multiple scripts in a single command

  • Production
npm run build

If you are not a fan of multiple terminals, use concurrently to run css:watch and remix dev in parallel

// package.json
"scripts": {
  // ...
  "dev": "concurrently npm run css:watch && remix dev",
  // ...
}

Tailwind and App styles presets

Tailwind styles

We need to explicitly declare the features we want to use in our CSS.
Here's a reference of what you can use.

/* styles/tailwind.css */
@tailwind base;
@tailwind components;
@tailwind utilities;
@tailwind screens;

App CSS presets

Some CSS defaults I prefer

/* styles/app.css */
:root {
  --color-primary-light: hsl(210, 100%, 98%);
  --color-primary-100: hsl(210, 100%, 95%);
  --color-primary-200: hsl(210, 100%, 85%);
  --color-primary-300: hsl(210, 100%, 80%);
  --color-primary-400: hsl(210, 100%, 75%);
  --color-primary-500: hsl(210, 100%, 60%);
  --color-primary-600: hsl(210, 100%, 50%);
  --color-primary-700: hsl(210, 100%, 40%);
  --color-primary-800: hsl(210, 100%, 30%);
  --color-primary-900: hsl(210, 100%, 20%);
  --color-primary-dark: hsl(210, 100%, 2%);
}

input,
select,
textarea {
  @apply text-black;
}

@media (prefers-color-scheme: dark) {
  html {
    @apply bg-black text-white;
  }
}

PostCSS and Tailwind configuration

PostCSS Config File

// postcss.config.js
module.exports = {
  plugins: [
    require("tailwindcss"),
    require("autoprefixer"),
    require("postcss-import"),
    process.env.NODE_ENV === "production" &&
      require("cssnano")({
        preset: "default",
      }),
  ],
};

Tailwind Config File

// tailwind.config.js
module.exports = {
  mode: process.env.NODE_ENV ? "jit" : undefined,
  // To purge CSS in .ts .tsx files
  purge: ["./app/**/*.{ts,tsx}"], 
  darkMode: "media", // Use media queries for dark mode
  theme: {
    extend: {
      colors: {
        // color scheme is defined in /app.css
        // To enable text-primary-xxx, bg-primary-xxx, or border-primary-xxx
        primary: {
          light: "var(--color-primary-light)",
          100: "var(--color-primary-100)",
          200: "var(--color-primary-200)",
          300: "var(--color-primary-300)",
          400: "var(--color-primary-400)",
          500: "var(--color-primary-500)",
          600: "var(--color-primary-600)",
          700: "var(--color-primary-700)",
          800: "var(--color-primary-800)",
          900: "var(--color-primary-900)",
          dark: "var(--color-primary-dark)",
        },
      },
    },
  },
  variants: {}, // activate any variant you want here
  plugins: [], // add any plugin you need here
};

Integrating styles in Remix Code

Add a reference of the generated CSS files using links in app/root.tsx

// app/root.js
// ...
import type { LinksFunction } from "remix";
import tailwindStyles from "~/styles/tailwind.css";
import appStyles from "~/styles/app.css";

export let links: LinksFunction = () => {
  return [
    { rel: "stylesheet", href: tailwindStyles },
    {
      rel: "stylesheet",
      href: appStyles,
    },
  ];
};
// ...

Styling a component

Use Tailwind, as usual; add Tailwind's class names added inside the className prop.

//app/components/word-form/index.tsx
import { Form, useTransition } from "remix";
import { Word, WordType } from "~/models/word";
import { Button } from "../basic/button";
import { Input } from "../basic/input";
import { Select } from "../basic/select";
import { TextArea } from "../basic/textarea";

export function WordForm({ word }: { word?: Word }) {
  let transition = useTransition();

  return (
    <Form
      method="post"
      className={`
        px-3 py-4 rounded flex flex-col gap-2 border-2
      `}
    >
      <div>Form State: {transition.state}</div>
      <div>
        <label className="block text-xs" htmlFor="name">
          Word
        </label>
        <Input
          id="name"
          name="name"
          type="text"
          placeholder="Word"
          required
          defaultValue={word?.name ?? ""}
          disabled={Boolean(word?.name)}
        />
      </div>
      <div>
        <label className="block text-xs" htmlFor="type">
          Type
        </label>
        <Select
          id="type"
          name="type"
          defaultValue={word?.type ?? WordType.NOUN}
        >
          <option value={WordType.NOUN}>Noun</option>
          <option value={WordType.VERB}>Verb</option>
          <option value={WordType.ADJECTIVE}>Adjective</option>
        </Select>
      </div>
      {/*TextAreas*/}
      <Button type="submit" color="primary">
        Submit
      </Button>
    </Form>
  );
}
// ...

If you're wondering where the above file came from, that is from my last blog post.

VSCode Plugins

Here are some plugins that you can use to get a better experience using Tailwind and PostCSS in VSCode.

Conclusion

Integrating Tailwind and PostCSS in Remix is straightforward as we don't need to hack into the framework to make them work. We quickly achieved an extendable and customizable CSS generation boilerplate by adding a few configurations.


This content originally appeared on DEV Community and was authored by Carlo Gino Catapang


Print Share Comment Cite Upload Translate Updates
APA

Carlo Gino Catapang | Sciencx (2021-12-04T04:46:25+00:00) Styling Remix using Tailwind and PostCSS. Retrieved from https://www.scien.cx/2021/12/04/styling-remix-using-tailwind-and-postcss/

MLA
" » Styling Remix using Tailwind and PostCSS." Carlo Gino Catapang | Sciencx - Saturday December 4, 2021, https://www.scien.cx/2021/12/04/styling-remix-using-tailwind-and-postcss/
HARVARD
Carlo Gino Catapang | Sciencx Saturday December 4, 2021 » Styling Remix using Tailwind and PostCSS., viewed ,<https://www.scien.cx/2021/12/04/styling-remix-using-tailwind-and-postcss/>
VANCOUVER
Carlo Gino Catapang | Sciencx - » Styling Remix using Tailwind and PostCSS. [Internet]. [Accessed ]. Available from: https://www.scien.cx/2021/12/04/styling-remix-using-tailwind-and-postcss/
CHICAGO
" » Styling Remix using Tailwind and PostCSS." Carlo Gino Catapang | Sciencx - Accessed . https://www.scien.cx/2021/12/04/styling-remix-using-tailwind-and-postcss/
IEEE
" » Styling Remix using Tailwind and PostCSS." Carlo Gino Catapang | Sciencx [Online]. Available: https://www.scien.cx/2021/12/04/styling-remix-using-tailwind-and-postcss/. [Accessed: ]
rf:citation
» Styling Remix using Tailwind and PostCSS | Carlo Gino Catapang | Sciencx | https://www.scien.cx/2021/12/04/styling-remix-using-tailwind-and-postcss/ |

Please log in to upload a file.




There are no updates yet.
Click the Upload button above to add an update.

You must be logged in to translate posts. Please log in or register.