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 auto and content spans #2317
Merged
Merged
Changes from 2 commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -6,6 +6,8 @@ import { | |
MantineTheme, | ||
} from '@mantine/styles'; | ||
|
||
export type ColSpan = number | 'auto' | 'content'; | ||
|
||
interface ColStyles { | ||
gutter: MantineNumberSize; | ||
columns: number; | ||
|
@@ -16,12 +18,12 @@ interface ColStyles { | |
offsetMd: number; | ||
offsetLg: number; | ||
offsetXl: number; | ||
span: number; | ||
xs: number; | ||
sm: number; | ||
md: number; | ||
lg: number; | ||
xl: number; | ||
span: ColSpan; | ||
xs: ColSpan; | ||
sm: ColSpan; | ||
md: ColSpan; | ||
lg: ColSpan; | ||
xl: ColSpan; | ||
order: React.CSSProperties['order']; | ||
orderXs: React.CSSProperties['order']; | ||
orderSm: React.CSSProperties['order']; | ||
|
@@ -30,8 +32,29 @@ interface ColStyles { | |
orderXl: React.CSSProperties['order']; | ||
} | ||
|
||
const getColumnWidth = (colSpan: number, columns: number) => | ||
colSpan ? `${100 / (columns / colSpan)}%` : undefined; | ||
const getColumnFlexBasis = (colSpan: ColSpan, columns: number) => { | ||
if (colSpan === 'content') { | ||
return 'auto'; | ||
} | ||
Comment on lines
+36
to
+38
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. |
||
if (colSpan === 'auto') { | ||
return '0px'; | ||
} | ||
return colSpan ? `${100 / (columns / colSpan)}%` : undefined; | ||
}; | ||
|
||
const getColumnMaxWidth = (colSpan: ColSpan, columns: number, grow: boolean) => { | ||
if (grow || colSpan === 'auto' || colSpan === 'content') { | ||
return 'unset'; | ||
} | ||
return getColumnFlexBasis(colSpan, columns); | ||
}; | ||
|
||
const getColumnFlexGrow = (colSpan: ColSpan, grow: boolean) => { | ||
if (!colSpan) { | ||
return undefined; | ||
} | ||
return colSpan === 'auto' || grow ? 1 : 0; | ||
}; | ||
|
||
const getColumnOffset = (offset: number, columns: number) => | ||
offset ? `${100 / (columns / offset)}%` : undefined; | ||
|
@@ -44,7 +67,7 @@ function getBreakpointsStyles({ | |
columns, | ||
grow, | ||
}: { | ||
sizes: Record<MantineSize, number>; | ||
sizes: Record<MantineSize, ColSpan>; | ||
offsets: Record<MantineSize, number>; | ||
orders: Record<MantineSize, React.CSSProperties['order']>; | ||
grow: boolean; | ||
|
@@ -54,10 +77,11 @@ function getBreakpointsStyles({ | |
return MANTINE_SIZES.reduce((acc, size) => { | ||
acc[`@media (min-width: ${theme.breakpoints[size] + 1}px)`] = { | ||
order: orders[size], | ||
flexBasis: getColumnWidth(sizes[size], columns), | ||
flexBasis: getColumnFlexBasis(sizes[size], columns), | ||
flexShrink: 0, | ||
maxWidth: grow ? 'unset' : getColumnWidth(sizes[size], columns), | ||
maxWidth: getColumnMaxWidth(sizes[size], columns, grow), | ||
marginLeft: getColumnOffset(offsets[size], columns), | ||
flexGrow: getColumnFlexGrow(sizes[size], grow), | ||
}; | ||
return acc; | ||
}, {}); | ||
|
@@ -92,13 +116,14 @@ export default createStyles( | |
) => ({ | ||
root: { | ||
boxSizing: 'border-box', | ||
flexGrow: grow ? 1 : 0, | ||
flexGrow: getColumnFlexGrow(span, grow), | ||
order, | ||
padding: theme.fn.size({ size: gutter, sizes: theme.spacing }) / 2, | ||
marginLeft: getColumnOffset(offset, columns), | ||
flexBasis: getColumnWidth(span, columns), | ||
flexBasis: getColumnFlexBasis(span, columns), | ||
flexShrink: 0, | ||
maxWidth: grow ? 'unset' : getColumnWidth(span, columns), | ||
width: span === 'content' ? 'auto' : undefined, | ||
maxWidth: getColumnMaxWidth(span, columns, grow), | ||
...getBreakpointsStyles({ | ||
sizes: { xs, sm, md, lg, xl }, | ||
offsets: { xs: offsetXs, sm: offsetSm, md: offsetMd, lg: offsetLg, xl: offsetXl }, | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
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="auto">span=auto</Col> | ||
<Col span={6}>span=6</Col> | ||
<Col span="auto">span=auto</Col> | ||
</Grid> | ||
); | ||
} | ||
`; | ||
|
||
function Demo() { | ||
return ( | ||
<Grid> | ||
<Col span="auto">1</Col> | ||
<Col span={6}>2</Col> | ||
<Col span="auto">3</Col> | ||
</Grid> | ||
); | ||
} | ||
|
||
export const auto: MantineDemo = { | ||
type: 'demo', | ||
code, | ||
component: Demo, | ||
}; |
31 changes: 31 additions & 0 deletions
31
src/mantine-demos/src/demos/core/Grid/Grid.demo.content.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
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="content">fit content</Col> | ||
<Col span={6}>2</Col> | ||
</Grid> | ||
); | ||
} | ||
`; | ||
|
||
function Demo() { | ||
return ( | ||
<Grid> | ||
<Col span="content">fit content</Col> | ||
<Col span={6}>2</Col> | ||
</Grid> | ||
); | ||
} | ||
|
||
export const content: MantineDemo = { | ||
type: 'demo', | ||
code, | ||
component: Demo, | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Unrelated - this documentation is incorrect.
grow
affects all rows, not just the last.