Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

perf: carousel #1098

Merged
merged 3 commits into from
May 15, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
48 changes: 27 additions & 21 deletions packages/zarm/src/carousel/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -68,8 +68,6 @@
moveTimeSpan,
} = props;

const [isDragging, setIsDragging] = useState(false);

const stateRef = useRef<StateProps>({
activeIndex: propActiveIndex!,
activeIndexChanged: false,
Expand Down Expand Up @@ -135,7 +133,10 @@
[isVertical],
);

const onMoving = useRef(false);

Check warning on line 136 in packages/zarm/src/carousel/index.tsx

View check run for this annotation

Codecov / codecov/patch

packages/zarm/src/carousel/index.tsx#L136

Added line #L136 was not covered by tests

const transitionEnd = useCallback(() => {
onMoving.current = false;

Check warning on line 139 in packages/zarm/src/carousel/index.tsx

View check run for this annotation

Codecov / codecov/patch

packages/zarm/src/carousel/index.tsx#L139

Added line #L139 was not covered by tests
const { activeIndex, activeIndexChanged } = stateRef.current;
const dom = carouselItemsRef.current;
const index = loop ? activeIndex + 1 : activeIndex;
Expand Down Expand Up @@ -199,25 +200,32 @@
onJumpTo(stateRef.current.activeIndex);
}, [onJumpTo]);

const intervalRef = useRef<number>();

Check warning on line 203 in packages/zarm/src/carousel/index.tsx

View check run for this annotation

Codecov / codecov/patch

packages/zarm/src/carousel/index.tsx#L203

Added line #L203 was not covered by tests

const bind = useDrag(
(state) => {
if (!state.intentional) return false;
setIsDragging(true);
let { activeIndex } = stateRef.current;
if (onMoving.current) {
if (activeIndex <= 0) {
onJumpTo(0);

Check warning on line 210 in packages/zarm/src/carousel/index.tsx

View check run for this annotation

Codecov / codecov/patch

packages/zarm/src/carousel/index.tsx#L210

Added line #L210 was not covered by tests
} else if (activeIndex >= count - 1) {
onJumpTo(count - 1);

Check warning on line 212 in packages/zarm/src/carousel/index.tsx

View check run for this annotation

Codecov / codecov/patch

packages/zarm/src/carousel/index.tsx#L212

Added line #L212 was not covered by tests
}
onMoving.current = false;

Check warning on line 214 in packages/zarm/src/carousel/index.tsx

View check run for this annotation

Codecov / codecov/patch

packages/zarm/src/carousel/index.tsx#L214

Added line #L214 was not covered by tests
}
if (!state.intentional) {
return false;

Check warning on line 217 in packages/zarm/src/carousel/index.tsx

View check run for this annotation

Codecov / codecov/patch

packages/zarm/src/carousel/index.tsx#L217

Added line #L217 was not covered by tests
};
intervalRef.current && window.clearInterval(intervalRef.current);

const { offset, elapsedTime } = state;

const [offsetX, offsetY] = offset;
const index = isVertical ? 1 : 0;
if (!offset[index]) {
return false;
}
if (loop) {
if (activeIndex <= 0) {
onJumpTo(0);
} else if (activeIndex >= count - 1) {
onJumpTo(count - 1);
}
}

const action = (!isVertical && offsetX > 0) || (isVertical && offsetY > 0) ? 'prev' : 'next';
if (
!loop &&
Expand All @@ -237,8 +245,10 @@
if (ratio >= moveDistanceRatio! || elapsedTime <= moveTimeSpan!) {
activeIndex = action === 'next' ? activeIndex + 1 : activeIndex - 1;
}
if (loop && (activeIndex >= count - 1 || activeIndex <= 1)) {
onMoving.current = true;

Check warning on line 249 in packages/zarm/src/carousel/index.tsx

View check run for this annotation

Codecov / codecov/patch

packages/zarm/src/carousel/index.tsx#L249

Added line #L249 was not covered by tests
}
onSlideTo(activeIndex);
setIsDragging(false);
return false;
}
doTransition(
Expand All @@ -257,18 +267,14 @@
);

useEffect(() => {
if (!autoPlay || isDragging) return;
const interval = window.setInterval(() => {
if (!loop && stateRef.current.activeIndex === count) {
interval && clearInterval(interval);
return false;
}
onSlideTo(stateRef.current.activeIndex + 1);
if (!autoPlay || count <= 1) return;
intervalRef.current = window.setInterval(() => {

Check warning on line 271 in packages/zarm/src/carousel/index.tsx

View check run for this annotation

Codecov / codecov/patch

packages/zarm/src/carousel/index.tsx#L271

Added line #L271 was not covered by tests
!onMoving.current && onSlideTo(stateRef.current.activeIndex + 1);
}, autoPlayIntervalTime);
return () => {
window.clearInterval(interval);
window.clearInterval(intervalRef.current);

Check warning on line 275 in packages/zarm/src/carousel/index.tsx

View check run for this annotation

Codecov / codecov/patch

packages/zarm/src/carousel/index.tsx#L275

Added line #L275 was not covered by tests
};
}, [isDragging, autoPlay, autoPlayIntervalTime, loop, onSlideTo]);
}, [autoPlay, autoPlayIntervalTime, loop, onSlideTo, stateRef.current.activeIndex]);

useEffect(() => {
// 监听窗口变化
Expand Down
3 changes: 2 additions & 1 deletion packages/zarm/src/carousel/style/component.scss
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,11 @@

position: relative;
overflow: hidden;
touch-action: none;

@include e(items) {
display: flex;
transform-style: preserve-3d;
// transform-style: preserve-3d;
transition-property: transform;
}

Expand Down