@antglobal/copilot-cards-web 1.0.6 → 1.0.8

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/README.md CHANGED
@@ -7,7 +7,7 @@ The package includes the core schema and action APIs, responsive rendering, Shad
7
7
  ## Installation
8
8
 
9
9
  ```bash
10
- npm install @antglobal/copilot-cards-web
10
+ npm install @antglobal/copilot-cards-web@1.0.7
11
11
  ```
12
12
 
13
13
  `@antglobal/copilot-cards-core` is installed automatically.
@@ -50,6 +50,50 @@ card.updateVariables({ userName: "Alice" });
50
50
  card.dispose();
51
51
  ```
52
52
 
53
+ ## Stable host updates
54
+
55
+ Create the card once and keep its `CardInstance`. Input events should update
56
+ only the variables that changed. Do not call `renderCard()` again for input updates,
57
+ because remounting the card recreates image elements and can cause visible flicker.
58
+
59
+ ```ts
60
+ import {
61
+ renderCard,
62
+ type CardInstance,
63
+ type CardSchema,
64
+ } from "@antglobal/copilot-cards-web";
65
+
66
+ let card: CardInstance | null = null;
67
+
68
+ export function mountDataRechargeCard(
69
+ container: HTMLElement,
70
+ schema: CardSchema,
71
+ ): void {
72
+ card?.dispose();
73
+ card = renderCard(container, schema, {
74
+ emit: (eventName, payload) => {
75
+ if (eventName !== "dataRecharge.phone.input") return;
76
+
77
+ const value = String(
78
+ (payload as { value?: unknown } | undefined)?.value ?? "",
79
+ );
80
+ if (!card) return;
81
+ card.updateVariables({ phoneNumber: value });
82
+ },
83
+ });
84
+ }
85
+
86
+ export function unmountDataRechargeCard(): void {
87
+ card?.dispose();
88
+ card = null;
89
+ }
90
+ ```
91
+
92
+ Keep `skuOptions` and `isSkuLoading` out of each keystroke patch unless their
93
+ values actually changed. Fetching or filtering SKU data can run from the
94
+ `dataRecharge.phone.commit` event instead of remounting the card on every
95
+ `dataRecharge.phone.input` event.
96
+
53
97
  ## Framework usage
54
98
 
55
99
  The renderer accesses browser APIs and should be loaded on the client in frameworks that perform server-side rendering.
package/dist/index.d.ts CHANGED
@@ -29,6 +29,33 @@ interface WebActionContextOptions {
29
29
  */
30
30
  declare function createWebActionContext(options?: WebActionContextOptions): ActionRunnerContext;
31
31
 
32
+ interface CardNodeHandle {
33
+ /** Stable schema/runtime node id represented by this logical handle. */
34
+ readonly id: string;
35
+ /** Current rendered node type, or null while the node is unavailable. */
36
+ readonly type: string | null;
37
+ /**
38
+ * Current custom-element host. This is a transient snapshot and may change
39
+ * after any render; consumers should keep the handle instead of this value.
40
+ */
41
+ readonly current: HTMLElement | null;
42
+ /** Measure the current custom-element host. */
43
+ getRect(): DOMRectReadOnly | null;
44
+ /** Focus the component's logical control. */
45
+ focus(options?: FocusOptions): boolean;
46
+ /** Blur the component's logical control. */
47
+ blur(): boolean;
48
+ /** Scroll the current custom-element host into view. */
49
+ scrollIntoView(options?: ScrollIntoViewOptions): boolean;
50
+ }
51
+ interface CardFocusChangeEvent {
52
+ /** Currently focused logical card node, or null after focus leaves the card. */
53
+ node: CardNodeHandle | null;
54
+ /** Previously focused logical card node, if any. */
55
+ previousNode: CardNodeHandle | null;
56
+ }
57
+ type CardFocusChangeListener = (event: CardFocusChangeEvent) => void;
58
+
32
59
  interface ResponsiveMobileOptions {
33
60
  readonly unit: 'rem';
34
61
  readonly rootValue: number;
@@ -77,6 +104,10 @@ interface CardInstance {
77
104
  dispose: () => void;
78
105
  /** Update variables and re-render. */
79
106
  updateVariables: (variables: Record<string, any>) => void;
107
+ /** Get a stable logical handle that resolves the current host lazily. */
108
+ getNode: (id: string) => CardNodeHandle;
109
+ /** Observe logical focus changes within this card. */
110
+ onFocusChange: (listener: CardFocusChangeListener) => () => void;
80
111
  }
81
112
  /**
82
113
  * Render a card schema into a container element.
@@ -470,6 +501,18 @@ declare abstract class BaseElement extends HTMLElement {
470
501
  protected toCSS(value: string | number): string;
471
502
  /** Assign component markup and convert only its CSS declarations. */
472
503
  protected setShadowHTML(html: string): void;
504
+ /**
505
+ * Patch freshly rendered markup around an existing interactive element.
506
+ *
507
+ * Replacing a focused native control through `shadowRoot.innerHTML` resets
508
+ * its selection and, on mobile WebViews, can also recreate the soft
509
+ * keyboard with a different layout. This helper keeps the selected element
510
+ * connected while reconciling its surrounding markup.
511
+ */
512
+ protected patchShadowHTMLPreservingElement(html: string, preserved: Element, selector: string): boolean;
513
+ private reconcilePreservedTree;
514
+ private patchPreservedElement;
515
+ private reconcileKey;
473
516
  /**
474
517
  * Escape a value before interpolating it into a double-quoted HTML
475
518
  * attribute. Browsers decode the entities before parsing inline CSS, so
@@ -620,9 +663,14 @@ declare class CardButton extends BaseElement {
620
663
 
621
664
  declare class CardInput extends BaseElement {
622
665
  static readonly is = "ai-card-input";
666
+ private readonly wiredInputs;
667
+ private readonly wiredNumberInputs;
623
668
  protected render(): void;
624
- private getAutoFocusControl;
625
- private isFirstAvailableAutoFocusInput;
669
+ /**
670
+ * Consume an interaction-driven focus request from the card renderer.
671
+ * Mounts and ordinary updates never call this method.
672
+ */
673
+ requestAutoFocus(): boolean;
626
674
  /** Escape HTML entities for safe insertion. */
627
675
  private escapeHtml;
628
676
  /** Escape attribute values for safe insertion. */
@@ -659,7 +707,13 @@ declare class CardInput extends BaseElement {
659
707
  declare class CardImage extends BaseElement {
660
708
  static readonly is = "ai-card-image";
661
709
  private _lightbox;
710
+ private readonly _wiredImages;
711
+ private readonly _wiredLightboxes;
712
+ private readonly _wiredCloseButtons;
713
+ private readonly _handleEscape;
662
714
  protected render(): void;
715
+ private applyHostDimension;
716
+ private resolveDeclaredDimension;
663
717
  private bindEvents;
664
718
  private openLightbox;
665
719
  private closeLightbox;
@@ -1020,10 +1074,12 @@ type InputAffix = string | InputAffixConfig;
1020
1074
  interface InputProps {
1021
1075
  /** Placeholder text */
1022
1076
  placeholder?: string;
1023
- /** Focus the native input after the component is mounted (default false) */
1077
+ /** Focus an empty input when a card interaction reveals it (default false) */
1024
1078
  autoFocus?: boolean;
1025
1079
  /** HTML input type: text / textarea / password / number etc. */
1026
1080
  inputType?: 'text' | 'textarea' | 'password' | (string & {});
1081
+ /** Mobile soft-keyboard hint; does not change validation or step controls. */
1082
+ inputMode?: 'none' | 'text' | 'tel' | 'url' | 'email' | 'numeric' | 'decimal' | 'search';
1027
1083
  /** Label text displayed above the input */
1028
1084
  label?: string;
1029
1085
  /** Initial value */
@@ -1061,9 +1117,9 @@ interface ImageProps {
1061
1117
  /** Alt text for accessibility */
1062
1118
  alt?: string;
1063
1119
  /** Image width (CSS value) */
1064
- width?: string;
1120
+ width?: string | number;
1065
1121
  /** Image height (CSS value) */
1066
- height?: string;
1122
+ height?: string | number;
1067
1123
  /** CSS object-fit mode (default 'cover') */
1068
1124
  objectFit?: 'cover' | 'contain' | 'fill' | 'none' | 'scale-down';
1069
1125
  /** Whether lightbox preview is enabled (default true) */
@@ -1785,4 +1841,4 @@ declare const componentRenderers: Record<string, ComponentRenderer>;
1785
1841
  declare function registerComponent(type: string, renderer: ComponentRenderer): void;
1786
1842
 
1787
1843
  export { BaseElement, BotSDK, CardButton, CardChoiceItem, CardChoiceList, CardCollapse, CardCounter, CardDivider, CardForm, CardHtml, CardIcon, CardImage, CardInput, CardLoading, CardPasscodeInput, CardProgress, CardRate, CardSelect, CardSteps, CardSwitch, CardTag, CardText, DEFAULT_MOBILE_RESPONSIVE, LocalActionConfigProvider, RemoteActionConfigProvider, buildStyleString, componentRenderers, connectSSE, connectStreaming, convertPixelTokens, createResponsiveContext, createWebActionContext, isMobileViewport, onViewportChange, pxToRem, pxToVw, registerComponent, renderCard, renderStreamingCard, resolveSize, sanitizeHtml, trimIncompleteTag };
1788
- export type { A2UIActionPayload, BotSDKOptions, ButtonProps, CardInstance, ChoiceIndicatorIcon, ChoiceItemProps, ChoiceListProps, ChoiceListValue, CollapseProps, ComponentRenderer, ConnectorConfig, CounterIcon, CounterProps, DividerProps, FormField, FormProps, FormRule, HtmlProps, IconProps, ImageProps, InputAffix, InputAffixConfig, InputProps, LoadingProps, PartialFinalizeResult, PasscodeInputProps, ProgressProps, ProgressSegment, RateProps, RenderCardOptions, ResponsiveContext, ResponsiveMobileOptions, ResponsiveOptions, SSEConnectOptions, SelectOption, SelectProps, SelectSize, SelectStyles, SelectVariant, StepIcon, StepItem, StepsProps, StreamingCardInstance, StreamingCardOptions, StreamingConnectOptions, StreamingConnection, SwitchProps, TagProps, TextProps, WebActionContextOptions };
1844
+ export type { A2UIActionPayload, BotSDKOptions, ButtonProps, CardFocusChangeEvent, CardFocusChangeListener, CardInstance, CardNodeHandle, ChoiceIndicatorIcon, ChoiceItemProps, ChoiceListProps, ChoiceListValue, CollapseProps, ComponentRenderer, ConnectorConfig, CounterIcon, CounterProps, DividerProps, FormField, FormProps, FormRule, HtmlProps, IconProps, ImageProps, InputAffix, InputAffixConfig, InputProps, LoadingProps, PartialFinalizeResult, PasscodeInputProps, ProgressProps, ProgressSegment, RateProps, RenderCardOptions, ResponsiveContext, ResponsiveMobileOptions, ResponsiveOptions, SSEConnectOptions, SelectOption, SelectProps, SelectSize, SelectStyles, SelectVariant, StepIcon, StepItem, StepsProps, StreamingCardInstance, StreamingCardOptions, StreamingConnectOptions, StreamingConnection, SwitchProps, TagProps, TextProps, WebActionContextOptions };