0.2.85 • Published 7 months ago

react-window-manager v0.2.85

Weekly downloads
-
License
ISC
Repository
github
Last release
7 months ago

React Window Manager

Intro

React Window Manager is a react context that manages element rendering with:

  • windowSpecsRef:
{
    [windowId]:{ 
        // Component: '', //this key is used by react-desktop-environment, you can also add your own key value pairs
        registeredId: [],
        props: {
            initialPosition: { left: 100, top: 100 },
            initialSize: { width: 100, height: 100 },
            initialTitle: ''
        },
        windows: {
            active: [],
            hidden: [],
            closed: []
        },
        states: {}
    }, 
    [windowId]:{
        registeredId: [ anotherWindowId, anotherWindowId, ... ],
        props: {
            initialPosition: { left: 100, top: 100 },
            initialSize: { width: 100, height: 100 },
            initialTitle: '',
            ['custom prop key']: 'custom prop value',
            ...
        },
        // Component: Component.name,
        windows: {
            active: [ childWindowId, childWindowId ],
            hidden: [ childWindowId, childWindowId, ...],
            closed: [ `${childWindowId}@${unixtimestamp}`, ...]
        },
        states: {
            ['state key']: 'state value',
            ...
        }
    }
}

Installation

npm i react-window-manager

Usage

// main.jsx
import React, { useContext } from 'react'
import ReactDOM from 'react-dom/client'
import './index.css'
import App from './App'
import WindowManagerRegistryProvider, {WindowManagerRegistryContext} from './lib';

ReactDOM.createRoot(document.getElementById('root')).render(<Wrapper/>);

function Wrapper(){
  return (
    <React.StrictMode>
      <WindowManagerRegistryProvider>
        <Main/>
      </WindowManagerRegistryProvider>
    </React.StrictMode>
  )
}

function Main(){
  const { initWindow } = useContext(WindowManagerRegistryContext);
  initWindow('app');
  return (
      <App/>
  )
}
import { useContext, useState, useEffect, useRef } from 'react';

import { WindowManagerContext, WindowManagerProvider, WindowManagerRegistryContext } from './lib';

function App() {
    const { initWindow } = useContext(WindowManagerRegistryContext)
    const { windows, registerWindow, hideWindow, unhideWindow, closeWindow} = useContext(WindowManagerContext);

    const [ idToAction, setIdToAction] = useState(0);
    
    return (
        <div style={{ width: '100vw', height: '100vh'}}>
            <button onClick={()=>{
                initWindow(idToAction)
                registerWindow(idToAction) 
            }}> initWindow </button> <br/>

            <input onChange={(e)=>{
                setIdToAction(e.target.value)
            }}></input><br/>
            <button onClick={()=>{ hideWindow(idToAction) }}> hideWindow </button><br/>
            <button onClick={()=>{ unhideWindow(idToAction) }}> unhideWindow </button><br/>
            <button onClick={()=>{ closeWindow(idToAction) }}> closeWindow </button><br/>
            
            { `active: ${ JSON.stringify(windows.active) }`}<br/>
            { `hidden: ${ JSON.stringify(windows.hidden) }`}<br/>
            { `closed: ${ JSON.stringify(windows.closed) }`}<br/>
        </div>
    )
}

export default function WrappedHome(){

    return (
        <WindowManagerProvider id={'app'}>
            <App/>
        </WindowManagerProvider>
    )
}

Refernece

WindowManagerRegistry

Provider

<WindowManagerRegistryProvider windowSpecsFromLastSession={Object} syncToDataBaseFunctions={Function}>
    {children}
<WindowManagerRegistryProvider/>
PropDescription
windowSpecsFromLastSessionwindowSpecsRef.current default value. this could be retrieved from an external storage such as database or localStorage.
TODO syncToDataBaseFunctionshis function is meant to save the current windowSpecsRef to your external store

Context

const {
    initWindow, // Function
    getAllWindowSpecs, // Function
    doesTargetWindowIdExist, // Function
    getTargetWindowSpecsById, // Function
    setTargetWindowSpecsById, // Function
    reassginTargetWindowId // Function
} = useContext( WindowManagerRegistryProviderContext );
ItemParamsDescription
initWindow()windowId: Stringthis adds a windowId key into windowSpecsRef.current
getAllWindowSpecs()n/adeep copy and returns windowSpecsRef.current
doesTargetWindowIdExist()targetWindowId: Stringreturns true if targetWindowId exists in windowSpecsRef.current else false
getTargetWindowSpecsById()targetWindowId: Stringdeep copy and returns windowSpecsRef.currenttargetWindowId
setTargetWindowSpecsById()targetWindowId: String, specFragment: Objectreplace the spcified specs of the targetWindowId. e.g. { Component: 'NewComponent', registeredIn: 'parentWindowId' } will replace Component and state key in the spec
usable reassignTargetWindowId()targetWindowId: String, newTargetWindowId: Stringthis renames targetWindowId in windowSpecsRef.current

WindowManager

Provider

<WindowManagerProvider id={String}/>
id = the current window id in windowSpecsRef.current
PropDescription
idthe id reference for provider to extract the correct windowSpecs from windowSpecsRef.current

Context

const {
    currentWindowId,
    // useState
    windows,
    states,
    // init
    registerWindow,
    // controllers
    liftWindowToTop,
    hideWindow,
    unhideWindow,
    closeWindow,
    // states
    isWindowStatesReady,
    initWindowState,
    setWindowState,
    getWindowState,
    useWindowState
} = useWindowManagerProvider
ItemParamsDescription
currentWindowIdn/athe current windowId
windowsn/athe current windows state of WindowContextProvider. windowSpecs.currentwindowId mirrors this value. this value is cleared when WindowContextProvider is unmounted. Useful for debugging but bad for readability
statesn/athe current states state of WindowContextProvider. windowSpecs.currentwindowId mirrors this value. this value is cleared when WindowContextProvider is unmounted. Useful for debugging but bad for readability
registerWindow()childWindowId: Stringregisters child window in current window
liftWindowToTop()childWindowId: Stringmoves childWindowId to the end of active array in windows state.
hideWindow()childWindowId: Stringmoves childWindowId to the end of hidden array in windows state from active array.
usablecloseWindow()childWindowId: Stringmoves childWindowId to the end of closed array in windows state from active or hidden array. TODO: behaviour when the closed window has its own child or is registered in multiple windows
isWindowStatesReady()stateTitlesArray: Stringchecks if statestitle is initialised in states state.
iniWindowState()title: String, value: anyreturns either statestitle if exist or mutate states by adding title:value in it, sync with windowSpecsRef then returns value. Use this when you need a state value that renders before or with WindowContextProvider.
setWindowState()title: String, value: anyupdates statestitle and sync with windowSpecsRef. Use this when you only need to set statetitle but not consume in the current WindowcontextProvider
getWindowState()title: Stringreturns statestitle. Use this when you only need statetitle but not update statetitle in the current WindowcontextProvider
useWindowState()title: String, value: anyreturns statestitle and it's setter. Like React.useState()
0.2.85

7 months ago

0.2.84

7 months ago

0.2.83

7 months ago

0.2.82

7 months ago

0.2.81

7 months ago

0.2.80

7 months ago

0.2.74

7 months ago

0.2.73

7 months ago

0.2.72

7 months ago

0.2.71

7 months ago

0.2.70

7 months ago

0.2.79

7 months ago

0.2.78

7 months ago

0.2.77

7 months ago

0.2.76

7 months ago

0.2.75

7 months ago

0.2.63

7 months ago

0.2.69

7 months ago

0.2.68

7 months ago

0.2.67

7 months ago

0.2.66

7 months ago

0.2.65

7 months ago

0.2.64

7 months ago

0.2.27

7 months ago

0.2.26

7 months ago

0.2.25

7 months ago

0.2.24

7 months ago

0.2.23

7 months ago

0.2.22

7 months ago

0.2.21

7 months ago

0.2.20

7 months ago

0.2.19

7 months ago

0.2.18

7 months ago

0.2.17

7 months ago

0.2.16

7 months ago

0.2.15

7 months ago

0.2.14

7 months ago

0.2.13

7 months ago

0.2.12

7 months ago

0.2.11

7 months ago

0.2.10

7 months ago

0.2.62

7 months ago

0.2.61

7 months ago

0.2.60

7 months ago

0.2.52

7 months ago

0.2.51

7 months ago

0.2.50

7 months ago

0.2.59

7 months ago

0.2.58

7 months ago

0.2.57

7 months ago

0.2.56

7 months ago

0.2.55

7 months ago

0.2.54

7 months ago

0.2.53

7 months ago

0.2.41

7 months ago

0.2.40

7 months ago

0.2.49

7 months ago

0.2.48

7 months ago

0.2.47

7 months ago

0.2.46

7 months ago

0.2.45

7 months ago

0.2.44

7 months ago

0.2.43

7 months ago

0.2.42

7 months ago

0.2.39

7 months ago

0.2.30

7 months ago

0.2.38

7 months ago

0.2.37

7 months ago

0.2.36

7 months ago

0.2.35

7 months ago

0.2.34

7 months ago

0.2.33

7 months ago

0.2.32

7 months ago

0.2.31

7 months ago

0.2.1

8 months ago

0.2.0

8 months ago

0.2.29

7 months ago

0.2.28

7 months ago

0.2.7

7 months ago

0.2.6

8 months ago

0.2.9

7 months ago

0.2.8

7 months ago

0.2.3

8 months ago

0.2.2

8 months ago

0.2.5

8 months ago

0.2.4

8 months ago

0.0.30

11 months ago

0.0.31

11 months ago

0.0.32

11 months ago

0.0.33

11 months ago

0.1.0

8 months ago

0.1.2

8 months ago

0.1.1

8 months ago

0.0.29

11 months ago

0.1.3

8 months ago

0.0.26

12 months ago

0.0.27

12 months ago

0.0.28

12 months ago

0.0.20

12 months ago

0.0.21

12 months ago

0.0.22

12 months ago

0.0.23

12 months ago

0.0.24

12 months ago

0.0.25

12 months ago

0.0.15

1 year ago

0.0.16

1 year ago

0.0.17

1 year ago

0.0.18

12 months ago

0.0.19

12 months ago

0.0.10

1 year ago

0.0.11

1 year ago

0.0.12

1 year ago

0.0.13

1 year ago

0.0.14

1 year ago

0.0.1

1 year ago

0.0.0

1 year ago

0.0.3

1 year ago

0.0.2

1 year ago

0.0.9

1 year ago

0.0.8

1 year ago

0.0.4

1 year ago

0.0.7

1 year ago

0.0.6

1 year ago

1.1.9

4 years ago

1.1.8

4 years ago

1.1.7

4 years ago

1.1.6

4 years ago

1.1.5

4 years ago

1.1.4

4 years ago

1.1.3

4 years ago

1.1.2

4 years ago

1.1.1

4 years ago

1.1.0

4 years ago

1.0.9

4 years ago

1.0.8

4 years ago

1.0.7

4 years ago

1.0.6

4 years ago

1.0.5

4 years ago

1.0.4

4 years ago

1.0.3

4 years ago

1.0.2

4 years ago

1.0.1

4 years ago

1.0.0

4 years ago