### Run Example Package in Development Mode Source: https://github.com/nerdyman/react-compare-slider/blob/main/docs/CONTRIBUTING.md Starts the example application in development mode. This showcases the usage of the library. ```sh # Run example only. pnpm run --filter @this/example dev ``` ```sh # You can also run the scripts from the directory itself if you prefer. cd docs/example pnpm run dev ``` -------------------------------- ### Install and Run Demo Source: https://github.com/nerdyman/react-compare-slider/blob/main/docs/example/README.md Use these commands to install project dependencies and start the local development server for the React Compare Slider demo. ```sh pnpm install pnpm dev ``` -------------------------------- ### Install React Compare Slider Source: https://github.com/nerdyman/react-compare-slider/blob/main/README.md Install the library using npm, yarn, or pnpm. ```sh npm install react-compare-slider # or yarn add react-compare-slider # or pnpm add react-compare-slider ``` -------------------------------- ### Custom Slider Example Source: https://github.com/nerdyman/react-compare-slider/blob/main/docs/storybook/content/02-components-api.mdx Demonstrates how to build a custom slider using the components provided by the library. This example utilizes the `CustomSlider` component, which is likely a pre-configured setup for demonstration purposes. ```javascript import { Canvas, Description, Meta, ArgTypes } from "@storybook/addon-docs/blocks"; import { Provider, Root, Item, Image, HandleRoot, Handle, } from "react-compare-slider/components"; import { useReactCompareSlider } from 'react-compare-slider/hooks'; import { CustomSlider } from './stories/00-demos/00-index.stories'; # Components API

**Note:** These components are automatically used by the pre-built `ReactCompareSlider` component. If you are using the `ReactCompareSlider` component you _should not_ use the components, other than `Handle`, should you want to customise the default handle.

You can build your own slider using the components exported from the `react-compare-slider/components` module. These components work similarly to libraries like Base UI and Radix. They give you full control over the components, state and event bindings and are used in conjunction with the `useReactCompareSlider` hook. ## Custom Slider Example ## Components ### `Provider` These props are returned by the `useReactCompareSlider` hook. ### `Root` Accepts all valid `div` component props. ### `Item` Accepts all valid `div` component props in addition to: ### `Image` Accepts all valid `img` component props. ### `HandleRoot` Accepts all valid `div` component props. ### `Handle` ``` -------------------------------- ### Bootstrap Monorepo with npm Source: https://github.com/nerdyman/react-compare-slider/blob/main/docs/CONTRIBUTING.md Run this command to set up the pnpm monorepo. Ensure you have the correct Node.js version installed. ```sh npm run bootstrap ``` -------------------------------- ### Run react-compare-slider Library in Watch Mode Source: https://github.com/nerdyman/react-compare-slider/blob/main/docs/CONTRIBUTING.md Starts the main library package in development watch mode. This command must be run before starting Storybook. ```sh # Run the library only, note this must run before starting Storybook. pnpm run --filter react-compare-slider dev ``` ```sh # You can also run the scripts from the directory itself if you prefer. cd lib pnpm run dev ``` -------------------------------- ### Custom Handle Component Example Source: https://github.com/nerdyman/react-compare-slider/blob/main/docs/storybook/content/handles.mdx This example demonstrates how to use a custom component for the slider handle. Ensure the custom component is a non-interactive element. ```jsx import { ReactCompareSlider, ReactCompareSliderHandle } from 'react-compare-slider'; My Custom Handle} />; ``` -------------------------------- ### Run Storybook in Development Mode Source: https://github.com/nerdyman/react-compare-slider/blob/main/docs/CONTRIBUTING.md Starts the Storybook development server. This is useful for developing and previewing UI components. ```sh # Run Storybook only. pnpm run --filter @this/storybook dev ``` ```sh # You can also run the scripts from the directory itself if you prefer. cd docs/storybook pnpm run dev ``` -------------------------------- ### Custom Standalone Handle Example Source: https://github.com/nerdyman/react-compare-slider/blob/main/docs/storybook/content/handles.mdx This example illustrates the usage of a custom component as a standalone handle. The custom component can be any valid React element. ```jsx import { ReactCompareSlider, ReactCompareSliderHandle } from 'react-compare-slider'; } />; ``` -------------------------------- ### Basic ReactCompareSlider Usage Source: https://context7.com/nerdyman/react-compare-slider/llms.txt Demonstrates the basic setup for `ReactCompareSlider` with required `itemOne` and `itemTwo` props. All other props are optional and control aspects like initial position, orientation, and transitions. ```tsx import { ReactCompareSlider, ReactCompareSliderImage } from 'react-compare-slider'; export const BasicExample = () => ( } itemTwo={} // Optional props with their defaults shown defaultPosition={50} // initial divider position (0–100) portrait={false} // true = vertical divider disabled={false} // freeze the handle changePositionOnHover={false} // move handle on hover instead of drag onlyHandleDraggable={false} // restrict drag to the handle element only boundsPadding="0px" // CSS unit; keeps handle away from edges keyboardIncrement="5%" // step size per arrow-key press transition="0.15s ease-out" // CSS transition shorthand for handle movement clip="all" // 'all' | 'itemOne' | 'itemTwo' onPositionChange={(pos) => console.log('position:', pos)} style={{ width: '100%', height: '60vh' }} /> ); ``` -------------------------------- ### StyleFitContainer Example Source: https://github.com/nerdyman/react-compare-slider/blob/main/docs/storybook/content/01-api.mdx Use the styleFitContainer utility to make an element fill its parent while maintaining aspect ratio. Defaults to object-fit: cover. ```jsx import { styleFitContainer } from "react-compare-slider";
; ``` -------------------------------- ### Set Bounds Padding Source: https://github.com/nerdyman/react-compare-slider/blob/main/docs/storybook/content/bounds-padding.mdx Use the `boundsPadding` prop to restrict the slider's movement. This example sets the padding to 5% of the slider's dimension. ```jsx boundsPadding="5%" ``` -------------------------------- ### Inherited Color Handle Example Source: https://github.com/nerdyman/react-compare-slider/blob/main/docs/storybook/content/handles.mdx This example shows how the colors of the ReactCompareSliderHandle are inherited from the CSS variable --rcs-handle-color. No specific imports are needed beyond the main component. ```jsx import { ReactCompareSlider, ReactCompareSliderHandle } from 'react-compare-slider'; } />; ``` -------------------------------- ### UseReactCompareSliderContext Example Source: https://github.com/nerdyman/react-compare-slider/blob/main/docs/storybook/content/01-api.mdx Access context from useReactCompareSliderContext within a component rendered inside ReactCompareSlider. Allows controlling position and accessing portrait mode. ```tsx import { useReactCompareSliderContext } from "react-compare-slider"; /** Custom component rendered within `Provider` or `ReactCompareSlider`. */ const CustomItem = () => { const { setPosition, portrait } = useReactCompareSliderContext(); return (

Is portrait: {portrait.toString()}

); }; ``` -------------------------------- ### Construct Custom Slider with React Compare Slider Source: https://github.com/nerdyman/react-compare-slider/blob/main/README.md Use `useReactCompareSlider` hook to access state and event bindings for building a custom slider. This example demonstrates a portrait orientation slider. ```tsx import * as Slider from 'react-compare-slider/components'; import { useReactCompareSlider } from 'react-compare-slider/hooks'; export const Example = () => { // Access to state and event bindings of the slider. const sliderProps = useReactCompareSlider({ portrait: true }); return ( }; ``` -------------------------------- ### Run Tests in CI Mode Source: https://github.com/nerdyman/react-compare-slider/blob/main/docs/CONTRIBUTING.md Executes the test suite in a mode suitable for Continuous Integration environments. Use this if Storybook is not running. ```sh pnpm run test:ci ``` -------------------------------- ### Run Tests with Storybook Running Source: https://github.com/nerdyman/react-compare-slider/blob/main/docs/CONTRIBUTING.md Executes the test suite when Storybook is already running. This is typically used during development. ```sh pnpm run test ``` -------------------------------- ### Build Custom Slider with Low-Level Components and Hooks Source: https://context7.com/nerdyman/react-compare-slider/llms.txt Compose primitive components and use the `useReactCompareSlider` hook to build a fully custom slider. This approach mirrors patterns used by libraries like Radix UI or Base UI. Override event handlers by always calling the original function after your custom logic. ```tsx import * as Slider from 'react-compare-slider/components'; import { useReactCompareSlider } from 'react-compare-slider/hooks'; import { useState, useCallback } from 'react'; export const FullyCustomSlider = () => { const [isDraggingLabel, setIsDraggingLabel] = useState('idle'); // Destructure all state + event handlers from the hook. const { isDragging, canTransition, onPointerDown, onPointerUp, setPosition, setPositionFromBounds, rootRef, ...sliderProps } = useReactCompareSlider({ defaultPosition: 50, portrait: false, transition: '0.2s ease-out', keyboardIncrement: '10%', onPositionChange: (pos) => console.log('pos:', pos), }); // Override pointer events while keeping original behaviour. const customPointerDown: typeof onPointerDown = useCallback( (ev) => { setIsDraggingLabel('dragging'); onPointerDown(ev); // always call the original }, [onPointerDown], ); const customPointerUp: typeof onPointerUp = useCallback( (ev) => { setIsDraggingLabel('idle'); onPointerUp(ev); }, [onPointerUp], ); return (

Status: {isDraggingLabel}

); }; ``` -------------------------------- ### Animated Position Changes with CSS Transition Source: https://context7.com/nerdyman/react-compare-slider/llms.txt Use the `transition` prop to animate handle movement. This prop accepts a CSS transition shorthand. It defaults to 'none' for users preferring reduced motion. ```tsx import { useEffect, useState } from 'react'; import { ReactCompareSlider, ReactCompareSliderImage } from 'react-compare-slider'; export const AnimatedSlider = () => { const [position, setPosition] = useState(50); // Auto-sweep on mount useEffect(() => { const steps = [10, 90, 50]; let i = 0; const id = setInterval(() => { setPosition(steps[i % steps.length]!); i++; if (i >= steps.length) clearInterval(id); }, 600); return () => clearInterval(id); }, []); return ( } itemTwo={} style={{ width: '100%', height: '500px' }} /> ); }; ``` -------------------------------- ### Image Comparison with ReactCompareSliderImage Source: https://context7.com/nerdyman/react-compare-slider/llms.txt Shows how to use `ReactCompareSliderImage` for comparing images, including `srcSet` for responsive images and overriding default object fit styles. ```tsx import { ReactCompareSlider, ReactCompareSliderImage } from 'react-compare-slider'; export const ImageComparison = () => ( } itemTwo={ } style={{ width: '100%', height: '500px' }} /> ); ``` -------------------------------- ### Basic Image Comparison with React Compare Slider Source: https://github.com/nerdyman/react-compare-slider/blob/main/README.md Demonstrates basic usage of ReactCompareSlider with ReactCompareSliderImage for comparing two images. Ensure you provide valid 'src' and 'srcSet' attributes for the images. ```tsx import { ReactCompareSlider, ReactCompareSliderImage } from 'react-compare-slider'; export const Example = () => { return ( } itemTwo={} /> ); }; ``` -------------------------------- ### Custom Handle Usage Source: https://github.com/nerdyman/react-compare-slider/blob/main/docs/storybook/content/handles.mdx Demonstrates how to use custom components as handles for the React Compare Slider. Custom handles can be passed via the 'handle' prop on the main slider component. ```APIDOC ## Custom Handle Usage ### Description Custom Handles can be used via the `handle` prop on the main slider component. If a custom `handle` is not supplied `ReactCompareSliderHandle` will be used instead. **Note**: You should use non-interactive elements (e.g., `div`, `span`) for custom `handle` components as they are announced as slider controls to screen readers. ``` -------------------------------- ### Support iframe/Popup with `browsingContext` Source: https://context7.com/nerdyman/react-compare-slider/llms.txt When rendering inside an iframe or popup window, pass the child `window` as `browsingContext` to ensure pointer events are captured correctly. This is crucial for maintaining slider functionality across different browsing contexts. ```tsx import { useState } from 'react'; import { createPortal } from 'react-dom'; import { ReactCompareSlider, ReactCompareSliderImage } from 'react-compare-slider'; export const IframeExample = () => { const [popupCtx, setPopupCtx] = useState(null); return (
{popupCtx && createPortal( } itemTwo={} style={{ width: '100%', height: '100%' }} />, popupCtx.document.body, )}
); }; ``` -------------------------------- ### Import CSS Variables Source: https://github.com/nerdyman/react-compare-slider/blob/main/docs/storybook/content/01-api.mdx Import the ReactCompareSliderCssVars object to reference CSS variables in your code. ```tsx import { ReactCompareSliderCssVars } from "react-compare-slider"; ``` -------------------------------- ### ReactCompareSliderImage Standard Usage Source: https://github.com/nerdyman/react-compare-slider/blob/main/docs/storybook/content/images.mdx Demonstrates the standard way to use the ReactCompareSliderImage component with src and srcSet props. ```jsx import { ReactCompareSliderImage } from 'react-compare-slider'; /** Standard usage. */ ``` -------------------------------- ### Apply CSS Transition to Slider Source: https://github.com/nerdyman/react-compare-slider/blob/main/docs/storybook/content/transition.mdx Use the `transition` prop to apply a CSS transition effect when the slider is moved. The value should be a CSS transition shorthand string, like `"0.25s cubic-bezier(.17,.67,.83,.67)"`. The transition is optimized for `pointerdown` events for immediate feedback during drag. ```jsx ``` -------------------------------- ### Hover Mode with `changePositionOnHover` Source: https://context7.com/nerdyman/react-compare-slider/llms.txt Enable hover mode by setting the `changePositionOnHover` prop. The slider will track the pointer position when hovered, eliminating the need for click-and-drag interaction. ```tsx import { ReactCompareSlider, ReactCompareSliderImage } from 'react-compare-slider'; export const HoverSlider = () => ( } itemTwo={} style={{ width: '100%', height: '500px' }} /> ); ``` -------------------------------- ### Keyboard Increment by Percentage Source: https://github.com/nerdyman/react-compare-slider/blob/main/docs/storybook/content/keyboard-increment.mdx Use a percentage string (e.g., '20%') for relative adjustments based on the slider's width or height. This ensures consistent behavior across different resolutions. ```jsx import { ReactCompareSlider } from 'react-compare-slider'; ``` -------------------------------- ### CSS Variables Source: https://github.com/nerdyman/react-compare-slider/blob/main/docs/storybook/content/01-api.mdx Provides access to CSS variables for styling the slider components. ```APIDOC ## CSS Variables ### Description The library exports the `ReactCompareSliderCssVars` object to easily reference the CSS variables in code. ### Usage ```tsx import { ReactCompareSliderCssVars } from "react-compare-slider"; ``` ### CSS Variables Values (Refer to the JSON output for specific variable values) ``` -------------------------------- ### Root Component Source: https://github.com/nerdyman/react-compare-slider/blob/main/docs/storybook/content/02-components-api.mdx The Root component serves as the main container for the slider. It accepts all valid div component props. ```APIDOC ## Root ### Description The Root component. Accepts all valid `div` component props. ``` -------------------------------- ### styleFitContainer Utility Source: https://github.com/nerdyman/react-compare-slider/blob/main/docs/storybook/content/01-api.mdx A utility function that returns a React style object to make an element fill its parent while maintaining aspect ratio. ```APIDOC ## styleFitContainer ### Description The `styleFitContainer` utility makes any [replaced element](https://developer.mozilla.org/en-US/docs/Web/CSS/Replaced_element) fill its parent while maintaining the correct aspect ratio. It returns a React `style` object and accepts a CSS object as an argument and defaults to `object-fit` to `cover`. ### Example ```jsx import { styleFitContainer } from "react-compare-slider";
; ``` ``` -------------------------------- ### Position Change Callback with `onPositionChange` Source: https://context7.com/nerdyman/react-compare-slider/llms.txt Utilize the `onPositionChange` prop to receive the current slider position (0-100) on every update. This is useful for synchronizing external state or displaying the current position. ```tsx import { useState, useCallback } from 'react'; import { ReactCompareSlider, ReactCompareSliderImage } from 'react-compare-slider'; export const PositionDisplay = () => { const [position, setPosition] = useState(50); const handlePositionChange = useCallback((pos: number) => { setPosition(Math.round(pos)); }, []); return (

Current position: {position}%

} itemTwo={} style={{ width: '100%', height: '500px' }} />
); }; ``` -------------------------------- ### Fitting Replaced Elements with styleFitContainer Source: https://context7.com/nerdyman/react-compare-slider/llms.txt The `styleFitContainer` utility generates a style object to make replaced elements like `img` or `video` fill their parent container. It's used internally by `ReactCompareSliderImage` but can be used independently. ```tsx import { ReactCompareSlider, styleFitContainer } from 'react-compare-slider'; export const VideoComparison = () => ( } itemTwo={