Skip to content
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

fix: 🐛 fix scrolling animation issue in MenuSelection #719

Merged
merged 2 commits into from
Jun 20, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,9 @@ struct MenuSelectionExample: View {
.border(.red)
.toolbar(content: {
Button("ScrollToBottom") {
scrollView.scrollTo("MenuSelection", anchor: .bottom)
withAnimation {
scrollView.scrollTo("MenuSelection", anchor: .bottom)
}
}
})
.onChange(of: self.isExpanded) { _ in
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,18 +17,18 @@ import SwiftUI
// Base Layout style
public struct MenuSelectionBaseStyle: MenuSelectionStyle {
@Environment(\.maxNumberOfItems) var maxNumberOfItems
@State var itemCount = 0
@State private var itemCount = 0

public func makeBody(_ configuration: MenuSelectionConfiguration) -> some View {
VStack(alignment: .leading) {
_CountableView(maxNumberOfItems: configuration.isExpanded ? 0 : self.maxNumberOfItems) {
_CountableView(maxNumberOfItems: !self.isListCollapsed(configuration) ? 0 : self.maxNumberOfItems) {
configuration.items
}
.onPreferenceChange(ItemCountPreferenceKey.self, perform: { value in
self.itemCount = value
})

if self.shouldShowAction(configuration) {
if self.isListCollapsed(configuration) {
Group {
if configuration.action.isEmpty {
self.defaultAction
Expand All @@ -37,16 +37,14 @@ public struct MenuSelectionBaseStyle: MenuSelectionStyle {
}
}
.onSimultaneousTapGesture {
withAnimation {
configuration.isExpanded = true
}
configuration.isExpanded = true
}
}
}
.frame(maxWidth: .infinity, alignment: .leading)
}

private func shouldShowAction(_ config: MenuSelectionConfiguration) -> Bool {
private func isListCollapsed(_ config: MenuSelectionConfiguration) -> Bool {
self.maxNumberOfItems > 0 && !config.isExpanded && self.itemCount > self.maxNumberOfItems
}

Expand Down
Loading