@magicx-eng/ai-autocomplete-vanilla 0.19.3 → 0.20.1

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
@@ -88,6 +88,8 @@ const ac = new AIAutocomplete(container, {
88
88
  closeDropdownOnBlur: true, // false = keep dropdown open even when input loses focus
89
89
  showNonTappableOptions: true, // false = hide non-tappable options from the dropdown
90
90
  showSkipButton: true, // false = hide the pill bar's trailing "skip" button
91
+ showOptionIcons: true, // false = render option rows as text only (no icons)
92
+ showChipIcons: true, // false = render completed chips as text only (no icons)
91
93
 
92
94
  // Focus
93
95
  autoFocus: true, // focus the input on mount (Tier 1 only)
@@ -455,6 +457,9 @@ unsub();
455
457
 
456
458
 
457
459
  > **Datepicker in a custom UI.** For a date parameter, `state.activeFormatType` is `"date"` and `state.filteredOptions` holds that month's day cells. Each cell's `text` is the date it commits (`"March 23"`, or `"Tuesday"` inside the next week), so rendering them as a plain list already works — `selectOption(cell)` behaves exactly as it does for an option. To draw an actual calendar, read `state.dateView` for the month on show, `cellDay(cell)` for the number to paint, and call `showPreviousMonth()` / `showNextMonth()` to page. Cells that pad the start and end of the month have `is_tappable: false`. For a **range** parameter `activeFormatType` is `"date-range"` and the same `selectOption(cell)` is called twice: the first records the start (`state.dateRangeStart` holds it, and nothing is committed yet), the second commits the span. `dateCellMarks` and `visibleDateRange` are exported to paint the two ends and the band between them the way the built-in calendars do.
460
+
461
+ > **Option icons.** An option may carry an icon: `icon_svg` is inline SVG markup and `icon` its name. The built-in dropdown draws the SVG before the option's text, in the text color, and hides it while the row shows its loading skeleton. When rendering options yourself, pass `icon_svg` through the exported `sanitizeOptionIconSvg` before inserting it — it reduces the markup to plain vector drawing and returns `null` for anything else — and use `optionLabel(option)` for the text, which keeps an `icon` that has no `icon_svg` as a legacy text prefix. A picked option's `icon` / `icon_svg` are copied onto its completed parameter, and the chip in the input draws the glyph before its text. Both surfaces can be switched off: `showOptionIcons: false` renders option rows as text only and `showChipIcons: false` renders chips as text only; either way the icon element is omitted rather than hidden.
462
+
458
463
  ### State shape (`CoreState`)
459
464
 
460
465
  | Field | Type | Description |
package/dist/index.d.mts CHANGED
@@ -7,7 +7,19 @@ interface CompletedParam {
7
7
  }
8
8
  interface SuggestionOption {
9
9
  text: string;
10
+ /**
11
+ * Icon for the option. When `icon_svg` is also set this is the icon's name
12
+ * (an identifier such as `"apple"`, exposed on the row as `data-aia-icon`)
13
+ * and the SVG is what renders. Without `icon_svg` it is legacy display text
14
+ * (an emoji, say) drawn as a prefix to `text`.
15
+ */
10
16
  icon?: string;
17
+ /**
18
+ * Inline SVG markup for the option's icon, drawn before `text` in the
19
+ * option's text color. Passed through `sanitizeOptionIconSvg` before it
20
+ * touches the DOM; markup that fails it renders no icon.
21
+ */
22
+ icon_svg?: string;
11
23
  tag?: string;
12
24
  is_tappable: boolean;
13
25
  kind: TaskKind | null;
@@ -78,6 +90,13 @@ interface CompletedParamState extends CompletedParam {
78
90
  /** Cached options from the suggestion that produced this param — re-edit shows these. */
79
91
  options: SuggestionOption[];
80
92
  metadata?: Record<string, unknown>;
93
+ /**
94
+ * The picked option's `icon` / `icon_svg`, carried over so the completed
95
+ * chip in the input draws the same glyph its option row did. Absent when a
96
+ * consumer restores params itself or the answer came from a calendar.
97
+ */
98
+ icon?: string;
99
+ icon_svg?: string;
81
100
  }
82
101
  /**
83
102
  * A suggestion the user dismissed with the skip key (→) instead of filling.
@@ -662,6 +681,10 @@ interface CoreOptions {
662
681
  closeDropdownOnBlur?: boolean;
663
682
  /** When true (default), non-tappable options are rendered in the dropdown alongside tappable ones. Set to false to hide them entirely. */
664
683
  showNonTappableOptions?: boolean;
684
+ /** When true (default), an option that carries an `icon_svg` draws its icon before its text in the dropdown. Set to false to render option rows as text only. */
685
+ showOptionIcons?: boolean;
686
+ /** When true (default), a completed chip in the input draws the icon of the option that answered it. Set to false to render chips as text only. */
687
+ showChipIcons?: boolean;
665
688
  /**
666
689
  * When true (default), the dropdown's pill bar ends in a small "skip" button
667
690
  * that dismisses the active pill — same action as pressing → at the end of
@@ -1426,6 +1449,31 @@ declare function previousGraphemeBoundary(text: string, offset: number): number;
1426
1449
  */
1427
1450
  declare function scrollCaretIntoView(root: HTMLElement): void;
1428
1451
 
1452
+ /**
1453
+ * Reduces an option's `icon_svg` markup to plain vector drawing before it is
1454
+ * inserted into the dropdown. The icons come from the server's own curated
1455
+ * set, but markup landing in a consumer's page is a trust boundary, so every
1456
+ * icon is parsed and filtered through an allowlist rather than inserted raw:
1457
+ * only the elements in `ALLOWED_ELEMENTS` survive, stripped of any attribute
1458
+ * that could run script or fetch a resource. The root is marked
1459
+ * `aria-hidden` / non-focusable — the option's text is its accessible name.
1460
+ *
1461
+ * Returns the serialized `<svg>` to insert, or `null` when the markup is not
1462
+ * a well-formed SVG document (or the environment has no `DOMParser`, as in
1463
+ * server rendering), in which case the option renders without an icon.
1464
+ */
1465
+ declare function sanitizeOptionIconSvg(markup: string): string | null;
1466
+ /**
1467
+ * The text an option row shows. With `icon_svg` the icon draws as its own
1468
+ * element and `icon` is just its name; without it, a legacy `icon` (an emoji
1469
+ * from an option override, say) is display text prefixed to the option.
1470
+ */
1471
+ declare function optionLabel(option: {
1472
+ text: string;
1473
+ icon?: string;
1474
+ icon_svg?: string;
1475
+ }): string;
1476
+
1429
1477
  /**
1430
1478
  * Types the starting-state placeholder into the input one character at a
1431
1479
  * time instead of popping it in whole.
@@ -1463,6 +1511,8 @@ interface RenderEditableArgs {
1463
1511
  editingParamId: string | null;
1464
1512
  placeholderText: string;
1465
1513
  isFocused: boolean;
1514
+ /** Whether completed chips draw the icon of the option that answered them. Default: true. */
1515
+ showChipIcons?: boolean;
1466
1516
  }
1467
1517
  /**
1468
1518
  * Renders text segments into the contentEditable input. Completed params are
@@ -1693,4 +1743,4 @@ interface SubmitResultExtras {
1693
1743
  */
1694
1744
  declare function buildSubmitResult(text: string, completedParams: CompletedParamState[], skippedParams?: SkippedParamState[], extras?: SubmitResultExtras): AutocompleteResult;
1695
1745
 
1696
- export { AIAutocomplete, type APIConfig, type APIKeyConfig, ATTRIBUTION_URL, type AccessTokenConfig, type AccessTokenResult, type AppearanceMode, type AutocompleteRequest, type AutocompleteResponse, type AutocompleteResult, type CompletedParam, type CompletedParamState, type CoreOptions, type CoreState, DATE_RANGE_META_END, DATE_RANGE_META_START, type DateMonthView, type DateRange, type DateSelection, type FormatType, type IdentifiedParam, type IdentifiedParamState, type InputItem, type LooseDateOptions, ModeController, OPTIONS_GRID_MOBILE_QUERY, OPTION_ENTER_DELAY_VAR, OPTION_ENTER_FADE_MS, OPTION_ENTER_RISE_MS, OPTION_ENTER_RISE_PX, OPTION_ENTER_STAGGER_MS, type OptionOverride, type OptionOverrides, type OptionsGridLayout, type OptionsGridPlan, PLACEHOLDER_FADE_OUT_MS, PLACEHOLDER_LEAVING_ATTR, PLACEHOLDER_SWAP_GAP_MS, PLACEHOLDER_TYPE_MS, PLACEHOLDER_WORD_PAUSE_MS, type Product, type ProductsConfig, RANGE_SEPARATOR, type RecentlySuggested, type RenderMode, SCROLL_ARROW_ATTR, SCROLL_ARROW_BOTTOM_VAR, SCROLL_ARROW_CLASS, SCROLL_ARROW_FADE_MS, SCROLL_ARROW_LABEL, SCROLL_ARROW_SCROLL_IDLE_MS, SCROLL_ARROW_VISIBLE_ATTR, SKIPPED_PARAM_TEXT, type ScrollArrowArgs, type ScrollArrowController, type Segment, type SkippedParamState, type Store, type SubmitResultExtras, type Suggestion, type SuggestionOption, type TaskKind, WEEKDAY_LABELS, addMonths, attachScrollArrow, buildAttributionUrl, buildDateOptions, buildQuery, buildSubmitResult, cellDay, cellIso, computeOptionsGridLayout, createStore, cursorIsAtEnd, dateCellMarks, dateSelectionFor, extractPlainText, formatAbsoluteDate, formatDate, formatDateRange, getCursorOffset, getFooterHint, identifiedParamLabel, isCalendarFormat, isOptionsGridMobileViewport, isoDate, measureOptionsGrid, monthLabel, needsOptionsGridMeasurement, optionEnterDelayMs, optionsEntranceDurationMs, optionsGridTemplateColumns, parseDate, parseLooseDate, parseLooseDateRange, plainTextLength, planOptionsGrid, previousGraphemeBoundary, renderEditableContent, resolveFormatType, resolveIdentifiedDate, scrollCaretIntoView, selectedIsoFromText, selectedRangeFor, setCursorOffset, toWireIdentifiedParams, visibleDateRange, withSkippedParams };
1746
+ export { AIAutocomplete, type APIConfig, type APIKeyConfig, ATTRIBUTION_URL, type AccessTokenConfig, type AccessTokenResult, type AppearanceMode, type AutocompleteRequest, type AutocompleteResponse, type AutocompleteResult, type CompletedParam, type CompletedParamState, type CoreOptions, type CoreState, DATE_RANGE_META_END, DATE_RANGE_META_START, type DateMonthView, type DateRange, type DateSelection, type FormatType, type IdentifiedParam, type IdentifiedParamState, type InputItem, type LooseDateOptions, ModeController, OPTIONS_GRID_MOBILE_QUERY, OPTION_ENTER_DELAY_VAR, OPTION_ENTER_FADE_MS, OPTION_ENTER_RISE_MS, OPTION_ENTER_RISE_PX, OPTION_ENTER_STAGGER_MS, type OptionOverride, type OptionOverrides, type OptionsGridLayout, type OptionsGridPlan, PLACEHOLDER_FADE_OUT_MS, PLACEHOLDER_LEAVING_ATTR, PLACEHOLDER_SWAP_GAP_MS, PLACEHOLDER_TYPE_MS, PLACEHOLDER_WORD_PAUSE_MS, type Product, type ProductsConfig, RANGE_SEPARATOR, type RecentlySuggested, type RenderMode, SCROLL_ARROW_ATTR, SCROLL_ARROW_BOTTOM_VAR, SCROLL_ARROW_CLASS, SCROLL_ARROW_FADE_MS, SCROLL_ARROW_LABEL, SCROLL_ARROW_SCROLL_IDLE_MS, SCROLL_ARROW_VISIBLE_ATTR, SKIPPED_PARAM_TEXT, type ScrollArrowArgs, type ScrollArrowController, type Segment, type SkippedParamState, type Store, type SubmitResultExtras, type Suggestion, type SuggestionOption, type TaskKind, WEEKDAY_LABELS, addMonths, attachScrollArrow, buildAttributionUrl, buildDateOptions, buildQuery, buildSubmitResult, cellDay, cellIso, computeOptionsGridLayout, createStore, cursorIsAtEnd, dateCellMarks, dateSelectionFor, extractPlainText, formatAbsoluteDate, formatDate, formatDateRange, getCursorOffset, getFooterHint, identifiedParamLabel, isCalendarFormat, isOptionsGridMobileViewport, isoDate, measureOptionsGrid, monthLabel, needsOptionsGridMeasurement, optionEnterDelayMs, optionLabel, optionsEntranceDurationMs, optionsGridTemplateColumns, parseDate, parseLooseDate, parseLooseDateRange, plainTextLength, planOptionsGrid, previousGraphemeBoundary, renderEditableContent, resolveFormatType, resolveIdentifiedDate, sanitizeOptionIconSvg, scrollCaretIntoView, selectedIsoFromText, selectedRangeFor, setCursorOffset, toWireIdentifiedParams, visibleDateRange, withSkippedParams };
package/dist/index.d.ts CHANGED
@@ -7,7 +7,19 @@ interface CompletedParam {
7
7
  }
8
8
  interface SuggestionOption {
9
9
  text: string;
10
+ /**
11
+ * Icon for the option. When `icon_svg` is also set this is the icon's name
12
+ * (an identifier such as `"apple"`, exposed on the row as `data-aia-icon`)
13
+ * and the SVG is what renders. Without `icon_svg` it is legacy display text
14
+ * (an emoji, say) drawn as a prefix to `text`.
15
+ */
10
16
  icon?: string;
17
+ /**
18
+ * Inline SVG markup for the option's icon, drawn before `text` in the
19
+ * option's text color. Passed through `sanitizeOptionIconSvg` before it
20
+ * touches the DOM; markup that fails it renders no icon.
21
+ */
22
+ icon_svg?: string;
11
23
  tag?: string;
12
24
  is_tappable: boolean;
13
25
  kind: TaskKind | null;
@@ -78,6 +90,13 @@ interface CompletedParamState extends CompletedParam {
78
90
  /** Cached options from the suggestion that produced this param — re-edit shows these. */
79
91
  options: SuggestionOption[];
80
92
  metadata?: Record<string, unknown>;
93
+ /**
94
+ * The picked option's `icon` / `icon_svg`, carried over so the completed
95
+ * chip in the input draws the same glyph its option row did. Absent when a
96
+ * consumer restores params itself or the answer came from a calendar.
97
+ */
98
+ icon?: string;
99
+ icon_svg?: string;
81
100
  }
82
101
  /**
83
102
  * A suggestion the user dismissed with the skip key (→) instead of filling.
@@ -662,6 +681,10 @@ interface CoreOptions {
662
681
  closeDropdownOnBlur?: boolean;
663
682
  /** When true (default), non-tappable options are rendered in the dropdown alongside tappable ones. Set to false to hide them entirely. */
664
683
  showNonTappableOptions?: boolean;
684
+ /** When true (default), an option that carries an `icon_svg` draws its icon before its text in the dropdown. Set to false to render option rows as text only. */
685
+ showOptionIcons?: boolean;
686
+ /** When true (default), a completed chip in the input draws the icon of the option that answered it. Set to false to render chips as text only. */
687
+ showChipIcons?: boolean;
665
688
  /**
666
689
  * When true (default), the dropdown's pill bar ends in a small "skip" button
667
690
  * that dismisses the active pill — same action as pressing → at the end of
@@ -1426,6 +1449,31 @@ declare function previousGraphemeBoundary(text: string, offset: number): number;
1426
1449
  */
1427
1450
  declare function scrollCaretIntoView(root: HTMLElement): void;
1428
1451
 
1452
+ /**
1453
+ * Reduces an option's `icon_svg` markup to plain vector drawing before it is
1454
+ * inserted into the dropdown. The icons come from the server's own curated
1455
+ * set, but markup landing in a consumer's page is a trust boundary, so every
1456
+ * icon is parsed and filtered through an allowlist rather than inserted raw:
1457
+ * only the elements in `ALLOWED_ELEMENTS` survive, stripped of any attribute
1458
+ * that could run script or fetch a resource. The root is marked
1459
+ * `aria-hidden` / non-focusable — the option's text is its accessible name.
1460
+ *
1461
+ * Returns the serialized `<svg>` to insert, or `null` when the markup is not
1462
+ * a well-formed SVG document (or the environment has no `DOMParser`, as in
1463
+ * server rendering), in which case the option renders without an icon.
1464
+ */
1465
+ declare function sanitizeOptionIconSvg(markup: string): string | null;
1466
+ /**
1467
+ * The text an option row shows. With `icon_svg` the icon draws as its own
1468
+ * element and `icon` is just its name; without it, a legacy `icon` (an emoji
1469
+ * from an option override, say) is display text prefixed to the option.
1470
+ */
1471
+ declare function optionLabel(option: {
1472
+ text: string;
1473
+ icon?: string;
1474
+ icon_svg?: string;
1475
+ }): string;
1476
+
1429
1477
  /**
1430
1478
  * Types the starting-state placeholder into the input one character at a
1431
1479
  * time instead of popping it in whole.
@@ -1463,6 +1511,8 @@ interface RenderEditableArgs {
1463
1511
  editingParamId: string | null;
1464
1512
  placeholderText: string;
1465
1513
  isFocused: boolean;
1514
+ /** Whether completed chips draw the icon of the option that answered them. Default: true. */
1515
+ showChipIcons?: boolean;
1466
1516
  }
1467
1517
  /**
1468
1518
  * Renders text segments into the contentEditable input. Completed params are
@@ -1693,4 +1743,4 @@ interface SubmitResultExtras {
1693
1743
  */
1694
1744
  declare function buildSubmitResult(text: string, completedParams: CompletedParamState[], skippedParams?: SkippedParamState[], extras?: SubmitResultExtras): AutocompleteResult;
1695
1745
 
1696
- export { AIAutocomplete, type APIConfig, type APIKeyConfig, ATTRIBUTION_URL, type AccessTokenConfig, type AccessTokenResult, type AppearanceMode, type AutocompleteRequest, type AutocompleteResponse, type AutocompleteResult, type CompletedParam, type CompletedParamState, type CoreOptions, type CoreState, DATE_RANGE_META_END, DATE_RANGE_META_START, type DateMonthView, type DateRange, type DateSelection, type FormatType, type IdentifiedParam, type IdentifiedParamState, type InputItem, type LooseDateOptions, ModeController, OPTIONS_GRID_MOBILE_QUERY, OPTION_ENTER_DELAY_VAR, OPTION_ENTER_FADE_MS, OPTION_ENTER_RISE_MS, OPTION_ENTER_RISE_PX, OPTION_ENTER_STAGGER_MS, type OptionOverride, type OptionOverrides, type OptionsGridLayout, type OptionsGridPlan, PLACEHOLDER_FADE_OUT_MS, PLACEHOLDER_LEAVING_ATTR, PLACEHOLDER_SWAP_GAP_MS, PLACEHOLDER_TYPE_MS, PLACEHOLDER_WORD_PAUSE_MS, type Product, type ProductsConfig, RANGE_SEPARATOR, type RecentlySuggested, type RenderMode, SCROLL_ARROW_ATTR, SCROLL_ARROW_BOTTOM_VAR, SCROLL_ARROW_CLASS, SCROLL_ARROW_FADE_MS, SCROLL_ARROW_LABEL, SCROLL_ARROW_SCROLL_IDLE_MS, SCROLL_ARROW_VISIBLE_ATTR, SKIPPED_PARAM_TEXT, type ScrollArrowArgs, type ScrollArrowController, type Segment, type SkippedParamState, type Store, type SubmitResultExtras, type Suggestion, type SuggestionOption, type TaskKind, WEEKDAY_LABELS, addMonths, attachScrollArrow, buildAttributionUrl, buildDateOptions, buildQuery, buildSubmitResult, cellDay, cellIso, computeOptionsGridLayout, createStore, cursorIsAtEnd, dateCellMarks, dateSelectionFor, extractPlainText, formatAbsoluteDate, formatDate, formatDateRange, getCursorOffset, getFooterHint, identifiedParamLabel, isCalendarFormat, isOptionsGridMobileViewport, isoDate, measureOptionsGrid, monthLabel, needsOptionsGridMeasurement, optionEnterDelayMs, optionsEntranceDurationMs, optionsGridTemplateColumns, parseDate, parseLooseDate, parseLooseDateRange, plainTextLength, planOptionsGrid, previousGraphemeBoundary, renderEditableContent, resolveFormatType, resolveIdentifiedDate, scrollCaretIntoView, selectedIsoFromText, selectedRangeFor, setCursorOffset, toWireIdentifiedParams, visibleDateRange, withSkippedParams };
1746
+ export { AIAutocomplete, type APIConfig, type APIKeyConfig, ATTRIBUTION_URL, type AccessTokenConfig, type AccessTokenResult, type AppearanceMode, type AutocompleteRequest, type AutocompleteResponse, type AutocompleteResult, type CompletedParam, type CompletedParamState, type CoreOptions, type CoreState, DATE_RANGE_META_END, DATE_RANGE_META_START, type DateMonthView, type DateRange, type DateSelection, type FormatType, type IdentifiedParam, type IdentifiedParamState, type InputItem, type LooseDateOptions, ModeController, OPTIONS_GRID_MOBILE_QUERY, OPTION_ENTER_DELAY_VAR, OPTION_ENTER_FADE_MS, OPTION_ENTER_RISE_MS, OPTION_ENTER_RISE_PX, OPTION_ENTER_STAGGER_MS, type OptionOverride, type OptionOverrides, type OptionsGridLayout, type OptionsGridPlan, PLACEHOLDER_FADE_OUT_MS, PLACEHOLDER_LEAVING_ATTR, PLACEHOLDER_SWAP_GAP_MS, PLACEHOLDER_TYPE_MS, PLACEHOLDER_WORD_PAUSE_MS, type Product, type ProductsConfig, RANGE_SEPARATOR, type RecentlySuggested, type RenderMode, SCROLL_ARROW_ATTR, SCROLL_ARROW_BOTTOM_VAR, SCROLL_ARROW_CLASS, SCROLL_ARROW_FADE_MS, SCROLL_ARROW_LABEL, SCROLL_ARROW_SCROLL_IDLE_MS, SCROLL_ARROW_VISIBLE_ATTR, SKIPPED_PARAM_TEXT, type ScrollArrowArgs, type ScrollArrowController, type Segment, type SkippedParamState, type Store, type SubmitResultExtras, type Suggestion, type SuggestionOption, type TaskKind, WEEKDAY_LABELS, addMonths, attachScrollArrow, buildAttributionUrl, buildDateOptions, buildQuery, buildSubmitResult, cellDay, cellIso, computeOptionsGridLayout, createStore, cursorIsAtEnd, dateCellMarks, dateSelectionFor, extractPlainText, formatAbsoluteDate, formatDate, formatDateRange, getCursorOffset, getFooterHint, identifiedParamLabel, isCalendarFormat, isOptionsGridMobileViewport, isoDate, measureOptionsGrid, monthLabel, needsOptionsGridMeasurement, optionEnterDelayMs, optionLabel, optionsEntranceDurationMs, optionsGridTemplateColumns, parseDate, parseLooseDate, parseLooseDateRange, plainTextLength, planOptionsGrid, previousGraphemeBoundary, renderEditableContent, resolveFormatType, resolveIdentifiedDate, sanitizeOptionIconSvg, scrollCaretIntoView, selectedIsoFromText, selectedRangeFor, setCursorOffset, toWireIdentifiedParams, visibleDateRange, withSkippedParams };