Skip to content

Commit 81c9c15

Browse files
committed
fix: apply z-index to itemOne container when clip is set to 'itemOne' to ensure both items are always visible #154
1 parent 888f6d2 commit 81c9c15

File tree

3 files changed

+21
-4
lines changed

3 files changed

+21
-4
lines changed

docs/storybook/content/stories/99-tests/clip.test.stories.tsx

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,9 @@ ClipBoth.play = async ({ canvasElement }) => {
4444
const [itemOne, itemTwo] = Array.from(
4545
sliderRoot.querySelectorAll('[data-rcs="clip-item"]'),
4646
) as HTMLElement[];
47+
48+
expect(itemOne).toBeVisible();
49+
expect(itemTwo).toBeVisible();
4750
expect(itemOne?.style.clipPath).toBe('inset(0px 25% 0px 0px)');
4851
expect(itemTwo?.style.clipPath).toBe('inset(0px 0px 0px 75%)');
4952
});
@@ -66,6 +69,9 @@ ClipItemOne.play = async ({ canvasElement }) => {
6669
const [itemOne, itemTwo] = Array.from(
6770
sliderRoot.querySelectorAll('[data-rcs="clip-item"]'),
6871
) as HTMLElement[];
72+
73+
expect(itemOne).toBeVisible();
74+
expect(itemTwo).toBeVisible();
6975
expect(itemOne?.style.clipPath).toBe('inset(0px 50% 0px 0px)');
7076
expect(itemTwo?.style.clipPath).toBe('none');
7177
});
@@ -83,6 +89,9 @@ ClipItemOne.play = async ({ canvasElement }) => {
8389
const [itemOne, itemTwo] = Array.from(
8490
sliderRoot.querySelectorAll('[data-rcs="clip-item"]'),
8591
) as HTMLElement[];
92+
93+
expect(itemOne).toBeVisible();
94+
expect(itemTwo).toBeVisible();
8695
expect(itemOne?.style.clipPath).toBe('inset(0px 25% 0px 0px)');
8796
expect(itemTwo?.style.clipPath).toBe('none');
8897
});

lib/src/Container.tsx

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,13 +5,14 @@ import type { ReactCompareSliderCommonProps } from './types';
55

66
type ContainerItemProps = ComponentPropsWithoutRef<'div'> &
77
Pick<ReactCompareSliderCommonProps, 'transition'> & {
8+
shouldOverlap?: boolean;
89
order?: number;
910
};
1011

1112
/** Container for clipped item. */
1213
export const ContainerItem = forwardRef<HTMLDivElement, ContainerItemProps>(
13-
({ transition, order, ...props }, ref): ReactElement => {
14-
const style: CSSProperties = {
14+
({ shouldOverlap, order, style, transition, ...props }, ref): ReactElement => {
15+
const appliedStyle: CSSProperties = {
1516
gridArea: '1 / 1 / 2 / 2',
1617
order,
1718
maxWidth: '100%',
@@ -20,12 +21,14 @@ export const ContainerItem = forwardRef<HTMLDivElement, ContainerItemProps>(
2021
transition: transition ? `clip-path ${transition}` : undefined,
2122
userSelect: 'none',
2223
willChange: 'clip-path, transition',
24+
zIndex: shouldOverlap ? 1 : undefined,
2325
KhtmlUserSelect: 'none',
2426
MozUserSelect: 'none',
2527
WebkitUserSelect: 'none',
28+
...style,
2629
};
2730

28-
return <div {...props} style={style} data-rcs="clip-item" ref={ref} />;
31+
return <div {...props} style={appliedStyle} data-rcs="clip-item" ref={ref} />;
2932
},
3033
);
3134

@@ -50,6 +53,7 @@ export const ContainerHandle = forwardRef<HTMLButtonElement, ContainerHandleProp
5053
appearance: 'none',
5154
WebkitAppearance: 'none',
5255
MozAppearance: 'none',
56+
zIndex: 1,
5357
outline: 0,
5458
transform: portrait ? `translate3d(0, -50% ,0)` : `translate3d(-50%, 0, 0)`,
5559
transition: transition ? `${targetAxis} ${transition}` : undefined,

lib/src/ReactCompareSlider.tsx

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -385,7 +385,11 @@ export const ReactCompareSlider = forwardRef<
385385

386386
return (
387387
<div {...props} ref={rootContainerRef} style={rootStyle} data-rcs="root">
388-
<ContainerItem ref={clipContainerOneRef} transition={appliedTransition}>
388+
<ContainerItem
389+
ref={clipContainerOneRef}
390+
transition={appliedTransition}
391+
shouldOverlap={clip === ReactCompareSliderClipOption.itemOne}
392+
>
389393
{itemOne}
390394
</ContainerItem>
391395

0 commit comments

Comments
 (0)