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] Grid: Add column prop #2143

Merged
merged 2 commits into from Aug 17, 2022
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
6 changes: 6 additions & 0 deletions docs/src/docs/core/Grid.mdx
Expand Up @@ -42,6 +42,12 @@ Set `offset` prop on `Grid.Col` component to create gaps in grid.

<Demo data={GridDemos.offset} />

## Order

Set the `order` prop on `Grid.Col` component to change the order of columns:

<Demo data={GridDemos.order} />

## Multiple rows

Once children columns span and offset sum exceeds `columns` prop (defaults to 12),
Expand Down
17 changes: 17 additions & 0 deletions src/mantine-core/src/Grid/Col/Col.styles.ts
Expand Up @@ -22,6 +22,12 @@ interface ColStyles {
md: number;
lg: number;
xl: number;
order: React.CSSProperties['order'];
orderXs: React.CSSProperties['order'];
orderSm: React.CSSProperties['order'];
orderMd: React.CSSProperties['order'];
orderLg: React.CSSProperties['order'];
orderXl: React.CSSProperties['order'];
}

const getColumnWidth = (colSpan: number, columns: number) => `${100 / (columns / colSpan)}%`;
Expand All @@ -31,19 +37,22 @@ const getColumnOffset = (offset: number, columns: number) =>
function getBreakpointsStyles({
sizes,
offsets,
orders,
theme,
columns,
grow,
}: {
sizes: Record<MantineSize, number>;
offsets: Record<MantineSize, number>;
orders: Record<MantineSize, React.CSSProperties['order']>;
grow: boolean;
theme: MantineTheme;
columns: number;
}) {
return MANTINE_SIZES.reduce((acc, size) => {
if (typeof sizes[size] === 'number') {
acc[`@media (min-width: ${theme.breakpoints[size] + 1}px)`] = {
order: orders[size],
flexBasis: getColumnWidth(sizes[size], columns),
flexShrink: 0,
maxWidth: grow ? 'unset' : getColumnWidth(sizes[size], columns),
Expand Down Expand Up @@ -73,11 +82,18 @@ export default createStyles(
md,
lg,
xl,
order,
orderXs,
orderSm,
orderMd,
orderLg,
orderXl,
}: ColStyles
) => ({
root: {
boxSizing: 'border-box',
flexGrow: grow ? 1 : 0,
order,
padding: theme.fn.size({ size: gutter, sizes: theme.spacing }) / 2,
marginLeft: getColumnOffset(offset, columns),
flexBasis: getColumnWidth(span, columns),
Expand All @@ -86,6 +102,7 @@ export default createStyles(
...getBreakpointsStyles({
sizes: { xs, sm, md, lg, xl },
offsets: { xs: offsetXs, sm: offsetSm, md: offsetMd, lg: offsetLg, xl: offsetXl },
orders: { xs: orderXs, sm: orderSm, md: orderMd, lg: orderLg, xl: orderXl },
theme,
columns,
grow,
Expand Down
30 changes: 30 additions & 0 deletions src/mantine-core/src/Grid/Col/Col.tsx
Expand Up @@ -11,6 +11,24 @@ export interface ColProps extends DefaultProps, React.ComponentPropsWithoutRef<'
/** Column left offset */
offset?: number;

/** Default col order */
order?: React.CSSProperties['order'];

/** Col order at (min-width: theme.breakpoints.xs) */
orderXs?: React.CSSProperties['order'];

/** Col order at (min-width: theme.breakpoints.sm) */
orderSm?: React.CSSProperties['order'];

/** Col order at (min-width: theme.breakpoints.md) */
orderMd?: React.CSSProperties['order'];

/** Col order at (min-width: theme.breakpoints.lg) */
orderLg?: React.CSSProperties['order'];

/** Col order at (min-width: theme.breakpoints.xl) */
orderXl?: React.CSSProperties['order'];

/** Column left offset at (min-width: theme.breakpoints.xs) */
offsetXs?: number;

Expand Down Expand Up @@ -70,6 +88,12 @@ export const Col = forwardRef<HTMLDivElement, ColProps>((props: ColProps, ref) =
md,
lg,
xl,
order,
orderXs,
orderSm,
orderMd,
orderLg,
orderXl,
className,
id,
unstyled,
Expand Down Expand Up @@ -97,6 +121,12 @@ export const Col = forwardRef<HTMLDivElement, ColProps>((props: ColProps, ref) =
md,
lg,
xl,
order,
orderXs,
orderSm,
orderMd,
orderLg,
orderXl,
grow: ctx.grow,
columns: ctx.columns,
span: colSpan,
Expand Down
37 changes: 37 additions & 0 deletions src/mantine-demos/src/demos/core/Grid/Grid.demo.order.tsx
@@ -0,0 +1,37 @@
import React from 'react';
import { Grid } from '@mantine/core';
import { ColWrapper as Col } from './_col-wrapper';

const code = `
import { Grid } from '@mantine/core';

function Demo() {
return (
<Grid>
<Col span={3} order={2}>2</Col>
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please add responsive props (let's say orderSm and orderLg to the demo)

Copy link
Contributor Author

@mdodell mdodell Aug 16, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sounds good - should I also use the useMediaQuery hook here to relabel the children of Grid.Col based on their new order?

I based this off of the offset docs which didn't have them, so I withheld the responsive props for consistency here.

<Col span={3} order={3}>3</Col>
<Col span={3}>1</Col>
</Grid>
);
}
`;

function Demo() {
return (
<Grid>
<Col span={3} order={2}>
2
</Col>
<Col span={3} order={3}>
3
</Col>
<Col span={3}>1</Col>
</Grid>
);
}

export const order: MantineDemo = {
type: 'demo',
code,
component: Demo,
};
1 change: 1 addition & 0 deletions src/mantine-demos/src/demos/core/Grid/index.ts
@@ -1,6 +1,7 @@
export { usage } from './Grid.demo.usage';
export { growConfigurator } from './Grid.demo.growConfigurator';
export { offset } from './Grid.demo.offset';
export { order } from './Grid.demo.order';
export { rows } from './Grid.demo.rows';
export { flexConfigurator } from './Grid.demo.flexConfigurator';
export { responsive } from './Grid.demo.responsive';
Expand Down