The Interactive component is a wrapper used to capture mouse or touch coordinates relative to its own bounding box. This is useful for building custom color selection areas where you need to map user interaction (clicks/drags) to specific X and Y coordinates within a container.
Props
| Prop | Type | Description |
|---|
onCoordinateChange | (final: boolean, x: number, y: number) => void | Callback triggered on move or end. final is true when the interaction ends (e.g., mouseup or touchend), and false during movement. x and y are clamped coordinates within the component's width and height. |
children | React.ReactNode | The content to be rendered inside the interactive area. |
disabled | boolean (optional) | If true, sets aria-disabled on the container. Note that the component logic currently handles interaction via event listeners attached to the document, so ensure you manage the disabled state in your own event handlers if you wish to prevent movement. |
import { Interactive } from 'react-color-palette';
// Example usage within a custom color picker component
const MyCustomPicker = () => {
const handleCoordinateChange = (isFinal: boolean, x: number, y: number) => {
console.log(`Interaction: ${isFinal ? 'Ended' : 'Moving'} at x: ${x}, y: ${y}`);
// Map x/y to a color value here
};
return (
<Interactive onCoordinateChange={handleCoordinateChange}>
<div style={{ width: 300, height: 300, background: 'linear-gradient(to right, red, blue)' }} />
</Interactive>
);
};