An open source design system that's fully customizable and agent ready
TypeScript
feat(native): Badge contract (#8028)
Cindy Zhangd76a228ef05f
diff --git a/packages/native/README.md b/packages/native/README.mdindex 4c433ad09c..ba9441e090 100644--- a/packages/native/README.md+++ b/packages/native/README.md@@ -22,9 +22,9 @@ rests on evidence.| Wire | `AstryxNode`, `AstryxAction` | Each component as JSON a TypeScript core sends a native host (arm C), and the action a host sends back when a view calls a callback. Swift decodes `<Name>Node` and turns it into `<Name>Props` with `viewProps(send:)`. || Logic core | `@astryxdesign/native/logic` | `createLogicCore` holds a screen's state, builds its node tree, and reduces actions; `connectBridge` carries it to a native host as JSON. ECMAScript built-ins only, so it runs in Hermes. Shaped after EJ Campbell's astryx-swift spike. |-Contracts cover Button, Card, Divider, Heading, HStack, ProgressBar, Selector,-Switch, Text, TextInput, and VStack. React Native and Android bindings are not built yet; see-[the experiment matrix](#experiment-matrix).+Contracts cover Badge, Button, Card, Divider, Heading, HStack, ProgressBar,+Selector, Switch, Text, TextInput, and VStack. React Native and Android+bindings are not built yet; see [the experiment matrix](#experiment-matrix).```ts// tsconfig.json for an iOS app: "customConditions": ["ios"]diff --git a/packages/native/apple/Sources/AstryxNative/Generated/AstryxNode.swift b/packages/native/apple/Sources/AstryxNative/Generated/AstryxNode.swiftindex 7ccf725f3a..7b3b7c66d9 100644--- a/packages/native/apple/Sources/AstryxNative/Generated/AstryxNode.swift+++ b/packages/native/apple/Sources/AstryxNative/Generated/AstryxNode.swift@@ -7,6 +7,7 @@ import Foundation/// or a known one with a value its enums lack or a required prop missing. An older/// app then keeps drawing the rest of a newer core's tree.public enum AstryxNode: Decodable, Equatable, Sendable {+ case badge(BadgeNode)case button(ButtonNode)case card(CardNode)case divider(DividerNode)@@ -22,6 +23,7 @@ public enum AstryxNode: Decodable, Equatable, Sendable {public var key: String {switch self {+ case .badge(let node): return node.keycase .button(let node): return node.keycase .card(let node): return node.keycase .divider(let node): return node.key@@ -45,6 +47,7 @@ public enum AstryxNode: Decodable, Equatable, Sendable {let key = try container.decode(String.self, forKey: .key)do {switch component {+ case "Badge": self = .badge(try BadgeNode(from: decoder))case "Button": self = .button(try ButtonNode(from: decoder))case "Card": self = .card(try CardNode(from: decoder))case "Divider": self = .divider(try DividerNode(from: decoder))diff --git a/packages/native/apple/Sources/AstryxNative/Generated/Badge.swift b/packages/native/apple/Sources/AstryxNative/Generated/Badge.swiftnew file mode 100644index 0000000000..f5603af1f7--- /dev/null+++ b/packages/native/apple/Sources/AstryxNative/Generated/Badge.swift@@ -0,0 +1,89 @@+// Generated by @astryxdesign/native. Do not edit.+public enum BadgeVariant: String, CaseIterable, Codable, Sendable {+ case neutral = "neutral"+ case info = "info"+ case success = "success"+ case warning = "warning"+ case error = "error"+ case blue = "blue"+ case cyan = "cyan"+ case green = "green"+ case orange = "orange"+ case pink = "pink"+ case purple = "purple"+ case red = "red"+ case teal = "teal"+ case yellow = "yellow"+}+#if os(iOS)+public struct BadgeProps {+ /// Base props attach to this part on every platform.+ public static let baseTarget: AstryxBaseTarget = .root+ public var base: AstryxBaseProps+ public var variant: BadgeVariant?+ public var label: String+ public init(variant: BadgeVariant? = nil, label: String, base: AstryxBaseProps = AstryxBaseProps()) {+ self.variant = variant+ self.label = label+ self.base = base+ }+}+public struct BadgeNode: Decodable, Equatable, Sendable {+ public struct Props: Decodable, Equatable, Sendable {+ public var variant: BadgeVariant?+ public var label: String+ }+ public let key: String+ public let base: AstryxBaseProps?+ public let props: Props+ /// The callbacks the core listens to, by prop name.+ public let events: [String]?++ public func listens(_ event: String) -> Bool { events?.contains(event) ?? false }++ /// The contract a Badge view takes; its callbacks send actions to the core.+ public func viewProps(send: @escaping @Sendable (AstryxAction) -> Void) -> BadgeProps {+ return BadgeProps(+ variant: props.variant,+ label: props.label,+ base: base ?? AstryxBaseProps()+ )+ }+}+#endif+#if os(macOS)+public struct BadgeProps {+ /// Base props attach to this part on every platform.+ public static let baseTarget: AstryxBaseTarget = .root+ public var base: AstryxBaseProps+ public var variant: BadgeVariant?+ public var label: String+ public init(variant: BadgeVariant? = nil, label: String, base: AstryxBaseProps = AstryxBaseProps()) {+ self.variant = variant+ self.label = label+ self.base = base+ }+}+public struct BadgeNode: Decodable, Equatable, Sendable {+ public struct Props: Decodable, Equatable, Sendable {+ public var variant: BadgeVariant?+ public var label: String+ }+ public let key: String+ public let base: AstryxBaseProps?+ public let props: Props+ /// The callbacks the core listens to, by prop name.+ public let events: [String]?++ public func listens(_ event: String) -> Bool { events?.contains(event) ?? false }++ /// The contract a Badge view takes; its callbacks send actions to the core.+ public func viewProps(send: @escaping @Sendable (AstryxAction) -> Void) -> BadgeProps {+ return BadgeProps(+ variant: props.variant,+ label: props.label,+ base: base ?? AstryxBaseProps()+ )+ }+}+#endifdiff --git a/packages/native/apple/Tests/AstryxNativeTests/ControlPropsTests.swift b/packages/native/apple/Tests/AstryxNativeTests/ControlPropsTests.swiftindex b1e46da406..d42c287174 100644--- a/packages/native/apple/Tests/AstryxNativeTests/ControlPropsTests.swift+++ b/packages/native/apple/Tests/AstryxNativeTests/ControlPropsTests.swift@@ -15,6 +15,8 @@ import Testinglet heading = HeadingProps(level: .v2, children: "Orders")#expect(heading.level.rawValue == 2)#expect(heading.children == "Orders")+ let badge = BadgeProps(variant: .success, label: "Paid")+ #expect(badge.label == "Paid")let divider = DividerProps(label: "Or")#expect(divider.label == "Or")}diff --git a/packages/native/src/components/Badge.ts b/packages/native/src/components/Badge.tsnew file mode 100644index 0000000000..0283e47707--- /dev/null+++ b/packages/native/src/components/Badge.ts@@ -0,0 +1,18 @@+// Copyright (c) Meta Platforms, Inc. and affiliates.++/**+ * @file components/Badge.ts+ * @input None+ * @output What @astryxdesign/native adds to the core Badge contract+ * @position Native additions read by fromComponentDoc together with core's+ * Badge documentation+ */++import type {NativeAdditions} from '../contract/fromComponentDoc.js';++export const badge: NativeAdditions = {+ name: 'Badge',+ baseTarget: 'root',+ // `icon` stays web-only until the core has an icon slot.+ slots: {label: 'text'},+};diff --git a/packages/native/src/components/index.ts b/packages/native/src/components/index.tsindex c693941bd1..69997380db 100644--- a/packages/native/src/components/index.ts+++ b/packages/native/src/components/index.ts@@ -9,6 +9,7 @@*/import type {NativeAdditions} from '../contract/fromComponentDoc.js';+import {badge} from './Badge.js';import {button} from './Button.js';import {card} from './Card.js';import {divider} from './Divider.js';@@ -22,6 +23,7 @@ import {textInput} from './TextInput.js';import {vStack} from './VStack.js';export const NATIVE_COMPONENTS: readonly NativeAdditions[] = [+ badge,button,card,divider,diff --git a/packages/native/src/contract/contracts.test.ts b/packages/native/src/contract/contracts.test.tsindex 3701994072..6aca001c22 100644--- a/packages/native/src/contract/contracts.test.ts+++ b/packages/native/src/contract/contracts.test.ts@@ -76,6 +76,13 @@ describe('native contracts', () => {values: [1, 2, 3, 4, 5, 6],});expect(byName.get('Text')!.core).not.toHaveProperty('id');+ expect(byName.get('Badge')!.core.label).toMatchObject({+ type: {kind: 'string'},+ optional: false,+ });+ expect(byName.get('Badge')!.webOnly.map(prop => prop.name)).toContain(+ 'icon',+ );});it('reports naming and portability errors', () => {