Skip to content

Commit a777f5a

Browse files
authored
fix: preserve explicitly passed undefined props (#5683)
When a user explicitly passes a prop as undefined (e.g., role={undefined}), attrs no longer overwrites it. This allows resetting inherited defaults, fixing compatibility with libraries like MUI and Radix UI. Props set to undefined via .attrs() without an explicit user prop are still stripped as before. Closes #4338
1 parent 5537aee commit a777f5a

3 files changed

Lines changed: 31 additions & 1 deletion

File tree

‎.changeset/fix-undefined-props.md‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"styled-components": patch
3+
---
4+
5+
Preserve explicitly passed `undefined` props instead of stripping them. This fixes compatibility with libraries like MUI and Radix UI that pass `undefined` to reset inherited defaults (e.g., `role={undefined}`). Props set to `undefined` via `.attrs()` are still stripped as before.

‎packages/styled-components/src/models/StyledComponent.ts‎

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -94,7 +94,9 @@ function resolveContext<Props extends BaseObject>(
9494
context.className = joinStrings(context.className, resolvedAttrDef[key] as string);
9595
} else if (key === 'style') {
9696
context.style = { ...context.style, ...(resolvedAttrDef[key] as React.CSSProperties) };
97-
} else {
97+
} else if (!(key in props && (props as any)[key] === undefined)) {
98+
// Apply attr value unless the user explicitly passed undefined for this prop,
99+
// which signals intent to reset the value.
98100
// @ts-expect-error attrs can dynamically add arbitrary properties
99101
context[key] = resolvedAttrDef[key];
100102
}

‎packages/styled-components/src/test/attrs.test.tsx‎

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -353,4 +353,27 @@ describe('attrs', () => {
353353

354354
expect(render(<Comp alternateTheme={{ foo: 'bar' }} />).asFragment()).toMatchSnapshot();
355355
});
356+
357+
it('should preserve explicitly passed undefined props', () => {
358+
const Inner = (props: { role?: string; children?: React.ReactNode }) => (
359+
<div role={props.role} data-has-role={String('role' in props)} />
360+
);
361+
362+
const Comp = styled(Inner).attrs({ role: 'button' })``;
363+
364+
// Without explicit prop, attrs value is used
365+
const withAttrs = render(<Comp />);
366+
expect(withAttrs.container.querySelector('div')!.getAttribute('role')).toBe('button');
367+
368+
// Explicitly passing undefined should override attrs
369+
const withUndefined = render(<Comp role={undefined} />);
370+
expect(withUndefined.container.querySelector('div')!.getAttribute('role')).toBe(null);
371+
});
372+
373+
it('should still strip undefined values from attrs', () => {
374+
const Comp = styled.div.attrs({ 'data-removed': undefined as string | undefined })``;
375+
376+
const { container } = render(<Comp />);
377+
expect(container.querySelector('div')!.hasAttribute('data-removed')).toBe(false);
378+
});
356379
});

0 commit comments

Comments
 (0)