Перейти к контенту

🎉 Material UI v5 is out! Head to the migration guide to get started.

Box

Box компонент используется как обертка компонента для большинства используемых CSS свойств.

The Box component packages all the style functions that are exposed in @material-ui/system. It's created using the styled() function of @material-ui/core/styles.

Пример

The palette style function.

Переопределение Material-UI компонентов

Box компонент оборачивает ваш компонент. Он создает новый DOM элемент, это <div> по умолчанию, хотя это можно изменить свойством component. Например если вы хотите использовать<span> взамен:

<Box component="span" m={1}>
  <Button />
</Box>

Это работает превосходно когда изменения могут быть изолированы в новый DOM элемент. Для сущности, вы можете изменить margin(внешний отступ) таким образом.

Тем не менее, иногда вам нужно ориентироваться на базовый элемент DOM. Например, вы хотите изменить цвет текста кнопки. Компонент Button определяет свой собственный цвет. Наследование CSS не помогает. Чтобы обойти проблему, у вас есть два варианта:

  1. Использовать React.cloneElement()

Компонент Box имеет свойство clone, которое позволяет использовать метод clone element из React.

<Box color="text.primary" clone>
  <Button />
</Box>
  1. Используйте render свойства

The Box children accepts a render props function. You can pull out the className.

<Box color="text.primary">
  {props => <Button {...props} />}
</Box>

⚠️ The CSS specificity relies on the import order. If you want the guarantee that the wrapped component's style will be overridden, you need to import the Box last.

API

import Box from '@material-ui/core/Box';
Имя Тип По-умолчанию Описание
children * union: node |
 func
Box render function or node.
clone bool false If true, the box will recycle its children DOM element. It's using React.cloneElement internally.
component union: string |
 func |
 object
'div' The component used for the root node. Either a string to use a DOM element or a component.

Any other properties supplied will be used by the style functions or spread to the root element.