Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Revert "[Slot] Deduplicate merged className prop (#2336)" #3162

Merged
merged 2 commits into from
Oct 1, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions .yarn/versions/9bca397e.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
declined:
- primitives
- "@radix-ui/react-slot"
37 changes: 0 additions & 37 deletions packages/react/slot/src/Slot.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -106,43 +106,6 @@ describe('given a slotted Trigger', () => {
expect(handleChildClick).toHaveBeenCalledTimes(1);
});
});

describe('with className on itself', () => {
beforeEach(() => {
render(
<Trigger as={Slot} className="btn">
<button type="button">Click me</button>
</Trigger>
);
});

it('should apply the className to the child', () => {
expect(screen.getByRole('button')).toHaveClass('btn');
});
});

describe('with className on itself AND the child', () => {
beforeEach(() => {
render(
<Trigger as={Slot} className="btn btn-sm">
<button type="button" className=" btn btn-primary ">
Click me
</button>
</Trigger>
);
});

it('should merge className and apply it to the child', () => {
expect(screen.getByRole('button')).toHaveClass('btn', 'btn-sm', 'btn-primary');
});

it('should deduplicate merged className', () => {
const classNames = screen.getByRole('button').className.split(' ');

expect(classNames).toHaveLength(3);
expect(classNames.filter((name) => name === 'btn')).toHaveLength(1);
});
});
});

describe('given a Button with Slottable', () => {
Expand Down
21 changes: 2 additions & 19 deletions packages/react/slot/src/Slot.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -115,25 +115,8 @@ function mergeProps(slotProps: AnyProps, childProps: AnyProps) {
// if it's `style`, we merge them
else if (propName === 'style') {
overrideProps[propName] = { ...slotPropValue, ...childPropValue };
}
// if it's `className`, we deduplicate and merge them
else if (propName === 'className') {
const classNameSet = new Set<string>();

if (slotPropValue && typeof slotPropValue === 'string') {
slotPropValue
.split(' ')
.filter(Boolean)
.forEach((name) => classNameSet.add(name));
}
if (childPropValue && typeof childPropValue === 'string') {
childPropValue
.split(' ')
.filter(Boolean)
.forEach((name) => classNameSet.add(name));
}

overrideProps[propName] = [...classNameSet].join(' ');
} else if (propName === 'className') {
overrideProps[propName] = [slotPropValue, childPropValue].filter(Boolean).join(' ');
}
}

Expand Down
Loading