Skip to content
All work

Personal · 2026 · Web

Open Source Library react-window-modals

A lightweight React library for draggable, resizable window modals with context-managed state, for building desktop-like interfaces on the web.

Role
Developer
Status
Completed
Stack
React · TypeScript · Tailwind CSS · Vite · tsup

Overview

react-window-modals is a headless React library for draggable, resizable window modals. I wrote it from scratch so that web apps can have desktop-like windows without being tied to a UI framework.

It keeps the modal logic (dragging, resizing, boundary constraints) separate from presentation, so you can style the windows with Tailwind, Material-UI, or plain CSS.

What it has

  • Draggable windows, with configurable boundaries.
  • Resizable windows, with dynamic width and height that respect constraints.
  • Hooks for context-managed state: useWindowContext, useDraggableWindow, and useResizableWindow.
  • No built-in styling, so you control the whole UI.
  • A fully typed TypeScript API.

Built with

  • React 18
  • TypeScript
  • Tailwind CSS (optional styling)
  • Vite for the dev environment
  • tsup for bundling

What I learned

Keeping drag and resize smooth was the core challenge. The library handles boundary constraints, screen resizing, and high-frequency mouse movement. Writing a fully headless, context-driven system taught me a lot about React hooks, state management, and DOM performance.

It is published on npm and open to contributions.

Usage Example

import { useRef } from "react";
import {
  WindowProvider,
  WindowWrapper,
  type WindowRefType,
  useDraggableWindow,
  useResizableWindow,
} from "react-window-modals";

function WindowResizer() {
  const { onResizeStart } = useResizableWindow("left");

  return (
    <span
      onMouseDown={onResizeStart}
      onTouchStart={onResizeStart}
      style={{
        cursor: "w-resize",
        width: 1,
        position: "absolute",
        top: 0,
        left: 0,
        bottom: 0,
      }}
    />
  );
}

function WindowHeader() {
  const { onMouseDown, onTouchStart } = useDraggableWindow();

  return (
    <div
      onMouseDown={onMouseDown}
      onTouchStart={onTouchStart}
      style={{ cursor: "move", background: "#ccc", padding: "10px" }}
    >
      Drag Me
    </div>
  );
}

export default function App() {
  const windowRef = useRef<WindowRefType>(null);

  return (
    <div>
      <button onClick={() => windowRef.current?.open()}>Open Window</button>

      <WindowProvider
        ref={windowRef}
        initialOpen={false}
        initialPosition={{ x: "center", y: "center" }} // Support for pixels, CSS percentages ("50%"), or "center"
        initialSize={{ width: 400, height: "40%" }}
        constrain={{ minX: 0, minY: 0, maxX: "100%", maxY: "100%" }}
      >
        <WindowWrapper
          style={{
            position: "relative",
            backgroundColor: "white",
            border: "1px solid black",
          }}
        >
          <WindowHeader />
          <WindowResizer />
          <div style={{ padding: "20px" }}>
            <p>Welcome to the custom react window modal!</p>
            <button onClick={() => windowRef.current?.close()}>Close</button>
          </div>
        </WindowWrapper>
      </WindowProvider>
    </div>
  );
}