Skip to content
Filter Chips preview
Rendered from the SwiftUI source on this page.

Filter Chips

From syxUI — written for both platforms, not translated between them.

A self-contained filter chips — SwiftUI, styled from system tokens.

Inputs · SwiftUI · iOS 17

  • inputs
  • swiftui

The actual source

ToggleChipsCard.swift
// Filter Chips · syxUI · https://syxui.dev/components/form-toggle-chips
// Free in any project. Keep this line and credit syxUI where a person can read it.

import SwiftUI

extension View {
    func formaCard(_ pad: CGFloat = 18) -> some View {
        self.padding(pad)
            .background(Color(.systemBackground), in: RoundedRectangle(cornerRadius: 20, style: .continuous))
            .overlay(RoundedRectangle(cornerRadius: 20, style: .continuous).stroke(Color.primary.opacity(0.06), lineWidth: 1))
            .shadow(color: .black.opacity(0.05), radius: 14, x: 0, y: 6)
    }
}

// Toggle chips (multi-select filter)
struct ToggleChipsCard: View {
    @State private var sel: Set<Int> = [0, 2]
    let tags = ["Video", "Image", "Code", "Music", "Voice", "Design", "Writing"]
    var body: some View {
        FlowHStack(spacing: 8) {
            ForEach(tags.indices, id: \.self) { i in
                Text(tags[i]).font(.system(size: 14, weight: .medium))
                    .foregroundStyle(sel.contains(i) ? Color(.systemBackground) : .primary)
                    .padding(.horizontal, 14).padding(.vertical, 9)
                    .background(sel.contains(i) ? Color.primary : Color.primary.opacity(0.06), in: Capsule())
            }
        }.formaCard()
    }
}


struct FlowHStack: Layout {
    var spacing: CGFloat = 8

    func sizeThatFits(proposal: ProposedViewSize, subviews: Subviews, cache: inout ()) -> CGSize {
        let maxWidth = proposal.width ?? .infinity
        var x: CGFloat = 0
        var y: CGFloat = 0
        var rowHeight: CGFloat = 0
        for s in subviews {
            let size = s.sizeThatFits(.unspecified)
            if x + size.width > maxWidth {
                x = 0
                y += rowHeight + spacing
                rowHeight = 0
            }
            x += size.width + spacing
            rowHeight = max(rowHeight, size.height)
        }
        return CGSize(width: maxWidth == .infinity ? x : maxWidth, height: y + rowHeight)
    }

    func placeSubviews(in bounds: CGRect, proposal: ProposedViewSize, subviews: Subviews, cache: inout ()) {
        var x = bounds.minX
        var y = bounds.minY
        var rowHeight: CGFloat = 0
        for s in subviews {
            let size = s.sizeThatFits(.unspecified)
            if x + size.width > bounds.maxX {
                x = bounds.minX
                y += rowHeight + spacing
                rowHeight = 0
            }
            s.place(at: CGPoint(x: x, y: y), proposal: .unspecified)
            x += size.width + spacing
            rowHeight = max(rowHeight, size.height)
        }
    }
}
iOS 17 · 2 dependencies

Dependencies

SwiftUI
  • formaCard
  • FlowHStack

Requires iOS 17