-
-
Notifications
You must be signed in to change notification settings - Fork 302
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
5 changed files
with
60 additions
and
32 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
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,32 +1,54 @@ | ||
import React, { useContext, useRef, MutableRefObject } from 'react' | ||
import React, { | ||
useContext, | ||
createContext, | ||
useState, | ||
useCallback, | ||
useMemo, | ||
} from 'react' | ||
|
||
interface InspectorContextInterface { | ||
activePropsRef: MutableRefObject<string[] | null> | ||
type UpdateProps = { | ||
addActiveProps: (propsName: string) => void | ||
clearActiveProps: () => void | ||
} | ||
|
||
const InspectorContext = React.createContext<InspectorContextInterface>({ | ||
activePropsRef: React.createRef(), | ||
type InspectorProviderProps = { children: React.ReactNode } | ||
|
||
const InspectorStateContext = createContext<string[]>([]) | ||
const InspectorUpdateContext = createContext<UpdateProps>({ | ||
addActiveProps: () => {}, | ||
clearActiveProps: () => {}, | ||
}) | ||
|
||
interface InspectorProviderProps { | ||
children: React.ReactNode | ||
} | ||
function InspectorProvider({ children }: InspectorProviderProps) { | ||
const [activeProps, setActiveProps] = useState<string[]>([]) | ||
|
||
const addActiveProps = useCallback((propsName: string) => { | ||
setActiveProps(prevActiveProps => [...prevActiveProps, propsName]) | ||
}, []) | ||
|
||
function InspectorProvider(props: InspectorProviderProps) { | ||
const activePropsRef = useRef<string[] | null>(null) | ||
const clearActiveProps = useCallback(() => { | ||
setActiveProps([]) | ||
}, []) | ||
|
||
const values = useMemo(() => { | ||
return { clearActiveProps, addActiveProps } | ||
}, [addActiveProps, clearActiveProps]) | ||
|
||
return ( | ||
<InspectorContext.Provider | ||
value={{ | ||
activePropsRef, | ||
}} | ||
{...props} | ||
/> | ||
<InspectorStateContext.Provider value={activeProps}> | ||
<InspectorUpdateContext.Provider value={values}> | ||
{children} | ||
</InspectorUpdateContext.Provider> | ||
</InspectorStateContext.Provider> | ||
) | ||
} | ||
|
||
function useInspectorContext() { | ||
return useContext(InspectorContext) | ||
function useInspectorState() { | ||
return useContext(InspectorStateContext) | ||
} | ||
|
||
function useInspectorUpdate() { | ||
return useContext(InspectorUpdateContext) | ||
} | ||
|
||
export { InspectorProvider, useInspectorContext } | ||
export { InspectorProvider, useInspectorState, useInspectorUpdate } |
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