JGuang

网格

Grid · Material UI

交互预览
npm install @mui/material@9.4.0 @emotion/react@11.14.0 @emotion/styled@11.14.1

网格

用原生 size、offset 和间距构建响应式行布局。

本指南覆盖默认 Emotion 环境的 Material UI Core;扩展包与额外示例按清单单独标注。

使用场景与业务边界

Grid 不是瀑布流或数据表格;本版本 size API 不等于旧 xs/item API,也不是 PigmentGrid 配置。

原生组成与范围

Grid

保持 Material UI 原生组合与类型,不替换为 JGuang/Base UI/Radix 同名 API。示例条目不是额外组件。

未纳入的集成: PigmentGrid (@mui/material-pigment-css). 不宣称已在默认 Emotion 环境验证。

原生安装与初始化

npm install @mui/material@9.4.0 @emotion/react@11.14.0 @emotion/styled@11.14.1

采用原生 npm 包与 Emotion,不提供虚构的 JGuang UI Registry 地址。将示例放在原生 ThemeProvider 内,按项目选择 CssBaseline 或 ScopedCssBaseline。先检查 React、现有 Provider、锁文件与 SSR 配置;不要覆盖用户文件或忽略 peer 冲突。本批实际验收 React 19.2.8;React 18 及以下须匹配 react-is。完整初始化规则见随附的双语 setup 文档。

关键原生参数

仅解释本指南选出的关键参数,不是平铺的通用 API。原生继承来源、slot 结构、可空值与泛型仍按固定源码保留。

所属组件.属性用途说明声明来源
Grid.containerGrid 布局标志或 Portal DOM 容器,二者类型不可混用。Grid
Grid.size所属组件的原生尺寸:控件使用枚举,Grid 使用响应式跨列值。Grid
Grid.offset原生响应式 Grid 列偏移。Grid
Grid.columns原生 Grid 的总列数,可按断点配置。Grid
Grid.spacing原生主题间距单位或受支持的 CSS 长度,不总是裸像素。Grid

可运行原生示例

下列机器键只选择示例入口,不会作为通用 variant 属性传入每个组件。原生示例 JSX 与数据保持上游原文;JGuang 编写的验收示例明确标注。

  • 基础网格 · BasicGrid · 上游原生示例
  • 多个断点 · FullWidthGrid · 上游原生示例
  • 行列间距 · RowAndColumnSpacing · 上游原生示例
  • 响应式属性值 · ResponsiveGrid · 上游原生示例
  • 自动布局 · AutoGrid · 上游原生示例
  • 可变宽度内容 · VariableWidthGrid · 上游原生示例
  • 继承间距 · NestedGrid · 上游原生示例
  • 继承列数 · NestedGridColumns · 上游原生示例
  • 列数 · ColumnsGrid · 上游原生示例
  • 偏移 · OffsetGrid · 上游原生示例
  • 元素居中 · CenteredElementGrid · 上游原生示例
  • 完整边框 · FullBorderedGrid · 上游原生示例
  • 半边框 · HalfBorderedGrid · 上游原生示例
  • 列方向 · ColumnLayoutInsideGrid · 上游原生示例

尚未支持的额外示例

这些条目仍保留在上游清单中,不静默跳过,也不展示为已完成预览。需要补充依赖/框架验收后再纳入可运行列表。

  • 间距 · SpacingGrid: @mui/internal-core-docs/HighlightedCode
  • 交互演示 · InteractiveGrid: @mui/internal-core-docs/HighlightedCode

真实使用示例

固定版本的未修改原生示例;复制时同时保留所需的局部辅助文件。

import { styled } from '@mui/material/styles';
import Box from '@mui/material/Box';
import Paper from '@mui/material/Paper';
import Grid from '@mui/material/Grid';

const Item = styled(Paper)(({ theme }) => ({
  backgroundColor: '#fff',
  ...theme.typography.body2,
  padding: theme.spacing(1),
  textAlign: 'center',
  color: (theme.vars ?? theme).palette.text.secondary,
  ...theme.applyStyles('dark', {
    backgroundColor: '#1A2027',
  }),
}));

export default function BasicGrid() {
  return (
    <Box sx={{ flexGrow: 1 }}>
      <Grid container spacing={2}>
        <Grid size={8}>
          <Item>size=8</Item>
        </Grid>
        <Grid size={4}>
          <Item>size=4</Item>
        </Grid>
        <Grid size={4}>
          <Item>size=4</Item>
        </Grid>
        <Grid size={8}>
          <Item>size=8</Item>
        </Grid>
      </Grid>
    </Box>
  );
}

示例依赖边界

@mui/material/styles, @mui/material/Box, @mui/material/Paper, @mui/material/Grid, @mui/material/Avatar, @mui/material/Stack

直接导入 Icons/System/Utils 的示例需要匹配 9.4.0 包。局部数据与辅助文件属于示例闭包,不是额外组件。远程图片和模拟操作不提供后端。

固定上游 API 元数据参考

以下表格保留上游元数据,不是重新推断的通用类型体系。条件泛型、继承属性与回调细节以以下源码声明为准。主题默认配置可能覆盖元数据默认值。

Grid

import Grid from '@mui/material/Grid';
import { Grid } from '@mui/material';
属性必需上游元数据类型/约束元数据默认值
children否node—
columns否Array<number> | number | object12
columnSpacing否Array<number | string> | number | object | string—
container否boolfalse
direction否'row-reverse' | 'row' | Array<'row-reverse' | 'row'> | object'row'
offset否string | number | Array<string | number> | object—
rowSpacing否Array<number | string> | number | object | string—
size否string | bool | number | Array<string | bool | number> | object—
spacing否Array<number | string> | number | object | string0
wrap否'nowrap' | 'wrap-reverse' | 'wrap''wrap'

原生源码声明

声明与 import 从固定源码提取,不平铺联合类型或外部继承。英文注释/类型属于原始源码参考,不代表上游正文已全文翻译。共享类型仍位于原生包中。

vendor/src/Grid/Grid.tsx

import PropTypes from 'prop-types';

import { createGrid } from '@mui/system/Grid';

import { SxProps } from '@mui/system';

import { OverridableComponent, OverrideProps } from '@mui/types';

import requirePropFactory from '../utils/requirePropFactory';

import { Theme, styled, Breakpoint } from '../styles';

import { useDefaultProps } from '../DefaultPropsProvider';

import useTheme from '../styles/useTheme';

type ResponsiveStyleValue<T> = T | Array<T | null> | { [key in Breakpoint]?: T | null };

export type GridDirection = 'row' | 'row-reverse';

export type GridSpacing = number | string;

export type GridWrap = 'nowrap' | 'wrap' | 'wrap-reverse';

export type GridSize = 'auto' | 'grow' | number | false;

export type GridOffset = 'auto' | number;

export interface GridBaseProps {
  /**
   * The content of the component.
   */
  children?: React.ReactNode;
  /**
   * The number of columns.
   * @default 12
   */
  columns?: ResponsiveStyleValue<number> | undefined;
  /**
   * Defines the horizontal space between the type `item` components.
   * It overrides the value of the `spacing` prop.
   */
  columnSpacing?: ResponsiveStyleValue<GridSpacing> | undefined;
  /**
   * If `true`, the component will have the flex *container* behavior.
   * You should be wrapping *items* with a *container*.
   * @default false
   */
  container?: boolean | undefined;
  /**
   * Defines the `flex-direction` style property for the container.
   *
   * ⚠️ Only `row` and `row-reverse` are supported. `column` and `column-reverse` are not supported,
   * because the Grid component is designed to subdivide layouts into **columns**, not rows.
   *
   * For vertical layouts, use `Stack` instead.
   *
   * @default 'row'
   */
  direction?: ResponsiveStyleValue<GridDirection> | undefined;
  /**
   * Defines the offset value for the type `item` components.
   */
  offset?: ResponsiveStyleValue<GridOffset> | undefined;
  /**
   * @internal
   * The level of the grid starts from `0` and increases when the grid nests
   * inside another grid. Nesting is defined as a container Grid being a direct
   * child of a container Grid.
   *
   * ```js
   * <Grid container> // level 0
   *   <Grid container> // level 1
   *     <Grid container> // level 2
   * ```
   *
   * Only consecutive grid is considered nesting. A grid container will start at
   * `0` if there are non-Grid container element above it.
   *
   * ```js
   * <Grid container> // level 0
   *   <div>
   *     <Grid container> // level 0
   * ```
   *
   * ```js
   * <Grid container> // level 0
   *   <Grid>
   *     <Grid container> // level 0
   * ```
   */
  unstable_level?: number | undefined;
  /**
   * Defines the vertical space between the type `item` components.
   * It overrides the value of the `spacing` prop.
   */
  rowSpacing?: ResponsiveStyleValue<GridSpacing> | undefined;
  /**
   * Defines the size of the the type `item` components.
   */
  size?: ResponsiveStyleValue<GridSize> | undefined;
  /**
   * Defines the space between the type `item` components.
   * It can only be used on a type `container` component.
   * @default 0
   */
  spacing?: ResponsiveStyleValue<GridSpacing> | undefined;
  /**
   * Defines the `flex-wrap` style property.
   * It's applied for all screen sizes.
   * @default 'wrap'
   */
  wrap?: GridWrap | undefined;
}

export interface GridTypeMap<P = {}, D extends React.ElementType = 'div'> {
  props: P & GridBaseProps & { sx?: SxProps<Theme> | undefined };
  defaultComponent: D;
}

export type GridProps<
  D extends React.ElementType = GridTypeMap['defaultComponent'],
  P = {
    component?: React.ElementType | undefined;
  },
> = OverrideProps<GridTypeMap<P, D>, D>;

安装后验收

执行严格类型检查、生产构建和实际交互。检查键盘标签/焦点、原生支持的禁用/错误/只读语义、深浅色、窄屏和减少动画。按真实宿主数据检查回调,不把复制提示词当作安装完成。初始化工具还需检查服务端 HTML 与无告警水合。

来源与许可

https://github.com/mui/material-ui/tree/809a7717b4c050ba3f69b75300689f07c050a16e

MIT · @mui/material@9.4.0. 保留上游原始内容与许可证。上游完整原文保留于 vendor/docs,不标为全文中文化。