0.2.47 • Published 7 months ago

react-desktop-environment v0.2.47

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

React Desktop Environment

React Desktop Environment is a react ui library that mimicks the behaviour of an OS ui.

Installation

npm i react-desktop-environment

Usage

// main.jsx
import React from 'react'
import ReactDOM from 'react-dom/client'
import App from './App.jsx'
import DesktopEnvironmentWithWindowManagerRegistryProvider from 'react-desktop-environment';
import Inception from './components/Inception';

const components = {
    // a list of components to be rendered in <Window />
    Inception
}
ReactDOM.createRoot(document.getElementById('root')).render(
    <DesktopEnvironmentWithWindowManagerRegistryProvider components={components}>
        <Main/>
    </DesktopEnvironmentWithWindowManagerRegistryProvider>
,
)

function Main(){
    const { initWindow } = useWindowManagerRegistryContext();
    initWindow('/index');
    return (
        <App/>
    )
}
// App.jsx
import { useContext, useState, useEffect, useRef } from 'react';
import { 
    useWindowManagerRegistryContext, useWindowManagerContext, 
    WindowManagerProvider,
    // components
    Desktop, Start, Window 
} from 'react-desktop-environment';
import Inception from './components/Inception';

function AppFragment() {
    const  { initWindow, getAllWindowSpecs } = useWindowManagerRegistryContext();
    const { currentWindowId, registerWindow, hideWindow, unhideWindow, closeWindow, windows} = useWindowManagerContext();

    const idRef = useRef()
    return (<>
        <Desktop style={{ width: '100vw', height: 'calc( 100vh - 30px - 2px )', backgroundColor: 'white'}}>
            <input onChange={(e)=>{ idRef.current = e.target.value }}></input><br/>
            <button onClick={()=>{
                if ( idRef.current === undefined ) {
                    alert( 'input empty')
                } else {
                    initWindow(idRef.current,{
                        Component: Inception.name,
                        props: {
                            initialTitle : `title: ${idRef.current}`,
                            initialPosition: {
                                left: 500,
                                top: 10
                            },
                            initialSize: {
                                width: 300,
                                height: 200
                            }
                        },
                    });
                    registerWindow(idRef.current); 
                }
            }}> initWindow </button> <br/>


            <button onClick={()=>{ hideWindow(idRef.current) }}> hideWindow </button><br/>
            <button onClick={()=>{ unhideWindow(idRef.current) }}> unhideWindow </button><br/>
            <button onClick={()=>{ closeWindow(idRef.current) }}> closeWindow </button><br/>
            
            { `active: ${ JSON.stringify(windows.active) }`}<br/>
            { `hidden: ${ JSON.stringify(windows.hidden) }`}<br/>
            { `closed: ${ JSON.stringify(windows.closed) }`}<br/>

        </Desktop>
        <Start/>
    </>)
}

export default function App({props}){
    return (
        <WindowManagerProvider id={'/index'} key={'/index'}>
            <AppFragment {...props} />
        </WindowManagerProvider>
    )
}
import { useRef } from 'react';
import { 
    useWindowManagerRegistryContext,
    useWindowManagerContext,
    //ui
    Desktop, StartFrame as Start
} from 'react-desktop-environment';


export default function Inception({...props}){
    const  { initWindow } = useWindowManagerRegistryContext();
    const { states, setWindowState, registerWindow, hideWindow, unhideWindow, closeWindow, windows } = useWindowManagerContext();

    function setTitle(value){
        setWindowState('title', value);
    }
    
    const idRef = useRef();
    return (<>
        <Desktop style={{ width: '100%', height: 'calc( 100% - 30px - 2px )', backgroundColor: 'white'}}>
            { Object.keys(states).map( key => {
                return (
                    <ul key={key}>
                        <li>
                            {key}
                        </li>
                        <ul>
                            <li>
                                {JSON.stringify(states[key])}
                            </li>
                        </ul>
                    </ul>
                )
            })}
            
            <label>set current Window Title</label><br/>
            <input onChange={(e)=>{ setTitle(e.target.value) }}></input><br/>
            <label>set current new Window Title</label><br/>
            <input onChange={(e)=>{ idRef.current = e.target.value }}></input><br/>
             <button onClick={()=>{
                if ( idRef.current === undefined ) {
                    alert( 'input empty')
                } else {
                    initWindow(idRef.current,{
                        Component: Inception.name,
                        props: {
                            initialTitle : `title: ${idRef.current}`,
                            initialPosition: {
                                left: 500,
                                top: 10
                            },
                            initialSize: {
                                width: 300,
                                height: 200
                            }
                        },
                    });
                    registerWindow(idRef.current); 
                }
            }}> initWindow </button> <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/>

        </Desktop>
        < Start.Bar>
            < Start.Menu>
                <div>settings</div>
            </ Start.Menu>
            < Start.Icons>
                <div>{'||'}</div>
                <div>{'||'}</div>
            </ Start.Icons>
            <Start.Windows>
                {windows.hidden.map( id => {
                    return (
                        <button key={id}
                            onClick={()=>{unhideWindow(id)}}
                        >
                            {id}
                        </button>
                    )
                })}
            </Start.Windows>
            <Start.Footer>

            </Start.Footer>
        </Start.Bar>
    </>)
}

Refernece

Context: DesktopEnvironment

Provider

import ReactComponent from './directory';
/**
 * a list of ReactComponent
*/
const components = {
    ReactComponent
}
/**
 * 
*/
<DesktopEnvironmentProvider components={components}>
    {children}
<DesktopEnvironmentProvider/>
            -OR-
<DesktopEnvironmentWithWindowManagerRegistryProvider components={components}>
    {children}
<DesktopEnvironmentWithWindowManagerRegistryProvider />
PropDescription
componentsthis gives a refernce of all Component that might be rendered. Used by or you can create your own render function

Context

const { components } = useDesktopEnvironmentContext();
ItemParamsDescription
componentsn/athis gives a refernce of all Component that might be rendered. Used by or you can create your own render function

Context: WindowManager

// please refer to react-window-manager

Component: Window

Component: Desktop

Component: Start

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.41

7 months ago

0.2.40

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.29

7 months ago

0.2.28

7 months ago

0.2.12

7 months ago

0.2.11

7 months ago

0.2.10

7 months ago

0.2.1

8 months ago

0.2.0

8 months ago

0.2.7

7 months ago

0.2.6

7 months ago

0.2.9

7 months ago

0.2.8

7 months ago

0.2.3

7 months ago

0.2.2

8 months ago

0.2.5

7 months ago

0.2.4

7 months ago

0.0.25

11 months ago

0.1.0

8 months ago

0.1.2

8 months ago

0.1.1

8 months ago

0.0.26

11 months ago

0.0.27

11 months ago

0.0.28

11 months ago

0.0.29

11 months ago

0.1.3

8 months ago

0.0.24

12 months ago

0.0.23

12 months ago

0.0.22

12 months ago

0.0.21

12 months ago

0.0.20

1 year ago

0.0.19

1 year ago

0.0.18

1 year ago

0.0.17

1 year ago

0.0.16

1 year ago

0.0.15

1 year ago

0.0.14

1 year ago

0.0.13

1 year ago

0.0.12

1 year ago

0.0.11

1 year ago

0.0.10

1 year ago

0.0.9

1 year ago

0.0.8

1 year ago

0.0.7

1 year ago

0.0.6

1 year ago

0.0.5

1 year ago

0.0.4

1 year ago

0.0.3

1 year ago

0.0.2

1 year ago

0.0.1

1 year ago

0.0.0

1 year ago