Skip to content

Add custom slot resolvers via .custom(Resolver.self)#3

Closed
kylebshr wants to merge 2 commits intomainfrom
custom-slot-resolvers
Closed

Add custom slot resolvers via .custom(Resolver.self)#3
kylebshr wants to merge 2 commits intomainfrom
custom-slot-resolvers

Conversation

@kylebshr
Copy link
Copy Markdown
Owner

Summary

  • Adds SlotResolver protocol allowing consumers to define custom slot options that map input types to views
  • The @Slot(.custom(Resolver.self)) annotation generates constrained extensions using Resolver.Input, Resolver.Output, and Resolver.makeView()
  • Supports .unlabeled modifier (.custom(R.self).unlabeled) and composition with built-in options (.text, .custom(R.self))
  • Includes a PriorityPriorityBadge example in SlotExamples

Example usage

// Define a resolver
enum PriorityResolver: SlotResolver {
    typealias Input = Priority
    typealias Output = PriorityBadge
    static func makeView(_ input: Priority) -> PriorityBadge {
        PriorityBadge(priority: input)
    }
}

// Use it in a component
@Slots struct TaskRow<Title: View, Badge: View>: View {
    @Slot(.text) var title: Title
    @Slot(.custom(PriorityResolver.self)) var badge: Badge?
}

// Call sites
TaskRow(title: "Buy groceries", badge: .high)
TaskRow(title: "No priority")
TaskRow(title: "Custom badge") { Image(systemName: "star.fill") }

Test plan

  • 4 new macro expansion tests (basic, optional, unlabeled, composition with built-in)
  • Existing tests pass (including restored .text.unlabeled test)
  • Example compiles and builds successfully

🤖 Generated with Claude Code

kylebshr and others added 2 commits March 30, 2026 13:49
…e to SlotExamples

Add `.unlabeled` modifier to `.text` slot option, generating `_ name:` instead
of `name:` in text/string convenience inits to match `Button(_ title:)` ergonomics.
Split monolithic Examples.swift into one file per component and rename target
from SlotsExamples to SlotExamples.

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
Allows consumers to define custom slot options by conforming to the
SlotResolver protocol and referencing the resolver type in @slot
annotations. The macro generates constrained extensions using
Resolver.Input, Resolver.Output, and Resolver.makeView().

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
@kylebshr kylebshr closed this Mar 31, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant