Skip to content

Commit

Permalink
fix: swiper adjust offset on window resize
Browse files Browse the repository at this point in the history
  • Loading branch information
2wheeh committed May 9, 2024
1 parent d4b5419 commit 89724a3
Showing 1 changed file with 15 additions and 1 deletion.
16 changes: 15 additions & 1 deletion ui/src/hooks/common/use-swiper.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
'use client';

import { useRef, useState } from 'react';
import { useEffect, useRef, useState } from 'react';

import { useStateRef } from '@/hooks/common/use-state-ref';

Expand Down Expand Up @@ -144,6 +144,20 @@ export function useSwiper() {
setOffsetX(-(containerWidthRef.current * idx));
};

useEffect(() => {
const resizeHandler = () => {
const containerEl = containerRef.current!;
const containerWidth = containerEl.offsetWidth;

containerEl.classList.add('transition-none');
setOffsetX(-(containerWidth * currentIdx));
containerEl.classList.remove('transition-none');
};

window.addEventListener('resize', resizeHandler);
return () => window.removeEventListener('resize', resizeHandler);
}, [currentIdx, setOffsetX]);

return {
offsetX,
onTouchStart,
Expand Down

0 comments on commit 89724a3

Please sign in to comment.