Skip to content
Rating Breakdown preview
Rendered from the SwiftUI source on this page.

Rating Breakdown

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

A rating summary that expands to a star distribution.

Accordions · SwiftUI · iOS 17

  • accordion
  • swiftui

The actual source

AccReviewBreakdown.swift
// Rating Breakdown · syxUI · https://syxui.dev/components/acc-review-breakdown
// 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)
    }
}

// Rating breakdown accordion
struct AccReviewBreakdown: View {
    @State private var open = true
    let dist = [(5, 0.7), (4, 0.2), (3, 0.06), (2, 0.02), (1, 0.02)]
    var body: some View {
        VStack(spacing: 12) {
            HStack(spacing: 10) {
                Text("4.8").font(.system(size: 28, weight: .bold))
                VStack(alignment: .leading, spacing: 2) {
                    HStack(spacing: 2) { ForEach(0..<5, id: \.self) { _ in Image(systemName: "star.fill").font(.system(size: 11)).foregroundStyle(.orange) } }
                    Text("2,140 reviews").font(.system(size: 12)).foregroundStyle(.secondary)
                }
                Spacer()
                Image(systemName: "chevron.down").font(.system(size: 13, weight: .semibold)).foregroundStyle(.secondary).rotationEffect(.degrees(open ? 180 : 0))
            }.contentShape(Rectangle()).onTapGesture { open.toggle() }
            if open {
                VStack(spacing: 6) {
                    ForEach(Array(dist.enumerated()), id: \.offset) { _, d in
                        HStack(spacing: 8) {
                            Text("\(d.0)").font(.system(size: 12)).foregroundStyle(.secondary).frame(width: 10)
                            GeometryReader { g in
                                ZStack(alignment: .leading) {
                                    Capsule().fill(Color.primary.opacity(0.08)).frame(height: 6)
                                    Capsule().fill(Color.orange).frame(width: g.size.width * d.1, height: 6)
                                }
                            }.frame(height: 6)
                        }
                    }
                }
            }
        }.formaCard().frame(width: 300)
    }
}
iOS 17 · No dependencies

Dependencies

SwiftUI
No external dependencies

Requires iOS 17