Skip to content

An open source design system that's fully customizable and agent ready

TypeScript

feat(native): Switch labelPosition and labelSpacing leave the Swift contract (#8445)

Cindy Zhangcdd75aecacc2
diff --git a/packages/native/apple/Sources/AstryxNative/Generated/Switch.swift b/packages/native/apple/Sources/AstryxNative/Generated/Switch.swift
index 6f1c2c97ad..ccba922aee 100644
--- a/packages/native/apple/Sources/AstryxNative/Generated/Switch.swift
+++ b/packages/native/apple/Sources/AstryxNative/Generated/Switch.swift
@@ -16,14 +16,6 @@ public struct SwitchStatus: Equatable, Codable, Sendable {
self.message = message
}
}
-public enum SwitchLabelPosition: String, CaseIterable, Codable, Sendable {
- case start = "start"
- case end = "end"
-}
-public enum SwitchLabelSpacing: String, CaseIterable, Codable, Sendable {
- case hug = "hug"
- case spread = "spread"
-}
#if os(iOS)
public struct SwitchProps {
/// Base props attach to this part on every platform.
@@ -42,9 +34,7 @@ public struct SwitchProps {
public var isRequired: Bool?
public var status: SwitchStatus?
public var labelTooltip: String?
- public var labelPosition: SwitchLabelPosition?
- public var labelSpacing: SwitchLabelSpacing?
- public init(label: String, value: Bool, onChange: ((Bool) -> Void)? = nil, isLoading: Bool? = nil, isLabelHidden: Bool? = nil, description: String? = nil, isDisabled: Bool? = nil, size: SwitchSize? = nil, disabledMessage: String? = nil, isOptional: Bool? = nil, isRequired: Bool? = nil, status: SwitchStatus? = nil, labelTooltip: String? = nil, labelPosition: SwitchLabelPosition? = nil, labelSpacing: SwitchLabelSpacing? = nil, base: AstryxBaseProps = AstryxBaseProps()) {
+ public init(label: String, value: Bool, onChange: ((Bool) -> Void)? = nil, isLoading: Bool? = nil, isLabelHidden: Bool? = nil, description: String? = nil, isDisabled: Bool? = nil, size: SwitchSize? = nil, disabledMessage: String? = nil, isOptional: Bool? = nil, isRequired: Bool? = nil, status: SwitchStatus? = nil, labelTooltip: String? = nil, base: AstryxBaseProps = AstryxBaseProps()) {
self.label = label
self.value = value
self.onChange = onChange
@@ -58,8 +48,6 @@ public struct SwitchProps {
self.isRequired = isRequired
self.status = status
self.labelTooltip = labelTooltip
- self.labelPosition = labelPosition
- self.labelSpacing = labelSpacing
self.base = base
}
}
@@ -77,8 +65,6 @@ public struct SwitchNode: Decodable, Equatable, Sendable {
public var isRequired: Bool?
public var status: SwitchStatus?
public var labelTooltip: String?
- public var labelPosition: SwitchLabelPosition?
- public var labelSpacing: SwitchLabelSpacing?
}
public let key: String
public let base: AstryxBaseProps?
@@ -105,8 +91,6 @@ public struct SwitchNode: Decodable, Equatable, Sendable {
isRequired: props.isRequired,
status: props.status,
labelTooltip: props.labelTooltip,
- labelPosition: props.labelPosition,
- labelSpacing: props.labelSpacing,
base: base ?? AstryxBaseProps()
)
}
@@ -130,9 +114,7 @@ public struct SwitchProps {
public var isRequired: Bool?
public var status: SwitchStatus?
public var labelTooltip: String?
- public var labelPosition: SwitchLabelPosition?
- public var labelSpacing: SwitchLabelSpacing?
- public init(label: String, value: Bool, onChange: ((Bool) -> Void)? = nil, isLoading: Bool? = nil, isLabelHidden: Bool? = nil, description: String? = nil, isDisabled: Bool? = nil, size: SwitchSize? = nil, disabledMessage: String? = nil, isOptional: Bool? = nil, isRequired: Bool? = nil, status: SwitchStatus? = nil, labelTooltip: String? = nil, labelPosition: SwitchLabelPosition? = nil, labelSpacing: SwitchLabelSpacing? = nil, base: AstryxBaseProps = AstryxBaseProps()) {
+ public init(label: String, value: Bool, onChange: ((Bool) -> Void)? = nil, isLoading: Bool? = nil, isLabelHidden: Bool? = nil, description: String? = nil, isDisabled: Bool? = nil, size: SwitchSize? = nil, disabledMessage: String? = nil, isOptional: Bool? = nil, isRequired: Bool? = nil, status: SwitchStatus? = nil, labelTooltip: String? = nil, base: AstryxBaseProps = AstryxBaseProps()) {
self.label = label
self.value = value
self.onChange = onChange
@@ -146,8 +128,6 @@ public struct SwitchProps {
self.isRequired = isRequired
self.status = status
self.labelTooltip = labelTooltip
- self.labelPosition = labelPosition
- self.labelSpacing = labelSpacing
self.base = base
}
}
@@ -165,8 +145,6 @@ public struct SwitchNode: Decodable, Equatable, Sendable {
public var isRequired: Bool?
public var status: SwitchStatus?
public var labelTooltip: String?
- public var labelPosition: SwitchLabelPosition?
- public var labelSpacing: SwitchLabelSpacing?
}
public let key: String
public let base: AstryxBaseProps?
@@ -193,8 +171,6 @@ public struct SwitchNode: Decodable, Equatable, Sendable {
isRequired: props.isRequired,
status: props.status,
labelTooltip: props.labelTooltip,
- labelPosition: props.labelPosition,
- labelSpacing: props.labelSpacing,
base: base ?? AstryxBaseProps()
)
}
diff --git a/packages/native/specs/swift/library/component-api.md b/packages/native/specs/swift/library/component-api.md
index 3a92cad431..227ab0302a 100644
--- a/packages/native/specs/swift/library/component-api.md
+++ b/packages/native/specs/swift/library/component-api.md
@@ -154,10 +154,8 @@ same generated contract.
- `AstryxNodeView` draws Card, Heading, Icon, and Text wire nodes. Any other
node, HStack and VStack included, renders in its place as unsupported, and
its siblings still render, until the logic host draws stacks (A9).
-- The generated iOS `SwitchProps` still declares `size`, the generated
- `SwitchProps` still declare `labelPosition` and `labelSpacing` on iOS and
- macOS, and `HStackProps` and `VStackProps` are still generated for iOS and
- macOS.
+- The generated iOS `SwitchProps` still declares `size`, and `HStackProps` and
+ `VStackProps` are still generated for iOS and macOS.
## Open questions
diff --git a/packages/native/src/components/Switch.ts b/packages/native/src/components/Switch.ts
index dba734db03..bf80c55b65 100644
--- a/packages/native/src/components/Switch.ts
+++ b/packages/native/src/components/Switch.ts
@@ -14,4 +14,17 @@ export const switchControl: NativeAdditions = {
name: 'Switch',
// The web Switch forwards base props to its root wrapper.
baseTarget: 'root',
+ // Apple switch rows follow the system toggle layout, label leading and
+ // system spacing (Swift component API A2); Android and Windows keep both
+ // props until a ruling covers them.
+ onlyOn: {
+ labelPosition: {
+ platforms: ['android', 'windows'],
+ reason: 'Apple rows follow the system toggle layout (component API A2)',
+ },
+ labelSpacing: {
+ platforms: ['android', 'windows'],
+ reason: "Apple rows use the system's spacing (component API A2)",
+ },
+ },
};
diff --git a/packages/native/src/contract/contracts.test.ts b/packages/native/src/contract/contracts.test.ts
index efc703e011..69b2969dd1 100644
--- a/packages/native/src/contract/contracts.test.ts
+++ b/packages/native/src/contract/contracts.test.ts
@@ -52,6 +52,20 @@ describe('native contracts', () => {
expect(selector!.core).not.toHaveProperty('controlXstyle');
});
+ it('moves a prop only some native platforms carry out of the core and onto those platforms', async () => {
+ const {contracts} = await buildContracts(loadCoreDoc);
+ const switchContract = contracts.find(
+ contract => contract.name === 'Switch',
+ )!;
+ for (const name of ['labelPosition', 'labelSpacing']) {
+ expect(switchContract.core).not.toHaveProperty(name);
+ expect(switchContract.platforms.android).toHaveProperty(name);
+ expect(switchContract.platforms.windows).toHaveProperty(name);
+ expect(switchContract.platforms.ios ?? {}).not.toHaveProperty(name);
+ expect(switchContract.platforms.macos ?? {}).not.toHaveProperty(name);
+ }
+ });
+
it('keeps value callbacks, typed text slots, and number sets in the core', async () => {
const {contracts} = await buildContracts(loadCoreDoc);
const byName = new Map(
diff --git a/packages/native/src/contract/fromComponentDoc.ts b/packages/native/src/contract/fromComponentDoc.ts
index 0f9898ac91..42dae22cd9 100644
--- a/packages/native/src/contract/fromComponentDoc.ts
+++ b/packages/native/src/contract/fromComponentDoc.ts
@@ -98,6 +98,18 @@ export interface NativeAdditions {
* reason, such as a web presentation heuristic a native platform replaces.
*/
readonly webOnly?: Readonly<Record<string, string>>;
+ /**
+ * Portable core-doc props that only some native platforms carry, with those
+ * platforms and the reason (AST-082 FR3): the prop leaves the core and joins
+ * each listed platform's contract unchanged, so a platform a ruling covers
+ * drops it while the others keep it.
+ */
+ readonly onlyOn?: Readonly<
+ Record<
+ string,
+ {readonly platforms: readonly NativePlatform[]; readonly reason: string}
+ >
+ >;
}
/** The documented props of the component the additions name. */
@@ -243,12 +255,34 @@ export function fromComponentDoc(
};
}
+ const platforms: Partial<Record<NativePlatform, Record<string, PropSpec>>> =
+ Object.fromEntries(
+ Object.entries(additions.platforms ?? {}).map(([p, props]) => [
+ p,
+ {...props},
+ ]),
+ );
+ for (const [name, {platforms: only}] of Object.entries(
+ additions.onlyOn ?? {},
+ )) {
+ const spec = core[name];
+ if (!spec) {
+ throw new Error(
+ `${additions.name}: onlyOn names "${name}", which is not a portable core prop`,
+ );
+ }
+ delete core[name];
+ for (const platform of only) {
+ platforms[platform] = {...platforms[platform], [name]: spec};
+ }
+ }
+
return {
name: additions.name,
baseTarget: additions.baseTarget,
types,
core,
- platforms: additions.platforms ?? {},
+ platforms,
webOnly,
projections,
sameMeaning: Object.keys(additions.sameMeaning ?? {}).filter(