Composite

Renders a widget based on the WAI-ARIA composite role, which provides a single tab stop on the page and arrow key navigation through the focusable descendants.

import { Composite } from '@wordpress/components';

View on Storybook

View source on GitHub

PropsPermalink to this section

NameDefaultDescription
activeId

string | null | undefined

The current active item id. The active item is the element within the composite widget that has either DOM or virtual focus (in case the virtualFocus prop is enabled).

  • null represents the base composite element (the one with a composite role). Users will be able to navigate out of it using arrow keys.
  • If activeId is initially set to null, the base composite element itself will have focus and users will be able to navigate to it using arrow keys.
defaultActiveId

string | null | undefined

The composite item id that should be active by default when the composite widget is rendered. If null, the composite element itself will have focus and users will be able to navigate to it using arrow keys. If undefined, the first enabled item will be focused.

setActiveId

(activeId: string | null | undefined) => void

A callback that gets called when the activeId state changes.

focusLoopfalse

boolean | Orientation | undefined

Determines how the focus behaves when the user reaches the end of the composite widget.

On one-dimensional composite widgets:

  • true loops from the last item to the first item and vice-versa.
  • horizontal loops only if orientation is horizontal or not set.
  • vertical loops only if orientation is vertical or not set.
  • If activeId is initially set to null, the composite element will be focused in between the last and first items.

On two-dimensional composite widgets (ie. when using CompositeRow):

  • true loops from the last row/column item to the first item in the same row/column and vice-versa. If it’s the last item in the last row, it moves to the first item in the first row and vice-versa.
  • horizontal loops only from the last row item to the first item in the same row.
  • vertical loops only from the last column item to the first item in the column row.
  • If activeId is initially set to null, vertical loop will have no effect as moving down from the last row or up from the first row will focus on the composite element.
  • If focusWrap matches the value of focusLoop, it’ll wrap between the last item in the last row or column and the first item in the first row or column and vice-versa.
focusWrapfalse

boolean | Orientation | undefined

Works only on two-dimensional composite widgets.

If enabled, moving to the next item from the last one in a row or column will focus on the first item in the next row or column and vice-versa.

  • true wraps between rows and columns.
  • horizontal wraps only between rows.
  • vertical wraps only between columns.
  • If focusLoop matches the value of focusWrap, it’ll wrap between the last item in the last row or column and the first item in the first row or column and vice-versa.
focusShiftfalse

boolean

Works only on two-dimensional composite widgets.

If enabled, moving up or down when there’s no next item or when the next item is disabled will shift to the item right before it.

virtualFocusfalse

boolean

If enabled, the composite element will act as an aria-activedescendant container instead of roving tabindex. DOM focus will remain on the composite element while its items receive virtual focus.

In both scenarios, the item in focus will carry the data-active-item attribute.

orientation'both'

"both" | "horizontal" | "vertical"

Defines the orientation of the composite widget. If the composite has a single row or column (one-dimensional), the orientation value determines which arrow keys can be used to move focus:

  • both: all arrow keys work.
  • horizontal: only left and right arrow keys work.
  • vertical: only up and down arrow keys work.

It doesn’t have any effect on two-dimensional composites.

rtlisRTL()

boolean

Controls how the previous and next items are determined. If rtl is set to true, they will be inverted.

This only affects the composite widget behavior. You still need to set dir="rtl" on HTML/CSS.

render

ReactElement<any, string | JSXElementConstructor<any>> | RenderProp<HTMLAttributes<any> & { ref?: Ref<any> | undefined; }> | undefined

Allows the component to be rendered as a different HTML element or React component. The value can be a React element or a function that takes in the original component props and gives back a React element with the props merged.

focusable

boolean

Makes the component a focusable element. When this element gains keyboard focus, it gets a data-focus-visible attribute and triggers the onFocusVisible prop. The component supports the disabled prop even for those elements not supporting the native disabled attribute. Disabled elements may be still accessible via keyboard by using the accessibleWhenDisabled prop. Non-native focusable elements will lose their focusability entirely. However, native focusable elements will retain their inherent focusability.

disabledfalse

boolean

Determines if the element is disabled. This sets the aria-disabled attribute accordingly, enabling support for all elements, including those that don’t support the native disabled attribute.

This feature can be combined with the accessibleWhenDisabled prop to make disabled elements still accessible via keyboard.

Note: For this prop to work, the focusable prop must be set to true, if it’s not set by default.

accessibleWhenDisabled

boolean

Indicates whether the element should be focusable even when it is disabled.

This is important when discoverability is a concern. For example:

A toolbar in an editor contains a set of special smart paste functions that are disabled when the clipboard is empty or when the function is not applicable to the current content of the clipboard. It could be helpful to keep the disabled buttons focusable if the ability to discover their functionality is primarily via their presence on the toolbar.

Learn more on Focusability of disabled controls.

onFocusVisible

BivariantCallback<(event: SyntheticEvent<HTMLElement, Event>) => void>

Custom event handler invoked when the element gains focus through keyboard interaction or a key press occurs while the element is in focus. This is the programmatic equivalent of the data-focus-visible attribute.

Note: For this prop to work, the focusable prop must be set to true if it’s not set by default.

children

ReactNode

The contents of the component.

ExamplesPermalink to this section

DefaultPermalink to this section

const Default = () => <Composite>(<>
        <Composite.Item>Item one</Composite.Item>
        <Composite.Item>Item two</Composite.Item>
        <Composite.Item>Item three</Composite.Item>
    </>)</Composite>;

GroupsPermalink to this section

const Groups = () => <Composite>(<>
        <Composite.Group>
            <Composite.GroupLabel>Group one</Composite.GroupLabel>
            <Composite.Item>Item 1.1</Composite.Item>
            <Composite.Item>Item 1.2</Composite.Item>
        </Composite.Group>
        <Composite.Group>
            <Composite.GroupLabel>Group two</Composite.GroupLabel>
            <Composite.Item>Item 2.1</Composite.Item>
            <Composite.Item>Item 2.1</Composite.Item>
        </Composite.Group>
    </>)</Composite>;

GridPermalink to this section

const Grid = () => <Composite role="grid" aria-label="Composite">(<>
        <Composite.Row role="row">
            <Composite.Item role="gridcell">Item A1</Composite.Item>
            <Composite.Item role="gridcell">Item A2</Composite.Item>
            <Composite.Item role="gridcell">Item A3</Composite.Item>
        </Composite.Row>
        <Composite.Row role="row">
            <Composite.Item role="gridcell">Item B1</Composite.Item>
            <Composite.Item role="gridcell">Item B2</Composite.Item>
            <Composite.Item role="gridcell">Item B3</Composite.Item>
        </Composite.Row>
        <Composite.Row role="row">
            <Composite.Item role="gridcell">Item C1</Composite.Item>
            <Composite.Item role="gridcell">Item C2</Composite.Item>
            <Composite.Item role="gridcell">Item C3</Composite.Item>
        </Composite.Row>
    </>)</Composite>;

HoverPermalink to this section

const Hover = () => <Composite>(<>
        <Composite.Hover render={ <Composite.Item /> }>
            Hover item one
        </Composite.Hover>
        <Composite.Hover render={ <Composite.Item /> }>
            Hover item two
        </Composite.Hover>
        <Composite.Hover render={ <Composite.Item /> }>
            Hover item three
        </Composite.Hover>
    </>)</Composite>;

TypeaheadPermalink to this section

const Typeahead = () => <Composite render={<Composite.Typeahead />}>(<>
        <Composite.Item>Apple</Composite.Item>
        <Composite.Item>Banana</Composite.Item>
        <Composite.Item>Peach</Composite.Item>
    </>)</Composite>;

With Slot FillPermalink to this section

const WithSlotFill = () => <Composite>(<>
        <Composite.Item>Item one (direct child)</Composite.Item>
        <Slot />
        <Composite.Item>Item four (direct child)</Composite.Item>
    </>)</Composite>;

With TooltipsPermalink to this section

Combining the Tooltip and Composite component has a few caveats. And while there are a few ways to compose these two components, our recommendation is to render Composite.Item as a child of Tooltip.

// 🔴 Does not work

</Tooltip>
const WithTooltips = () => <Composite>(<>
        <Tooltip text="Tooltip one">
            <Composite.Item>Item one</Composite.Item>
        </Tooltip>
        <Tooltip text="Tooltip two">
            <Composite.Item>Item two</Composite.Item>
        </Tooltip>
        <Tooltip text="Tooltip three">
            <Composite.Item>Item three</Composite.Item>
        </Tooltip>
    </>)</Composite>;