Skip to content

Implement View Transition API for Website-Level DOM State Transitions #86

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 1 commit into from

Conversation

maclong9
Copy link
Owner

@maclong9 maclong9 commented Jul 4, 2025

Summary

Implements a comprehensive View Transition API that provides website-level configuration for smooth transitions between DOM states. This API integrates with WebUI's existing Swift-style architecture while leveraging modern CSS View Transitions API for enhanced user experience.

Features Implemented

Element-Level Transitions

  • Core Modifier: .viewTransition() with full parameter support
  • Convenience Methods: .fadeTransition(), .slideTransition(), .scaleTransition()
  • Responsive Support: Works with .on { sm { ... } md { ... } } responsive blocks
  • State Modifiers: Compatible with hover, focus, and other interaction states
  • Global DSL: Declarative functions for use in responsive contexts

Document-Level Transitions

  • Configuration: DocumentViewTransitionConfiguration for page-level settings
  • CSS Generation: Automatic generation of View Transitions CSS with keyframes
  • JavaScript Integration: Cross-document navigation with view transitions
  • Enhanced Rendering: renderWithViewTransitions() method for seamless integration
  • Website Defaults: Support for website-level default transition configuration

Comprehensive Type System

  • 13 Transition Types: fade, slide (4 directions), scale (3 variants), flip (3 variants), none
  • Timing Functions: Linear, ease variants, and custom cubic-bezier curves
  • Configuration Options: Duration, delay, slide directions, scale origins, behaviors
  • Swift 6 Ready: Full Sendable conformance for concurrency safety

Technical Architecture

StyleOperation Integration

Follows WebUI's established StyleOperation protocol patterns:

  • ViewTransitionStyleOperation: Core implementation with Parameters struct
  • CSS Class Generation: Generates appropriate Tailwind-compatible classes
  • Responsive Integration: Full integration with ResponsiveBuilder system

Document Integration

Extends Document protocol with view transition support:

  • Optional Configuration: Documents can optionally specify view transitions
  • Automatic Integration: CSS and JS automatically included in enhanced rendering
  • Website Inheritance: Documents can inherit default settings from Website

SwiftUI-Inspired API

Maintains consistency with WebUI's SwiftUI-like design:

  • Method Chaining: .fadeTransition().padding().background()
  • Parameter Labels: Clear, descriptive parameter names following Swift guidelines
  • Optional Parameters: Sensible defaults with optional customization

Usage Examples

Basic Element Transitions

Text("Animated Text")
    .fadeTransition(duration: 300, timing: .easeInOut)

Stack()
    .slideTransition(.left, duration: 400)
    .scaleTransition(.scaleUp, origin: .center)

Responsive Transitions

Stack {
    Text("Content")
}
.on {
    sm { fadeTransition(duration: 200) }
    md { slideTransition(.up, duration: 300) }
    lg { scaleTransition(.scale, origin: .center, duration: 400) }
}

Document-Level Transitions

struct HomePage: Document {
    var metadata: Metadata {
        Metadata(title: "Home", description: "Welcome")
    }
    
    var body: some Markup {
        Text("Welcome Home")
    }
    
    var viewTransitions: DocumentViewTransitionConfiguration? {
        DocumentViewTransitionConfiguration(
            defaultTransition: .fade,
            duration: 300,
            timing: .easeInOut,
            enableCrossDocument: true
        )
    }
}

Website-Level Configuration

struct MyWebsite: Website {
    var defaultViewTransitions: DocumentViewTransitionConfiguration? {
        DocumentViewTransitionConfiguration(
            defaultTransition: .slideLeft,
            duration: 400,
            enableCrossDocument: true
        )
    }
    
    @WebsiteRouteBuilder
    var routes: [any Document] {
        HomePage()
        AboutPage()
    }
}

Testing

  • 50+ Test Cases: Comprehensive coverage of all functionality
  • Unit Tests: Parameter validation, CSS generation, edge cases
  • Integration Tests: Element rendering, document integration, responsive behavior
  • Type Tests: All enum values and edge case handling

Implementation Details

  • Files Added: 6 new files (3 source, 3 test)
  • Lines of Code: ~1,900 lines including comprehensive tests and documentation
  • Documentation: Full DocC documentation following Swift guidelines
  • Performance: Zero runtime overhead, compile-time CSS class generation

Browser Compatibility

  • Modern Browsers: Leverages CSS View Transitions API where available
  • Graceful Degradation: Falls back gracefully on unsupported browsers
  • JavaScript Detection: Runtime feature detection for cross-document transitions

Related

Breaking Changes

None - this is a purely additive API that doesn't modify existing functionality.

## Summary
- Adds CSS View Transitions API support with SwiftUI-inspired declarative syntax
- Enables smooth DOM state transitions at both element and document levels
- Provides comprehensive test coverage with 50+ test cases

## Key Features

### Element-Level Transitions
- `.viewTransition()` modifier with full parameter support
- Convenience methods: `.fadeTransition()`, `.slideTransition()`, `.scaleTransition()`
- Responsive and state modifier support (hover, focus, etc.)
- Global DSL functions for declarative usage

### Document-Level Transitions
- `DocumentViewTransitionConfiguration` for page-to-page transitions
- CSS and JavaScript generation for cross-document transitions
- `renderWithViewTransitions()` method for enhanced rendering
- Website-level default configuration support

### Type System
- `ViewTransitionType` enum with 13 transition variants
- `SlideDirection`, `ScaleOrigin`, `ViewTransitionTiming` enums
- `ViewTransitionBehavior` for browser behavior control
- Full Sendable conformance for Swift 6 compatibility

### Architecture
- Follows established `StyleOperation` protocol patterns
- Consistent with existing WebUI styling architecture
- SwiftUI-inspired API design following Swift API guidelines
- Comprehensive DocC documentation

## Implementation Details
- **ViewTransitionStyleOperation**: Core styling operation following WebUI patterns
- **DocumentViewTransition**: Document-level integration with rendering pipeline
- **ViewTransitionTypes**: Complete type system for transition configuration
- **Comprehensive Tests**: 50+ tests covering all functionality and edge cases

## Usage Examples
```swift
// Element transitions
Text("Hello")
    .fadeTransition(duration: 300)
    .viewTransition(.slide, slideDirection: .left)

// Document transitions
struct MyPage: Document {
    var viewTransitions: DocumentViewTransitionConfiguration? {
        DocumentViewTransitionConfiguration(
            defaultTransition: .fade,
            duration: 300,
            enableCrossDocument: true
        )
    }
}

// Responsive transitions
Stack()
    .on {
        sm { fadeTransition(duration: 200) }
        lg { slideTransition(.up, duration: 400) }
    }
```

Addresses issue #85
@maclong9
Copy link
Owner Author

maclong9 commented Jul 4, 2025

Successfully merged View Transition API to main with all tests passing. The comprehensive API includes element-level and document-level transitions with SwiftUI-inspired syntax, 50+ tests, and full responsive support.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant