Skip to content

Commit

Permalink
fix: Fix unable Scroll to Top
Browse files Browse the repository at this point in the history
  • Loading branch information
Mercen-Lee committed Apr 4, 2024
1 parent ad2b045 commit 6e964a2
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 2 deletions.
3 changes: 2 additions & 1 deletion Source/DDS/Component/ScrollView/ScrollView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ public struct DodamScrollView<C: View>: DodamNavigationViewProtocol {
)
}

@Environment(\.tabViewIdx) var tabViewIdx: Int?
@State private var topInset: CGFloat!
@State private var blueOpacity: CGFloat = 0

Expand All @@ -60,7 +61,7 @@ public struct DodamScrollView<C: View>: DodamNavigationViewProtocol {
}
}
)
.id(-1)
.id("ScrollToTop-\(tabViewIdx ?? -1)")
}
.mask(alignment: .bottom) {
VStack(spacing: 0) {
Expand Down
6 changes: 5 additions & 1 deletion Source/DDS/Component/TabView/TabView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ public struct DodamTabView: View {
ForEach(contents.indices, id: \.self) { idx in
if loadedViews.contains(idx) {
contents[idx].content
.environment(\.tabViewIdx, idx)
.opacity(selected == idx ? 1 : 0)
}
}
Expand All @@ -54,7 +55,10 @@ public struct DodamTabView: View {
selected = idx
} else {
withAnimation(.spring) {
scrollViewProxy.scrollTo(-1, anchor: .top)
scrollViewProxy.scrollTo(
"ScrollToTop-\(idx)",
anchor: .top
)
}
}
} label: {
Expand Down
18 changes: 18 additions & 0 deletions Source/DDS/Component/TabView/TabViewIdxEnvironment.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import SwiftUI

@available(macOS 12, iOS 15, *)
private struct TabViewIdxEnvironmentKey: EnvironmentKey {

static var defaultValue: Int?
}

@available(macOS 12, iOS 15, *)
public extension EnvironmentValues {

var tabViewIdx: Int? {
get { self [TabViewIdxEnvironmentKey.self] }
set {
self [TabViewIdxEnvironmentKey.self] = newValue
}
}
}

0 comments on commit 6e964a2

Please sign in to comment.