Skip to content

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

TypeScript

feat(native): Button width projects to content or fill on native platforms (#8305)

Cindy Zhang585c603ba0f3
diff --git a/packages/native/apple/Sources/AstryxNative/Generated/Button.swift b/packages/native/apple/Sources/AstryxNative/Generated/Button.swift
index d89ec022a6..98daf6fb5e 100644
--- a/packages/native/apple/Sources/AstryxNative/Generated/Button.swift
+++ b/packages/native/apple/Sources/AstryxNative/Generated/Button.swift
@@ -16,6 +16,10 @@ public enum ButtonElevation: String, CaseIterable, Codable, Sendable {
case med = "med"
case high = "high"
}
+public enum ButtonWidth: String, CaseIterable, Codable, Sendable {
+ case content = "content"
+ case fill = "fill"
+}
#if os(iOS)
public struct ButtonProps {
/// Base props attach to this part on every platform.
@@ -28,10 +32,11 @@ public struct ButtonProps {
public var isLoading: Bool?
public var isInterruptible: Bool?
public var isDisabled: Bool?
+ public var width: ButtonWidth?
public var tooltip: String?
public var onClick: (() -> Void)?
public var href: String?
- public init(label: String, variant: ButtonVariant? = nil, size: ButtonSize? = nil, elevation: ButtonElevation? = nil, isLoading: Bool? = nil, isInterruptible: Bool? = nil, isDisabled: Bool? = nil, tooltip: String? = nil, onClick: (() -> Void)? = nil, href: String? = nil, base: AstryxBaseProps = AstryxBaseProps()) {
+ public init(label: String, variant: ButtonVariant? = nil, size: ButtonSize? = nil, elevation: ButtonElevation? = nil, isLoading: Bool? = nil, isInterruptible: Bool? = nil, isDisabled: Bool? = nil, width: ButtonWidth? = nil, tooltip: String? = nil, onClick: (() -> Void)? = nil, href: String? = nil, base: AstryxBaseProps = AstryxBaseProps()) {
self.label = label
self.variant = variant
self.size = size
@@ -39,6 +44,7 @@ public struct ButtonProps {
self.isLoading = isLoading
self.isInterruptible = isInterruptible
self.isDisabled = isDisabled
+ self.width = width
self.tooltip = tooltip
self.onClick = onClick
self.href = href
@@ -54,6 +60,7 @@ public struct ButtonNode: Decodable, Equatable, Sendable {
public var isLoading: Bool?
public var isInterruptible: Bool?
public var isDisabled: Bool?
+ public var width: ButtonWidth?
public var tooltip: String?
public var href: String?
}
@@ -76,6 +83,7 @@ public struct ButtonNode: Decodable, Equatable, Sendable {
isLoading: props.isLoading,
isInterruptible: props.isInterruptible,
isDisabled: props.isDisabled,
+ width: props.width,
tooltip: props.tooltip,
onClick: listens("onClick") ? { send(AstryxAction(key: key, component: "Button", event: "onClick", args: [])) } : nil,
href: props.href,
@@ -96,10 +104,11 @@ public struct ButtonProps {
public var isLoading: Bool?
public var isInterruptible: Bool?
public var isDisabled: Bool?
+ public var width: ButtonWidth?
public var tooltip: String?
public var onClick: (() -> Void)?
public var href: String?
- public init(label: String, variant: ButtonVariant? = nil, size: ButtonSize? = nil, elevation: ButtonElevation? = nil, isLoading: Bool? = nil, isInterruptible: Bool? = nil, isDisabled: Bool? = nil, tooltip: String? = nil, onClick: (() -> Void)? = nil, href: String? = nil, base: AstryxBaseProps = AstryxBaseProps()) {
+ public init(label: String, variant: ButtonVariant? = nil, size: ButtonSize? = nil, elevation: ButtonElevation? = nil, isLoading: Bool? = nil, isInterruptible: Bool? = nil, isDisabled: Bool? = nil, width: ButtonWidth? = nil, tooltip: String? = nil, onClick: (() -> Void)? = nil, href: String? = nil, base: AstryxBaseProps = AstryxBaseProps()) {
self.label = label
self.variant = variant
self.size = size
@@ -107,6 +116,7 @@ public struct ButtonProps {
self.isLoading = isLoading
self.isInterruptible = isInterruptible
self.isDisabled = isDisabled
+ self.width = width
self.tooltip = tooltip
self.onClick = onClick
self.href = href
@@ -122,6 +132,7 @@ public struct ButtonNode: Decodable, Equatable, Sendable {
public var isLoading: Bool?
public var isInterruptible: Bool?
public var isDisabled: Bool?
+ public var width: ButtonWidth?
public var tooltip: String?
public var href: String?
}
@@ -144,6 +155,7 @@ public struct ButtonNode: Decodable, Equatable, Sendable {
isLoading: props.isLoading,
isInterruptible: props.isInterruptible,
isDisabled: props.isDisabled,
+ width: props.width,
tooltip: props.tooltip,
onClick: listens("onClick") ? { send(AstryxAction(key: key, component: "Button", event: "onClick", args: [])) } : nil,
href: props.href,
diff --git a/packages/native/src/components/Button.ts b/packages/native/src/components/Button.ts
index 4e5e1ee6f9..332f825c62 100644
--- a/packages/native/src/components/Button.ts
+++ b/packages/native/src/components/Button.ts
@@ -15,6 +15,7 @@ export const button: NativeAdditions = {
baseTarget: 'root',
callbacks: ['onClick'],
sameMeaning: {onClick: 'a press, as in Compose'},
+ projections: {width: {content: 'auto', fill: '100%'}},
webOnly: {
type: 'HTML form submission type',
name: 'HTML form field name',
diff --git a/packages/native/src/contract/contracts.test.ts b/packages/native/src/contract/contracts.test.ts
index 6aca001c22..80a9ad4082 100644
--- a/packages/native/src/contract/contracts.test.ts
+++ b/packages/native/src/contract/contracts.test.ts
@@ -68,6 +68,16 @@ describe('native contracts', () => {
kind: 'fn',
args: [],
});
+ // AST-082 FR4: Button `width` projects the web's size value to the two
+ // values every platform supports; other widths stay on the web.
+ expect(byName.get('Button')!.core.width?.type).toEqual({
+ kind: 'enum',
+ values: ['content', 'fill'],
+ });
+ expect(byName.get('Button')!.projections?.width).toEqual({
+ content: 'auto',
+ fill: '100%',
+ });
expect(byName.get('Heading')!.core.children?.type).toEqual({
kind: 'string',
});
diff --git a/packages/native/src/contract/fromComponentDoc.test.ts b/packages/native/src/contract/fromComponentDoc.test.ts
index c4f02e7f35..5638c00fcc 100644
--- a/packages/native/src/contract/fromComponentDoc.test.ts
+++ b/packages/native/src/contract/fromComponentDoc.test.ts
@@ -52,6 +52,35 @@ describe('fromComponentDoc', () => {
expect(contract.webOnly.map(prop => prop.name)).toContain('onPick');
});
+ it('projects a wider web type to its portable values, each one web value', () => {
+ const sized = fromComponentDoc(
+ {
+ name: 'Box',
+ props: [{name: 'width', type: 'SizeValue', description: 'Width.'}],
+ },
+ {
+ name: 'Box',
+ baseTarget: 'root',
+ projections: {width: {content: 'auto', fill: '100%'}},
+ },
+ );
+ expect(sized.core.width).toEqual({
+ type: {kind: 'enum', values: ['content', 'fill']},
+ optional: true,
+ doc: 'Width. Native values: `content` is the web\'s `"auto"`, `fill` is the web\'s `"100%"`.',
+ });
+ expect(sized.webOnly).toEqual([]);
+ const webValues = Object.values(sized.projections?.width ?? {});
+ expect(new Set(webValues).size).toBe(webValues.length);
+ // Without a projection the same prop stays on the web.
+ const plain = fromComponentDoc(
+ {name: 'Box', props: [{name: 'width', type: 'SizeValue'}]},
+ {name: 'Box', baseTarget: 'root'},
+ );
+ expect(plain.core).not.toHaveProperty('width');
+ expect(plain.webOnly.map(prop => prop.name)).toContain('width');
+ });
+
it('refuses a toolkit name unless the component declares the same meaning', () => {
const plain = fromComponentDoc(doc, {
name: 'Toggle',
diff --git a/packages/native/src/contract/fromComponentDoc.ts b/packages/native/src/contract/fromComponentDoc.ts
index 9f5c776170..4487d49036 100644
--- a/packages/native/src/contract/fromComponentDoc.ts
+++ b/packages/native/src/contract/fromComponentDoc.ts
@@ -82,6 +82,16 @@ export interface NativeAdditions {
/** Documented types renamed to a native definition, e.g. `SelectorOption[]`. */
readonly typeNames?: Readonly<Record<string, string>>;
readonly platforms?: Readonly<Partial<Record<NativePlatform, PropMap>>>;
+ /**
+ * Props whose web type is wider than a portable kind, with the closed set of
+ * values every platform supports, each mapped to the one web value it means
+ * (AST-082 FR4), such as Button `width`: `content` is the web's `'auto'`
+ * and `fill` its `'100%'`. The first value is the default. The prop's other
+ * web values stay in the web contract.
+ */
+ readonly projections?: Readonly<
+ Record<string, Readonly<Record<string, string>>>
+ >;
/**
* Portable-typed props that still belong to the web contract, with the
* reason, such as a web presentation heuristic a native platform replaces.
@@ -118,6 +128,7 @@ export function fromComponentDoc(
): ComponentContract {
const core: Record<string, PropSpec> = {};
const webOnly: WebOnlyProp[] = [];
+ const projections: Record<string, Readonly<Record<string, string>>> = {};
const types = additions.types ?? {};
const describe = (prop: DocProp) =>
typeof prop.description === 'string' ? {doc: prop.description} : {};
@@ -144,6 +155,22 @@ export function fromComponentDoc(
webOnly.push({name: prop.name, reason: 'HTML attribute'});
continue;
}
+ const projection = additions.projections?.[prop.name];
+ if (projection) {
+ const mapping = Object.entries(projection)
+ .map(
+ ([value, web]) =>
+ `\`${value}\` is the web's \`${JSON.stringify(web)}\``,
+ )
+ .join(', ');
+ core[prop.name] = {
+ type: {kind: 'enum', values: Object.keys(projection)},
+ optional: prop.required !== true,
+ doc: `${typeof prop.description === 'string' ? `${prop.description} ` : ''}Native values: ${mapping}.`,
+ };
+ projections[prop.name] = projection;
+ continue;
+ }
const slot = additions.slots?.[prop.name];
const slotNameProblem =
slot &&
@@ -221,6 +248,7 @@ export function fromComponentDoc(
core,
platforms: additions.platforms ?? {},
webOnly,
+ projections,
sameMeaning: Object.keys(additions.sameMeaning ?? {}).filter(
name => name in core,
),
diff --git a/packages/native/src/contract/types.ts b/packages/native/src/contract/types.ts
index f9d72d5544..0d98539da6 100644
--- a/packages/native/src/contract/types.ts
+++ b/packages/native/src/contract/types.ts
@@ -53,6 +53,13 @@ export interface ComponentContract {
readonly platforms: Readonly<Partial<Record<NativePlatform, PropMap>>>;
/** Core-doc props that stay in the web contract. */
readonly webOnly: readonly WebOnlyProp[];
+ /**
+ * Core props projected from a wider web type: each portable value and the
+ * one web value it means (AST-082 FR4).
+ */
+ readonly projections?: Readonly<
+ Record<string, Readonly<Record<string, string>>>
+ >;
/** Core props that share a toolkit name with the same meaning (FR8). */
readonly sameMeaning?: readonly string[];
}