I Tried a Way to Detect Unknown Properties in Emotion's CSS Objects

Contents

Introduction

When using CSS-in-JS (Emotion, styled-components, etc.), you can miss typos like the one below.

const style = {
  color: "red",
  colr: "blue", // a typo, but no error
};

With plain CSS, Stylelint can detect unknown properties, but in Stylelint the package for parsing CSS-in-JS has been deprecated.

In this post, I'd like to share the results of trying a way to detect unknown CSS properties inline in the editor in a project that uses Emotion.

※ I tried approaches that make it an error at the system level, not approaches like adding types, where human carelessness can still let a typo through.

What this post aims to detect

Using the css/no-unknown-property rule from eslint-plugin-css, we detect "unknown properties" in object-style CSS inline in the editor.

Detection by writing style

Pattern Required setting Detection rule
css={{ }} inline attributes: ["css"] css/no-unknown-property
css({ }) variable Add @emotion/react to defineFunctions css/no-unknown-property
Object variable → css attribute attributes: ["css"] css/no-unknown-property
  • attributes: treats objects passed to JSX attributes as CSS
  • defineFunctions: treats argument objects of function calls as CSS

Why Stylelint cannot be used

Parsing CSS-in-JS with Stylelint required @stylelint/postcss-css-in-js, but this package was archived in February 2023 and is deprecated.

This syntax is deprecated. Please refer to the Stylelint v15 migration guide.

Source: postcss-css-in-js README

If you try to install it with npm, the following warning appears.

npm warn deprecated @stylelint/postcss-css-in-js@0.37.3:
Package no longer supported.

Official sources:

It is also incompatible with Stylelint v16, and the following error occurs at runtime.

TypeError: Cannot read properties of undefined (reading 'stringify')

(To begin with) what Stylelint covers

Even if it did work, Stylelint can only parse the template literal style (css`...`) and does not support the object style.

// Covered by Stylelint (template literal)
const style = css`
  color: red;
`;

// Not covered by Stylelint (object style)
const style = css({
  color: "red",
});

Adding eslint-plugin-css

eslint-plugin-css is a plugin that lets ESLint check the object style of CSS-in-JS.

Installation

npm install --save-dev eslint-plugin-css

ESLint configuration

// eslint.config.mjs
import * as cssPlugin from "eslint-plugin-css";

export default [
  // other config

  cssPlugin.configs["flat/recommended"],
  {
    settings: {
      css: {
        target: {
          // target the css={...} attribute
          attributes: ["css"],
          // target the css({ }) function
          defineFunctions: {
            "@emotion/react": ["css"],
          },
        },
      },
    },
    rules: {
      // allow Emotion's nested selectors (&:hover, etc.)
      "css/no-unknown-property": [
        "error",
        {
          ignoreProperties: ["/^&/"], // allow selectors starting with &
        },
      ],
    },
  },
];

Caution

eslint-plugin-css detects errors by checking against a list of standard CSS properties.

In Emotion you can write nested selectors such as &:hover as object keys, but css/no-unknown-property interprets these as "property names", so you need to exclude them with ignoreProperties.

Syntax Example Fix
Nested selector &:hover, & > div ignoreProperties: ["/^&/"]
CSS variable --primary-color ignoreProperties: ["/^--/"]

The configuration example above allows nested selectors that start with &. If you also use CSS variables, add them as follows.

ignoreProperties: ["/^&/", "/^--/"]

Details by writing pattern

Pattern 1: css={{ }}

<div
  css={{
    color: "red",
    colr: "blue",  // error
  }}
>

This is the form where you pass an object directly to the JSX css attribute. It is detected inline.

Pattern 2: Object variable

const style = {
  color: "red",
  colr: "blue",  // error
};

<div css={style}>

Pattern 3: css({ }) variable

const cssStyle = css({
  color: "red",
  colr: "blue", // error
});

Setting: add @emotion/react to defineFunctions

The argument object of the css() function is detected automatically.

Note: when using MUI

You can add MUI's sx attribute as a detection target in the same way. Add "sx" to attributes in eslint.config.mjs.

attributes: ["css", "sx"],

With this, typos are detected in code like the following as well.

import { Box } from "@mui/material";

<Box
  sx={{
    color: "red",
    colr: "blue",  // error
  }}
/>

Closing

This post is a summary of what a beginner researched and tested.


References

Translated from the Japanese original.

Thanks. I'll look into it.

Spotted a mistake?

Tell me what's wrong and I'll fix it. Only I see what you send.