Skip to content

Commit 4b739fd

Browse files
authored
docs: add RAC GridList Section docs (#8749)
* docs: add RAC GridList Section docs * review comments
1 parent 3bc78ae commit 4b739fd

File tree

2 files changed

+112
-1
lines changed

2 files changed

+112
-1
lines changed

packages/react-aria-components/docs/GridList.mdx

Lines changed: 111 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -573,6 +573,102 @@ Items in a GridList may also be links to another page or website. This can be ac
573573

574574
The `<GridListItem>` component works with frameworks and client side routers like [Next.js](https://nextjs.org/) and [React Router](https://reactrouter.com/en/main). As with other React Aria components that support links, this works via the <TypeLink links={docs.links} type={docs.exports.RouterProvider} /> component at the root of your app. See the [client side routing guide](routing.html) to learn how to set this up.
575575

576+
## Sections <VersionBadge version="alpha" style={{marginLeft: 4, verticalAlign: 'bottom'}} />
577+
578+
GridList supports sections in order to group options. Sections can be used by wrapping groups of items in a `GridListSection` element. A `<GridListHeader>` element may also be included to label the section.
579+
580+
### Static items
581+
582+
```tsx example
583+
import {GridListSection, GridListHeader} from 'react-aria-components';
584+
585+
<GridList aria-label="Sandwich contents" selectionMode="multiple">
586+
<GridListSection>
587+
<GridListHeader>Veggies</GridListHeader>
588+
<MyItem id="lettuce">Lettuce</MyItem>
589+
<MyItem id="tomato">Tomato</MyItem>
590+
<MyItem id="onion">Onion</MyItem>
591+
</GridListSection>
592+
<GridListSection>
593+
<GridListHeader>Protein</GridListHeader>
594+
<MyItem id="ham">Ham</MyItem>
595+
<MyItem id="tuna">Tuna</MyItem>
596+
<MyItem id="tofu">Tofu</MyItem>
597+
</GridListSection>
598+
<GridListSection>
599+
<GridListHeader>Condiments</GridListHeader>
600+
<MyItem id="mayo">Mayonaise</MyItem>
601+
<MyItem id="mustard">Mustard</MyItem>
602+
<MyItem id="ranch">Ranch</MyItem>
603+
</GridListSection>
604+
</GridList>
605+
```
606+
607+
<details>
608+
<summary style={{fontWeight: 'bold'}}><ChevronRight size="S" /> Show CSS</summary>
609+
610+
```css
611+
.react-aria-GridList {
612+
.react-aria-GridListSection:not(:first-child) {
613+
margin-top: 12px;
614+
}
615+
616+
.react-aria-GridListHeader {
617+
font-size: 1.143rem;
618+
font-weight: bold;
619+
padding: 0 0.714rem;
620+
}
621+
}
622+
```
623+
624+
</details>
625+
626+
627+
### Dynamic items
628+
629+
The above example shows sections with static items. Sections can also be populated from a hierarchical data structure.
630+
Similarly to the props on GridList, `<GridListSection>` takes an array of data using the `items` prop. If the section also has a header,
631+
the <TypeLink links={docs.links} type={docs.exports.Collection} /> component can be used to render the child items.
632+
633+
```tsx example
634+
import type {Selection} from 'react-aria-components';
635+
import {Collection} from 'react-aria-components';
636+
637+
function Example() {
638+
let options = [
639+
{name: 'Australian', children: [
640+
{id: 2, name: 'Koala'},
641+
{id: 3, name: 'Kangaroo'},
642+
{id: 4, name: 'Platypus'}
643+
]},
644+
{name: 'American', children: [
645+
{id: 6, name: 'Bald Eagle'},
646+
{id: 7, name: 'Bison'},
647+
{id: 8, name: 'Skunk'}
648+
]}
649+
];
650+
let [selected, setSelected] = React.useState<Selection>(new Set());
651+
652+
return (
653+
<GridList
654+
aria-label="Pick an animal"
655+
items={options}
656+
selectedKeys={selected}
657+
selectionMode="single"
658+
onSelectionChange={setSelected}>
659+
{section => (
660+
<GridListSection id={section.name}>
661+
<GridListHeader>{section.name}</GridListHeader>
662+
<Collection items={section.children}>
663+
{item => <MyItem >{item.name}</MyItem>}
664+
</Collection>
665+
</GridListSection>
666+
)}
667+
</GridList>
668+
);
669+
}
670+
```
671+
576672
## Disabled items
577673

578674
A `GridListItem` can be disabled with the `isDisabled` prop. This will disable all interactions on the row,
@@ -1757,6 +1853,21 @@ function DndGridList(props: DndGridListProps) {
17571853

17581854
<PropTable component={docs.exports.GridList} links={docs.links} />
17591855

1856+
### GridListSection
1857+
1858+
A `<GridListSection>` defines the child items for a section within a `<GridList>`. It may also contain an optional `<GridListHeader>` element. If there is no header, then an `aria-label` must be provided to identify the section to assistive technologies.
1859+
1860+
<details>
1861+
<summary style={{fontWeight: 'bold'}}><ChevronRight size="S" /> Show props</summary>
1862+
1863+
<PropTable component={docs.exports.GridListSection} links={docs.links} />
1864+
1865+
</details>
1866+
1867+
### GridListHeader
1868+
1869+
A `<GridListHeader>` defines the title for a `<GridListSection>`. It accepts all DOM attributes.
1870+
17601871
### GridListItem
17611872

17621873
A `<GridListItem>` defines a single option within a `<GridList>`. If the `children` are not plain text, then the `textValue` prop must also be set to a plain text representation, which will be used for typeahead in the GridList.

packages/react-aria-components/src/GridList.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -622,7 +622,7 @@ export const GridListHeader = /*#__PURE__*/ createLeafComponent(HeaderNode, func
622622
let rowHeaderProps = useContext(GridListHeaderContext);
623623

624624
return (
625-
<header {...props} ref={ref}>
625+
<header className="react-aria-GridListHeader" ref={ref} {...props}>
626626
<div {...rowHeaderProps} style={{display: 'contents'}}>
627627
{props.children}
628628
</div>

0 commit comments

Comments
 (0)