diff --git a/docs/rules/merge-spread-props-classname.md b/docs/rules/merge-spread-props-classname.md new file mode 100644 index 0000000..bc19284 --- /dev/null +++ b/docs/rules/merge-spread-props-classname.md @@ -0,0 +1,75 @@ +# Ensure className is merged with spread props (merge-spread-props-classname) + +When using spread props (`{...rest}`, `{...props}`, etc.) before a `className` prop, the className should be merged using a utility like `clsx` to avoid unintentionally overriding the className from the spread props. + +## Rule details + +This rule enforces that when a JSX element has both spread props and a `className` prop, and the spread props come before the `className`, the className should use `clsx` or a similar utility to merge both class names. + +👎 Examples of **incorrect** code for this rule: + +```jsx +/* eslint primer-react/merge-spread-props-classname: "error" */ + +// ❌ className after spread - not merged + + +// ❌ className expression after spread - not merged + + +// ❌ Multiple spreads with className - not merged + +``` + +👍 Examples of **correct** code for this rule: + +```jsx +/* eslint primer-react/merge-spread-props-classname: "error" */ + +// ✅ className merged with clsx + + +// ✅ className merged with classnames + + +// ✅ className before spread (spread will override, which is expected) + + +// ✅ Only spread props + + +// ✅ Only className + +``` + +## Why this matters + +When you have spread props before a className, the className from the spread props can be overridden if you don't merge them properly: + +```jsx +// ❌ Bad: className from rest gets overridden +function MyComponent({className, ...rest}) { + return