DEV Community

Naxrul Ahmed
Naxrul Ahmed

Posted on

Introducing ONCSS: A Lightweight CSS-in-JS Library for Modern JavaScript Applications

I've been working on ONCSS, a CSS-in-JS library designed to make styling modern web applications simple, flexible, and developer-friendly.

Instead of writing separate CSS files for every component, ONCSS lets you define styles directly in JavaScript using a familiar object-based API.

Why ONCSS?

ONCSS provides a css() function that generates and injects CSS dynamically.

Here's a quick example:

import css from "oncss";

const button = css({
  backgroundColor: "#2563eb",
  color: "#fff",
  padding: "10px 20px",
  borderRadius: "8px",

  "&:hover": {
    backgroundColor: "#1d4ed8",
  },

  "@media (min-width: 768px)": {
    padding: "14px 28px",
  },
});

function Button() {
  return <button className={button}>Click me</button>;
}
Enter fullscreen mode Exit fullscreen mode

Features

  • Object-based CSS styling
  • Nested selectors and pseudo-classes
  • Responsive media queries
  • Custom breakpoints
  • Dynamic keyframes and animations
  • Global styles
  • CSS at-rules: @media, @container, @layer, @supports
  • Custom aliases and CSS property transformations
  • Configurable style injection
  • TypeScript support
  • Server-side rendering support

Installation

npm install oncss
Enter fullscreen mode Exit fullscreen mode

Built for flexibility

ONCSS also supports customization through configuration options such as classPrefix, breakpoints, aliases, injectStyle, and custom property handlers.

It also provides CSSFactory for collecting generated styles during server-side rendering.

I'm building ONCSS as an open-source project and would appreciate feedback from developers who work with React and modern JavaScript styling.

Check it out:

If you try it, I'd love to hear what you think and what features you'd like to see next.

javascript #react #css #opensource #webdev

Top comments (0)