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

[@mantine/core] Group: use css selectors to style children #2162

Merged
merged 1 commit into from Sep 17, 2022
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
13 changes: 0 additions & 13 deletions docs/src/docs/core/Group.mdx
Expand Up @@ -20,19 +20,6 @@ import { GroupDemos } from '@mantine/demos';

## Group children

Group component uses `className` prop to add styles to its children, child components must accept it::

```tsx
// Will not work with Group – component does not handle className
const MyButton = ({ label }) => <button>{label}</button>;

// Will work with Group – handles className
const MyButton = ({ label, className }) => <button className={className}>{label}</button>;

// Will work with Group – handles className
const MyButton = ({ label, ...others }) => <button {...others}>{label}</button>;
```

**!important** Group will work only with React elements. Strings, numbers, fragments and other parts will
not have correct styles:

Expand Down
21 changes: 10 additions & 11 deletions src/mantine-core/src/Group/Group.styles.ts
Expand Up @@ -29,17 +29,16 @@ export default createStyles(
flexWrap: noWrap ? 'nowrap' : 'wrap',
justifyContent: GROUP_POSITIONS[position],
gap: theme.fn.size({ size: spacing, sizes: theme.spacing }),
},

child: {
boxSizing: 'border-box',
maxWidth: grow
? `calc(${100 / count}% - ${
theme.fn.size({ size: spacing, sizes: theme.spacing }) -
theme.fn.size({ size: spacing, sizes: theme.spacing }) / count
}px)`
: undefined,
flexGrow: grow ? 1 : 0,
'& > *': {
boxSizing: 'border-box',
maxWidth: grow
? `calc(${100 / count}% - ${
theme.fn.size({ size: spacing, sizes: theme.spacing }) -
theme.fn.size({ size: spacing, sizes: theme.spacing }) / count
}px)`
: undefined,
flexGrow: grow ? 1 : 0,
},
},
})
);
2 changes: 1 addition & 1 deletion src/mantine-core/src/Group/Group.test.tsx
Expand Up @@ -17,6 +17,6 @@ describe('@mantine/core/Group', () => {
it('has no falsy children', () => {
const children = [undefined, null, <div key="1" />];
const { container } = render(<Group>{children}</Group>);
expect(container.querySelectorAll('.mantine-Group-child')).toHaveLength(1);
expect(container.querySelectorAll('.mantine-Group-root > *')).toHaveLength(1);
});
});
12 changes: 1 addition & 11 deletions src/mantine-core/src/Group/Group.tsx
Expand Up @@ -43,19 +43,9 @@ export const Group = forwardRef<HTMLDivElement, GroupProps>((props: GroupProps,
{ unstyled, name: 'Group' }
);

const items = filteredChildren.map((child) => {
if (typeof child === 'object' && child !== null && 'props' in child) {
return React.cloneElement(child, {
className: cx(classes.child, child.props?.className),
});
}

return child;
});

return (
<Box className={cx(classes.root, className)} ref={ref} {...others}>
{items}
{filteredChildren}
</Box>
);
});
Expand Down