Files
PyUIBuilder/src/canvas/context/widgetContext.js

24 lines
946 B
JavaScript
Raw Normal View History

2025-03-09 11:07:08 +05:30
import React, { createContext, useContext, useEffect, useRef, useState } from 'react'
2024-09-15 12:08:29 +05:30
2025-03-09 11:07:08 +05:30
export const WidgetContext = createContext()
2024-09-15 12:08:29 +05:30
2025-03-09 11:07:08 +05:30
export const useWidgetContext = () => useContext(WidgetContext)
2024-09-15 12:08:29 +05:30
2025-03-08 20:11:22 +05:30
export const WidgetContextProvider = ({ children }) => {
2025-03-09 11:07:08 +05:30
const [activeWidget, setActiveWidget] = useState(null)
const [widgets, setWidgets] = useState([]) // stores the mapping to widgetRefs, stores id and WidgetType, later used for rendering [{id: , widgetType: WidgetClass, children: [], parent: "", initialData: {}}]
// don't useState here because the refs are changing often
const widgetRefs = useRef({}) // stores the actual refs to the widgets inside the canvas {id: ref, id2, ref2...}
2024-09-15 12:08:29 +05:30
2025-03-08 20:11:22 +05:30
return (
2025-03-09 11:07:08 +05:30
<WidgetContext.Provider value={{ widgets, setWidgets, widgetRefs,
activeWidget, setActiveWidget }}>
2025-03-08 20:11:22 +05:30
{children}
2025-03-09 11:07:08 +05:30
</WidgetContext.Provider>
2025-03-08 20:11:22 +05:30
)
}