Documentation

svelte-windows API guide

Build desktop-like interfaces in Svelte with drag, resize, stacking, mobile touch handling, and iframe-safe focus behavior.

Quick start

Install the package, then place one or more windows inside a manager.

npm install svelte-windows

Core model

  • WindowManager sets up global move/up handlers and provides context.
  • Window registers itself, manages z-order, and renders drag/resize handles.
  • MouseContext multiplexes movement to one active drag/resize target.
  • WindowContext tracks active window and stacking history.

Basic usage

Use snippet children to receive the manager context. Define draggable hit areas with windowDragRegions.

Example.svelte
    

WindowManager props

You can pass your own context instances when coordinating multiple managers or integrating external window state.

PropTypeDefaultRequiredDescription
childrenSnippet<[context]>undefinedNoSnippet render function that receives `{ mouseContext, windowContext, desktop }`.
mouseContextMouseContextnew MouseContext()NoOptional shared mouse coordinator instance for advanced composition.
windowContextWindowContextnew WindowContext()NoOptional shared window stack manager instance.

WindowDragConfig

Each drag region requires width and height and can be positioned using either top or bottom plus either left or right. You can optionally pass color to visualize a drag region while designing custom chrome.

  • Vertical anchor: choose exactly one of top or bottom.
  • Horizontal anchor: choose exactly one of left or right.
  • Size keys width and height are required strings.
  • color is optional and useful for debugging hit zones.
WindowDragConfig example
    

Window props

Dimensions and position props are bindable and use px strings. You can style both the shell and content layers.

PropTypeDefaultRequiredDescription
idstring-YesUnique id for registration and stacking order.
context{ mouseContext, windowContext, desktop }-YesContext provided by WindowManager snippet.
childrenSnippetundefinedNoWindow content rendered inside the managed window shell.
windowDragRegionsWindowDragConfig[][]NoAbsolute drag target regions inside the window.
resizablebooleantrueNoEnable or disable all edge and corner resize handles.
resizeWhenInactivebooleanfalseNoAllow resize handles on inactive windows.
width / heightstring (px)300pxNoCurrent dimensions. Bindable.
left / topstring (px)100pxNoCurrent position relative to manager desktop. Bindable.
minWidth / minHeightnumber300NoMinimum window dimensions during resize.
innerStyle / outerStylestringundefinedNoInline style hooks for interior or shell.
innerClassName / outerClassNamestringundefinedNoClass hooks for interior or shell.

Lifecycle callbacks

React to active state, drag events, and resize events. Useful for syncing layout state to your own stores.

onActiveStateChanged

Payload: boolean

Fires when this window becomes active/inactive.

onDragStart

Payload: { top: string, left: string }

Fires when drag begins on this window.

onDragEnd

Payload: { top: string, left: string }

Fires when drag finishes.

onResizeStart

Payload: { width: string, height: string }

Fires when any edge/corner resize starts.

onResizeEnd

Payload: { width: string, height: string }

Fires when resize finishes.

Callback example
    

Styling and behavior

Drag regions

Dragging is opt-in. Define one or more absolute regions to control where users can grab the window.

Resize handles

All four edges and four corners are supported. Touch and mouse are both wired through the same manager.

Shell vs content

Use outerStyle/outerClassName for shadows and chrome, and innerStyle/innerClassName for content.

Window stacking

Active windows are promoted to the front automatically. Iframe focus is detected and translated into window activation.

Exports

  • WindowManager, Window
  • MouseContext, WindowContext
  • INACTIVE_MOUSE_ID
  • WindowDragConfig, WindowPosition, WindowDimensions, ActualWindowProps