swiftui-api-patterns

Enforce SwiftUI API usage rules for macOS view code.

Updated Mar 14, 2026
One-click install
npx skills add https://github.com/smykla-skalski/harness --skill swiftui-api-patterns
Or copy as Structured Prompt for Agent
Please help me install this Agent Skill.
Skill: swiftui-api-patterns
Source: https://github.com/smykla-skalski/harness/tree/main/.claude/skills/swiftui-api-patterns
Command: npx skills add https://github.com/smykla-skalski/harness --skill swiftui-api-patterns

SYSTEM DOCUMENTATION & REQUIREMENTS

What problem does it solve?

Provides a comprehensive, enforceable set of SwiftUI API usage rules to prevent subtle UI bugs, identity-loss during diffing, stale selection states, and inconsistent UX in the Harness Monitor macOS app.

Core Features & Use Cases

  • State management rules: guidance on when to use @State, @Binding, @Observable, @Bindable, @FocusState, @AppStorage and where state should live to avoid stale updates and unnecessary view recomputation.
  • View composition & identity: prescriptions to prefer struct views over free functions, extract subviews, avoid closure properties on view structs, and avoid identity-breaking modifier branches.
  • Selection and Picker safety: rules to always render matching tags, clamp selections before render, and provide regression test recommendations to prevent invalid selection warnings.
  • Button styling and accessibility: enforced use of native glass styles, prohibition of .plain, and requirement that custom behaviors conform to ButtonStyle.
  • Drag-and-drop best practices: unconditional .draggable installation, explicit drop rejection feedback, exhaustive DragSession.Phase handling, single-phase drag state, and macOS-only API guidance.
  • Navigation, lists, animation, layout, keyboard, window, and commands: targeted recommendations for NavigationStack, List usage, explicit animations, layout primitives, focus management, and macOS window/command patterns.
  • Anti-patterns and tests: lists of forbidden patterns (AnyView, work in initializers, index-based ForEach) and expectations for regression and store-level tests.

Quick Start

Ask the assistant to review a SwiftUI view file in apps/harness-monitor-macos/Sources and list any violations of the Harness Monitor SwiftUI API patterns, explaining why each issue breaks identity, state, or UX and suggesting a fix.

Frequently Asked Questions about swiftui-api-patterns

High-intent search queries and answers about installing and using this skill.

FAQPage Schema
How do I prevent stale selection states and identity loss during view diffing in SwiftUI?

Prevent stale selection states in SwiftUI by preferring struct views over free functions, extracting subviews, avoiding closure properties on view structs, and refraining from identity-breaking modifier branches during view diffing.

Why does my SwiftUI Picker show invalid selection warnings on macOS?

Invalid SwiftUI Picker selection warnings occur when tags are missing or mismatched. Resolve this by always rendering explicit matching tags, clamping selections before render, and adding regression tests to validate selection state.

What is the best way to handle drag and drop sessions in a SwiftUI macOS app?

Handle SwiftUI macOS drag and drop by unconditionally installing .draggable, providing explicit drop rejection feedback, managing single-phase drag state, and exhaustively handling every DragSession.Phase to prevent inconsistent UX.

When should I use @State vs @Observable for SwiftUI state management?

Use @State for local view state and @Observable or @Bindable for shared state to avoid unnecessary view recomputation. Proper placement prevents stale updates and ensures correct state propagation across your SwiftUI view hierarchy.

What SwiftUI anti-patterns cause UI bugs and regressions in macOS apps?

SwiftUI anti-patterns causing UI bugs include using AnyView, performing work in view initializers, index-based ForEach loops, and applying .plain button styles instead of conforming to native ButtonStyle protocols.

Can I use custom button styles in SwiftUI without breaking native macOS accessibility?

Use custom button styles in SwiftUI macOS by conforming behaviors to ButtonStyle and using native glass styles. Avoid .plain styles to maintain consistent accessibility and visual UX across the application.