Compose a component from its children: write the icon, the text and the buttons inside it, in place of icon, title or actions props. A child takes its place by its slot, and can be a function of the component's state.
Slots
When a component has several places for its content, set the slot prop on each child to choose its place, as with React Aria's slots.
import { Icon, IconButton, List, ListItem } from '@material-web/react'
import { Text } from 'react-aria-components'
export default function Slots() {
return (
<List aria-label="Contacts" variant="segmented" className="w-80">
<ListItem id="ada" textValue="Ada Lovelace">
<Icon slot="leading" name="person" />
<Text slot="label">Ada Lovelace</Text>
<Text slot="description">Mathematician</Text>
<IconButton slot="trailing" aria-label="Call Ada Lovelace">
<Icon name="call" />
</IconButton>
</ListItem>
<ListItem id="alan" textValue="Alan Turing">
<Icon slot="leading" name="person" />
<Text slot="label">Alan Turing</Text>
<Text slot="description">Computer scientist</Text>
<IconButton slot="trailing" aria-label="Call Alan Turing">
<Icon name="call" />
</IconButton>
</ListItem>
</List>
)
}These are the slots of the most used components. A component's page lists its own.
| Component | Slots |
|---|---|
AppBar | leading, headline, subtitle, search, trailing |
ListItem | leading, overline, label, description, trailing, selection, drag |
MenuItem | leading, label, description, trailing |
Chip | leading, label, and trailing or remove by its variant |
Fab, NavigationBarItem, NavigationRailItem | label |
NavigationRailSection | header |
SearchField | leading, trailing |
Dialog, BottomSheet | title, description, close |
SideSheet | title, close |
SnackbarContent | title, description |
CheckboxGroup, RadioGroup | description |
Calendar, RangeCalendar | previous, next, errorMessage |
- Children: Use
Text,Heading,ButtonandKeyboardfromreact-aria-components, andIcon,IconButtonandCheckboxfrom@material-web/react, as the children. See Imports. - Order: A child is placed by its slot, wherever it is written. Children in the same slot keep the order they are written in.
- Unknown slots: A child with a slot that the component does not have throws an
Invalid sloterror, which names the slots it does have. - Required slots: Where a component gives a child no place without a slot, the child throws an error too. A
Textin aFabmust haveslot="label". - Plain elements: A plain element, such as an
<img>or an avatar, is also placed by itsslotattribute, but its slot name is not checked. - No slots: A component with a single place for its content takes plain children with no
slot, such as the label and icon of aButtonor aTab.
Icons
An icon is a child of Button, IconButton, Fab and SplitButtonLeading, and a child in the leading or trailing slot of a Chip. Any icon works: the Icon component, an icon from an SVG library, or your own component.
// The default: Material Symbols by ligature name
<Button><Icon name="add" />Create</Button>
// An SVG icon library, or your own component, sized to fill the slot
<Button><Plus className="size-5" />Create</Button>A component sizes the Icon it holds. Give any other icon the same size, such as size-5 for the 20dp icon of a default Button.
Children as a function
Pass a function as children to render content from the component's state, as with React Aria's render props. The function receives the state as one object and returns the children.
import { Icon, IconButton } from '@material-web/react'
import { useState } from 'react'
export default function RenderProps() {
const [isFavorite, setFavorite] = useState(false)
return (
<IconButton aria-label="Favorite" isSelectable isSelected={isFavorite} onChange={setFavorite}>
{({ isSelected, isHovered }) => (
<Icon name="favorite" className={isSelected || isHovered ? 'icon-filled' : undefined} />
)}
</IconButton>
)
}| Component | State |
|---|---|
Button, IconButton | isHovered, isPressed, isFocused, isFocusVisible, isDisabled, isPending. A toggle also has isSelected and state. |
Fab, FabMenuButton, SplitButtonLeading | isHovered, isPressed, isFocused, isFocusVisible, isDisabled, isPending |
SnackbarAction, SnackbarCloseButton | the same |
ListItem | isHovered, isPressed, isFocused, isFocusVisible, isFocusVisibleWithin, isSelected, isDisabled, selectionMode, selectionBehavior, allowsDragging, isDragging, isDropTarget, id, state |
MenuItem, FabMenuItem | isHovered, isPressed, isFocused, isFocusVisible, isSelected, isDisabled, hasSubmenu, isOpen, selectionMode, selectionBehavior, allowsDragging, isDragging, isDropTarget |
Chip | isHovered, isPressed, isFocused, isFocusVisible, isSelected, isDisabled, allowsRemoving, selectionMode, selectionBehavior |
Tab | isHovered, isPressed, isFocused, isFocusVisible, isSelected, isDisabled |
NavigationBarItem, NavigationRailItem | isHovered, isPressed, isFocused, isFocusVisible, isSelected, isDisabled, state |
Checkbox | isSelected, isIndeterminate, isHovered, isPressed, isFocused, isFocusVisible, isDisabled, isReadOnly, isInvalid, isRequired |
Radio | isSelected, isHovered, isPressed, isFocused, isFocusVisible, isDisabled, isReadOnly, isInvalid, isRequired |
Switch | isSelected, isHovered, isPressed, isFocused, isFocusVisible, isDisabled, isReadOnly, isInvalid, isRequired, state |
CheckboxGroup | isDisabled, isReadOnly, isRequired, isInvalid, state |
RadioGroup | isDisabled, isReadOnly, isRequired, isInvalid, orientation, state |
TextField, its leading and trailing | isHovered, isFocusWithin, isFocusVisible, isDisabled, isInvalid |
NavigationRailSection | the same |
SearchField | isEmpty, isDisabled, isInvalid, isReadOnly, isRequired, state |
Slider, ButtonGroup | orientation, isDisabled, state |
Tabs, Toolbar, NavigationBar, NavigationRail | orientation |
TabPanel | isFocused, isFocusVisible, isInert, isEntering, isExiting, state |
Dialog, SearchView, DatePickerDialog, TimePickerDialog | isEntering, isExiting, state |
BottomSheet, SideSheet | isEntering, isExiting, state, position, swipeDirection, stackIndex, hasDescendants, isExpanded |
MenuPopover, FabMenuPopover, TooltipPopover | isEntering, isExiting, placement, trigger |
Tooltip | isEntering, isExiting, placement, state |
Snackbar | toast, isFocused, isFocusVisible |
LinearProgressIndicator, CircularProgressIndicator, LoadingIndicator | percentage, valueText, isIndeterminate |
Calendar, RangeCalendar | isDisabled, isInvalid, state |
CalendarCell | date, formattedDate, isSelected, isSelectionStart, isSelectionEnd, isHovered, isPressed, isFocused, isFocusVisible, isDisabled, isUnavailable, isInvalid, isToday, isOutsideMonth, isOutsideVisibleRange |
DatePicker, DateRangePicker | displayMode |
TimePicker | displayMode, selection |
- Focus:
isFocusedis any focus, andisFocusVisibleis keyboard focus only. On aMenuItemor aFabMenuItem,isFocusedis also true while the pointer is on the item. - Toggles: A
Buttonor anIconButtonreportsisSelectedonly when it is a toggle, withisSelectable. - Text fields: On a
TextField, pass the function asleadingortrailinginstead of children.isFocusWithinis true while the input has the focus. - One child: The function must be the only child. To return several children, use a fragment or an array with keys.
- Slots: The children a function returns take slots as written children do.
- Types: The type of the state is exported under the component's name for most components, such as
ListItemRenderPropsandMenuItemRenderProps. - Classes:
classNameandstyletake a function of the state too. See Styling.
Collections
Write the items of a List, a Menu, a FabMenu, a ChipGroup or a TabList as children, or pass items and a function that renders each one, as with React Aria's collections.
import { List, ListItem } from '@material-web/react'
import { useState } from 'react'
import type { Selection } from 'react-aria-components'
const files = [
{ id: 'report', name: 'Annual report' },
{ id: 'budget', name: 'Budget' },
{ id: 'notes', name: 'Meeting notes' },
]
export default function Collections() {
const [selected, setSelected] = useState<Selection>(new Set(['budget']))
return (
<List
aria-label="Files"
variant="segmented"
className="w-80"
items={files}
selectionMode="multiple"
selectedKeys={selected}
onSelectionChange={setSelected}
>
{(file) => <ListItem>{file.name}</ListItem>}
</List>
)
}- Ids: Give each item an
id, which selection and actions refer to. Withitems, an item takes theidof its object unless you set one, as in<ListItem id={file.path}>. - Text values: Give an item a
textValuewhen its children are not plain text, so that typing can find it. - Actions: Set
onActionon aList, aMenu, aFabMenuor aChipGroupto handle a press on an item. It is called with the item'sid, as inonAction={(id) => open(id)}. - Selection: Set
selectionModeon aList, aMenuor aChipGroup, and read the selectedids withselectedKeysandonSelectionChange. See React Aria's Selection guide. - Other values: With
items, a rendered item is reused until its object changes. Passdependencieswith the other values the function reads.
Triggers
To open an overlay from an element, wrap the two in a trigger, with the element first.
import {
Button,
Dialog,
DialogTrigger,
Icon,
IconButton,
Menu,
MenuItem,
MenuPopover,
} from '@material-web/react'
import { Heading, MenuTrigger } from 'react-aria-components'
export default function Triggers() {
return (
<>
<MenuTrigger>
<IconButton aria-label="More options">
<Icon name="more_vert" />
</IconButton>
<MenuPopover>
<Menu>
<MenuItem id="rename">Rename</MenuItem>
<MenuItem id="duplicate">Duplicate</MenuItem>
<MenuItem id="remove">Remove</MenuItem>
</Menu>
</MenuPopover>
</MenuTrigger>
<DialogTrigger>
<Button variant="tonal">Remove</Button>
<Dialog>
<Heading
slot="title"
className="m-0 px-space-300 pb-space-200 text-headline-sm text-on-surface"
>
Remove this file?
</Heading>
<div className="flex shrink-0 justify-end gap-space-100 px-space-300 pt-space-200">
<Button variant="text" slot="close">
Cancel
</Button>
<Button variant="text" slot="close">
Remove
</Button>
</div>
</Dialog>
</DialogTrigger>
</>
)
}| Trigger | Import from | Opens |
|---|---|---|
MenuTrigger | react-aria-components | A MenuPopover, or a FabMenuPopover from a FabMenuButton |
SubmenuTrigger | react-aria-components | A MenuPopover from a MenuItem |
DialogTrigger | @material-web/react | A Dialog, a DatePickerDialog, a TimePickerDialog, a SearchView, or a TooltipPopover on a press |
SheetTrigger | @material-web/react | A modal BottomSheet or SideSheet |
TooltipTrigger | @material-web/react | A Tooltip |
PreviewTrigger | @material-web/react | A TooltipPopover on hover or focus |
- Element: The element of a trigger is a
Button, anIconButtonor any other React Aria button. ATooltipTriggertakes any focusable React Aria component. - Open state: A trigger keeps the open state itself. To control it, set
isOpenandonOpenChangeon the trigger. SetdefaultOpento start with the overlay open. - No trigger: To open an overlay from anywhere, control it with
isOpenandonOpenChangeon the overlay itself. - Closing: A button with
slot="close"inside a dialog or a sheet closes it.