I Tried a Way to Detect Unknown Properties in Emotion's CSS Objects
Contents
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.
Using the css/no-unknown-property rule from eslint-plugin-css, we detect "unknown properties" in object-style CSS inline in the editor.
| 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 CSSdefineFunctions: treats argument objects of function calls as CSS
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:
- GitHub: [stylelint/postcss-css-in-js](https://github.com/stylelint/postcss-css-in-js: "This repository was archived by the owner on Feb 9, 2023. It is now read-only."
- npm: [@stylelint/postcss-css-in-js](https://www.npmjs.com/package/@stylelint/postcss-css-in-js: "This package has been deprecated"
It is also incompatible with Stylelint v16, and the following error occurs at runtime.
TypeError: Cannot read properties of undefined (reading 'stringify')
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",
});
eslint-plugin-css is a plugin that lets ESLint check the object style of CSS-in-JS.
npm install --save-dev eslint-plugin-css
// 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: ["/^&/", "/^--/"]
<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.
const style = {
color: "red",
colr: "blue", // error
};
<div css={style}>
const cssStyle = css({
color: "red",
colr: "blue", // error
});
Setting: add @emotion/react to defineFunctions
The argument object of the css() function is detected automatically.
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
}}
/>
This post is a summary of what a beginner researched and tested.
Translated from the Japanese original.
Thanks. I'll look into it.
Sorry, that didn't go through. Please try again later.