DEV Community

j1ngzoue
j1ngzoue

Posted on

1

Type-safe use of css custom-property in Vite

Introduce

When staring with css modules in react, I often encounter situations where custom properties are used.
When staring with css modules in react, I often encounter situations where custom properties are used.
We have released a tool that solves those problems, and we would like to introduce it to you.
If you are using Vite, you can read and use css-preprocessoroptions.

Repo

https://github.com/activeguild/css-custom-property-extractor

Usage

  • Install the package.
npm i -D css-custom-property-extractor
Enter fullscreen mode Exit fullscreen mode
  • For example. Parsing bootstrap.scss outputs the following typescript file.
npx ccpe -i ./samples/scss/bootstrap.scss
Enter fullscreen mode Exit fullscreen mode

output.

/**
 *  #0d6efd;
 */
export const bsBlue = "var(--bs-blue)"
/**
 *  #6610f2;
 */
export const bsIndigo = "var(--bs-indigo)"
/**
 *  #6f42c1;
 */
export const bsPurple = "var(--bs-purple)"
...
Enter fullscreen mode Exit fullscreen mode
  • Import and use the output typescript file.

At the end

We have shown how to bring custom properties into the TS world and use them in type safety.
In addition to this, please read on to find out how to load styles and use classes in a type-safe manner.
https://dev.to/activeguild/i-made-a-typescript-plugin-for-css-modules-that-can-be-used-with-vite-3ofg
https://dev.to/activeguild/type-safe-development-with-vite-x-react-x-css-modules-x-typescript-1ebc

Sentry image

Hands-on debugging session: instrument, monitor, and fix

Join Lazar for a hands-on session where you’ll build it, break it, debug it, and fix it. You’ll set up Sentry, track errors, use Session Replay and Tracing, and leverage some good ol’ AI to find and fix issues fast.

RSVP here →

Top comments (0)

A Workflow Copilot. Tailored to You.

Pieces.app image

Our desktop app, with its intelligent copilot, streamlines coding by generating snippets, extracting code from screenshots, and accelerating problem-solving.

Read the docs

👋 Kindness is contagious

Please leave a ❤️ or a friendly comment on this post if you found it helpful!

Okay