MCPcopy
hub / github.com/premieroctet/openchakra / useInteractive

Function useInteractive

src/hooks/useInteractive.ts:11–66  ·  view source on GitHub ↗
(
  component: IComponent,
  enableVisualHelper = false,
  withoutComponentProps = false,
)

Source from the content-addressed store, hash-verified

9import { getShowLayout, getFocusedComponent } from '../core/selectors/app'
10
11export 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}

Callers 15

PreviewContainerFunction · 0.90
InputLeftAddonPreviewFunction · 0.90
InputRightElementPreviewFunction · 0.90
ButtonPreviewFunction · 0.90
NumberInputPreviewFunction · 0.90
BreadcrumbPreviewFunction · 0.90
IconButtonPreviewFunction · 0.90
AlertPreviewFunction · 0.90
InputRightAddonPreviewFunction · 0.90
HighlightPreviewFunction · 0.90
SelectPreviewFunction · 0.90

Calls 4

getIsSelectedComponentFunction · 0.90
getIsHoveredFunction · 0.90
getFocusedComponentFunction · 0.90
useDispatchFunction · 0.85

Tested by

no test coverage detected