Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(components): consistent selection display for menus and listbox #1596

Merged
merged 4 commits into from
Mar 28, 2025
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/lovely-hotels-switch.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@launchpad-ui/components": minor
---

Consistent selection handling for menu and listbox
18 changes: 14 additions & 4 deletions packages/components/src/ListBox.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,16 +4,17 @@ import type {
ListBoxProps as AriaListBoxProps,
} from 'react-aria-components';

import { Icon } from '@launchpad-ui/icons';
import { cva } from 'class-variance-authority';
import {
ListBox as AriaListBox,
ListBoxItem as AriaListBoxItem,
composeRenderProps,
} from 'react-aria-components';

import { Icon } from '@launchpad-ui/icons';
import { CheckboxInner } from './Checkbox';
import { checkbox } from './Checkbox';
import styles from './styles/ListBox.module.css';

const box = cva(styles.box);
const item = cva(styles.item);

Expand Down Expand Up @@ -58,10 +59,19 @@ const ListBoxItem = <T extends object>({ ref, ...props }: ListBoxItemProps<T>) =
item({ ...renderProps, className }),
)}
>
{composeRenderProps(props.children, (children, { isSelected }) => (
{composeRenderProps(props.children, (children, { selectionMode, isDisabled, isSelected }) => (
<>
{selectionMode === 'multiple' && (
<div
className={checkbox()}
data-selected={isSelected || undefined}
data-disabled={isDisabled || undefined}
>
<CheckboxInner isSelected={isSelected} />
</div>
)}
<span className={styles.content}>{children}</span>
{isSelected && <Icon name="check" size="medium" />}
{selectionMode === 'single' && isSelected && <Icon name="check-circle" size="small" />}
</>
))}
</AriaListBoxItem>
Expand Down
11 changes: 1 addition & 10 deletions packages/components/src/Menu.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ import {
} from 'react-aria-components';

import { CheckboxInner, checkbox } from './Checkbox';
import { RadioInner, radio } from './Radio';
import styles from './styles/Menu.module.css';

const menu = cva(styles.menu);
Expand Down Expand Up @@ -75,16 +74,8 @@ const MenuItem = <T extends object>({ variant = 'default', ref, ...props }: Menu
<CheckboxInner isSelected={isSelected} />
</div>
)}
{selectionMode === 'single' && (
<div
className={radio()}
data-selected={isSelected || undefined}
data-disabled={isDisabled || undefined}
>
<RadioInner isSelected={isSelected} />
</div>
)}
<span className={styles.content}>{children}</span>
{selectionMode === 'single' && isSelected && <Icon name="check-circle" size="small" />}
{hasSubmenu && <Icon name="chevron-right" size="small" />}
</>
),
Expand Down
Loading