Introducing PrimeReact v11-alpha 🎉Discover Now

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 clipPath calculation 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#

NameTypeDefault
refRef<unknown>—
The reference to the component instance.
pIfbooleantrue
Whether the component should be rendered.
styleCSSProperties | ((instance?: CompareRootInstance) => CSSProperties)—
The style to apply to the component.
classNamestring | ((instance?: CompareRootInstance) => string)—
The class name to apply to the component.
asstring | 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.
asChildbooleanfalse
Whether the component should be rendered as a child component.
instanceCompareRootInstance—
The instance to pass to the component.
ptSafeRecord<CompareRootPassThrough>—
The pass-through props to pass to the component.
ptOptionsPassThroughOptions—
The pass-through options to pass to the component.
unstyledboolean—
Whether the component should be rendered without classes.
dtunknown—
The design token to use for the component.
stylesStylesOptions<ComponentInstance>—
The styles to use for the component.
render(instance: CompareRootInstance) => ReactNode—
The render function to render the component with instance access.
childrenany—
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.
slideOnHoverboolean—
Whether slider moves on hover.
tabIndexnumber—
The tab index of the hidden input.
ariaLabelstring—
Establishes a string value that labels the component.
ariaLabelledbystring—
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.
valuenumber | number[]—
Value of the component.
defaultValuenumber | number[]—
The default value for the input when not controlled by `value` .
minnumber0
Mininum boundary value.
maxnumber100
Maximum boundary value.
orientation"horizontal" | "vertical"horizontal
Orientation of the slider.
stepnumber1
Step factor to increment/decrement the value.
onValueChangeEnd(event: useSliderChangeEvent) => void—
Callback fired when the pointer interaction ends.
disabledbooleanfalse
Whether the component is disabled.
readOnlybooleanfalse
Whether the component is read-only.
invalidbooleanfalse
When present, it specifies that the element should be invalid.
namestring—
The name of the compare.
inputIdstring—
The input id of the compare.
inputStyleCSSProperties—
The input style of the compare.
inputClassNamestring—
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.
AttributeValue
data-orientation"horizontal" or "vertical"
data-disabledPresent when disabled
data-draggingPresent when dragging

CompareItem#

NameTypeDefault
refRef<unknown>—
The reference to the component instance.
pIfbooleantrue
Whether the component should be rendered.
styleCSSProperties | ((instance?: CompareItemInstance) => CSSProperties)—
The style to apply to the component.
classNamestring | ((instance?: CompareItemInstance) => string)—
The class name to apply to the component.
asstring | 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.
asChildbooleanfalse
Whether the component should be rendered as a child component.
instanceCompareItemInstance—
The instance to pass to the component.
ptSafeRecord<CompareItemPassThrough>—
The pass-through props to pass to the component.
ptOptionsPassThroughOptions—
The pass-through options to pass to the component.
unstyledboolean—
Whether the component should be rendered without classes.
dtunknown—
The design token to use for the component.
stylesStylesOptions<ComponentInstance>—
The styles to use for the component.
render(instance: CompareItemInstance) => ReactNode—
The render function to render the component with instance access.
childrenany—
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.
AttributeValue
data-orientation"horizontal" or "vertical"
data-disabledPresent when disabled
data-draggingPresent when dragging

CompareHandle#

NameTypeDefault
refRef<unknown>—
The reference to the component instance.
pIfbooleantrue
Whether the component should be rendered.
styleCSSProperties | ((instance?: CompareHandleInstance) => CSSProperties)—
The style to apply to the component.
classNamestring | ((instance?: CompareHandleInstance) => string)—
The class name to apply to the component.
asstring | 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.
asChildbooleanfalse
Whether the component should be rendered as a child component.
instanceCompareHandleInstance—
The instance to pass to the component.
ptSafeRecord<CompareHandlePassThrough>—
The pass-through props to pass to the component.
ptOptionsPassThroughOptions—
The pass-through options to pass to the component.
unstyledboolean—
Whether the component should be rendered without classes.
dtunknown—
The design token to use for the component.
stylesStylesOptions<ComponentInstance>—
The styles to use for the component.
render(instance: CompareHandleInstance) => ReactNode—
The render function to render the component with instance access.
childrenany—
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.
AttributeValue
data-orientation"horizontal" or "vertical"
data-disabledPresent when disabled
data-draggingPresent when dragging

CompareIndicator#

NameTypeDefault
refRef<unknown>—
The reference to the component instance.
pIfbooleantrue
Whether the component should be rendered.
styleCSSProperties | ((instance?: CompareIndicatorInstance) => CSSProperties)—
The style to apply to the component.
classNamestring | ((instance?: CompareIndicatorInstance) => string)—
The class name to apply to the component.
asstring | 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.
asChildbooleanfalse
Whether the component should be rendered as a child component.
instanceCompareIndicatorInstance—
The instance to pass to the component.
ptSafeRecord<CompareIndicatorPassThrough>—
The pass-through props to pass to the component.
ptOptionsPassThroughOptions—
The pass-through options to pass to the component.
unstyledboolean—
Whether the component should be rendered without classes.
dtunknown—
The design token to use for the component.
stylesStylesOptions<ComponentInstance>—
The styles to use for the component.
render(instance: CompareIndicatorInstance) => ReactNode—
The render function to render the component with instance access.
childrenany—
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.
AttributeValue
data-orientation"horizontal" or "vertical"
data-disabledPresent when disabled
data-draggingPresent 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#

KeyFunction
tabMoves focus to the component.
left arrow / up arrowDecrements the value.
right arrow / down arrowIncrements the value.
homeSet the minimum value.
endSet the maximum value.
page upIncrements the value by 10 steps.
page downDecrements the value by 10 steps.