Skip to content

Commit

Permalink
fix: apply z-index to itemOne container when clip is set to `'ite…
Browse files Browse the repository at this point in the history
…mOne'` to ensure both items are always visible #154
  • Loading branch information
nerdyman committed Nov 25, 2024
1 parent 888f6d2 commit 81c9c15
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 4 deletions.
9 changes: 9 additions & 0 deletions docs/storybook/content/stories/99-tests/clip.test.stories.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,9 @@ ClipBoth.play = async ({ canvasElement }) => {
const [itemOne, itemTwo] = Array.from(
sliderRoot.querySelectorAll('[data-rcs="clip-item"]'),
) as HTMLElement[];

expect(itemOne).toBeVisible();
expect(itemTwo).toBeVisible();
expect(itemOne?.style.clipPath).toBe('inset(0px 25% 0px 0px)');
expect(itemTwo?.style.clipPath).toBe('inset(0px 0px 0px 75%)');
});
Expand All @@ -66,6 +69,9 @@ ClipItemOne.play = async ({ canvasElement }) => {
const [itemOne, itemTwo] = Array.from(
sliderRoot.querySelectorAll('[data-rcs="clip-item"]'),
) as HTMLElement[];

expect(itemOne).toBeVisible();
expect(itemTwo).toBeVisible();
expect(itemOne?.style.clipPath).toBe('inset(0px 50% 0px 0px)');
expect(itemTwo?.style.clipPath).toBe('none');
});
Expand All @@ -83,6 +89,9 @@ ClipItemOne.play = async ({ canvasElement }) => {
const [itemOne, itemTwo] = Array.from(
sliderRoot.querySelectorAll('[data-rcs="clip-item"]'),
) as HTMLElement[];

expect(itemOne).toBeVisible();
expect(itemTwo).toBeVisible();
expect(itemOne?.style.clipPath).toBe('inset(0px 25% 0px 0px)');
expect(itemTwo?.style.clipPath).toBe('none');
});
Expand Down
10 changes: 7 additions & 3 deletions lib/src/Container.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,14 @@ import type { ReactCompareSliderCommonProps } from './types';

type ContainerItemProps = ComponentPropsWithoutRef<'div'> &
Pick<ReactCompareSliderCommonProps, 'transition'> & {
shouldOverlap?: boolean;
order?: number;
};

/** Container for clipped item. */
export const ContainerItem = forwardRef<HTMLDivElement, ContainerItemProps>(
({ transition, order, ...props }, ref): ReactElement => {
const style: CSSProperties = {
({ shouldOverlap, order, style, transition, ...props }, ref): ReactElement => {
const appliedStyle: CSSProperties = {
gridArea: '1 / 1 / 2 / 2',
order,
maxWidth: '100%',
Expand All @@ -20,12 +21,14 @@ export const ContainerItem = forwardRef<HTMLDivElement, ContainerItemProps>(
transition: transition ? `clip-path ${transition}` : undefined,
userSelect: 'none',
willChange: 'clip-path, transition',
zIndex: shouldOverlap ? 1 : undefined,
KhtmlUserSelect: 'none',
MozUserSelect: 'none',
WebkitUserSelect: 'none',
...style,
};

return <div {...props} style={style} data-rcs="clip-item" ref={ref} />;
return <div {...props} style={appliedStyle} data-rcs="clip-item" ref={ref} />;
},
);

Expand All @@ -50,6 +53,7 @@ export const ContainerHandle = forwardRef<HTMLButtonElement, ContainerHandleProp
appearance: 'none',
WebkitAppearance: 'none',
MozAppearance: 'none',
zIndex: 1,
outline: 0,
transform: portrait ? `translate3d(0, -50% ,0)` : `translate3d(-50%, 0, 0)`,
transition: transition ? `${targetAxis} ${transition}` : undefined,
Expand Down
6 changes: 5 additions & 1 deletion lib/src/ReactCompareSlider.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -385,7 +385,11 @@ export const ReactCompareSlider = forwardRef<

return (
<div {...props} ref={rootContainerRef} style={rootStyle} data-rcs="root">
<ContainerItem ref={clipContainerOneRef} transition={appliedTransition}>
<ContainerItem
ref={clipContainerOneRef}
transition={appliedTransition}
shouldOverlap={clip === ReactCompareSliderClipOption.itemOne}
>
{itemOne}
</ContainerItem>

Expand Down

0 comments on commit 81c9c15

Please sign in to comment.