( component: IComponent, enableVisualHelper = false, withoutComponentProps = false, )
| 9 | import { getShowLayout, getFocusedComponent } from '../core/selectors/app' |
| 10 | |
| 11 | export const useInteractive = ( |
| 12 | component: IComponent, |
| 13 | enableVisualHelper = false, |
| 14 | withoutComponentProps = false, |
| 15 | ) => { |
| 16 | const dispatch = useDispatch() |
| 17 | const showLayout = useSelector(getShowLayout) |
| 18 | const isComponentSelected = useSelector(getIsSelectedComponent(component.id)) |
| 19 | const isHovered = useSelector(getIsHovered(component.id)) |
| 20 | const focusInput = useSelector(getFocusedComponent(component.id)) |
| 21 | |
| 22 | const [, drag] = useDrag({ |
| 23 | item: { id: component.id, type: component.type, isMoved: true }, |
| 24 | }) |
| 25 | |
| 26 | const ref = useRef<HTMLDivElement>(null) |
| 27 | let props = { |
| 28 | ...(withoutComponentProps ? {} : component.props), |
| 29 | onMouseOver: (event: MouseEvent) => { |
| 30 | event.stopPropagation() |
| 31 | dispatch.components.hover(component.id) |
| 32 | }, |
| 33 | onMouseOut: () => { |
| 34 | dispatch.components.unhover() |
| 35 | }, |
| 36 | onClick: (event: MouseEvent) => { |
| 37 | event.preventDefault() |
| 38 | event.stopPropagation() |
| 39 | dispatch.components.select(component.id) |
| 40 | }, |
| 41 | onDoubleClick: (event: MouseEvent) => { |
| 42 | event.preventDefault() |
| 43 | event.stopPropagation() |
| 44 | if (focusInput === false) { |
| 45 | dispatch.app.toggleInputText() |
| 46 | } |
| 47 | }, |
| 48 | } |
| 49 | |
| 50 | if (showLayout && enableVisualHelper) { |
| 51 | props = { |
| 52 | ...props, |
| 53 | border: `1px dashed #718096`, |
| 54 | padding: props.p || props.padding ? props.p || props.padding : 4, |
| 55 | } |
| 56 | } |
| 57 | |
| 58 | if (isHovered || isComponentSelected) { |
| 59 | props = { |
| 60 | ...props, |
| 61 | boxShadow: `${focusInput ? '#ffc4c7' : '#4FD1C5'} 0px 0px 0px 2px inset`, |
| 62 | } |
| 63 | } |
| 64 | |
| 65 | return { props, ref: drag(ref), drag } |
| 66 | } |
no test coverage detected