Skip to content
Open
6 changes: 6 additions & 0 deletions src/components/space/space.less
Original file line number Diff line number Diff line change
Expand Up @@ -25,12 +25,18 @@
}
> .@{class-prefix-space}-item {
margin-right: var(--gap-horizontal);
&:last-child {
margin-right: 0;
}
}
&.@{class-prefix-space}-wrap {
flex-wrap: wrap;
margin-bottom: calc(var(--gap-vertical) * -1);
> .@{class-prefix-space}-item {
padding-bottom: var(--gap-vertical);
&:last-child {
padding-bottom: 0;
}

Choose a reason for hiding this comment

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

critical

这部分修改会为换行布局引入新的 bug。:last-child 选择器只会匹配最后一个子元素。如果启用了 wrap 并且最后一行有多个元素,那么只有最后一个元素(即整组元素的最后一个)的 padding-bottom 会被移除,导致最后一行内的元素垂直方向无法对齐。

实际上,父容器上的 margin-bottom: calc(var(--gap-vertical) * -1) 已经正确地处理了最后一行多余的 padding-bottom,它通过负外边距将整个容器的底部“拉上来”,从而抵消了最后一行的内边距。因此,这里的修改是不必要的,并且是有害的,应当移除。

}
}
Comment on lines 26 to 38

Choose a reason for hiding this comment

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

medium

虽然移除最后一个元素的 margin-right 确实解决了 justify-content: center 时的居中问题,但目前通过子元素 margin 和父元素负 margin 来实现间距的整体方案比较复杂,并且容易在边缘场景下出错(比如这次 wrap 模式下的问题)。

一个更现代、更健壮的方案是直接在 flex 容器上使用 gap 属性。这能极大地简化 CSS 代码,并且能更好地处理换行场景。

建议考虑将这部分重构为使用 gap 属性,例如:

.@{class-prefix-space}-horizontal {
  flex-direction: row;
  column-gap: var(--gap-horizontal); // 水平间距
  &.@{class-prefix-space}-wrap {
    flex-wrap: wrap;
    row-gap: var(--gap-vertical); // 换行后的垂直间距
  }
}

这样就可以移除所有在 .@{class-prefix-space}-item 上设置的 margin/padding 规则,以及父容器上的负 margin 规则。考虑到项目中已经使用了 CSS 变量,浏览器的 gap 属性支持度应该不是问题。

}
Expand Down
Loading