diff --git a/docs/rules/no-unmerged-classname.md b/docs/rules/no-unmerged-classname.md new file mode 100644 index 0000000..02c253f --- /dev/null +++ b/docs/rules/no-unmerged-classname.md @@ -0,0 +1,75 @@ +# Ensure className is merged with spread props (no-unmerged-classname) + +When using spread props (`{...rest}`, `{...props}`, etc.) along with a `className` prop, you should merge the className from the spread props with your custom className to avoid unintentionally overriding classes. + +## Rule details + +This rule warns when a component has spread props before a `className` prop, but the `className` doesn't appear to be merging values using a utility like `clsx()` or `classNames()`. + +👎 Examples of **incorrect** code for this rule: + +```jsx +/* eslint primer-react/no-unmerged-classname: "error" */ + +// ❌ className may override className from rest + + +// ❌ className expression doesn't merge + + +// ❌ Template literal doesn't merge with rest + +``` + +👍 Examples of **correct** code for this rule: + +```jsx +/* eslint primer-react/no-unmerged-classname: "error" */ + +// ✅ Using clsx to merge className from rest + + +// ✅ Using classNames to merge + + +// ✅ Using cn utility to merge + + +// ✅ className before spread (spread will override) + + +// ✅ No spread props + +``` + +## Why this matters + +When you spread props and then specify a className, you might be overriding a className that was passed in through the spread: + +```jsx +// ❌ Bad: className from rest gets lost +function MyComponent({className, ...rest}) { + return