Skip to content

UI and Layout Implementation ​

This document consolidates information about the UI components and layout algorithms used in the Family Shapes application, including implementation details, features, and optimizations.

Layout Algorithms ​

Dagre Layout ​

The Dagre layout algorithm provides hierarchical visualization for family trees, organizing nodes in a top-down or left-to-right structure.

Implementation Details ​

  • Library: Uses the Dagre library integrated with XYFlow
  • Direction: Configurable as TB (top to bottom) or LR (left to right)
  • Node Spacing: Customizable spacing between nodes
  • Ranking: Hierarchical ranking based on generations
  • Edge Routing: Orthogonal edge routing with minimal crossings

Performance Optimizations ​

  • Asynchronous layout calculation to prevent UI blocking
  • Caching of layout results for similar tree structures
  • Incremental layout updates for minor changes

Manual Layout ​

The manual layout allows users to position nodes freely using drag-and-drop interactions.

Implementation Details ​

  • Drag Handling: Custom drag handlers for smooth node movement
  • Position Persistence: Node positions are saved to the database
  • Snap-to-Grid: Optional grid snapping for alignment
  • Collision Detection: Prevents node overlap during positioning

Removal Summary ​

The original manual layout implementation had several limitations:

  • Performance issues with large trees
  • Inconsistent behavior across different devices
  • Difficulty maintaining proper family relationships

These issues were addressed by:

  1. Implementing a hybrid approach that combines algorithm-based layouts with manual adjustments
  2. Adding position persistence that respects the family structure
  3. Improving the drag-and-drop interface with better visual feedback

Responsive Utilities ​

The application includes responsive utilities to ensure proper display across different devices and screen sizes.

Key Features ​

  • Breakpoint System: Custom breakpoints for mobile, tablet, and desktop views
  • Responsive Components: UI components that adapt to available space
  • Canvas Scaling: Automatic scaling of the family tree canvas
  • Touch Support: Enhanced interactions for touch devices
  • Viewport Adjustments: Dynamic adjustments based on viewport size

Implementation ​

typescript
// Example of responsive utility for canvas scaling
export const getCanvasScale = (containerWidth: number): number => {
  if (containerWidth < 576) return 0.6;  // Mobile
  if (containerWidth < 992) return 0.8;  // Tablet
  return 1;  // Desktop
};

Generation-Based Visualization ​

The generation-based visualization organizes family members by generation, providing a clear hierarchical view of family relationships.

Key Features ​

  • Generation Detection: Automatic detection of generations based on relationships
  • Horizontal Alignment: Members of the same generation are aligned horizontally
  • Vertical Spacing: Consistent spacing between generations
  • Collapsible Generations: Option to collapse/expand specific generations
  • Visual Indicators: Color coding or badges to indicate generation level

Implementation ​

The generation-based visualization uses a combination of relationship analysis and layout algorithms:

  1. Relationship Analysis:

    • Traverses the family tree to identify parent-child relationships
    • Assigns generation levels based on relationship depth
    • Handles complex cases like multiple parents or adoption
  2. Layout Application:

    • Applies generation constraints to the layout algorithm
    • Ensures consistent vertical spacing between generations
    • Maintains proper alignment within generations
  3. Visual Enhancements:

    • Adds visual indicators for generation levels
    • Provides interactive controls for generation filtering
    • Supports zooming to specific generations

Initial Layout Fixes ​

Several issues with the initial layout implementation were addressed to improve usability and performance:

Issues Addressed ​

  1. Blank Canvas Problem: Fixed issue where the canvas would appear blank on initial load
  2. Node Positioning: Corrected inconsistent node positioning in different layouts
  3. Edge Rendering: Fixed edge rendering issues, particularly with complex relationships
  4. Layout Transitions: Improved transitions between different layout algorithms
  5. Performance: Optimized layout calculation for large family trees

Implementation Changes ​

typescript
// Example of improved layout initialization
const initializeLayout = async (nodes, edges, layoutType) => {
  // Pre-process nodes and edges
  const processedNodes = preprocessNodes(nodes);
  const processedEdges = preprocessEdges(edges);
  
  // Apply layout asynchronously
  return new Promise((resolve) => {
    requestAnimationFrame(() => {
      const layout = applyLayout(processedNodes, processedEdges, layoutType);
      resolve(layout);
    });
  });
};

XYFlow Integration and Cleanup ​

The application uses XYFlow (React Flow) for interactive visualizations, with several customizations and optimizations.

Cleanup Summary ​

The XYFlow integration was improved through:

  1. Component Refactoring:

    • Separated node and edge components for better maintainability
    • Created custom node types for different family member roles
    • Improved edge styling for relationship visualization
  2. Performance Optimizations:

    • Implemented virtualization for large trees
    • Added memoization for expensive calculations
    • Optimized re-renders with React.memo and useMemo
  3. Event Handling:

    • Improved drag-and-drop behavior
    • Enhanced selection and multi-selection capabilities
    • Added custom context menus for nodes and edges
  4. State Management:

    • Centralized flow state management
    • Implemented undo/redo functionality
    • Added persistent layout state

Conclusion ​

The UI and layout implementation in the Family Shapes application combines multiple layout algorithms, responsive design, and performance optimizations to provide a seamless user experience for family tree visualization. The system balances algorithmic layout with manual adjustments, ensuring both structure and flexibility.