diff --git a/src/ReactCompareImage.tsx b/src/ReactCompareImage.tsx index 899ff37..f1fd02a 100644 --- a/src/ReactCompareImage.tsx +++ b/src/ReactCompareImage.tsx @@ -65,6 +65,7 @@ const ReactCompareImage: React.FC = props => { const horizontal = !vertical; // 0 to 1 + const [sliderPosition, setSliderPosition] = useState( sliderPositionPercentage, ); @@ -78,6 +79,11 @@ const ReactCompareImage: React.FC = props => { const rightImageRef = useRef(null); const leftImageRef = useRef(null); + // control the slider position from outside + useEffect(() => { + setSliderPosition(sliderPositionPercentage); + }, [sliderPositionPercentage]) + // make the component responsive useEffect(() => { const containerElement = containerRef.current; diff --git a/stories/index.stories.tsx b/stories/index.stories.tsx index bfbbb18..7a188cd 100644 --- a/stories/index.stories.tsx +++ b/stories/index.stories.tsx @@ -52,6 +52,24 @@ storiesOf('Basic', module) /> )) + .add('controlled slider position', () => { + const [position, setPosition] = useState(0); + const [value, setValue] = useState(0); + return ( +
+
+ setValue(e.target.value)} /> + +
+ +
slider position: {position}
+
+ ); + }) .add('detect slider position change', () => { const [position, setPosition] = useState(null); return (