Overview
TheDashedBox component creates the classic Windows XP drag-to-select rectangle that appears when clicking and dragging on the desktop. It dynamically calculates position and size based on mouse movements.
Location: src/components/DashedBox/index.jsx
Visual Behavior
When a user clicks and drags on the desktop:- Mouse Down: Records starting position
- Mouse Move: DashedBox expands/contracts to follow cursor
- Mouse Up: Selection completes, DashedBox disappears
Props API
Mouse position object from
react-use/lib/useMouse hookStarting position when drag began, or null if not dragging
Usage Example
Basic Implementation
Real-World Example: WinXP Desktop
Source:src/WinXP/index.jsx:26, 192-204, 264
Component Implementation
Rectangle Calculation
Source:src/components/DashedBox/index.jsx:4-11
- x: Leftmost point (minimum of start and current)
- y: Topmost point (minimum of start and current)
- w: Width (absolute difference in X)
- h: Height (absolute difference in Y)
Conditional Rendering
Source:src/components/DashedBox/index.jsx:12-27
- Only renders when
startPosis truthy (drag in progress) - Uses
transform: translate()for positioning (GPU-accelerated) - Returns
nullwhen not selecting
Styling Details
Inline Styles
The component uses inline styles for dynamic values:Why Transform Instead of Top/Left?
transform provides smoother animations during rapid mouse movements.
Mouse Hook Integration
react-use/lib/useMouse
Source:src/WinXP/index.jsx:3-4, 56-57
docX: X coordinate relative to documentdocY: Y coordinate relative to document
State Management Pattern
Redux-Style Reducer
Reducer Actions:Component Integration
Icon Selection Logic
The DashedBox is typically paired with icon selection logic: Source:src/WinXP/Icons/index.jsx (conceptual)
Event Flow
Complete Selection Cycle
Performance Considerations
Optimizations
- Transform over Top/Left: GPU-accelerated positioning
- Conditional Rendering: Only renders when actively selecting
- Simple Calculation: Minimal math per mouse move
- No Styled-Components: Inline styles avoid CSS-in-JS overhead
Mouse Move Frequency
Mouse move events fire at ~60fps. The component recalculates on every move:- Pure JavaScript math (no DOM manipulation)
- Single element update (transform is fast)
- No layout recalculation
Edge Cases
Dragging Off-Screen
Zero-Size Box
Negative Direction
Styling Variations
Windows 10 Style
macOS Style
Animated Border
Testing
Unit Test Example
Accessibility
The DashedBox is purely visual and does not require accessibility features:- Not keyboard-navigable (mouse-only interaction)
- Not announced to screen readers
- Decorative element (selection feedback)
Common Issues
Box Doesn’t Appear
Problem:startPos is never set
Box Position Offset
Problem: Parent element has unexpected positioningBox Doesn’t Clear
Problem:startPos not reset on mouse up
See Also
- react-use Documentation - useMouse hook
- Desktop Icons - Icon selection logic
- Window Management - Desktop window management
