separator.tsx
Overview
The separator.tsx file defines a reusable React UI component named Separator. This component serves as a visual divider or separator, commonly used in user interfaces to separate content sections either horizontally or vertically. It leverages the @radix-ui/react-separator primitive for accessibility and behavior, while adding custom styling and default props to fit the design system of the application.
Key features of this component include:
Support for horizontal and vertical orientation.
Accessibility compliance by default (
decorativeprop).Customizable styling through class names, with sensible defaults.
Forwarding of refs to the underlying DOM element for integration flexibility.
This component fits in the UI layer of the application, typically used within layouts, menus, lists, or between content blocks to improve visual hierarchy and readability.
Detailed Documentation
Separator Component
Description
A React functional component that renders a separator line with configurable orientation and styling. It is built as a wrapper around the SeparatorPrimitive.Root component from the Radix UI library, adding default styles and props.
Declaration
const Separator: React.ForwardRefExoticComponent<
React.PropsWithoutRef<React.ComponentPropsWithoutRef<typeof SeparatorPrimitive.Root>> &
React.RefAttributes<React.ElementRef<typeof SeparatorPrimitive.Root>>
>
Props
Separator accepts all props that SeparatorPrimitive.Root accepts, extending them with some default values and styling.
Prop | Type | Default | Description |
|---|---|---|---|
|
|
| Additional CSS class names to apply custom styles to the separator element. |
| `'horizontal' | 'vertical'` |
|
|
|
| Indicates if the separator is purely decorative (affects accessibility). |
|
| N/A | Any other props accepted by |
Parameters
props: An object containing the properties described above.ref: A React ref forwarded to the underlying DOM element for direct access.
Returns
A React element representing a styled separator line (<div> or equivalent) with appropriate accessibility attributes.
Usage Example
import { Separator } from './separator';
function Example() {
return (
<div>
<p>Section 1</p>
<Separator />
<p>Section 2</p>
<Separator orientation="vertical" className="mx-2" />
<p>Section 3</p>
</div>
);
}
This example renders a horizontal separator between "Section 1" and "Section 2", and a vertical separator between "Section 2" and "Section 3".
Implementation Details
Forwarding Refs: The component uses
React.forwardRefto allow parent components to access the underlying separator DOM node if needed.Default Styling: The
cnutility function (imported from@/lib/utils) is used to compose class names. The component applies a base style of'shrink-0 bg-border'which ensures the separator does not shrink and uses the theme's border color.Orientation Handling: Based on the
orientationprop, the component applies height and width styles:Horizontal: height of
1pxand full width.Vertical: full height and width of
1px.
Accessibility: By default, the
decorativeprop is set totrue, marking the separator as non-interactive and purely visual, so screen readers ignore it. This aligns with WAI-ARIA guidelines for separators.
Interaction with Other Parts of the System
Dependency on
@radix-ui/react-separator: The component wraps Radix UI's primitive separator, leveraging its accessible implementation.Styling Utilities: Uses the
cnutility from the local@/lib/utilsmodule, which likely handles conditional class concatenation.UI Layer: Designed to be used by other React components in the UI, such as layout components, menus, or any content grouping requiring visual separation.
Ref Forwarding: Enables integration with higher-order components or libraries that need direct DOM access for animations, measurements, or focus control.
Visual Diagram
componentDiagram
component Separator {
+props: {
className?: string
orientation?: 'horizontal' | 'vertical'
decorative?: boolean
...other SeparatorPrimitive.Root props
}
+ref: React.Ref<HTMLDivElement>
+render()
}
component SeparatorPrimitive.Root {
+props
+ref
+render()
}
Separator --> SeparatorPrimitive.Root : wraps
Summary
The separator.tsx file provides a small, focused React component that enhances the Radix UI separator primitive with default styling and prop defaults tailored for the application's design language. It supports both horizontal and vertical orientations and is accessible by default. This component helps developers maintain consistency and accessibility in UI dividers across the project.