-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: 공용 swiper 모듈이 여러개 일때도 사용할수 있도록 구현
- Loading branch information
Showing
6 changed files
with
97 additions
and
36 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,4 @@ | ||
export const customEvents = { | ||
SWIPER_REAL_INDEX_CHANGE: 'SWIPER_REAL_INDEX_CHANGE', | ||
SWIPER_INIT: 'SWIPER_INIT', | ||
} as const; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,18 +1,36 @@ | ||
import { useState } from 'react'; | ||
import { useEffect, useState } from 'react'; | ||
import Swiper from 'swiper'; | ||
import { ContextValueOceanSwiper } from '@/components/OceanSwiper/OceanSwiper.context'; | ||
import { customEvents } from '@/const/customEvents'; | ||
import { getStaticContext } from '@/utils/context/StaticContext'; | ||
import { eventManager } from '@/utils/eventManager'; | ||
import useEventListener from './useEventListener'; | ||
|
||
export const useOceanSwiperIndex = () => { | ||
const { swiperWrapperRef } = getStaticContext(ContextValueOceanSwiper); | ||
const [readIndex, setIndex] = useState(0); | ||
|
||
useEventListener<CustomEvent<Swiper>>( | ||
typeof window !== 'undefined' ? window : undefined, | ||
const { add, remove } = eventManager( | ||
swiperWrapperRef, | ||
customEvents.SWIPER_REAL_INDEX_CHANGE, | ||
(e) => { | ||
setIndex(e.detail.realIndex); | ||
setIndex((e as CustomEvent<Swiper>).detail.realIndex); | ||
}, | ||
); | ||
|
||
useEventListener<CustomEvent<HTMLDivElement>>( | ||
typeof window !== 'undefined' ? window : null, | ||
customEvents.SWIPER_INIT, | ||
(e) => { | ||
if (e.detail === swiperWrapperRef.current) add(); | ||
}, | ||
); | ||
|
||
useEffect(() => { | ||
return () => { | ||
remove(); | ||
}; | ||
}, []); | ||
|
||
return { readIndex }; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
export const eventManager = <T extends Event>( | ||
target: { | ||
current: EventTarget | null; | ||
}, | ||
event: string, | ||
callback: (event: T) => void, | ||
options?: boolean | AddEventListenerOptions, | ||
) => { | ||
const eventListener: EventListener = (event: Event) => { | ||
return callback(event as T); | ||
}; | ||
|
||
const add = () => { | ||
if (target.current) | ||
target.current.addEventListener(event, eventListener, options); | ||
}; | ||
|
||
const remove = () => { | ||
if (target.current) | ||
target.current.removeEventListener(event, eventListener, options); | ||
}; | ||
|
||
return { | ||
add, | ||
remove, | ||
}; | ||
}; |