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

add support for custom select indicator #667

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all 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
11 changes: 11 additions & 0 deletions apps/playground/app/sink/page.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -601,6 +601,17 @@ export default function Sink() {
</tbody>
</table>

<Text as="p" my="5">
Custom indicator:
</Text>

<Select.Root defaultValue="apple" indicator={<StarIcon />}>
<Select.Trigger />
<Select.Content>
<SelectItemsDemo />
</Select.Content>
</Select.Root>

<Text as="p" my="5">
<Code>radius</Code> can be set per instance:
</Text>
Expand Down
2 changes: 2 additions & 0 deletions packages/radix-ui-themes/src/components/select.props.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,10 @@ const sizes = ['1', '2', '3'] as const;

const selectRootPropDefs = {
size: { type: 'enum', className: 'rt-r-size', values: sizes, default: '2', responsive: true },
indicator: { type: 'ReactNode' },
} satisfies {
size: PropDef<(typeof sizes)[number]>;
indicator: PropDef<React.ReactNode>;
};

const triggerVariants = ['classic', 'surface', 'soft', 'ghost'] as const;
Expand Down
10 changes: 7 additions & 3 deletions packages/radix-ui-themes/src/components/select.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import { useThemeContext, Theme } from './theme.js';
import type { MarginProps } from '../props/margin.props.js';
import type { GetPropDefTypes } from '../props/prop-def.js';
import type { ComponentPropsWithout, RemovedProps } from '../helpers/component-props.js';
import { Box } from './box';

type SelectRootOwnProps = GetPropDefTypes<typeof selectRootPropDefs>;

Expand All @@ -28,10 +29,10 @@ interface SelectRootProps
extends React.ComponentPropsWithoutRef<typeof SelectPrimitive.Root>,
SelectContextValue {}
const SelectRoot: React.FC<SelectRootProps> = (props) => {
const { children, size = selectRootPropDefs.size.default, ...rootProps } = props;
const { children, size = selectRootPropDefs.size.default, indicator = <ThickCheckIcon />, ...rootProps } = props;
return (
<SelectPrimitive.Root {...rootProps}>
<SelectContext.Provider value={React.useMemo(() => ({ size }), [size])}>
<SelectContext.Provider value={React.useMemo(() => ({ size, indicator }), [size, indicator])}>
{children}
</SelectContext.Provider>
</SelectPrimitive.Root>
Expand Down Expand Up @@ -141,6 +142,7 @@ interface SelectItemProps
extends ComponentPropsWithout<typeof SelectPrimitive.Item, RemovedProps> {}
const SelectItem = React.forwardRef<SelectItemElement, SelectItemProps>((props, forwardedRef) => {
const { className, children, ...itemProps } = props;
const context = React.useContext(SelectContext);
return (
<SelectPrimitive.Item
{...itemProps}
Expand All @@ -149,7 +151,9 @@ const SelectItem = React.forwardRef<SelectItemElement, SelectItemProps>((props,
className={classNames('rt-SelectItem', className)}
>
<SelectPrimitive.ItemIndicator className="rt-SelectItemIndicator">
<ThickCheckIcon className="rt-SelectItemIndicatorIcon" />
<Box asChild className="rt-SelectItemIndicatorIcon">
{context.indicator}
</Box>
</SelectPrimitive.ItemIndicator>
<SelectPrimitive.ItemText>{children}</SelectPrimitive.ItemText>
</SelectPrimitive.Item>
Expand Down