Skip to content
Color Swatches preview
Rendered from the SwiftUI source on this page.

Color Swatches

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

A color picker row of real swatches - the selected color gets a ring and a checkmark.

Inputs · SwiftUI · iOS 17

  • inputs
  • swiftui

The actual source

ColorSwatchCard.swift
// Color Swatches · syxUI · https://syxui.dev/components/sel-color-swatch
// 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)
    }
}

// Color swatch picker - real colors, checked + ringed when selected (Etsy)
struct ColorSwatchCard: View {
    @State private var selected = 2
    let swatches: [Color] = [
        Color(red: 0.13, green: 0.13, blue: 0.14),
        Color(red: 0.90, green: 0.22, blue: 0.27),
        Color(red: 0.0, green: 0.48, blue: 1.0),
        Color(red: 0.20, green: 0.70, blue: 0.42),
        Color(red: 0.98, green: 0.62, blue: 0.11),
        Color(red: 0.60, green: 0.35, blue: 0.85),
    ]
    let names = ["Onyx", "Coral", "Ocean Blue", "Fern", "Amber", "Violet"]
    var body: some View {
        VStack(alignment: .leading, spacing: 14) {
            HStack {
                Text("Color").font(.system(size: 15, weight: .semibold))
                Spacer()
                Text(names[selected]).font(.system(size: 13)).foregroundStyle(.secondary)
            }
            HStack(spacing: 0) {
                ForEach(Array(swatches.enumerated()), id: \.offset) { i, c in
                    let on = selected == i
                    Circle().fill(c).frame(width: 32, height: 32)
                        .overlay {
                            if on { Image(systemName: "checkmark").font(.system(size: 12, weight: .bold)).foregroundStyle(.white) }
                        }
                        .overlay(Circle().stroke(Color.primary.opacity(on ? 0 : 0.08), lineWidth: 1))
                        .padding(3)
                        .overlay { if on { Circle().stroke(c, lineWidth: 2) } }
                        .contentShape(Circle())
                        .onTapGesture { withAnimation(.snappy) { selected = i } }
                    if i < swatches.count - 1 { Spacer(minLength: 0) }
                }
            }
        }.formaCard()
    }
}
iOS 17 · 1 dependencies

Dependencies

SwiftUI
  • formaCard

Requires iOS 17