Styling

Radix Primitives are unstyled—and compatible with any styling solution—giving you complete control over styling.

You are in control of all aspects of styling, including functional styles. For example—by default—a Dialog Overlay won't cover the entire viewport. You're responsible for adding those styles, plus any presentation styles.

All components and their parts accept a className prop. This class will be passed through to the DOM element. You can use it in CSS as expected.

When components are stateful, their state will be exposed in a data-state attribute. For example, when an Accordion Item is opened, it includes a data-state="open" attribute.

You can style a component part by targeting the className that you provide. The primitives can be wrapped to add your class.

import * as React from 'react';
import * as AccordionPrimitive from '@radix-ui/react-accordion';
const AccordionItem = React.forwardRef<
React.ElementRef<typeof AccordionPrimitive.Item>,
React.ComponentProps<typeof AccordionPrimitive.Item>
>((props, forwardedRef) => {
const { className, ...itemProps } = props;
return (
<AccordionPrimitive.Item {...itemProps} ref={forwardedRef} className={'accordion-item ' + className} />
);
});

You can style a component state by targeting its data-state attribute.

.accordion-item {
border-bottom: 1px solid gainsboro;
}
.accordion-item[data-state='open'] {
border-bottom-width: 2px;
}

The examples below are using Stitches, but you can use any CSS-in-JS library of your choice.

You can style a component part by wrapping it in a styled function (or equivalent).

import { styled } from '@stitches/react';
import * as Accordion from '@radix-ui/react-accordion';
const StyledItem = styled(Accordion.Item, {
borderBottom: '1px solid gainsboro',
});
const StyledPanel = styled(Accordion.Panel, {
padding: 10,
});

You can style a component state by targeting its data-state attribute.

import { styled } from '@stitches/react';
import * as Accordion from '@radix-ui/react-accordion';
const StyledItem = styled(Accordion.Item, {
borderBottom: '1px solid gainsboro',
'&[data-state=open]': {
borderBottomWidth: '2px',
},
});

Here's an example of how you can style the Accordion component with CSS-in-JS.

import { styled } from '@stitches/react';
import * as Accordion from '@radix-ui/react-accordion';
const StyledAccordion = styled(Accordion.Root, {});
const StyledItem = styled(Accordion.Item, {});
const StyledHeader = styled(Accordion.Header, {});
const StyledTrigger = styled(Accordion.Trigger, {});
const StyledPanel = styled(Accordion.Content, {});
export default () => {
return (
<StyledAccordion type="single">
<StyledItem value="item-1">
<StyledHeader>
<StyledTrigger>Trigger text</StyledTrigger>
</StyledHeader>
<StyledPanel>Panel content</StyledPanel>
</StyledItem>
</StyledAccordion>
);
};

Extending a primitive is done the same way you extend any React component. One minor difference is the use of Radix.ComponentPropsWithoutRef instead of React.ComponentPropsWithoutRef which is just a workaround until the fixed types are released.

import * as React from 'react';
import * as AccordionPrimitive from '@radix-ui/react-accordion';
import type * as Radix from '@radix-ui/react-primitive';
const AccordionItem = React.forwardRef< React.ElementRef<typeof AccordionPrimitive.Item>, Radix.ComponentPropsWithoutRef<typeof AccordionPrimitive.Item> >((props, forwardedRef) => (
<AccordionPrimitive.Item {...props} ref={forwardedRef} />
));
AccordionItem.displayName = 'AccordionItem';

All component parts that render a DOM element have an asChild prop. This is useful when you want to change the rendered element.

Here's an example of how you can use a custom Button as the trigger for a Dialog.Trigger:

import { styled } from '@stitches/react';
import * as Dialog from '@radix-ui/react-dialog';
// This is a generic button, eg: from your design system
const Button = styled('button', {...});
export default () => {
return (
<Dialog.Root>
<Dialog.Trigger asChild>
<Button>Open dialog</Button>
</Dialog.Trigger>
<Dialog.Content>...</Dialog.Content>
</Dialog.Root>
);
};

Radix Primitives were designed to encapsulate accessibility concerns and other complex functionalities, while ensuring you retain complete control over styling.

For convenience, stateful components include a data-state attribute.