Skip to content

Handle animations in SwiftUI with @Default #193

Open

Description

@Vincz

Hi! First of all, I'm sorry if it's a stupid question or if it has nothing to do with these package.

I'm trying to animate a List where the order of elements depends on a variable in Defaults.
If I'm using a @State, it's working as expected.
If I'm using a @Default, animation doesn't work.

These works:

enum OrderBy: String, Codable, Defaults.Serializable {
    case index, name
}

struct Element: Identifiable {
    let id: String
    let index: Int
    let name: String
}

let allElements: [Element] = [
    .init(id: "1", index: 5, name: "Element 1"),
    .init(id: "2", index: 4, name: "Element 2"),
    .init(id: "3", index: 1, name: "Element 3"),
    .init(id: "4", index: 3, name: "Element 4"),
    .init(id: "5", index: 2, name: "Element 5"),
]

struct MyView: View {
        @State var order: OrderBy = .index
        func toggleOrder() {
            withAnimation {
                order = order == .index ? .name : .index
            }
        }
        
        var elements: [Element] {
            allElements.sorted(by: { order == .index ? $0.index < $1.index : $0.name < $1.name })
        }

        var body: some View {
            Picker("Order by", selection: $order) {
                Text("index").tag(OrderBy.index)
                Text("name").tag(OrderBy.name)
            }
        
            List {
                ForEach(elements, id: \.id) { element in 
                    Text("Element \(element.name)")
                }
         }
     }
}

These doesn't work:

extension Defaults.Keys {
    static let order = Key<OrderBy>("orderBy", default: OrderBy.index)
}

struct MyView: View {
        @Default(.order) var order
        func toggleOrder() {
            withAnimation {
                listSortOption = listSortOption == .index ? .name : .index
            }
        }
        ...
}

Is these a limitation of the package or is it completely something else?
Thanks

Activity

  1. sindresorhus commented on Nov 29, 2024

    @sindresorhus
    Owner

    Most likely a SwiftUI bug. The issue was introduced by 2dad0e4, but that was done to work around another SwiftUI bug, so we cannot just revert that.

  2. Vincz commented on Dec 11, 2024

    @Vincz
    Author

    Is there any way we can bypass the issue without an intermediate variables somewhere?
    Will it be fixed in the package or when the SwiftUI bug will be fixed?
    Thanks!

  3. richardkunkli commented on Jan 21, 2025

    @richardkunkli

    Hi! Any update on this specific issue? Faced the same with version 9.0.1.

  4. Vincz commented on Jun 5, 2025

    @Vincz
    Author

    Hi @sindresorhus! Any idea when this issue with animation could be fixed?
    Is there something we can do to work around this issue by having an intermediate storage for example?

  5. sindresorhus commented on Jun 6, 2025

    @sindresorhus
    Owner

    I haven't looked into yet, and I don't plan to until I know whether macOS 16 (or 26) fixes it.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions