Repository Public
1// SP Notes Core2// File: Sources/SPNotesDesign/NVMotion.swift3// Description: Note Visual System 2.0 motion tokens (durations, easings, spring) and the Reduce Motion rule: every4// animation degrades to a plain cross-fade of at most 120 ms.5//6// Author: Simon-Pierre Boucher7// Contact: contact@spboucher.ai8import SwiftUI910/// Motion tokens. Every helper takes Reduce Motion into account.11public enum NVMotion {12 /// A motion token.13 public enum Token: String, Sendable, Hashable, CaseIterable {14 /// 90 ms. Hover, press.15 case instant16 /// 160 ms. Small state changes.17 case fast18 /// 240 ms. The default.19 case base20 /// 420 ms. Large surfaces.21 case slow22 /// 900 ms. Ambient effects (pulse, shimmer).23 case ambient24 /// 160 ms with the exit easing. Elements leaving.25 case exit26 /// Spring (response 0.38, damping 0.82).27 case spring2829 /// Nominal duration in seconds (the spring's is its response).30 public var duration: Double {31 switch self {32 case .instant: return 0.09033 case .fast, .exit: return 0.16034 case .base: return 0.24035 case .slow: return 0.42036 case .ambient: return 0.90037 case .spring: return NVMotion.springResponse38 }39 }40 }4142 /// A cubic Bézier easing.43 public struct Curve: Sendable, Hashable {44 public let x1: Double, y1: Double, x2: Double, y2: Double45 /// `cubic-bezier(.2,.8,.2,1)`.46 public static let standard = Curve(x1: 0.2, y1: 0.8, x2: 0.2, y2: 1)47 /// `cubic-bezier(.4,0,1,1)`.48 public static let exit = Curve(x1: 0.4, y1: 0, x2: 1, y2: 1)4950 public func animation(duration: Double) -> Animation { .timingCurve(x1, y1, x2, y2, duration: duration) }51 }5253 public static let springResponse: Double = 0.3854 public static let springDamping: Double = 0.8255 /// Longest animation allowed under Reduce Motion (a plain cross-fade).56 public static let reducedMaxDuration: Double = 0.1205758 /// Living Pages: delay between two consecutive blocks.59 public static let staggerStep: Double = 0.01860 /// Living Pages: only the first 24 blocks are animated.61 public static let staggerCap: Int = 2462 /// Living Pages: distance the blocks rise.63 public static let revealRise: CGFloat = 66465 /// Effective duration of a token: at most 120 ms under Reduce Motion.66 public static func duration(_ token: Token, reduceMotion: Bool) -> Double {67 reduceMotion ? min(token.duration, reducedMaxDuration) : token.duration68 }6970 /// The animation of a token. Under Reduce Motion: a linear fade of at most 120 ms (no spring, no overshoot).71 public static func animation(_ token: Token = .base, reduceMotion: Bool) -> Animation {72 if reduceMotion { return .linear(duration: duration(token, reduceMotion: true)) }73 switch token {74 case .spring: return .spring(response: springResponse, dampingFraction: springDamping)75 case .exit: return Curve.exit.animation(duration: token.duration)76 default: return Curve.standard.animation(duration: token.duration)77 }78 }7980 /// Whether block number `index` (0-based) takes part in the staggered reveal.81 public static func isStaggered(index: Int) -> Bool { index >= 0 && index < staggerCap }8283 /// Delay of block `index` in the staggered reveal: 18 ms per block for the first 24, none beyond, none under Reduce Motion.84 public static func staggerDelay(index: Int, reduceMotion: Bool) -> Double {85 guard !reduceMotion, isStaggered(index: index) else { return 0 }86 return Double(index) * staggerStep87 }88}8990/// `withAnimation` with a motion token; under Reduce Motion the change cross-fades in at most 120 ms.91public func withNVAnimation<Result>(_ token: NVMotion.Token = .base, reduceMotion: Bool, _ body: () throws -> Result) rethrows -> Result {92 try withAnimation(NVMotion.animation(token, reduceMotion: reduceMotion), body)93}9495/// Animates changes of `value` with a motion token, reading Reduce Motion from the environment.96public struct NVAnimationModifier<Value: Equatable>: ViewModifier {97 let token: NVMotion.Token98 let value: Value99 @Environment(\.accessibilityReduceMotion) private var reduceMotion100101 public func body(content: Content) -> some View {102 content.animation(NVMotion.animation(token, reduceMotion: reduceMotion), value: value)103 }104}105106extension View {107 /// Animates changes of `value` with a motion token (Reduce Motion aware).108 public func nvAnimation<Value: Equatable>(_ token: NVMotion.Token = .base, value: Value) -> some View {109 modifier(NVAnimationModifier(token: token, value: value))110 }111}112