|
| 1 | +# ListToolbar control |
| 2 | + |
| 3 | +This control renders a flexible toolbar for building list and data grid command bars. It is built with Fluent UI 9 components and supports item grouping, left/right aligned items, dividers, tooltips, automatic overflow menu, responsive design, loading states, and custom rendering. |
| 4 | + |
| 5 | +Here is an example of the control in action: |
| 6 | + |
| 7 | + |
| 8 | + |
| 9 | + |
| 10 | + |
| 11 | + |
| 12 | +## How to use this control in your solutions |
| 13 | + |
| 14 | +- Check that you installed the `@pnp/spfx-controls-react` dependency. Check out the [getting started](../../#getting-started) page for more information about installing the dependency. |
| 15 | +- Import the following modules to your component: |
| 16 | + |
| 17 | +```TypeScript |
| 18 | +import { ListToolbar } from '@pnp/spfx-controls-react/lib/controls/ListToolbar'; |
| 19 | +import { IToolbarItem } from '@pnp/spfx-controls-react/lib/controls/ListToolbar'; |
| 20 | +``` |
| 21 | + |
| 22 | +- Use the `ListToolbar` control in your code as follows: |
| 23 | + |
| 24 | +```TypeScript |
| 25 | +<ListToolbar |
| 26 | + items={[ |
| 27 | + { key: 'new', label: 'New', icon: <AddRegular />, onClick: () => console.log('New') }, |
| 28 | + { key: 'edit', label: 'Edit', icon: <EditRegular />, onClick: () => console.log('Edit') }, |
| 29 | + { key: 'delete', label: 'Delete', icon: <DeleteRegular />, onClick: () => console.log('Delete') }, |
| 30 | + ]} |
| 31 | + context={this.props.context} |
| 32 | + ariaLabel="Document toolbar" |
| 33 | +/> |
| 34 | +``` |
| 35 | + |
| 36 | +- With the `farItems` property you can add items that are aligned to the right side of the toolbar: |
| 37 | + |
| 38 | +```TypeScript |
| 39 | +<ListToolbar |
| 40 | + items={[ |
| 41 | + { key: 'new', label: 'New', icon: <AddRegular />, onClick: () => {} }, |
| 42 | + ]} |
| 43 | + farItems={[ |
| 44 | + { key: 'filter', label: 'Filter', icon: <FilterRegular />, onClick: () => {} }, |
| 45 | + { key: 'settings', icon: <SettingsRegular />, tooltip: 'Settings', onClick: () => {} }, |
| 46 | + ]} |
| 47 | + context={this.props.context} |
| 48 | +/> |
| 49 | +``` |
| 50 | + |
| 51 | +- Use the `group` property on items to visually group them with dividers: |
| 52 | + |
| 53 | +```TypeScript |
| 54 | +<ListToolbar |
| 55 | + items={[ |
| 56 | + { key: 'new', label: 'New', icon: <AddRegular />, group: 'file', onClick: () => {} }, |
| 57 | + { key: 'edit', label: 'Edit', icon: <EditRegular />, group: 'file', onClick: () => {} }, |
| 58 | + { key: 'copy', label: 'Copy', icon: <CopyRegular />, group: 'clipboard', onClick: () => {} }, |
| 59 | + { key: 'paste', label: 'Paste', icon: <ClipboardPasteRegular />, group: 'clipboard', onClick: () => {} }, |
| 60 | + ]} |
| 61 | + showGroupDividers={true} |
| 62 | + context={this.props.context} |
| 63 | +/> |
| 64 | +``` |
| 65 | + |
| 66 | +- Use the `totalCount` property to display a count badge in the toolbar: |
| 67 | + |
| 68 | +```TypeScript |
| 69 | +<ListToolbar |
| 70 | + items={items} |
| 71 | + totalCount={42} |
| 72 | + context={this.props.context} |
| 73 | +/> |
| 74 | +``` |
| 75 | + |
| 76 | +- Use the `isLoading` property to disable all toolbar items during a loading state: |
| 77 | + |
| 78 | +```TypeScript |
| 79 | +<ListToolbar |
| 80 | + items={items} |
| 81 | + isLoading={true} |
| 82 | + context={this.props.context} |
| 83 | +/> |
| 84 | +``` |
| 85 | + |
| 86 | +- Use the `onRender` property on an item for complete custom rendering: |
| 87 | + |
| 88 | +```TypeScript |
| 89 | +const farItems: IToolbarItem[] = [ |
| 90 | + { |
| 91 | + key: 'search', |
| 92 | + onRender: () => ( |
| 93 | + <SearchBox placeholder="Search..." onChange={(e, data) => onSearch(data.value)} /> |
| 94 | + ), |
| 95 | + }, |
| 96 | +]; |
| 97 | +
|
| 98 | +<ListToolbar items={items} farItems={farItems} context={this.props.context} /> |
| 99 | +``` |
| 100 | + |
| 101 | +### Overflow & responsive behavior |
| 102 | + |
| 103 | +When the toolbar is too narrow to show all left-side items, they automatically collapse into a **"..."** overflow menu. The overflow menu appears right next to the last visible item and lists every hidden action. |
| 104 | + |
| 105 | +On small screens (≤ 768 px), far-item **labels are hidden** and only their icons are shown. The total-count badge is also hidden at that breakpoint. |
| 106 | + |
| 107 | +## Implementation |
| 108 | + |
| 109 | +The `ListToolbar` control can be configured with the following properties: |
| 110 | + |
| 111 | +| Property | Type | Required | Description | Default | |
| 112 | +| ---- | ---- | ---- | ---- | ---- | |
| 113 | +| items | IToolbarItem[] | yes | Array of toolbar items to display on the left side. | | |
| 114 | +| farItems | IToolbarItem[] | no | Items that appear on the right side of the toolbar. | `[]` | |
| 115 | +| isLoading | boolean | no | When `true`, all items are disabled. | `false` | |
| 116 | +| ariaLabel | string | no | Accessibility label for the toolbar. | `'Toolbar'` | |
| 117 | +| totalCount | number | no | Displays a count badge in the toolbar. | | |
| 118 | +| className | string | no | Additional CSS class name to apply to the toolbar. | | |
| 119 | +| showGroupDividers | boolean | no | Whether to show dividers between item groups. | `true` | |
| 120 | +| theme | Theme | no | Fluent UI v8 theme (auto-converted to v9 via `createV9Theme`). | | |
| 121 | +| context | BaseComponentContext | no | SPFx component context. Enables automatic Teams theme detection (dark, high-contrast). | | |
| 122 | + |
| 123 | +### IToolbarItem |
| 124 | + |
| 125 | +| Property | Type | Required | Description | Default | |
| 126 | +| ---- | ---- | ---- | ---- | ---- | |
| 127 | +| key | string | yes | Unique identifier for the item. | | |
| 128 | +| label | string | no | Button text label. | | |
| 129 | +| tooltip | string | no | Tooltip content shown on hover. | | |
| 130 | +| icon | ReactElement | no | Icon element to display. | | |
| 131 | +| onClick | () => void | no | Click handler for the item. | | |
| 132 | +| disabled | boolean | no | Whether the item is disabled. | `false` | |
| 133 | +| visible | boolean | no | Whether to show or hide the item. | `true` | |
| 134 | +| group | string | no | Group name — items with the same group are grouped together with dividers between groups. | `'default'` | |
| 135 | +| isFarItem | boolean | no | Place the item on the right side of the toolbar. | `false` | |
| 136 | +| appearance | ToolbarButtonProps['appearance'] | no | Button appearance style. | | |
| 137 | +| onRender | () => ReactElement | no | Custom render function for complete control over item rendering. | | |
| 138 | +| dividerAfter | boolean | no | Add a divider after this item. | `false` | |
| 139 | +| dividerBefore | boolean | no | Add a divider before this item. | `false` | |
| 140 | +| ariaLabel | string | no | Accessibility label override. | | |
| 141 | + |
| 142 | + |
0 commit comments