Skip to content

Commit 19ff93c

Browse files
committed
fix(grid): fix responsive breakpoints and gutter wrapping
CSS used max-width (broken) instead of min-width (mobile-first), had a duplicate sm query, and was missing xs. Row gutter used index-based padding causing indent on wrapped columns; switch to negative-margin approach with equal padding on all cols.
1 parent 30be362 commit 19ff93c

4 files changed

Lines changed: 60 additions & 15 deletions

File tree

components/grid/demo/responsive.md

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
<demo>
2+
3+
### Responsive
4+
5+
Columns respond to viewport width using `xs`, `sm`, `md`, `lg`, `xl`, `xxl` breakpoints. Resize the browser to see columns reflow.
6+
7+
```jsx live
8+
() => {
9+
const style = (bg) => ({
10+
background: bg,
11+
color: '#fff',
12+
padding: '12px 0',
13+
textAlign: 'center',
14+
borderRadius: 4,
15+
marginBottom: 8,
16+
});
17+
18+
return (
19+
<>
20+
<Row gutter={[16, 16]}>
21+
<Col xs={24} sm={12} md={8} lg={6}>
22+
<div style={style('rgba(110, 65, 191, 0.9)')}>xs=24 sm=12 md=8 lg=6</div>
23+
</Col>
24+
<Col xs={24} sm={12} md={8} lg={6}>
25+
<div style={style('rgba(110, 65, 191, 0.7)')}>xs=24 sm=12 md=8 lg=6</div>
26+
</Col>
27+
<Col xs={24} sm={12} md={8} lg={6}>
28+
<div style={style('rgba(110, 65, 191, 0.9)')}>xs=24 sm=12 md=8 lg=6</div>
29+
</Col>
30+
<Col xs={24} sm={12} md={8} lg={6}>
31+
<div style={style('rgba(110, 65, 191, 0.7)')}>xs=24 sm=12 md=8 lg=6</div>
32+
</Col>
33+
</Row>
34+
</>
35+
);
36+
}
37+
```
38+
39+
</demo>

components/grid/index.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import Gutter from './demo/gutter.md'
33
import Offset from './demo/offset.md'
44
import Order from './demo/order.md'
55
import Alignment from './demo/alignment.md'
6+
import Responsive from './demo/responsive.md'
67

78
# Grid
89

@@ -21,6 +22,7 @@ import { Row, Col } from 'tiny-ui';
2122
<Offset />
2223
<Order />
2324
<Alignment />
25+
<Responsive />
2426

2527
## API
2628

components/grid/row.tsx

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -28,18 +28,24 @@ const Row = React.forwardRef<HTMLDivElement, RowProps>((props, ref) => {
2828
};
2929

3030
const normalisedGutter = getGutter();
31+
32+
const rowGutterStyle = gutter
33+
? {
34+
marginLeft: gutterSide ? 0 : -normalisedGutter[0] / 2,
35+
marginRight: gutterSide ? 0 : -normalisedGutter[0] / 2,
36+
rowGap: normalisedGutter[1] || undefined,
37+
}
38+
: {};
39+
3140
return (
32-
<div {...otherProps} ref={ref} className={cls} style={style}>
33-
{React.Children.map(children, (child, idx: number) => {
41+
<div {...otherProps} ref={ref} className={cls} style={{ ...style, ...rowGutterStyle }}>
42+
{React.Children.map(children, (child) => {
3443
const childElement = child as React.FunctionComponentElement<ColProps>;
3544
if (childElement.type.displayName === 'Col') {
3645
const gutterStyle = gutter
3746
? {
38-
paddingLeft: !gutterSide && idx === 0 ? 0 : normalisedGutter[0] / 2, // first child left padding
39-
paddingRight:
40-
!gutterSide && idx === React.Children.count(children) - 1
41-
? 0
42-
: normalisedGutter[0] / 2,
47+
paddingLeft: normalisedGutter[0] / 2,
48+
paddingRight: normalisedGutter[0] / 2,
4349
}
4450
: {};
4551
const childProps: Partial<ColProps> = {

components/grid/style/_index.scss

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -70,23 +70,21 @@
7070
box-sizing: border-box;
7171
display: block;
7272
@include grid();
73+
@include grid('-xs');
7374

74-
@media screen and (max-width: $size-sm) {
75+
@media screen and (min-width: $size-sm) {
7576
@include grid('-sm');
7677
}
77-
@media screen and (max-width: $size-sm) {
78-
@include grid('-sm');
79-
}
80-
@media screen and (max-width: $size-md) {
78+
@media screen and (min-width: $size-md) {
8179
@include grid('-md');
8280
}
83-
@media screen and (max-width: $size-lg) {
81+
@media screen and (min-width: $size-lg) {
8482
@include grid('-lg');
8583
}
86-
@media screen and (max-width: $size-xl) {
84+
@media screen and (min-width: $size-xl) {
8785
@include grid('-xl');
8886
}
89-
@media screen and (max-width: $size-xxl) {
87+
@media screen and (min-width: $size-xxl) {
9088
@include grid('-xxl');
9189
}
9290
}

0 commit comments

Comments
 (0)