Compare
An unstyled, accessible before/after comparison component with compound composition.
Build fully custom before/after comparison layouts with complete control over handle, indicator, and item rendering.


basic-demo
Pre-styled Versions
Choose a pre-styled library to use Compare with ready-made designs.
For hook-based usage without components, see the Headless API.
Features#
- Compound component API with four sub-components:
Root,Item,Handle,Indicator - Automatic
clipPathcalculation for before/after layers - Horizontal and vertical orientation
- Slide-on-hover mode for pointer-follow behavior
Usage#
import { Compare } from 'primereact/compare';<Compare.Root>
<Compare.Item position="before"></Compare.Item>
<Compare.Item position="after"></Compare.Item>
<Compare.Handle>
<Compare.Indicator />
</Compare.Handle>
</Compare.Root>Behavior#
Polymorphic Rendering#
Use as on any sub-component to change the rendered HTML element.
<Compare.Root as="section">
<Compare.Item as="figure" position="before">
...
</Compare.Item>
<Compare.Item as="figure" position="after">
...
</Compare.Item>
<Compare.Handle as="div">
<Compare.Indicator as="div">...</Compare.Indicator>
</Compare.Handle>
</Compare.Root>Default elements: Root=div, Item=div, Handle=span, Indicator=span.
Render Function Children#
Indicator accepts a render function as children, providing access to the component instance. The instance exposes compare (root instance with state.value and state.isDragging).
<Compare.Indicator>{(instance) => <span>{instance.compare?.state.value}%</span>}</Compare.Indicator>Pass Through#
Some parts may not be visible in the preview depending on the component's current state.
Loading...
/* Select a part to see its CSS selector for custom styling */API#
CompareRoot#
| Name | Type | Default |
|---|---|---|
ref | Ref<unknown> | — |
| The reference to the component instance. | ||
pIf | boolean | true |
| Whether the component should be rendered. | ||
style | CSSProperties | ((instance?: CompareRootInstance) => CSSProperties) | — |
| The style to apply to the component. | ||
className | string | ((instance?: CompareRootInstance) => string) | — |
| The class name to apply to the component. | ||
as | string | number | bigint | boolean | ComponentClass<any, any> | FunctionComponent<any> | ReactElement<unknown, string | JSXElementConstructor<any>> | Iterable<ReactNode, any, any> | ReactPortal | Promise<AwaitedReactNode> | — |
| The component type to render. | ||
asChild | boolean | false |
| Whether the component should be rendered as a child component. | ||
instance | CompareRootInstance | — |
| The instance to pass to the component. | ||
pt | SafeRecord<CompareRootPassThrough> | — |
| The pass-through props to pass to the component. | ||
ptOptions | PassThroughOptions | — |
| The pass-through options to pass to the component. | ||
unstyled | boolean | — |
| Whether the component should be rendered without classes. | ||
dt | unknown | — |
| The design token to use for the component. | ||
styles | StylesOptions<ComponentInstance> | — |
| The styles to use for the component. | ||
render | (instance: CompareRootInstance) => ReactNode | — |
| The render function to render the component with instance access. | ||
children | any | — |
| The children to render. Accepts `React.ReactNode` for static content or a render function `(instance: I) => React.ReactNode` for instance access. Typed as `any` to avoid JSX type errors when used directly in templates. | ||
slideOnHover | boolean | — |
| Whether slider moves on hover. | ||
tabIndex | number | — |
| The tab index of the hidden input. | ||
ariaLabel | string | — |
| Establishes a string value that labels the component. | ||
ariaLabelledby | string | — |
| Establishes relationships between the component and label(s) where its value should be one or more element IDs. | ||
onFocus | (event: FocusEvent<HTMLInputElement>) => void | — |
| Callback fired when the hidden input is focused. | ||
onBlur | (event: FocusEvent<HTMLInputElement>) => void | — |
| Callback fired when the hidden input loses focus. | ||
value | number | number[] | — |
| Value of the component. | ||
defaultValue | number | number[] | — |
| The default value for the input when not controlled by `value` . | ||
min | number | 0 |
| Mininum boundary value. | ||
max | number | 100 |
| Maximum boundary value. | ||
orientation | "horizontal" | "vertical" | horizontal |
| Orientation of the slider. | ||
step | number | 1 |
| Step factor to increment/decrement the value. | ||
onValueChangeEnd | (event: useSliderChangeEvent) => void | — |
| Callback fired when the pointer interaction ends. | ||
disabled | boolean | false |
| Whether the component is disabled. | ||
readOnly | boolean | false |
| Whether the component is read-only. | ||
invalid | boolean | false |
| When present, it specifies that the element should be invalid. | ||
name | string | — |
| The name of the compare. | ||
inputId | string | — |
| The input id of the compare. | ||
inputStyle | CSSProperties | — |
| The input style of the compare. | ||
inputClassName | string | — |
| The input class name of the compare. | ||
onValueChange | (event: CompareRootChangeEvent) => void | — |
| Callback fired when the ToggleButton's pressed state changes. | ||
[key: string] | any | — |
pt-{optionName}-* | - | — |
| Pass through attributes for customizing component. For more info, see Pass Through tab. | ||
| Attribute | Value |
|---|---|
data-orientation | "horizontal" or "vertical" |
data-disabled | Present when disabled |
data-dragging | Present when dragging |
CompareItem#
| Name | Type | Default |
|---|---|---|
ref | Ref<unknown> | — |
| The reference to the component instance. | ||
pIf | boolean | true |
| Whether the component should be rendered. | ||
style | CSSProperties | ((instance?: CompareItemInstance) => CSSProperties) | — |
| The style to apply to the component. | ||
className | string | ((instance?: CompareItemInstance) => string) | — |
| The class name to apply to the component. | ||
as | string | number | bigint | boolean | ComponentClass<any, any> | FunctionComponent<any> | ReactElement<unknown, string | JSXElementConstructor<any>> | Iterable<ReactNode, any, any> | ReactPortal | Promise<AwaitedReactNode> | — |
| The component type to render. | ||
asChild | boolean | false |
| Whether the component should be rendered as a child component. | ||
instance | CompareItemInstance | — |
| The instance to pass to the component. | ||
pt | SafeRecord<CompareItemPassThrough> | — |
| The pass-through props to pass to the component. | ||
ptOptions | PassThroughOptions | — |
| The pass-through options to pass to the component. | ||
unstyled | boolean | — |
| Whether the component should be rendered without classes. | ||
dt | unknown | — |
| The design token to use for the component. | ||
styles | StylesOptions<ComponentInstance> | — |
| The styles to use for the component. | ||
render | (instance: CompareItemInstance) => ReactNode | — |
| The render function to render the component with instance access. | ||
children | any | — |
| The children to render. Accepts `React.ReactNode` for static content or a render function `(instance: I) => React.ReactNode` for instance access. Typed as `any` to avoid JSX type errors when used directly in templates. | ||
[key: string] | any | — |
pt-{optionName}-* | - | — |
| Pass through attributes for customizing component. For more info, see Pass Through tab. | ||
| Attribute | Value |
|---|---|
data-orientation | "horizontal" or "vertical" |
data-disabled | Present when disabled |
data-dragging | Present when dragging |
CompareHandle#
| Name | Type | Default |
|---|---|---|
ref | Ref<unknown> | — |
| The reference to the component instance. | ||
pIf | boolean | true |
| Whether the component should be rendered. | ||
style | CSSProperties | ((instance?: CompareHandleInstance) => CSSProperties) | — |
| The style to apply to the component. | ||
className | string | ((instance?: CompareHandleInstance) => string) | — |
| The class name to apply to the component. | ||
as | string | number | bigint | boolean | ComponentClass<any, any> | FunctionComponent<any> | ReactElement<unknown, string | JSXElementConstructor<any>> | Iterable<ReactNode, any, any> | ReactPortal | Promise<AwaitedReactNode> | — |
| The component type to render. | ||
asChild | boolean | false |
| Whether the component should be rendered as a child component. | ||
instance | CompareHandleInstance | — |
| The instance to pass to the component. | ||
pt | SafeRecord<CompareHandlePassThrough> | — |
| The pass-through props to pass to the component. | ||
ptOptions | PassThroughOptions | — |
| The pass-through options to pass to the component. | ||
unstyled | boolean | — |
| Whether the component should be rendered without classes. | ||
dt | unknown | — |
| The design token to use for the component. | ||
styles | StylesOptions<ComponentInstance> | — |
| The styles to use for the component. | ||
render | (instance: CompareHandleInstance) => ReactNode | — |
| The render function to render the component with instance access. | ||
children | any | — |
| The children to render. Accepts `React.ReactNode` for static content or a render function `(instance: I) => React.ReactNode` for instance access. Typed as `any` to avoid JSX type errors when used directly in templates. | ||
[key: string] | any | — |
pt-{optionName}-* | - | — |
| Pass through attributes for customizing component. For more info, see Pass Through tab. | ||
| Attribute | Value |
|---|---|
data-orientation | "horizontal" or "vertical" |
data-disabled | Present when disabled |
data-dragging | Present when dragging |
CompareIndicator#
| Name | Type | Default |
|---|---|---|
ref | Ref<unknown> | — |
| The reference to the component instance. | ||
pIf | boolean | true |
| Whether the component should be rendered. | ||
style | CSSProperties | ((instance?: CompareIndicatorInstance) => CSSProperties) | — |
| The style to apply to the component. | ||
className | string | ((instance?: CompareIndicatorInstance) => string) | — |
| The class name to apply to the component. | ||
as | string | number | bigint | boolean | ComponentClass<any, any> | FunctionComponent<any> | ReactElement<unknown, string | JSXElementConstructor<any>> | Iterable<ReactNode, any, any> | ReactPortal | Promise<AwaitedReactNode> | — |
| The component type to render. | ||
asChild | boolean | false |
| Whether the component should be rendered as a child component. | ||
instance | CompareIndicatorInstance | — |
| The instance to pass to the component. | ||
pt | SafeRecord<CompareIndicatorPassThrough> | — |
| The pass-through props to pass to the component. | ||
ptOptions | PassThroughOptions | — |
| The pass-through options to pass to the component. | ||
unstyled | boolean | — |
| Whether the component should be rendered without classes. | ||
dt | unknown | — |
| The design token to use for the component. | ||
styles | StylesOptions<ComponentInstance> | — |
| The styles to use for the component. | ||
render | (instance: CompareIndicatorInstance) => ReactNode | — |
| The render function to render the component with instance access. | ||
children | any | — |
| The children to render. Accepts `React.ReactNode` for static content or a render function `(instance: I) => React.ReactNode` for instance access. Typed as `any` to avoid JSX type errors when used directly in templates. | ||
[key: string] | any | — |
pt-{optionName}-* | - | — |
| Pass through attributes for customizing component. For more info, see Pass Through tab. | ||
| Attribute | Value |
|---|---|
data-orientation | "horizontal" or "vertical" |
data-disabled | Present when disabled |
data-dragging | Present when dragging |
Accessibility#
Screen Reader#
Compare component uses a native range slider internally. Value to describe the component can be defined using aria-labelledby and aria-label props.
<span id="compare_label">Compare Images</span>
<Compare.Root aria-labelledby="compare_label">...</Compare.Root>
<Compare.Root aria-label="Compare Images">...</Compare.Root>Keyboard Support#
| Key | Function |
|---|---|
tab | Moves focus to the component. |
left arrow / up arrow | Decrements the value. |
right arrow / down arrow | Increments the value. |
home | Set the minimum value. |
end | Set the maximum value. |
page up | Increments the value by 10 steps. |
page down | Decrements the value by 10 steps. |