packages/react-aria-components/docs/examples/user-combobox.mdx
{/* Copyright 2023 Adobe. All rights reserved. This file is licensed to you under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License. */}
import {ExampleLayout} from '@react-spectrum/docs'; export default ExampleLayout;
import docs from 'docs:react-aria-components'; import {TypeLink} from '@react-spectrum/docs'; import styles from '@react-spectrum/docs/src/docs.css'; import ComboBox from '@react-spectrum/docs/pages/assets/component-illustrations/ComboBox.svg'; import Button from '@react-spectrum/docs/pages/assets/component-illustrations/ActionButton.svg'; import Popover from '@react-spectrum/docs/pages/assets/component-illustrations/Popover.svg'; import ListBox from '@react-spectrum/docs/pages/assets/component-illustrations/ListBox.svg'; import {ExampleCard} from '@react-spectrum/docs/src/ExampleCard'; import ChevronRight from '@spectrum-icons/workflow/ChevronRight';
A user search ComboBox styled with Tailwind CSS.
import './tailwind.global.css';
const people = [
{
id: 1,
avatar:
"https://images.unsplash.com/photo-1599566150163-29194dcaad36?ixid=MnwxMjA3fDB8MHxwaG90by1wYWdlfHx8fGVufDB8fHx8&ixlib=rb-1.2.1auto=format&fit=facearea&facepad=2&w=256&h=256&q=80",
name: "Gilberto Miguel",
username: "@gilberto_miguel"
},
{
id: 2,
avatar:
"https://images.unsplash.com/photo-1494790108377-be9c29b29330?ixid=MnwxMjA3fDB8MHxwaG90by1wYWdlfHx8fGVufDB8fHx8&ixlib=rb-1.2.1&auto=format&fit=facearea&facepad=2&w=256&h=256&q=80",
name: "Maia Pettegree",
username: "@mpettegree"
},
{
id: 3,
avatar:
"https://images.unsplash.com/photo-1531427186611-ecfd6d936c79?ixid=MnwxMjA3fDB8MHxwaG90by1wYWdlfHx8fGVufDB8fHx8&ixlib=rb-1.2.1&auto=format&fit=facearea&facepad=2&w=256&h=256&q=80",
name: "Wade Redington",
username: "@redington"
},
{
id: 4,
avatar:
"https://images.unsplash.com/photo-1528763380143-65b3ac89a3ff?ixid=MnwxMjA3fDB8MHxwaG90by1wYWdlfHx8fGVufDB8fHx8&ixlib=rb-1.2.1&auto=format&fit=facearea&facepad=2&w=256&h=256&q=80",
name: "Kurtis Gurrado",
username: "@kurtis"
},
{
id: 5,
avatar:
"https://images.unsplash.com/photo-1569913486515-b74bf7751574?ixid=MnwxMjA3fDB8MHxwaG90by1wYWdlfHx8fGVufDB8fHx8&ixlib=rb-1.2.1&auto=format&fit=facearea&facepad=2&w=256&h=256&q=80",
name: "Sonja Balmann",
username: "@sbalmann"
},
{
id: 6,
avatar:
"https://images.unsplash.com/photo-1570295999919-56ceb5ecca61?ixid=MnwxMjA3fDB8MHxwaG90by1wYWdlfHx8fGVufDB8fHx8&ixlib=rb-1.2.1&auto=format&fit=facearea&facepad=2&w=256&h=256&q=80",
name: "Brent Mickelwright",
username: "@brent_m"
},
{
id: 7,
avatar:
"https://images.unsplash.com/photo-1500648767791-00dcc994a43e?ixlib=rb-1.2.1&ixid=eyJhcHBfaWQiOjEyMDd9&auto=format&fit=facearea&facepad=2.25&w=256&h=256&q=80",
name: "Charles Webb",
username: "@cwebb"
}
];
import {ComboBox, Label, Input, Group, Button, Popover, ListBox, ListBoxItem} from 'react-aria-components';
import type {ListBoxItemProps} from 'react-aria-components';
import {Check, ChevronsUpDown} from 'lucide-react';
function ComboBoxExample() {
return (
<div className="bg-linear-to-r from-sky-300 to-cyan-300 p-8 sm:h-[300px] rounded-lg flex justify-center">
<ComboBox className="group flex flex-col gap-1 w-[200px]">
<Label className="text-black cursor-default">Assignee</Label>
<Group className="flex rounded-lg bg-white/90 focus-within:bg-white transition shadow-md ring-1 ring-black/10 focus-visible:ring-2 focus-visible:ring-black">
<Input className="flex-1 w-full border-none py-2 px-3 leading-5 text-gray-900 bg-transparent outline-hidden text-base" />
<Button className="px-3 flex items-center text-gray-700 transition border-0 border-solid border-l border-l-sky-200 bg-transparent rounded-r-lg pressed:bg-sky-100">
<ChevronsUpDown className="w-4 h-4" />
</Button>
</Group>
<Popover className="max-h-60 w-(--trigger-width) overflow-auto rounded-md bg-white text-base shadow-lg ring-1 ring-black/5 entering:animate-in entering:fade-in exiting:animate-out exiting:fade-out">
<ListBox className="outline-hidden p-1" items={people}>
{item => (
<UserItem textValue={item.name}>
<span className="truncate">{item.name}</span>
</UserItem>
)}
</ListBox>
</Popover>
</ComboBox>
</div>
);
}
function UserItem(props: ListBoxItemProps & {children: React.ReactNode}) {
return (
<ListBoxItem
{...props}
className="group flex items-center gap-2 cursor-default select-none py-2 pl-2 pr-4 outline-hidden rounded-sm text-gray-900 focus:bg-sky-600 focus:text-white">
{({ isSelected }) => (
<>
<span className="flex-1 flex items-center gap-3 truncate font-normal group-selected:font-medium">{props.children}</span>
{isSelected &&
<span className="w-5 flex items-center text-sky-600 group-focus:text-white">
<Check className="w-4 h-4" />
</span>
}
</>
)}
</ListBoxItem>
);
}
This example uses the following plugins:
When using Tailwind v4, add them to your CSS:
@import "tailwindcss";
@plugin "tailwindcss-react-aria-components";
@plugin "tailwindcss-animate";
When using Tailwind v3, add the plugins to your tailwind.config.js instead:
module.exports = {
// ...
plugins: [
require('tailwindcss-react-aria-components'),
require('tailwindcss-animate')
]
};
Note: When using Tailwind v3, install tailwindcss-react-aria-components version 1.x instead of 2.x.
<ExampleCard url="../ComboBox.html" title="ComboBox" description="A combobox combines a text input with a listbox, and allows a user to filter a list of options."> <ComboBox style={{background: 'var(--anatomy-gray-100)', width: 'calc(100% - 20px)', padding: 10, maxHeight: 132}} /> </ExampleCard>
<ExampleCard url="../ListBox.html" title="ListBox" description="A listbox allows a user to select one or more options from a list."> <ListBox style={{background: 'var(--anatomy-gray-100)', width: 'calc(100% - 20px)', padding: 10, maxHeight: 132}} /> </ExampleCard>
<ExampleCard url="../Popover.html" title="Popover" description="A popover displays content in context with a trigger element."> <Popover /> </ExampleCard>
<ExampleCard url="../Button.html" title="Button" description="A button allows a user to perform an action."> <Button /> </ExampleCard>
</section>