-
-
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.
- Loading branch information
Showing
26 changed files
with
334 additions
and
32 deletions.
There are no files selected for viewing
File renamed without changes.
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 |
---|---|---|
@@ -0,0 +1,38 @@ | ||
import React, { | ||
forwardRef, | ||
useCallback, | ||
useImperativeHandle, | ||
useState, | ||
} from 'react'; | ||
import { Text, View, StyleSheet, TouchableHighlight } from 'react-native'; | ||
|
||
const CustomComponent = forwardRef(({ value }, ref) => { | ||
const [count, setCount] = useState(0); | ||
useImperativeHandle(ref, () => ({ | ||
test: () => console.log('YOOO'), | ||
})); | ||
|
||
const handleOnPress = useCallback(() => { | ||
setCount(state => state + 1); | ||
}, []); | ||
|
||
return ( | ||
<View style={styles.container}> | ||
<TouchableHighlight onPress={handleOnPress}> | ||
<Text style={styles.text}>{`CustomComponent: ${value}.${count}`}</Text> | ||
</TouchableHighlight> | ||
</View> | ||
); | ||
}); | ||
|
||
const styles = StyleSheet.create({ | ||
container: { | ||
padding: 16, | ||
backgroundColor: 'red', | ||
}, | ||
text: { | ||
color: 'white', | ||
}, | ||
}); | ||
|
||
export default CustomComponent; |
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 |
---|---|---|
@@ -0,0 +1,30 @@ | ||
import { memo, useEffect, useMemo } from 'react'; | ||
import { nanoid } from 'nanoid/non-secure'; | ||
import { usePortal } from '../../hooks'; | ||
import type { PortalProps } from './types'; | ||
|
||
const PortalComponent = ({ key: _providedKey, children }: PortalProps) => { | ||
//#region hooks | ||
const { mount, unmount } = usePortal(); | ||
//#endregion | ||
|
||
//#region variables | ||
const key = useMemo(() => _providedKey || nanoid(), [_providedKey]); | ||
//#endregion | ||
|
||
//#region effects | ||
useEffect(() => { | ||
mount(key, children); | ||
return () => { | ||
unmount(key); | ||
}; | ||
// eslint-disable-next-line react-hooks/exhaustive-deps | ||
}, [key, children]); | ||
//#endregion | ||
|
||
return null; | ||
}; | ||
|
||
const Portal = memo(PortalComponent); | ||
|
||
export default Portal; |
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 @@ | ||
export { default } from './Portal'; |
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,6 @@ | ||
import type { ReactNode } from 'react'; | ||
|
||
export interface PortalProps { | ||
key?: string; | ||
children?: ReactNode | ReactNode[]; | ||
} |
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,44 @@ | ||
import React, { | ||
forwardRef, | ||
memo, | ||
useCallback, | ||
useImperativeHandle, | ||
} from 'react'; | ||
import { useNodes } from '../../hooks'; | ||
import type { PortalMethods } from '../../types'; | ||
|
||
const PortalContainerComponent = forwardRef<PortalMethods, any>((_, ref) => { | ||
//#region state | ||
const { state, add, remove } = useNodes(); | ||
//#endregion | ||
|
||
//#region | ||
const mount = useCallback( | ||
(key, node) => { | ||
add(key, node); | ||
}, | ||
[add] | ||
); | ||
const unmount = useCallback( | ||
key => { | ||
remove(key); | ||
}, | ||
[remove] | ||
); | ||
//#endregion | ||
|
||
//#region forward methods | ||
useImperativeHandle(ref, () => ({ | ||
mount, | ||
unmount, | ||
})); | ||
//#endregion | ||
|
||
//#region render | ||
return <>{Object.keys(state).map(key => state[key]) || null}</>; | ||
//#endregion | ||
}); | ||
|
||
const PortalContainer = memo(PortalContainerComponent); | ||
|
||
export default PortalContainer; |
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 @@ | ||
export { default } from './PortalContainer'; |
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 @@ | ||
export interface PortalContainerProps {} |
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,41 @@ | ||
import React, { memo, useCallback, useMemo, useRef } from 'react'; | ||
import PortalContainer from '../portalContainer'; | ||
import { PortalContext } from '../../contexts'; | ||
import type { PortalMethods } from '../../types'; | ||
import type { PortalHostProps } from './types'; | ||
|
||
const PortalHostComponent = ({ children }: PortalHostProps) => { | ||
const containerRef = useRef<PortalMethods>(null); | ||
|
||
//#region | ||
const mount = useCallback((key, node) => { | ||
if (containerRef.current) { | ||
containerRef.current.mount(key, node); | ||
} | ||
}, []); | ||
const unmount = useCallback(key => { | ||
if (containerRef.current) { | ||
containerRef.current.unmount(key); | ||
} | ||
}, []); | ||
//#endregion | ||
|
||
const value = useMemo( | ||
() => ({ | ||
mount, | ||
unmount, | ||
}), | ||
[mount, unmount] | ||
); | ||
|
||
return ( | ||
<PortalContext.Provider value={value}> | ||
{children} | ||
<PortalContainer ref={containerRef} /> | ||
</PortalContext.Provider> | ||
); | ||
}; | ||
|
||
const PortalHost = memo(PortalHostComponent); | ||
|
||
export default PortalHost; |
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 @@ | ||
export { default } from './PortalHost'; |
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 @@ | ||
import type { ReactNode } from 'react'; | ||
|
||
export interface PortalHostProps { | ||
children: ReactNode | ReactNode[]; | ||
} |
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 @@ | ||
export { PortalContext } from './portal'; |
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,9 @@ | ||
import { createContext } from 'react'; | ||
import type { PortalMethods } from '../types'; | ||
|
||
interface PortalContextType extends PortalMethods {} | ||
|
||
export const PortalContext = createContext<PortalContextType>({ | ||
mount: () => {}, | ||
unmount: () => {}, | ||
}); |
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,2 @@ | ||
export { usePortal } from './usePortal'; | ||
export { useNodes } from './useNodes'; |
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,23 @@ | ||
import { ReactNode, useCallback, useReducer } from 'react'; | ||
import { reducer } from '../state'; | ||
|
||
export const useNodes = () => { | ||
const [state, dispatch] = useReducer(reducer, {}); | ||
|
||
const add = useCallback((key: string, node: ReactNode) => { | ||
dispatch({ | ||
type: 'ADD_ACTION', | ||
key, | ||
node, | ||
}); | ||
}, []); | ||
|
||
const remove = useCallback((key: string) => { | ||
dispatch({ | ||
type: 'REMOVE_ACTION', | ||
key, | ||
}); | ||
}, []); | ||
|
||
return { state, add, remove }; | ||
}; |
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,4 @@ | ||
import { useContext } from 'react'; | ||
import { PortalContext } from '../contexts'; | ||
|
||
export const usePortal = () => useContext(PortalContext); |
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,5 +1,2 @@ | ||
export default { | ||
multiply(a: number, b: number) { | ||
return Promise.resolve(a * b); | ||
}, | ||
}; | ||
export { default as Portal } from './components/portal'; | ||
export { default as PortalHost } from './components/portalHost'; |
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 @@ | ||
const ADD_ACTION = 'ADD_ACTION'; | ||
const UPDATE_ACTION = 'UPDATE_ACTION'; | ||
const REMOVE_ACTION = 'REMOVE_ACTION'; | ||
|
||
export { ADD_ACTION, UPDATE_ACTION, REMOVE_ACTION }; |
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,2 @@ | ||
export { reducer } from './reducer'; | ||
export { selector } from './selector'; |
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,28 @@ | ||
import type { ReactNode } from 'react'; | ||
import { ADD_ACTION, REMOVE_ACTION } from './constants'; | ||
import { selector } from './selector'; | ||
import type { ActionType } from './types'; | ||
|
||
export const reducer = ( | ||
state: Record<string, any>, | ||
action: ActionType | ||
): Record<string, ReactNode> => { | ||
const { type, key } = action; | ||
|
||
switch (type) { | ||
case ADD_ACTION: | ||
return { | ||
...state, | ||
[key]: action.node, | ||
}; | ||
|
||
case REMOVE_ACTION: | ||
if (selector(state, key)) { | ||
const newState = { ...state }; | ||
delete newState[key]; | ||
return newState; | ||
} | ||
break; | ||
} | ||
return state; | ||
}; |
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 @@ | ||
export const selector = (state: Record<string, any>, key: string) => state[key]; |
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,7 @@ | ||
import * as Actions from './constants'; | ||
|
||
type ActionType = { | ||
type: keyof typeof Actions; | ||
key: string; | ||
node?: any; | ||
}; |
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,6 @@ | ||
import type { ReactNode } from 'react'; | ||
|
||
interface PortalMethods { | ||
mount: (key: string, node: ReactNode) => void; | ||
unmount: (key: string) => void; | ||
} |
Oops, something went wrong.