Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions src/ReactCompareImage.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@ const ReactCompareImage: React.FC<IProps> = props => {
const horizontal = !vertical;

// 0 to 1

const [sliderPosition, setSliderPosition] = useState<number>(
sliderPositionPercentage,
);
Expand All @@ -78,6 +79,11 @@ const ReactCompareImage: React.FC<IProps> = 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;
Expand Down
18 changes: 18 additions & 0 deletions stories/index.stories.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,24 @@ storiesOf('Basic', module)
/>
</div>
))
.add('controlled slider position', () => {
const [position, setPosition] = useState<number|string>(0);
const [value, setValue] = useState<number|string>(0);
return (
<div style={{ maxWidth: '640px' }}>
<div style={{display: 'flex', gridGap: '5px'}} >
<input value={value} placeholder="0 to 1" onChange={(e) => setValue(e.target.value)} />
<button onClick={() => setPosition(value || 0)}>Override</button>
</div>
<ReactCompareImage
leftImage={img1Src}
rightImage={img2Src}
sliderPositionPercentage={position as number || 0}
/>
<div>slider position: {position}</div>
</div>
);
})
.add('detect slider position change', () => {
const [position, setPosition] = useState(null);
return (
Expand Down