Skip to content

Commit

Permalink
feat: Revers Slider behavior on Slide (#70)
Browse files Browse the repository at this point in the history
  • Loading branch information
t0yohei authored Dec 8, 2024
1 parent d4b04f2 commit a44a5bd
Show file tree
Hide file tree
Showing 5 changed files with 23 additions and 24 deletions.
1 change: 0 additions & 1 deletion src/components/Slider/Slider.css.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import { tokens } from '../../styles/variables.css';
const barStyles = {
position: 'absolute',
top: '50%',
left: 0,
marginTop: -2,
height: 4,
borderRadius: 2,
Expand Down
1 change: 1 addition & 0 deletions src/components/Slider/Slider.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ export type Props = {
value?: number;
defaultValue?: number;
onChange?: (value: number) => void;
reverse?: boolean;
};

export const Slider = ({ onChange, ...rest }: Props) => (
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -146,6 +146,7 @@ export const ComparisonView = ({
step={1}
value={slideValue}
onChange={handleSlideChange}
reverse={true}
/>
</span>
<span className={typography.subTitle3}>After</span>
Expand Down
6 changes: 2 additions & 4 deletions src/components/Viewer/internal/ComparisonView/Slide.css.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,14 +40,14 @@ const view = style({
transform: 'translate(-50%, 0)',
});

export const before = style([
export const after = style([
view,
{
zIndex: 0,
},
]);

export const after = style([
export const before = style([
view,
{
zIndex: 1,
Expand All @@ -59,8 +59,6 @@ export const handle = style({
position: 'absolute',
top: 0,
bottom: 0,
marginLeft: -22,
width: 44,
zIndex: 5,
cursor: 'ew-resize',
});
Expand Down
38 changes: 19 additions & 19 deletions src/components/Viewer/internal/ComparisonView/Slide.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ export const Slide = ({ before, after, value, matching, onChange }: Props) => {
const rangeRef = useRef<HTMLInputElement>(null);

const handleChange = (e: React.ChangeEvent<HTMLInputElement>) => {
onChange(parseInt(e.target.value, 10));
onChange(100 - parseInt(e.target.value, 10));
};

useEffect(() => {
Expand All @@ -33,7 +33,7 @@ export const Slide = ({ before, after, value, matching, onChange }: Props) => {
const { left } = inner.getBoundingClientRect();
const x = Math.min(Math.max(0, pageX - left), image.width);

onChange((x / image.width) * 100);
onChange(100 - (x / image.width) * 100);
};

const handleMousedown = (e: MouseEvent | TouchEvent) => {
Expand Down Expand Up @@ -89,40 +89,40 @@ export const Slide = ({ before, after, value, matching, onChange }: Props) => {
/>

<div
className={styles.before}
className={styles.after}
style={{
width: image.before.width,
height: image.before.height,
width: image.after.width,
height: image.after.height,
}}
>
<Image
ref={image.before.ref}
src={before}
onLoad={image.before.handleLoad}
ref={image.after.ref}
src={after}
onLoad={image.after.handleLoad}
/>
<Markers variant="before" matching={matching} />
<Markers variant="after" matching={matching} />
</div>

<div className={styles.frame} style={{ width: `${value}%` }}>
<div className={styles.frame} style={{ width: `${100 - value}%` }}>
<div
className={styles.after}
className={styles.before}
style={{
top: 0,
left: canvas.width / 2 - image.after.width / 2,
width: image.after.width,
height: image.after.height,
left: canvas.width / 2 - image.before.width / 2,
width: image.before.width,
height: image.before.height,
}}
>
<Image
ref={image.after.ref}
src={after}
onLoad={image.after.handleLoad}
ref={image.before.ref}
src={before}
onLoad={image.before.handleLoad}
/>
<Markers variant="after" matching={matching} />
<Markers variant="before" matching={matching} />
</div>
</div>

<span className={styles.handle} style={{ left: `${value}%` }}>
<span className={styles.handle} style={{ right: `${value}%` }}>
<span className={styles.handleBar} />
</span>
</div>
Expand Down

0 comments on commit a44a5bd

Please sign in to comment.