Skip to content

Commit

Permalink
Fix custom props panel (#73)
Browse files Browse the repository at this point in the history
  • Loading branch information
baptadn authored Feb 21, 2020
1 parent 258e073 commit 0da95f6
Show file tree
Hide file tree
Showing 5 changed files with 60 additions and 32 deletions.
5 changes: 4 additions & 1 deletion src/App.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import { HotKeys } from 'react-hotkeys'
import useShortcuts, { keyMap } from './hooks/useShortcuts'
import EditorErrorBoundary from './components/errorBoundaries/EditorErrorBoundary'
import useProducthunt from './hooks/useProducthunt'
import { InspectorProvider } from './contexts/inspector-context'

const App = () => {
const { handlers } = useShortcuts()
Expand Down Expand Up @@ -46,7 +47,9 @@ const App = () => {
overflowX="visible"
borderLeft="1px solid #cad5de"
>
<Inspector />
<InspectorProvider>
<Inspector />
</InspectorProvider>
</Box>
</Flex>
</DndProvider>
Expand Down
8 changes: 4 additions & 4 deletions src/components/inspector/Inspector.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ import {
import ActionButton from './ActionButton'
import { generateComponentCode } from '../../utils/code'
import useClipboard from '../../hooks/useClipboard'
import { useInspectorContext } from '../../contexts/inspector-context'
import { useInspectorUpdate } from '../../contexts/inspector-context'

const CodeActionButton = memo(() => {
const [isLoading, setIsLoading] = useState(false)
Expand Down Expand Up @@ -49,7 +49,7 @@ const Inspector = () => {
const dispatch = useDispatch()
const component = useSelector(getSelectedComponent)

let { activePropsRef } = useInspectorContext()
const { clearActiveProps } = useInspectorUpdate()

const { type, rootParentType, id, children } = component

Expand All @@ -60,8 +60,8 @@ const Inspector = () => {
const componentHasChildren = children.length > 0

useEffect(() => {
activePropsRef.current = []
}, [activePropsRef])
clearActiveProps()
}, [clearActiveProps])

return (
<>
Expand Down
7 changes: 4 additions & 3 deletions src/components/inspector/panels/CustomPropsPanel.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import React, { memo, useState, FormEvent, ChangeEvent, useRef } from 'react'
import { useInspectorContext } from '../../../contexts/inspector-context'
import { useInspectorState } from '../../../contexts/inspector-context'
import { getSelectedComponent } from '../../../core/selectors/components'
import { useSelector } from 'react-redux'
import { IoIosFlash } from 'react-icons/io'
Expand All @@ -22,7 +22,7 @@ const CustomPropsPanel = () => {
const dispatch = useDispatch()
const inputRef = useRef<HTMLInputElement>(null)

const { activePropsRef } = useInspectorContext()
const activePropsRef = useInspectorState()
const { props, id } = useSelector(getSelectedComponent)
const { setValue } = useForm()

Expand All @@ -36,7 +36,7 @@ const CustomPropsPanel = () => {
})
}

const activeProps = activePropsRef.current || []
const activeProps = activePropsRef || []
const customProps = Object.keys(props).filter(
propsName => !activeProps.includes(propsName),
)
Expand Down Expand Up @@ -76,6 +76,7 @@ const CustomPropsPanel = () => {

{customProps.map((propsName, i) => (
<Flex
key={propsName}
alignItems="center"
px={2}
bg={i % 2 === 0 ? 'white' : 'gray.50'}
Expand Down
60 changes: 41 additions & 19 deletions src/contexts/inspector-context.tsx
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 }
12 changes: 7 additions & 5 deletions src/hooks/usePropsSelector.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,16 @@
import { useSelector } from 'react-redux'
import { RootState } from '../core/store'
import { getDefaultFormProps } from '../utils/defaultProps'
import { useInspectorContext } from '../contexts/inspector-context'
import { useInspectorUpdate } from '../contexts/inspector-context'
import { useEffect } from 'react'

const usePropsSelector = (propsName: string) => {
const { activePropsRef } = useInspectorContext()
const { addActiveProps } = useInspectorUpdate()

if (activePropsRef.current) {
activePropsRef.current.push(propsName)
}
useEffect(() => {
// Register form props name for custom props panel
addActiveProps(propsName)
}, [addActiveProps, propsName])

const value = useSelector((state: RootState) => {
const component =
Expand Down

0 comments on commit 0da95f6

Please sign in to comment.