-
Notifications
You must be signed in to change notification settings - Fork 39
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* refactor: Add createRegisterOverlaysStore * refactor: Add createOverlay function * refactor: Add createOverlayProvider function * refactor: Refactor overlay provider structure and relocate content overlay controller * feat: Add experimental_createOverlayContext function * Create fair-shrimps-begin.md * feat: improve overlay context management with createOverlaySafeContext function * refactor: streamline overlay context creation and enhance dispatch logic * test: Update overlay-kit test
- Loading branch information
Showing
14 changed files
with
240 additions
and
191 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
--- | ||
"overlay-kit": minor | ||
--- | ||
|
||
feat: locally overlay-kit |
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,12 +1,16 @@ | ||
import { type OverlayData } from './store'; | ||
import { createSafeContext } from '../utils/create-safe-context'; | ||
|
||
export const [OverlayContextProvider, useOverlayContext] = createSafeContext<OverlayData>('overlay-kit/OverlayContext'); | ||
export function createOverlaySafeContext() { | ||
const [OverlayContextProvider, useOverlayContext] = createSafeContext<OverlayData>('overlay-kit/OverlayContext'); | ||
|
||
export function useCurrentOverlay() { | ||
return useOverlayContext().current; | ||
} | ||
function useCurrentOverlay() { | ||
return useOverlayContext().current; | ||
} | ||
|
||
function useOverlayData() { | ||
return useOverlayContext().overlayData; | ||
} | ||
|
||
export function useOverlayData() { | ||
return useOverlayContext().overlayData; | ||
return { OverlayContextProvider, useCurrentOverlay, useOverlayData }; | ||
} |
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,2 +1 @@ | ||
export { OverlayProvider } from './provider'; | ||
export { useCurrentOverlay, useOverlayData } from './context'; | ||
export { OverlayProvider, useCurrentOverlay, useOverlayData } from './provider'; |
This file was deleted.
Oops, something went wrong.
55 changes: 55 additions & 0 deletions
55
packages/src/context/provider/content-overlay-controller.tsx
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,55 @@ | ||
import { type FC, useEffect, useRef } from 'react'; | ||
|
||
type OverlayControllerProps = { | ||
overlayId: string; | ||
isOpen: boolean; | ||
close: () => void; | ||
unmount: () => void; | ||
}; | ||
|
||
type OverlayAsyncControllerProps<T> = Omit<OverlayControllerProps, 'close'> & { | ||
close: (param: T) => void; | ||
}; | ||
|
||
export type OverlayControllerComponent = FC<OverlayControllerProps>; | ||
export type OverlayAsyncControllerComponent<T> = FC<OverlayAsyncControllerProps<T>>; | ||
|
||
type ContentOverlayControllerProps = { | ||
isOpen: boolean; | ||
current: string | null; | ||
overlayId: string; | ||
onMounted: () => void; | ||
onCloseModal: () => void; | ||
onExitModal: () => void; | ||
controller: OverlayControllerComponent; | ||
}; | ||
|
||
export function ContentOverlayController({ | ||
isOpen, | ||
current, | ||
overlayId, | ||
onMounted, | ||
onCloseModal, | ||
onExitModal, | ||
controller: Controller, | ||
}: ContentOverlayControllerProps) { | ||
const prevCurrent = useRef(current); | ||
const onMountedRef = useRef(onMounted); | ||
|
||
/** | ||
* @description Executes when closing and reopening an overlay without unmounting. | ||
*/ | ||
if (prevCurrent.current !== current) { | ||
prevCurrent.current = current; | ||
|
||
if (current === overlayId) { | ||
onMountedRef.current(); | ||
} | ||
} | ||
|
||
useEffect(() => { | ||
onMountedRef.current(); | ||
}, []); | ||
|
||
return <Controller overlayId={overlayId} isOpen={isOpen} close={onCloseModal} unmount={onExitModal} />; | ||
} |
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,49 @@ | ||
import { useEffect, type PropsWithChildren } from 'react'; | ||
import { ContentOverlayController } from './content-overlay-controller'; | ||
import { useSyncOverlayStore } from './use-sync-overlay-store'; | ||
import { createOverlay } from '../../event'; | ||
import { createOverlaySafeContext } from '../context'; | ||
import { type OverlayStore } from '../store'; | ||
|
||
export function createOverlayProvider(overlayStore: OverlayStore) { | ||
const overlay = createOverlay(overlayStore); | ||
const { OverlayContextProvider, useCurrentOverlay, useOverlayData } = createOverlaySafeContext(); | ||
|
||
function OverlayProvider({ children }: PropsWithChildren) { | ||
const overlayState = useSyncOverlayStore(overlayStore); | ||
|
||
useEffect(() => { | ||
return () => { | ||
overlayStore.dispatchOverlay({ type: 'REMOVE_ALL' }); | ||
}; | ||
}, []); | ||
|
||
return ( | ||
<OverlayContextProvider value={overlayState}> | ||
{children} | ||
{overlayState.overlayOrderList.map((item) => { | ||
const { id: currentOverlayId, isOpen, controller: currentController } = overlayState.overlayData[item]; | ||
|
||
return ( | ||
<ContentOverlayController | ||
key={currentOverlayId} | ||
isOpen={isOpen} | ||
current={overlayState.current} | ||
overlayId={currentOverlayId} | ||
onMounted={() => { | ||
requestAnimationFrame(() => { | ||
overlayStore.dispatchOverlay({ type: 'OPEN', overlayId: currentOverlayId }); | ||
}); | ||
}} | ||
onCloseModal={() => overlay.close(currentOverlayId)} | ||
onExitModal={() => overlay.unmount(currentOverlayId)} | ||
controller={currentController} | ||
/> | ||
); | ||
})} | ||
</OverlayContextProvider> | ||
); | ||
} | ||
|
||
return { overlay, OverlayProvider, useCurrentOverlay, useOverlayData }; | ||
} |
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,10 @@ | ||
import { useSyncExternalStore } from 'use-sync-external-store/shim/index.js'; | ||
import { type OverlayStore } from '../store'; | ||
|
||
export function useSyncOverlayStore(overlayStore: OverlayStore) { | ||
const { | ||
registerOverlaysStore: { subscribe, getSnapshot }, | ||
} = overlayStore; | ||
|
||
return useSyncExternalStore(subscribe, getSnapshot, getSnapshot); | ||
} |
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 was deleted.
Oops, something went wrong.
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
Oops, something went wrong.