> ## Documentation Index
> Fetch the complete documentation index at: https://anvil.servicetitan.com/llms.txt
> Use this file to discover all available pages before exploring further.

# Multi Select Menu - Web

> MultiSelectMenu components provide a searchable dropdown menu for selecting multiple options, attachable to any trigger element, with support for async data loading and client-side filtering.

> [!NOTE]
>
> For development, import `MultiSelectMenu` from
> `@servicetitan/anvil2`. For design, find it in the
> [Anvil2 file](https://www.figma.com/design/xaNQsvRKczoxu7pj0M6QT8/Anvil2-v3?node-id=2263-3578).

> [!WARNING]
>
> Importing `MultiSelectMenu` from `@servicetitan/anvil2/beta` is
> deprecated. Import from `@servicetitan/anvil2` instead. The beta export will
> be removed in Anvil2 4.0.

> [tab] Implementation
>
> ## Overview
>
> The multi-select menu family includes two components for different use cases:
>
> * **`MultiSelectMenu`** — For async data loading with support for pagination (page-based, offset-based, or group-based lazy loading)
>   * Includes automatic debouncing of the search input (configurable via the `debounceMs` prop)
>   * Includes automatic caching of the search input (configurable via the `cache` prop)
> * **`MultiSelectMenuSync`** — For client-side filtering of static option arrays
>
> Both components attach a searchable dropdown to any trigger element via a `trigger` render prop. The menu stays open after selecting an option, allowing multiple selections. Use `MultiSelectMenu` when you need multi-selection behavior outside of a form field context — for example, attaching a dropdown to a button, icon, or custom element.
>
> > [!NOTE]
> >
> > Looking for a form field with a built-in label, error state, and chip display? Use [`MultiSelectField`](https://anvil.servicetitan.com/docs/components/multi-select-field/web) instead.
>
> ## MultiSelectMenuSync (Static Options)
>
> Use `MultiSelectMenuSync` when you have a static list of options that can be filtered client-side.
>
> ```tsx theme={null}
> import { useState } from "react";
> import { MultiSelectMenuSync } from "@servicetitan/anvil2";
> import { Button } from "@servicetitan/anvil2";
>
> const options = [
>   { id: 1, label: "Option One" },
>   { id: 2, label: "Option Two" },
>   { id: 3, label: "Option Three" },
> ];
>
> const ExampleComponent = () => {
>   const [selectedOptions, setSelectedOptions] = useState([]);
>
>   return (
>     <MultiSelectMenuSync
>       label="Options"
>       options={options}
>       value={selectedOptions}
>       onSelectedOptionsChange={setSelectedOptions}
>       trigger={(props) => (
>         <Button {...props}>
>           {selectedOptions.length > 0
>             ? `${selectedOptions.length} selected`
>             : "Select options"}
>         </Button>
>       )}
>     />
>   );
> };
> ```
>
> ### Filtering and Sorting
>
> By default, `MultiSelectMenuSync` uses [match-sorter](https://github.com/kentcdodds/match-sorter) to filter options by their `label` and `searchText` fields. Results are also ranked by match quality, so the best matches appear first. Before any search is performed, options appear in the order they are supplied.
>
> multiselectmenu-match-sorter
>
> [StackBlitz demo](https://stackblitz.com/github/servicetitan/anvil2-docs-live-code/tree/main/examples/multiselectmenu-match-sorter?file=src/App.tsx)
>
> ```tsx lines expandable theme={null}
> import { useState } from "react";
> import {
>   Button,
>   MultiSelectMenuSync,
>   type MultiSelectMenuOption,
> } from "@servicetitan/anvil2";
>
> const allBooks: MultiSelectMenuOption[] = [
>   { id: 1, label: "The Martian by Andy Weir" },
>   { id: 2, label: "Gone Girl by Gillian Flynn" },
>   { id: 3, label: "Mistborn: The Final Empire by Brandon Sanderson" },
>   { id: 4, label: "Murder on the Orient Express by Agatha Christie" },
>   { id: 5, label: "Dune by Frank Herbert" },
>   { id: 6, label: "Beloved by Toni Morrison" },
>   { id: 7, label: "Atomic Habits by James Clear" },
>   { id: 8, label: "Sapiens by Yuval Noah Harari" },
>   { id: 9, label: "The Name of the Wind by Patrick Rothfuss" },
>   { id: 10, label: "Project Hail Mary by Andy Weir" },
> ];
>
> function App() {
>   const [value, setValue] = useState<MultiSelectMenuOption[]>([]);
>
>   return (
>     <div style={{ minWidth: "384px" }}>
>       <MultiSelectMenuSync
>         label="Favorite books"
>         options={allBooks}
>         value={value}
>         onSelectedOptionsChange={setValue}
>         trigger={(props) => (
>           <Button {...props}>
>             {value.length > 0
>               ? `${value.length} selected`
>               : "Try typing to filter..."}
>           </Button>
>         )}
>       />
>     </div>
>   );
> }
>
> export default App;
> ```
>
> You can customize this behavior in two ways:
>
> #### Using match-sorter options
>
> Pass a [match-sorter options object](https://github.com/kentcdodds/match-sorter#options) to customize the default filtering and sorting behavior (e.g., change which keys are matched or adjust ranking):
>
> ```tsx theme={null}
> <MultiSelectMenuSync
>   options={options}
>   filter={{
>     keys: ["label", "searchText"],
>     // ...other match-sorter options
>   }}
>   // ...other props
> />
> ```
>
> #### Using a custom filter function
>
> Pass a function for full control over both filtering and sort order. The returned array determines the exact order options appear in the dropdown:
>
> ```tsx theme={null}
> <MultiSelectMenuSync
>   options={options}
>   filter={(options, searchValue) => {
>     return options.filter((option) =>
>       option.label?.toLowerCase().includes(searchValue.toLowerCase())
>     );
>   }}
>   // ...other props
> />
> ```
>
> ## MultiSelectMenu (Async Loading)
>
> Use `MultiSelectMenu` when options need to be fetched from an API or when dealing with large datasets that require server-side filtering.
>
> ### Basic Usage
>
> ```tsx theme={null}
> import { useState } from "react";
> import { MultiSelectMenu } from "@servicetitan/anvil2";
> import { Button } from "@servicetitan/anvil2";
>
> const ExampleComponent = () => {
>   const [selectedOptions, setSelectedOptions] = useState([]);
>
>   return (
>     <MultiSelectMenu
>       label="Tags"
>       searchPlaceholder="Search tags..."
>       loadOptions={async (searchValue) => {
>         const response = await fetch(`/api/tags?q=${searchValue}`);
>         const tags = await response.json();
>         return tags.map((tag) => ({
>           id: tag.id,
>           label: tag.name,
>         }));
>       }}
>       value={selectedOptions}
>       onSelectedOptionsChange={setSelectedOptions}
>       trigger={(props) => (
>         <Button {...props}>
>           {selectedOptions.length > 0
>             ? `${selectedOptions.length} tags selected`
>             : "Select tags"}
>         </Button>
>       )}
>     />
>   );
> };
> ```
>
> ### Lazy Loading Modes
>
> `MultiSelectMenu` supports three lazy loading modes for paginated data:
>
> #### Page-based Pagination
>
> ```tsx theme={null}
> <MultiSelectMenu
>   lazy="page"
>   lazyOptions={{ pageSize: 10 }}
>   loadOptions={async (searchValue, pageNumber, pageSize) => {
>     const response = await fetch(
>       `/api/items?q=${searchValue}&page=${pageNumber}&size=${pageSize}`
>     );
>     const { data, totalCount } = await response.json();
>     return {
>       options: data.map((item) => ({ id: item.id, label: item.name })),
>       hasMore: pageNumber * pageSize + data.length < totalCount,
>     };
>   }}
>   // ...other props
> />
> ```
>
> #### Offset-based Pagination
>
> ```tsx theme={null}
> <MultiSelectMenu
>   lazy="offset"
>   lazyOptions={{ limit: 15 }}
>   loadOptions={async (searchValue, offset, limit) => {
>     const response = await fetch(
>       `/api/items?q=${searchValue}&offset=${offset}&limit=${limit}`
>     );
>     const { data, totalCount } = await response.json();
>     return {
>       options: data.map((item) => ({ id: item.id, label: item.name })),
>       hasMore: offset + limit < totalCount,
>     };
>   }}
>   // ...other props
> />
> ```
>
> #### Group-based Loading
>
> For loading grouped options incrementally:
>
> ```tsx theme={null}
> <MultiSelectMenu
>   lazy="group"
>   groupToString={(groupValue) => String(groupValue)}
>   loadOptions={async (searchValue, previousGroupKey) => {
>     const response = await fetch(
>       `/api/items?q=${searchValue}&afterGroup=${previousGroupKey || ""}`
>     );
>     const { data, hasMore } = await response.json();
>     return {
>       options: data.map((item) => ({
>         id: item.id,
>         label: item.name,
>         group: item.category,
>       })),
>       hasMore,
>     };
>   }}
>   // ...other props
> />
> ```
>
> ## Select All
>
> Enable bulk selection with the `selectAll` prop.
>
> > [!NOTE]
> >
> > Select All is shown only when the search input is empty.
>
> ### MultiSelectMenu
>
> With `MultiSelectMenu`, the parent component is responsible for handling the select/deselect logic via `onClick` and managing the `checkState`:
>
> ```tsx theme={null}
> import { useState } from "react";
> import { MultiSelectMenu } from "@servicetitan/anvil2";
> import { Button } from "@servicetitan/anvil2";
>
> const allOptions = [
>   { id: 1, label: "Option One" },
>   { id: 2, label: "Option Two" },
>   { id: 3, label: "Option Three" },
> ];
>
> const ExampleComponent = () => {
>   const [selectedOptions, setSelectedOptions] = useState([]);
>
>   const handleSelectAll = () => {
>     if (selectedOptions.length === allOptions.length) {
>       setSelectedOptions([]);
>     } else {
>       setSelectedOptions(allOptions);
>     }
>   };
>
>   return (
>     <MultiSelectMenu
>       label="Items"
>       loadOptions={async () => allOptions}
>       value={selectedOptions}
>       onSelectedOptionsChange={setSelectedOptions}
>       selectAll={{
>         label: "Select All Items",
>         onClick: handleSelectAll,
>         checkState:
>           selectedOptions.length === allOptions.length
>             ? "checked"
>             : selectedOptions.length > 0
>               ? "indeterminate"
>               : "unchecked",
>       }}
>       trigger={(props) => (
>         <Button {...props}>
>           {selectedOptions.length > 0
>             ? `${selectedOptions.length} selected`
>             : "Select items"}
>         </Button>
>       )}
>     />
>   );
> };
> ```
>
> ### MultiSelectMenuSync
>
> `MultiSelectMenuSync` provides a simplified `selectAll` prop. Click handling and check state are managed automatically:
>
> ```tsx theme={null}
> // Enable with default label
> <MultiSelectMenuSync
>   label="Tags"
>   options={options}
>   value={selectedOptions}
>   onSelectedOptionsChange={setSelectedOptions}
>   selectAll
>   trigger={(props) => <Button {...props}>Select tags</Button>}
> />
>
> // Enable with custom label
> <MultiSelectMenuSync
>   label="Tags"
>   options={options}
>   value={selectedOptions}
>   onSelectedOptionsChange={setSelectedOptions}
>   selectAll={{ label: "Select All Tags" }}
>   trigger={(props) => <Button {...props}>Select tags</Button>}
> />
>
> // Enable with dynamic label based on check state
> <MultiSelectMenuSync
>   label="Tags"
>   options={options}
>   value={selectedOptions}
>   onSelectedOptionsChange={setSelectedOptions}
>   selectAll={{ label: (checked) => checked ? "Deselect All" : "Select All" }}
>   trigger={(props) => <Button {...props}>Select tags</Button>}
> />
> ```
>
> ### Integrating with Confirmation
>
> When [confirmation](#confirmation) is on, `selectAll` configured with plain `onClick` bypasses the draft: the consumer's callback commits immediately, overwriting any pending option toggles. To make Select All participate in the draft instead, provide `compute` rather than `onClick`. The menu invokes `compute(currentDraft)` and writes the returned array into the draft; the bulk action then commits with the rest of the selection on Apply.
>
> Pass a function for `checkState` to derive the checkbox state from the live selection. The menu calls it with the current draft (or the committed `value` when confirmation is off), so Select All reflects the draft as the user toggles options — without recomputing the value yourself:
>
> ```tsx theme={null}
> <MultiSelectMenu
>   label="Items"
>   confirmation
>   loadOptions={loadOptions}
>   value={selectedOptions}
>   onSelectedOptionsChange={setSelectedOptions}
>   selectAll={{
>     // compute receives the current draft and returns the next draft.
>     compute: (current) => {
>       const allIds = new Set(allOptions.map((o) => o.id));
>       const currentIds = new Set(current.map((v) => v.id));
>       const allSelected = allOptions.every((o) => currentIds.has(o.id));
>       return allSelected
>         ? current.filter((v) => !allIds.has(v.id))
>         : [...current, ...allOptions.filter((o) => !currentIds.has(o.id))];
>     },
>     // checkState receives the current draft and returns the checkbox state.
>     checkState: (current) =>
>       current.length === 0
>         ? "unchecked"
>         : current.length >= allOptions.length
>           ? "checked"
>           : "indeterminate",
>   }}
> />
> ```
>
> `compute` may also return a `Promise<MultiSelectMenuOption[]>`. While the promise is pending the menu enters a loading state.
>
> `MultiSelectMenuSync` always uses `compute` internally, so its `selectAll` works correctly under confirmation with no changes.
>
> ## Select Filtered
>
> Enable selection of options matching the current search term with the `selectFiltered` prop. Select All and Select Filtered are mutually exclusive: Select All is shown when the search input is empty, and Select Filtered is shown when a search term is active.
>
> ### MultiSelectMenu
>
> With `MultiSelectMenu`, provide a function that receives the current `searchValue` and returns a config object with `onClick`, `checkState`, and an optional `label`:
>
> ```tsx theme={null}
> import { useState } from "react";
> import { MultiSelectMenu } from "@servicetitan/anvil2";
> import { Button } from "@servicetitan/anvil2";
>
> const allOptions = [
>   { id: 1, label: "Apple" },
>   { id: 2, label: "Apricot" },
>   { id: 3, label: "Banana" },
>   { id: 4, label: "Cherry" },
> ];
>
> const filterBySearch = (searchValue: string) =>
>   allOptions.filter((opt) =>
>     opt.label.toLowerCase().includes(searchValue.toLowerCase()),
>   );
>
> const ExampleComponent = () => {
>   const [selectedOptions, setSelectedOptions] = useState([]);
>   const selectedIds = new Set(selectedOptions.map((o) => o.id));
>
>   return (
>     <MultiSelectMenu
>       label="Fruits"
>       loadOptions={async (searchValue) => filterBySearch(searchValue)}
>       value={selectedOptions}
>       onSelectedOptionsChange={setSelectedOptions}
>       selectFiltered={(searchValue) => {
>         const filtered = filterBySearch(searchValue);
>         const allFilteredSelected =
>           filtered.length > 0 &&
>           filtered.every((o) => selectedIds.has(o.id));
>         const someFilteredSelected = filtered.some((o) =>
>           selectedIds.has(o.id),
>         );
>         return {
>           onClick: () => {
>             if (allFilteredSelected) {
>               const filteredIds = new Set(filtered.map((o) => o.id));
>               setSelectedOptions(
>                 selectedOptions.filter((o) => !filteredIds.has(o.id)),
>               );
>             } else {
>               const merged = [...selectedOptions];
>               for (const opt of filtered) {
>                 if (!selectedIds.has(opt.id)) merged.push(opt);
>               }
>               setSelectedOptions(merged);
>             }
>           },
>           checkState: allFilteredSelected
>             ? "checked"
>             : someFilteredSelected
>               ? "indeterminate"
>               : "unchecked",
>         };
>       }}
>       trigger={(props) => (
>         <Button {...props}>
>           {selectedOptions.length > 0
>             ? `${selectedOptions.length} selected`
>             : "Select fruits"}
>         </Button>
>       )}
>     />
>   );
> };
> ```
>
> ### MultiSelectMenuSync
>
> `MultiSelectMenuSync` provides a simplified `selectFiltered` prop. Click handling and check state are managed automatically based on the filtered options and current selection:
>
> ```tsx theme={null}
> // Enable with default dynamic label
> <MultiSelectMenuSync
>   label="Fruits"
>   options={options}
>   value={selectedOptions}
>   onSelectedOptionsChange={setSelectedOptions}
>   selectFiltered
>   trigger={(props) => <Button {...props}>Select fruits</Button>}
> />
>
> // Enable with custom label based on search value
> <MultiSelectMenuSync
>   label="Fruits"
>   options={options}
>   value={selectedOptions}
>   onSelectedOptionsChange={setSelectedOptions}
>   selectFiltered={(searchValue) => ({
>     label: `Select items matching "${searchValue}"`,
>   })}
>   trigger={(props) => <Button {...props}>Select fruits</Button>}
> />
>
> // Combine with selectAll for full bulk selection
> <MultiSelectMenuSync
>   label="Fruits"
>   options={options}
>   value={selectedOptions}
>   onSelectedOptionsChange={setSelectedOptions}
>   selectAll
>   selectFiltered
>   trigger={(props) => <Button {...props}>Select fruits</Button>}
> />
> ```
>
> ## Display Modes
>
> Control how the options menu is displayed using the `displayMenuAs` prop:
>
> ```tsx theme={null}
> // Automatically choose based on device (default)
> <MultiSelectMenu displayMenuAs="auto" {...props} />
>
> // Always show as popover (not recommended for mobile)
> <MultiSelectMenu displayMenuAs="popover" {...props} />
>
> // Always show as dialog
> <MultiSelectMenu displayMenuAs="dialog" {...props} />
> ```
>
> ## Popover Width
>
> Control the width of the popover using the `width` prop:
>
> ```tsx theme={null}
> // Fixed pixel width
> <MultiSelectMenu width={300} {...props} />
>
> // CSS string value
> <MultiSelectMenu width="20rem" {...props} />
> ```
>
> ## Caching
>
> `MultiSelectMenu` caches `loadOptions` results by default. Configure caching behavior:
>
> ```tsx theme={null}
> // Disable caching
> <MultiSelectMenu cache={{ enabled: false }} {...props} />
>
> // Configure max cache size (default: 15)
> <MultiSelectMenu cache={{ maxSize: 100 }} {...props} />
> ```
>
> ### Clearing the Cache
>
> Use a ref to imperatively clear the cache:
>
> ```tsx theme={null}
> import { useRef } from "react";
> import { MultiSelectMenu } from "@servicetitan/anvil2";
>
> const ExampleComponent = () => {
>   const multiSelectMenuRef = useRef(null);
>
>   const handleRefresh = () => {
>     multiSelectMenuRef.current?.clearCache();
>   };
>
>   return (
>     <>
>       <MultiSelectMenu ref={multiSelectMenuRef} {...props} />
>       <button onClick={handleRefresh}>Refresh Options</button>
>     </>
>   );
> };
> ```
>
> ### Invalidating Options
>
> Call `invalidate()` to clear the cache and reload options from the data source. Use this when the underlying data has changed and the component needs to reflect the update:
>
> ```tsx theme={null}
> import { useRef } from "react";
> import { MultiSelectMenu } from "@servicetitan/anvil2";
>
> const ExampleComponent = () => {
>   const multiSelectMenuRef = useRef(null);
>
>   const handleDataSourceChange = () => {
>     multiSelectMenuRef.current?.invalidate();
>   };
>
>   return (
>     <MultiSelectMenu ref={multiSelectMenuRef} {...props} />
>   );
> };
> ```
>
> > [!NOTE]
> >
> > `MultiSelectMenuSync` handles this automatically when its `options` prop changes.
>
> ## Initial Load Behavior
>
> Control when options are first loaded with the `initialLoad` prop:
>
> ```tsx theme={null}
> // Load immediately on mount (default for "auto")
> <MultiSelectMenu initialLoad="immediate" {...props} />
>
> // Load when user opens the dropdown
> <MultiSelectMenu initialLoad="open" {...props} />
>
> // Auto (currently equivalent to "immediate")
> <MultiSelectMenu initialLoad="auto" {...props} />
> ```
>
> ## Disabling Search
>
> Pass `disableSearch` to remove the search input from inside the menu. The menu renders only the option list, and keyboard focus moves directly to the list container.
>
> This is useful when the option list is short and well-known or you are integrating with an API that does not support search.
>
> ```tsx theme={null}
> import { useState } from "react";
> import { MultiSelectMenuSync } from "@servicetitan/anvil2";
> import { Button } from "@servicetitan/anvil2";
>
> const options = [
>   { id: 1, label: "Red" },
>   { id: 2, label: "Green" },
>   { id: 3, label: "Blue" },
>   { id: 4, label: "Yellow" },
> ];
>
> const ExampleComponent = () => {
>   const [selectedOptions, setSelectedOptions] = useState([]);
>
>   return (
>     <MultiSelectMenuSync
>       disableSearch
>       selectAll
>       label="Colors"
>       options={options}
>       value={selectedOptions}
>       onSelectedOptionsChange={setSelectedOptions}
>       trigger={(props) => (
>         <Button {...props}>
>           {selectedOptions.length > 0
>             ? `${selectedOptions.length} colors selected`
>             : "Select colors"}
>         </Button>
>       )}
>     />
>   );
> };
> ```
>
> > [!NOTE]
> >
> > When `disableSearch` is enabled, `selectFiltered` has no effect since there is no search input to produce filtered results.
>
> ## Disabled Options
>
> Individual options can be disabled by setting `disabled: true` on the option:
>
> ```tsx theme={null}
> const options = [
>   { id: 1, label: "Available Option" },
>   { id: 2, label: "Unavailable Option", disabled: true },
>   { id: 3, label: "Another Available Option" },
> ];
>
> <MultiSelectMenuSync
>   label="Select options"
>   options={options}
>   value={selectedOptions}
>   onSelectedOptionsChange={setSelectedOptions}
>   trigger={(props) => <Button {...props}>Choose</Button>}
> />
> ```
>
> ## Pinned Options
>
> Pin frequently used or suggested options above the list using the `pinned` prop. Each pinned section requires a `label` and an `options` value, which can be a static array or a dynamic loader function.
>
> ### Static Pinned Options
>
> Pass an object with `label` and a static `options` array:
>
> multiselectmenu-pinned
>
> [StackBlitz demo](https://stackblitz.com/github/servicetitan/anvil2-docs-live-code/tree/main/examples/multiselectmenu-pinned?file=src/App.tsx)
>
> ```tsx lines expandable theme={null}
> import { useEffect, useRef, useState } from "react";
> import {
>   Button,
>   MultiSelectMenu,
>   type MultiSelectMenuOption,
> } from "@servicetitan/anvil2";
>
> const allBooks: MultiSelectMenuOption[] = [
>   { id: 1, label: "The Martian by Andy Weir" },
>   { id: 2, label: "Gone Girl by Gillian Flynn" },
>   { id: 3, label: "Mistborn: The Final Empire by Brandon Sanderson" },
>   { id: 4, label: "Murder on the Orient Express by Agatha Christie" },
>   { id: 5, label: "Dune by Frank Herbert" },
>   { id: 6, label: "Beloved by Toni Morrison" },
>   { id: 7, label: "Atomic Habits by James Clear" },
>   { id: 8, label: "Sapiens by Yuval Noah Harari" },
>   { id: 9, label: "The Name of the Wind by Patrick Rothfuss" },
>   { id: 10, label: "Project Hail Mary by Andy Weir" },
> ];
>
> function App() {
>   const [value, setValue] = useState<MultiSelectMenuOption[]>([]);
>   const containerRef = useRef<HTMLDivElement>(null);
>
>   useEffect(() => {
>     const timer = setTimeout(() => {
>       const trigger = containerRef.current?.querySelector("button");
>       trigger?.click();
>     }, 100);
>     return () => clearTimeout(timer);
>   }, []);
>
>   return (
>     <div ref={containerRef} style={{ minWidth: "384px", minHeight: "480px" }}>
>       <MultiSelectMenu
>         label="Favorite books"
>         pinned={{
>           label: "Recently viewed",
>           options: [
>             { id: 5, label: "Dune by Frank Herbert" },
>             { id: 1, label: "The Martian by Andy Weir" },
>             { id: 10, label: "Project Hail Mary by Andy Weir" },
>           ],
>         }}
>         loadOptions={(searchValue) => {
>           const lower = searchValue.toLowerCase();
>           return allBooks.filter((b) => b.label.toLowerCase().includes(lower));
>         }}
>         value={value}
>         onSelectedOptionsChange={setValue}
>         trigger={(props) => (
>           <Button {...props}>
>             {value.length > 0 ? `${value.length} selected` : "Select books"}
>           </Button>
>         )}
>       />
>     </div>
>   );
> }
>
> export default App;
> ```
>
> ### Dynamic Pinned Options
>
> Pass a function as `options` to compute pinned options based on the current search value:
>
> ```tsx theme={null}
> <MultiSelectMenu
>   label="Books"
>   pinned={{
>     label: "AI Suggestions",
>     options: async (searchValue) => {
>       const suggestions = await fetchAISuggestions(searchValue);
>       return suggestions.map((s) => ({ id: s.id, label: s.title }));
>     },
>   }}
>   loadOptions={fetchBooks}
>   value={selectedOptions}
>   onSelectedOptionsChange={setSelectedOptions}
>   trigger={(props) => <Button {...props}>Select books</Button>}
> />
> ```
>
> By default, the loader re-runs whenever the search value changes. Set `searchReactive: false` to call the loader once and reuse the result across all search values:
>
> ```tsx theme={null}
> <MultiSelectMenu
>   label="Books"
>   pinned={{
>     label: "Your Favorites",
>     options: async () => {
>       return await fetchFavorites();
>     },
>     searchReactive: false,
>   }}
>   loadOptions={fetchBooks}
>   value={selectedOptions}
>   onSelectedOptionsChange={setSelectedOptions}
>   trigger={(props) => <Button {...props}>Select books</Button>}
> />
> ```
>
> ### Multiple Pinned Sections
>
> Pass an array of pinned section objects:
>
> ```tsx theme={null}
> <MultiSelectMenu
>   label="Books"
>   pinned={[
>     {
>       label: "AI Suggestions",
>       options: async (searchValue) => {
>         const suggestions = await fetchAISuggestions(searchValue);
>         return suggestions.map((s) => ({ id: s.id, label: s.title }));
>       },
>     },
>     {
>       label: "Your Favorites",
>       options: [
>         { id: "fav-1", label: "Dune" },
>         { id: "fav-2", label: "Foundation" },
>       ],
>     },
>   ]}
>   loadOptions={fetchBooks}
>   value={selectedOptions}
>   onSelectedOptionsChange={setSelectedOptions}
>   trigger={(props) => <Button {...props}>Select books</Button>}
> />
> ```
>
> ## Grouping Options
>
> Options can be organized into visual groups by adding a `group` property to each option. Groups appear as labeled sections in the dropdown.
>
> ### Basic Grouping
>
> Add a `group` property to options and provide a `groupToString` function to display group labels:
>
> ```tsx theme={null}
> const options = [
>   { id: 1, label: "Apple", group: "fruits" },
>   { id: 2, label: "Banana", group: "fruits" },
>   { id: 3, label: "Carrot", group: "vegetables" },
>   { id: 4, label: "Broccoli", group: "vegetables" },
> ];
>
> <MultiSelectMenu
>   label="Food"
>   loadOptions={async () => options}
>   groupToString={(group) =>
>     group === "fruits" ? "Fruits" : "Vegetables"
>   }
>   value={selectedOptions}
>   onSelectedOptionsChange={setSelectedOptions}
>   trigger={(props) => <Button {...props}>Select food</Button>}
> />
> ```
>
> ### Group Sorting
>
> Use `groupSorter` to control the order of groups. By default, groups appear in the order they are first encountered in the options array. This prop is available on `MultiSelectMenuSync` and non-lazy `MultiSelectMenu`:
>
> > [!WARNING]
> >
> > Avoid using `groupSorter` with `lazy="group"`. When groups load incrementally from the server and get re-sorted, the menu content shifts unexpectedly, creating a disorienting user experience. Instead, have your server return groups in the desired order.
>
> ```tsx theme={null}
> const options = [
>   { id: 1, label: "Item A", group: 3 },
>   { id: 2, label: "Item B", group: 1 },
>   { id: 3, label: "Item C", group: 2 },
> ];
>
> <MultiSelectMenu
>   label="Items"
>   loadOptions={async () => options}
>   groupToString={(group) => `Priority ${group}`}
>   groupSorter={(a, b) => Number(a) - Number(b)}
>   value={selectedOptions}
>   onSelectedOptionsChange={setSelectedOptions}
>   trigger={(props) => <Button {...props}>Select items</Button>}
> />
> ```
>
> ## Virtualization
>
> By default, all dropdown options render to the DOM at once. This works well for typical lists but degrades performance with very large option sets. Pass `virtualize` to enable windowed rendering, which only renders the items currently visible in the scroll viewport plus a small overscan buffer.
>
> Consider enabling `virtualize` when the dropdown feels sluggish to open or keyboard navigation becomes laggy. These symptoms typically appear around 200-500 items depending on device performance and item complexity.
>
> Virtualization works with all existing features including lazy loading, pinned options, grouping, select-all/select-filtered bulk actions, and keyboard navigation.
>
> multiselectmenu-virtualization
>
> [StackBlitz demo](https://stackblitz.com/github/servicetitan/anvil2-docs-live-code/tree/main/examples/multiselectmenu-virtualization?file=src/App.tsx)
>
> ```tsx lines expandable theme={null}
> import { useState } from "react";
> import {
>   Button,
>   MultiSelectMenuSync,
>   type MultiSelectMenuOption,
> } from "@servicetitan/anvil2";
>
> const options: MultiSelectMenuOption[] = Array.from(
>   { length: 5000 },
>   (_, i) => ({
>     id: i,
>     label: `Option ${i + 1}`,
>   }),
> );
>
> function App() {
>   const [value, setValue] = useState<MultiSelectMenuOption[]>([]);
>
>   return (
>     <div style={{ minWidth: "384px" }}>
>       <MultiSelectMenuSync
>         virtualize
>         selectAll
>         label="Large dataset (5000 options)"
>         options={options}
>         value={value}
>         onSelectedOptionsChange={setValue}
>         trigger={(props) => (
>           <Button {...props}>
>             {value.length > 0 ? `${value.length} selected` : "Select options"}
>           </Button>
>         )}
>       />
>     </div>
>   );
> }
>
> export default App;
> ```
>
> ## Confirmation
>
> Set `confirmation` to make the menu buffer the user's checks and unchecks rather than commit each one immediately. While the menu is open, toggles update an internal draft and the trigger continues to display the committed `value`. The footer renders Cancel and Apply buttons; clicking **Apply** fires `onSelectedOptionsChange(draft)` once and closes the menu. Cancel, Escape, click-outside, and toggling the trigger all discard the draft. Apply is disabled until the draft differs from `value`.
>
> If the parent updates `value` while the menu is open, the draft re-seeds from the new value.
>
> > [!NOTE]
> >
> > `selectAll` and `selectFiltered` configured with plain `onClick` bypass confirmation — they commit immediately to the controlled `value`. To make those bulk actions participate in the draft instead, provide `compute` rather than `onClick`. See [Integrating with Confirmation](#integrating-with-confirmation) above. `MultiSelectMenuSync` already uses `compute` internally, so its `selectAll` / `selectFiltered` work correctly under confirmation with no changes.
>
> Pass `true` for the default "Apply" / "Cancel" labels, or an object to customize them:
>
> ```tsx theme={null}
> <MultiSelectMenu
>   confirmation
>   label="Statuses"
>   value={statuses}
>   onSelectedOptionsChange={setStatuses}
>   loadOptions={loadStatuses}
>   trigger={(props) => (
>     <Button {...props}>{`${statuses.length} statuses`}</Button>
>   )}
> />
>
> <MultiSelectMenu
>   confirmation={{ applyLabel: "Apply filters", cancelLabel: "Discard" }}
>   …
> />
> ```
>
> ## Clear Button
>
> Pass a `clear` config to render a "Clear" button in the menu footer. Clicking Clear fires the provided `onClick` handler and closes the menu; consumers typically use it to reset their selection state.
>
> ```tsx theme={null}
> <MultiSelectMenu
>   label="Owners"
>   // …
>   clear={{
>     onClick: () => setSelected([]),
>   }}
> />
> ```
>
> The footer layout adapts automatically when other footer affordances are present:
>
> * **Clear alone** — full-width row.
> * **Clear + Apply/Cancel** — Clear is left-aligned on the same row as Apply/Cancel.
> * **Clear + Add new** — Add-new stacks full-width above Clear.
> * **Clear + Apply/Cancel + Add new** — Add-new stacks full-width above the Clear / Apply/Cancel row.
>
> In confirmation mode, clicking Clear is treated as an intentional commit (not a cancel) — the draft is not rolled back to the prior committed value.
>
> ## Adding New Items
>
> Provide an `onAddNewItem` handler to render an "Add new item" button below the option list. This affordance lets users create a value that does not exist yet — a new tag or filter value — quickly from the menu.
>
> The button sits in a footer region inside the menu. Clicking or activating it via keyboard closes the menu and invokes the `onAddNewItem` handler with the current search text.
>
> From here, it is your responsibility to launch a `Dialog` (or `Drawer`, or any other overlay) for collecting the new item's details, append the result to the option source, invalidate the cache, and update selection state.
>
> You may pair `onAddNewItem` with `addItemLabel` to control the button text. The label accepts a static node or a function of the current search text — for example, returning `Add "foo"` when the user has typed `foo`.
>
> When `addItemLabel` is a function, return `null` (or `undefined`) to hide the button for the current search text. Use this when the affordance should only appear for valid input — for example, showing "Add email" only after a well-formed address is typed:
>
> ```tsx theme={null}
> addItemLabel={(searchText) =>
>   isValidEmail(searchText) ? `Add "${searchText}"` : null
> }
> onAddNewItem={openAddEmailDialog}
> ```
>
> multiselectmenu-add-new-item
>
> [StackBlitz demo](https://stackblitz.com/github/servicetitan/anvil2-docs-live-code/tree/main/examples/multiselectmenu-add-new-item?file=src/App.tsx)
>
> ```tsx lines expandable theme={null}
> import { useEffect, useRef, useState } from "react";
> import {
>   MultiSelectMenu,
>   type MultiSelectMenuHandle,
>   type MultiSelectMenuOption,
> } from "@servicetitan/anvil2";
> import { Button, Dialog, Flex, TextField } from "@servicetitan/anvil2";
>
> type Tag = MultiSelectMenuOption & { color: string };
>
> function App() {
>   const [tags, setTags] = useState<Tag[]>([
>     { id: "bug", label: "bug", color: "#e53935" },
>     { id: "feature", label: "feature", color: "#1e88e5" },
>     { id: "browser", label: "browser", color: "#7b1fa2" },
>     { id: "performance", label: "performance", color: "#f9a825" },
>     { id: "docs", label: "docs", color: "#43a047" },
>   ]);
>   const [activeFilters, setActiveFilters] = useState<Tag[]>([]);
>   const menuRef = useRef<MultiSelectMenuHandle>(null);
>   const containerRef = useRef<HTMLDivElement>(null);
>
>   const [dialogOpen, setDialogOpen] = useState(false);
>   const [draftLabel, setDraftLabel] = useState("");
>
>   // Open the menu on mount so the Add-new button is visible in screenshots.
>   useEffect(() => {
>     const timer = setTimeout(() => {
>       const trigger = containerRef.current?.querySelector("button");
>       trigger?.focus();
>       trigger?.click();
>     }, 100);
>     return () => clearTimeout(timer);
>   }, []);
>
>   const openTagDialog = (initialLabel: string) => {
>     setDraftLabel(initialLabel.trim().toLowerCase().replace(/\s+/g, "-"));
>     setDialogOpen(true);
>   };
>
>   const saveTag = () => {
>     const newTag: Tag = {
>       id: draftLabel,
>       label: draftLabel,
>       color: "#43a047",
>     };
>     setTags((prev) => [...prev, newTag]);
>     setActiveFilters((prev) => [...prev, newTag]);
>     menuRef.current?.invalidate();
>     setDialogOpen(false);
>   };
>
>   return (
>     <div ref={containerRef} style={{ minWidth: "384px", minHeight: "420px" }}>
>       <MultiSelectMenu
>         ref={menuRef}
>         label="Filter by tag"
>         searchPlaceholder="Search or create a tag..."
>         initialLoad="open"
>         loadOptions={async (searchValue) =>
>           searchValue
>             ? tags.filter((t) =>
>                 t.label.toLowerCase().includes(searchValue.toLowerCase()),
>               )
>             : tags
>         }
>         value={activeFilters}
>         onSelectedOptionsChange={(opts) => setActiveFilters(opts as Tag[])}
>         addItemLabel={(searchText) =>
>           searchText ? `Create tag "${searchText}"` : "Create new tag"
>         }
>         onAddNewItem={openTagDialog}
>         trigger={(props) => (
>           <Button {...props} appearance="secondary">
>             {activeFilters.length > 0
>               ? `Filter (${activeFilters.length})`
>               : "Filter"}
>           </Button>
>         )}
>       />
>
>       <Dialog open={dialogOpen} onClose={() => setDialogOpen(false)}>
>         <Dialog.Header>Create new tag</Dialog.Header>
>         <Dialog.Content>
>           <Flex direction="column" gap="3">
>             <TextField
>               label="Tag name"
>               description="Lowercase, hyphenated."
>               required
>               value={draftLabel}
>               onChange={(e) => setDraftLabel(e.target.value)}
>             />
>           </Flex>
>         </Dialog.Content>
>         <Dialog.Footer>
>           <Dialog.CancelButton appearance="ghost">Cancel</Dialog.CancelButton>
>           <Button
>             appearance="primary"
>             onClick={saveTag}
>             disabled={!draftLabel.trim()}
>           >
>             Create tag
>           </Button>
>         </Dialog.Footer>
>       </Dialog>
>     </div>
>   );
> }
>
> export default App;
> ```
>
> > [!NOTE]
> >
> > Call `invalidate()` on the imperative handle after appending the new option.
> > This clears the cached search results and forces a refetch the next time the
> > menu opens, so the new item appears immediately. `clearCache()` alone wipes
> > the cache but keeps the previously displayed options on screen until
> > something else triggers a reload.
>
> ## Testing
>
> Interactive sub-elements carry a stable `data-anv-part` attribute so end-to-end tests can target them. The library owns the part names; you supply the instance identifier.
>
> There is no wrapper root — attach your own `data-*` (such as `data-cy`) to `MultiSelectMenu`. It is merged into the trigger render-prop bag, so spread `{...props}` onto your trigger element:
>
> ```tsx theme={null}
> <MultiSelectMenu
>   data-cy="tags-menu"
>   trigger={(props) => <Button {...props}>Tags</Button>}
>   {...rest}
> />
> ```
>
> ```ts theme={null}
> cy.get('[data-cy="tags-menu"][data-anv-part="trigger"]').click();
> cy.get('[data-anv-part="input"]').type("red");
> ```
>
> | `data-anv-part` | Element |
> | - | - |
> | `trigger` | Your trigger element (via the `trigger` render prop) |
> | `input` | Search field inside the open menu |
> | `menu` | Option list container |
> | `option` | An individual option row |
> | `dialog` | Menu container in dialog display mode |
> | `popover` | Menu container in popover display mode |
> | `apply-button` | Footer apply action (confirmation mode) |
> | `cancel-button` | Footer cancel action (confirmation mode) |
> | `clear-button` | Footer clear action (when `clear` is configured) |
>
> > [!NOTE]
> >
> > The menu renders in the browser's top layer (`menu`, `option`, `dialog`, and `popover` parts), outside the trigger. Descendant queries scoped under the trigger will not match these parts. Target them by their part attribute directly. Only one menu is open at a time.

> [tab] MultiSelectMenu Props
>
> ## `MultiSelectMenu` Props
>
> ```tsx theme={null}
> import { useState } from "react";
> import { MultiSelectMenu } from "@servicetitan/anvil2";
> import { Button } from "@servicetitan/anvil2";
>
> const ExampleComponent = () => {
>   const [selectedOptions, setSelectedOptions] = useState([]);
>
>   return (
>     <MultiSelectMenu
>       label="Tags"
>       loadOptions={async (searchValue) => {
>         const response = await fetch(`/api/tags?q=${searchValue}`);
>         const tags = await response.json();
>         return tags.map((tag) => ({
>           id: tag.id,
>           label: tag.name,
>         }));
>       }}
>       onSelectedOptionsChange={setSelectedOptions}
>       value={selectedOptions}
>       trigger={(props) => (
>         <Button {...props}>
>           {selectedOptions.length > 0
>             ? `${selectedOptions.length} selected`
>             : "Select tags"}
>         </Button>
>       )}
>     />
>   );
> };
> ```
>
> **label** `string` *required*
>
> Accessible label for the menu. Used by screen readers and as the dialog title on mobile. Not rendered visually in the trigger.
>
> **loadOptions** `function` *required*
>
> Function to load options. The signature depends on the `lazy` mode:
>
> * **Non-lazy**: `(searchValue: string) => MultiSelectMenuOption[] | Promise<MultiSelectMenuOption[]>`
> * **Page-based**: `(searchValue: string, pageNumber: number, pageSize: number) => { options: MultiSelectMenuOption[], hasMore?: boolean }`
> * **Offset-based**: `(searchValue: string, offset: number, limit: number) => { options: MultiSelectMenuOption[], hasMore?: boolean }`
> * **Group-based**: `(searchValue: string, previousGroupKey: string | number | null) => { options: MultiSelectMenuGroupedOption[], hasMore?: boolean }`
>
> **onSelectedOptionsChange** `(options: MultiSelectMenuOption[]) =\> void` *required*
>
> Callback fired when the selected options change. Receives the new array of selected options.
>
> **trigger** `(props: MultiSelectMenuTriggerProps) =\> ReactElement` *required*
>
> Render function that receives trigger props (`ref`, `onClick`, `onKeyDown`, ARIA attributes) and returns the element that opens the menu. Spread the props onto a button or interactive element.
>
> **value** `MultiSelectMenuOption[]` *required*
>
> The currently selected options. Must be controlled state.
>
> **addItemLabel** `ReactNode | (searchText: string) =\> ReactNode`
>
> Label rendered inside the "Add new item" button. May be a static node or a callback that receives the current search text and returns a dynamic label (for example, `Add "${searchText}"`). When the callback returns `null` or `undefined`, the button is not rendered — use this to hide the affordance until the search text is valid (for example, a well-formed email). Defaults to `"Add new item"` when omitted but `onAddNewItem` is provided.
>
> **cache** `MultiSelectMenuCacheOptions`
>
> Configuration for caching `loadOptions` results:
>
> * `enabled` — Whether caching is enabled (default: `true`)
> * `maxSize` — Maximum number of search values to cache before clearing (default: `15`)
>
> **clear** `\{ onClick: () =\> void; label?: string; disabled?: boolean \}`
>
> Configures a "Clear" button rendered in the menu footer. Presence of this prop enables the button — omit to hide it.
>
> * `onClick` — Called when the user clicks Clear. The menu also closes automatically after `onClick` runs; consumers typically use this to clear their selection state. In confirmation mode, the close is treated as an intentional commit (not a cancel) so the draft is not rolled back.
> * `label` — Button label. Defaults to `"Clear"`.
> * `disabled` — Whether the button is inert.
>
> Footer layout adapts to the other present affordances:
>
> * **Clear + Apply/Cancel** — Clear is left-aligned on the same row as Apply/Cancel.
> * **Clear + Apply/Cancel + Add new** — Add-new stacks full-width above the Clear / Apply/Cancel row.
> * **Clear + Add new** — Add-new stacks full-width above Clear.
> * **Clear alone** — full-width row.
>
> **confirmation** `boolean | \{ applyLabel?: string; cancelLabel?: string \}`
>
> Enables confirmation mode. When set, option toggles update an internal draft and `onSelectedOptionsChange` only fires after the user clicks Apply. Dismissing the menu (Escape, click outside, toggling the trigger) discards the draft. To integrate `selectAll` / `selectFiltered` with the draft, configure them with `compute` instead of `onClick`. Pass `true` for the default "Apply" / "Cancel" labels, or an object to customize them.
>
> **debounceMs** `number` (default: 200)
>
> Milliseconds to debounce search input before calling `loadOptions`.
>
> **disableSearch** `boolean` (default: false)
>
> Removes the search input from inside the menu. When true, the menu renders only the option list and keyboard focus moves to the list container. The `selectAll` bulk action remains available; `selectFiltered` has no effect.
>
> **displayMenuAs** `"auto" | "popover" | "dialog"` (default: auto)
>
> How to display the options menu:
>
> * `auto` — Popover on desktop, dialog on mobile
> * `popover` — Always display as popover
> * `dialog` — Always display as dialog
>
> **groupSorter** `(a: MultiSelectMenuGroupByValue, b: MultiSelectMenuGroupByValue) =\> number`
>
> Function to compare two group values for sorting. When provided, groups are sorted using this comparator. Without this, groups appear in the order they are first encountered. Best used with `MultiSelectMenuSync` or non-lazy `MultiSelectMenu`. Avoid using with `lazy="group"` as it causes the menu to shift unexpectedly when new groups load.
>
> **groupToString** `(groupValue: MultiSelectMenuGroupByValue) =\> string`
>
> Function to convert group values to display labels. Only used with grouped options. `MultiSelectMenuGroupByValue` is `string | number`.
>
> **id** `string`
>
> The id of the multi-select menu.
>
> **initialLoad** `"auto" | "immediate" | "open"` (default: auto)
>
> Controls when `loadOptions` is first called:
>
> * `auto` — Currently equivalent to `immediate`
> * `immediate` — Load on component mount
> * `open` — Load when the menu is opened
>
> **lazy** `"page" | "offset" | "group" | false` (default: false)
>
> Enables lazy loading with the specified pagination strategy.
>
> **lazyOptions** `object`
>
> Configuration for lazy loading:
>
> * **Page mode**: `{ pageSize?: number }` (default pageSize: 20)
> * **Offset mode**: `{ limit?: number }` (default limit: 20)
> * **Group mode**: `{}`
>
> **onAddNewItem** `(searchText: string) =\> void`
>
> Click handler for the "Add new item" button. The button is only rendered when this prop is provided. Receives the current search text. The menu closes synchronously on click and the handler runs immediately after — typically opening a Dialog or Drawer to collect the new item's details. Consumers own overlay presentation, focus return, option list updates, and selection state. See the [Adding New Items](#adding-new-items) section for a full example.
>
> **onSearchChange** `(searchValue: string) =\> void`
>
> Callback when the search value changes.
>
> **pinned** `MultiSelectMenuPinnedOptions`
>
> Options to pin to the top of the list. Accepts a single section object or an array of section objects. Each section requires:
>
> * `label` — Display label for the section header
> * `options` — Static array (`MultiSelectMenuOption[]`) or dynamic loader function (`(searchValue: string) => MultiSelectMenuOption[] | Promise<MultiSelectMenuOption[]>`)
> * `searchReactive` — (Optional) Whether to re-call the loader when the search value changes. Defaults to `true`. Set to `false` to call the loader once and reuse the result.
> * `cacheSize` — (Optional) Maximum number of search results to cache per section. Defaults to `15`. Only applies when `searchReactive` is `true`.
>
> **searchPlaceholder** `string`
>
> Placeholder text shown in the search input inside the menu.
>
> **searchValue** `string`
>
> Controlled search value. In most cases you do not need this — lean on the `searchValue` parameter in `loadOptions` instead.
>
> **selectAll** `object`
>
> Configuration for the "Select All" option. Pass exactly one of `onClick` or `compute`:
>
> * `label` — The label to display (default: "Select All")
> * `checkState` — State: `true`/`"checked"`, `false`/`"unchecked"`, `"indeterminate"`, or `"loading"`. Also accepts a function `(current: MultiSelectMenuOption[]) => boolean | CheckState` that derives the state from the current selection (the draft when confirmation is on, otherwise the committed `value`).
> * `onClick` — Fire-and-forget callback. The consumer is responsible for committing the next selection via `onSelectedOptionsChange`. Under `confirmation`, `onClick` bypasses the draft and logs a development warning.
> * `compute` — `(current) => MultiSelectMenuOption[] | Promise<MultiSelectMenuOption[]>`. Receives the current draft (or `value` when confirmation is off) and returns the next selection. The menu writes the result into the draft, so the bulk action participates in confirmation correctly. While a returned promise is pending, the menu shows a loading state, disables all option rows, and disables Apply.
>
> **selectFiltered** `(searchValue: string) =\> object`
>
> Function that receives the current search value and returns configuration for the "Select Filtered" option (mutually exclusive with `selectAll`, shown when a search term is active). The returned object accepts the same `onClick` / `compute` choice as `selectAll`:
>
> * `label` — The label to display. Default: `Select items matching "${searchValue}"`
> * `checkState` — State: `true`/`"checked"`, `false`/`"unchecked"`, `"indeterminate"`, or `"loading"`. Also accepts a function `(current: MultiSelectMenuOption[]) => boolean | CheckState` that derives the state from the current selection.
> * `onClick` or `compute` — See `selectAll` above.
>
> **virtualize** `boolean` (default: false)
>
> Enables windowed rendering for the dropdown list. Improves performance for large option sets by only rendering visible items. Consider enabling when the dropdown feels sluggish to open or keyboard navigation becomes laggy, typically around 200-500+ items.
>
> **width** `number | string` (default: 320)
>
> Width of the popover menu. Accepts a number (pixels) or a CSS string value (e.g., `"20rem"`).

> [tab] MultiSelectMenuSync Props
>
> ## `MultiSelectMenuSync` Props
>
> `MultiSelectMenuSync` accepts all props from `MultiSelectMenu` except `loadOptions`, `lazy`, `debounceMs`, `cache`, `initialLoad`, `selectAll`, and `selectFiltered`, plus the following:
>
> ```tsx theme={null}
> import { useState } from "react";
> import { MultiSelectMenuSync } from "@servicetitan/anvil2";
> import { Button } from "@servicetitan/anvil2";
>
> const options = [
>   { id: 1, label: "Option One" },
>   { id: 2, label: "Option Two" },
>   { id: 3, label: "Option Three" },
> ];
>
> const ExampleComponent = () => {
>   const [selectedOptions, setSelectedOptions] = useState([]);
>
>   return (
>     <MultiSelectMenuSync
>       label="Options"
>       onSelectedOptionsChange={setSelectedOptions}
>       options={options}
>       value={selectedOptions}
>       trigger={(props) => (
>         <Button {...props}>
>           {selectedOptions.length > 0
>             ? `${selectedOptions.length} selected`
>             : "Select options"}
>         </Button>
>       )}
>     />
>   );
> };
> ```
>
> **label** `string` *required*
>
> Accessible label for the menu.
>
> **onSelectedOptionsChange** `(options: MultiSelectMenuOption[]) =\> void` *required*
>
> Callback fired when the selected options change. Receives the new array of selected options.
>
> **options** `MultiSelectMenuOption[]` *required*
>
> The array of options to display in the multi-select menu.
>
> **trigger** `(props: MultiSelectMenuTriggerProps) =\> ReactElement` *required*
>
> Render function that receives trigger props and returns the element that opens the menu.
>
> **value** `MultiSelectMenuOption[]` *required*
>
> The currently selected options. Must be controlled state.
>
> **addItemLabel** `ReactNode | (searchText: string) =\> ReactNode`
>
> Label rendered inside the "Add new item" button. May be a static node or a callback that receives the current search text and returns a dynamic label (for example, `Add "${searchText}"`). When the callback returns `null` or `undefined`, the button is not rendered — use this to hide the affordance until the search text is valid (for example, a well-formed email). Defaults to `"Add new item"` when omitted but `onAddNewItem` is provided.
>
> **clear** `\{ onClick: () =\> void; label?: string; disabled?: boolean \}`
>
> Configures a "Clear" button rendered in the menu footer. Presence of this prop enables the button — omit to hide it. The menu closes automatically after `onClick` runs. See the [`MultiSelectMenu` Props](#multiselectmenu-props) tab for full footer-layout details.
>
> **disableSearch** `boolean` (default: false)
>
> Removes the search input from inside the menu. When true, the menu renders only the option list. The `selectAll` bulk action remains available; `selectFiltered` has no effect.
>
> **displayMenuAs** `"auto" | "popover" | "dialog"` (default: auto)
>
> How to display the options menu.
>
> **filter** `function | MatchSorterOptions`
>
> Controls how options are filtered and sorted when the user types a search value. Can be:
>
> * A function: `(options: MultiSelectMenuOption[], searchValue: string) => MultiSelectMenuOption[]` — receives all options and returns the filtered subset in the desired display order
> * A [match-sorter options object](https://github.com/kentcdodds/match-sorter#options) to customize the default match-sorter behavior (e.g., which keys to match against, ranking strategy)
>
> Before any search is performed, options appear in the order they are supplied. Default: Filters by `label` and `searchText` fields using match-sorter, with results ranked by match quality.
>
> **groupSorter** `(a: MultiSelectMenuGroupByValue, b: MultiSelectMenuGroupByValue) =\> number`
>
> Function to compare two group values for sorting. When provided, options are sorted by group using this comparator, then by match-sort order within each group. Ungrouped options appear last.
>
> **groupToString** `(groupValue: MultiSelectMenuGroupByValue) =\> string`
>
> Function to convert group values to display labels. Only used with grouped options. `MultiSelectMenuGroupByValue` is `string | number`.
>
> **id** `string`
>
> The id of the multi-select menu.
>
> **onAddNewItem** `(searchText: string) =\> void`
>
> Click handler for the "Add new item" button. The button is only rendered when this prop is provided. Receives the current search text. The menu closes synchronously on click and the handler runs immediately after — typically opening a Dialog or Drawer to collect the new item's details. Consumers own overlay presentation, focus return, option list updates, and selection state. See the [Adding New Items](#adding-new-items) section for a full example.
>
> **onSearchChange** `(searchValue: string) =\> void`
>
> Callback when the search value changes.
>
> **pinned** `MultiSelectMenuPinnedOptions`
>
> Options to pin to the top of the list. Accepts the same section object format as `MultiSelectMenu`'s `pinned` prop.
>
> **searchPlaceholder** `string`
>
> Placeholder text shown in the search input inside the menu.
>
> **searchValue** `string`
>
> Controlled search value. In most cases you do not need this.
>
> **selectAll** `boolean | \{ label?: string | ((checked: boolean) =\> string) \}`
>
> Enables the "Select All" option at the top of the list. Can be:
>
> * `true` — Enable with default label "Select All"
> * `{ label: "Custom Label" }` — Enable with custom static label
> * `{ label: (checked) => checked ? "Deselect All" : "Select All" }` — Enable with dynamic label based on check state
>
> Click handling and check state are managed automatically based on comparing `options` with `value`.
>
> **selectFiltered** `boolean | (searchValue: string) =\> \{ label?: string \}`
>
> Enables the "Select Filtered" option when a search term is active (mutually exclusive with `selectAll`). Can be:
>
> * `true` — Enable with default dynamic label
> * `(searchValue) => ({ label })` — A function that receives the search value and returns a config with an optional custom label
>
> Click handling and check state are managed automatically based on comparing filtered options with `value`.
>
> **virtualize** `boolean` (default: false)
>
> Enables windowed rendering for the dropdown list. Improves performance for large option sets by only rendering visible items.
>
> **width** `number | string`
>
> Width of the popover menu. Accepts a number (pixels) or a CSS string value.

> [tab] MultiSelectMenuOption Type
>
> ## `MultiSelectMenuOption` Type
>
> Options passed to or returned from `MultiSelectMenu` components must conform to this shape. This is the same type as `SelectFieldOption`.
>
> ```tsx theme={null}
> type MultiSelectMenuOption = {
>   /** Unique identifier for the option */
>   id: string | number;
>
>   /** Display text for the option */
>   label: string;
>
>   /** Optional text used for search matching (in addition to label) */
>   searchText?: string;
>
>   /** Group key for grouped options (required when using lazy="group") */
>   group?: string | number;
>
>   /** Whether the option is disabled and cannot be selected */
>   disabled?: boolean;
>
>   /** Optional rich content configuration */
>   content?: {
>     title?: string;
>     description?: string;
>     chips?: Pick<ChipProps, "label" | "color" | "textWrap">[];
>     avatar?: Pick<AvatarProps, "name" | "status" | "color" | "image">;
>     icon?: Pick<IconProps, "svg" | "color">;
>   };
> };
> ```
>
> ### Content Properties
>
> The `content` object supports the following optional properties:
>
> * `title` — Replaces the `label` as the primary display text in the dropdown.
> * `description` — Secondary text displayed below the title.
> * `chips` — Array of chip configurations rendered below the title and description. Each chip accepts `label`, `color`, and `textWrap`.
> * `avatar` — Renders an avatar to the left of the option text. Accepts `name`, `status`, `color`, and `image`.
> * `icon` — Renders an icon to the right of the option text. Accepts `svg` and `color`.
>
> ### Example with Rich Content
>
> ```tsx theme={null}
> const options: MultiSelectMenuOption[] = [
>   {
>     id: 1,
>     label: "John Doe",
>     searchText: "john.doe@example.com",
>     content: {
>       title: "John Doe",
>       description: "Engineering",
>     },
>   },
>   {
>     id: 2,
>     label: "Jane Smith",
>     searchText: "jane.smith@example.com",
>     content: {
>       title: "Jane Smith",
>       description: "Design",
>     },
>   },
> ];
> ```
>
> ### Example with Grouped Options
>
> When using `lazy="group"`, options must include the `group` property:
>
> ```tsx theme={null}
> const groupedOptions: MultiSelectMenuOption[] = [
>   {
>     id: 1,
>     label: "John Doe",
>     group: "Engineering",
>   },
>   {
>     id: 2,
>     label: "Jane Smith",
>     group: "Design",
>   },
>   {
>     id: 3,
>     label: "Bob Johnson",
>     group: "Engineering",
>   },
> ];
> ```
>
> ### Example with Disabled Options
>
> ```tsx theme={null}
> const options: MultiSelectMenuOption[] = [
>   {
>     id: 1,
>     label: "Available Option",
>   },
>   {
>     id: 2,
>     label: "Unavailable Option",
>     disabled: true,
>   },
>   {
>     id: 3,
>     label: "Another Available Option",
>   },
> ];
> ```


This documentation is built and hosted on [Mintlify](https://mintlify.com), a developer documentation platform.