0.0.2-alpha.0 • Published 1 year ago

react-window-system v0.0.2-alpha.0

Weekly downloads
-
License
MIT
Repository
github
Last release
1 year ago

🪟 React Window System

License: MIT PRs Welcome Created by GunseiKPaseri npm version

React Window System is a reproduction of the Window system using React. It provides a Window system.

  • maximize
  • minimize
  • close
  • taskbar
  • Snap (like windows)

How to use

Demo: CodeSandBox

npm install react-window-system

See Example.tsx

const windows = [
  {
    id: `${Math.random()}`,
    defaultWindowPos: {
      x: 300 * Math.random(),
      y: 250 * Math.random(),
      height: 200,
      width: 200,
    },
    header: <strong>Title</strong>,
    body: <>Example</>,
  }
]
const WindowSystem = () => {
  <WindowSystem
    windows={windows}
    style={{
      width: 500,
      height: 500,
    }}
  />
}

Documentation

WindowSystem

The <WindowSystem> component will render the window system environment. Please use the element's styles to specify the width and height.

The windows to be displayed can be specified using the windows property. Please manage it externally using useState or similar state management techniques.

windows (required)

PropertyTypeDescription
idstringThe unique identifier of the window.
defaultWindowPos{ x: number, y: number, height: number, width: number }The initial position and size of the window.
headerReactNodeThe title of the window.
bodyReactNodeThe content of the window.

Window

In the components specified for the header and body, you can use the useWindow hook to retrieve window information.

By using the useWindow hook, you can access window-related information such as window state, position, and size within the specified components. This allows you to dynamically update and interact with the windows in the WindowSystem component.

  const properties = useWindow()
PropertyTypeDescription
windowAreaNodeReact.RefObject<HTMLDivElement>The reference to the window area. (Moveable area from <WindowSystem> excluding the taskbar)
windowProviderNodeReact.RefObject<HTMLDivElement>The reference to the <WindowSystem>
layerQueuestring[]List of window IDs sorted in stacked order.
wsIdstringThe ID of the <WindowSystem>.
closedbooleanWhether the window is closed.
idstringThe unique identifier of the window.
defaultWindowPos{ x: number, y: number, height: number, width: number }The initial position and size of the window.
headerReactNodeThe title of the window.
bodyReactNodeThe content of the window.
maximizedbooleanWhether the window is maximized.
minimizedbooleanWhether the window is minimized.
windowPos{ x: number, y: number, height: number, width: number }The position and size of the window.
layerIndexnumberThe index of the window in the layerQueue.
isActivebooleanWhether the window is active.
bigWindowSuggest{bigWindow: string}Show size when maximized at Snap. (for developper)
activeWindowstringThe ID of the active window.
closeWindow() => voidA function to close the window.
maximizeWindow() => voidA function to maximize the window.
minimizeWindow() => voidA function to minimize the window.
resizeWindow(ref: {x: number, y: number, height: number, width: number}, maximize?: string) => voidA function to resize the window.
hideWindow() => voidA function to hide the window.

Customization

You can define custum Window & TaskBar. See DefaultWindow.tsx, DefaultTaskBar.tsx.

import { Window } from "../window/Window";
import type { WindowUIProps } from "../windowSystem/type";

export const CustomWindow = (props: WindowUIProps) => {
  const { window, ...WindowAttr } = props;
  return (
    <Window
      {...WindowAttr}
      window={window}
      style={{
        position: "absolute",
        zIndex: window.layerIndex,
        border: "1px solid #000",
        borderRadius: "4px",
      }}
    >
      <Window.TitleBar
        style={{
          backgroundColor: window.isActive ? "#99f" : "#ccf",
        }}
      >
        <Window.TitleBar.Title
          style={{
            paddingLeft: 4,
          }}
        >
          {window.header}
        </Window.TitleBar.Title>
        <Window.TitleBar.MinimizeButton>_</Window.TitleBar.MinimizeButton>
        <Window.TitleBar.MaximizeButton>
          {window.maximize === "full" ? "ā’" : "ā–”"}
        </Window.TitleBar.MaximizeButton>
        <Window.TitleBar.CloseButton>Ɨ</Window.TitleBar.CloseButton>
      </Window.TitleBar>
      <Window.Body
        style={{
          backgroundColor: "#fff",
        }}
      >
        {window.body}
      </Window.Body>
    </Window>
  );
};

const CustomWindowSystem = () => {
  return (
    <WindowSystem
      windows={windows}
      style={{
        width: 500,
        height: 500,
      }}
      Window={CustomWindow}
    />
  );
};

Development

To participate in the development, please make sure to install bun as your development environment.

Install

bun install

Run develop server

bun run dev

Check pre-commit

Before committing, please execute the following:

bun run pre-commit
0.0.2-alpha.0

1 year ago

0.0.1

1 year ago