@api-client/ui 0.2.3 → 0.2.5

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.
Files changed (204) hide show
  1. package/.aiexclude +3 -0
  2. package/.vscode/settings.json +6 -3
  3. package/build/src/elements/authorization/ui/ApiKeyAuthorization.d.ts +1 -1
  4. package/build/src/elements/authorization/ui/ApiKeyAuthorization.d.ts.map +1 -1
  5. package/build/src/elements/authorization/ui/ApiKeyAuthorization.js +7 -7
  6. package/build/src/elements/authorization/ui/ApiKeyAuthorization.js.map +1 -1
  7. package/build/src/elements/authorization/ui/Authorization.styles.js +4 -4
  8. package/build/src/elements/authorization/ui/Authorization.styles.js.map +1 -1
  9. package/build/src/elements/authorization/ui/BasicAuthorization.d.ts +1 -1
  10. package/build/src/elements/authorization/ui/BasicAuthorization.d.ts.map +1 -1
  11. package/build/src/elements/authorization/ui/BasicAuthorization.js +5 -5
  12. package/build/src/elements/authorization/ui/BasicAuthorization.js.map +1 -1
  13. package/build/src/elements/authorization/ui/BearerAuthorization.d.ts +1 -1
  14. package/build/src/elements/authorization/ui/BearerAuthorization.d.ts.map +1 -1
  15. package/build/src/elements/authorization/ui/BearerAuthorization.js +3 -3
  16. package/build/src/elements/authorization/ui/BearerAuthorization.js.map +1 -1
  17. package/build/src/elements/authorization/ui/NtlmAuthorization.d.ts +1 -1
  18. package/build/src/elements/authorization/ui/NtlmAuthorization.d.ts.map +1 -1
  19. package/build/src/elements/authorization/ui/NtlmAuthorization.js +7 -7
  20. package/build/src/elements/authorization/ui/NtlmAuthorization.js.map +1 -1
  21. package/build/src/elements/authorization/ui/OAuth2Authorization.d.ts +1 -1
  22. package/build/src/elements/authorization/ui/OAuth2Authorization.d.ts.map +1 -1
  23. package/build/src/elements/authorization/ui/OAuth2Authorization.js +32 -27
  24. package/build/src/elements/authorization/ui/OAuth2Authorization.js.map +1 -1
  25. package/build/src/elements/authorization/ui/OidcAuthorization.js +4 -4
  26. package/build/src/elements/authorization/ui/OidcAuthorization.js.map +1 -1
  27. package/build/src/elements/autocomplete/autocomplete-input.d.ts +10 -0
  28. package/build/src/elements/autocomplete/autocomplete-input.d.ts.map +1 -0
  29. package/build/src/{md/text-field/ui-text-field.js → elements/autocomplete/autocomplete-input.js} +9 -9
  30. package/build/src/elements/autocomplete/autocomplete-input.js.map +1 -0
  31. package/build/src/elements/autocomplete/internals/autocomplete.d.ts +257 -0
  32. package/build/src/elements/autocomplete/internals/autocomplete.d.ts.map +1 -0
  33. package/build/src/elements/autocomplete/internals/autocomplete.js +619 -0
  34. package/build/src/elements/autocomplete/internals/autocomplete.js.map +1 -0
  35. package/build/src/elements/autocomplete/internals/autocomplete.styles.d.ts +3 -0
  36. package/build/src/elements/autocomplete/internals/autocomplete.styles.d.ts.map +1 -0
  37. package/build/src/elements/autocomplete/internals/autocomplete.styles.js +25 -0
  38. package/build/src/elements/autocomplete/internals/autocomplete.styles.js.map +1 -0
  39. package/build/src/elements/dialog/internals/DeleteCookieAction.element.d.ts +1 -1
  40. package/build/src/elements/dialog/internals/DeleteCookieAction.element.d.ts.map +1 -1
  41. package/build/src/elements/dialog/internals/DeleteCookieAction.element.js +5 -5
  42. package/build/src/elements/dialog/internals/DeleteCookieAction.element.js.map +1 -1
  43. package/build/src/elements/dialog/internals/Rename.d.ts +1 -1
  44. package/build/src/elements/dialog/internals/Rename.d.ts.map +1 -1
  45. package/build/src/elements/dialog/internals/Rename.js +3 -3
  46. package/build/src/elements/dialog/internals/Rename.js.map +1 -1
  47. package/build/src/elements/dialog/internals/SetCookieAction.element.d.ts +1 -1
  48. package/build/src/elements/dialog/internals/SetCookieAction.element.d.ts.map +1 -1
  49. package/build/src/elements/dialog/internals/SetCookieAction.element.js +9 -9
  50. package/build/src/elements/dialog/internals/SetCookieAction.element.js.map +1 -1
  51. package/build/src/elements/environment/EnvironmentEditor.d.ts +1 -1
  52. package/build/src/elements/environment/EnvironmentEditor.d.ts.map +1 -1
  53. package/build/src/elements/environment/EnvironmentEditor.js +3 -3
  54. package/build/src/elements/environment/EnvironmentEditor.js.map +1 -1
  55. package/build/src/elements/environment/EnvironmentEditor.styles.js +1 -1
  56. package/build/src/elements/environment/EnvironmentEditor.styles.js.map +1 -1
  57. package/build/src/elements/environment/ServerEditor.d.ts +1 -1
  58. package/build/src/elements/environment/ServerEditor.d.ts.map +1 -1
  59. package/build/src/elements/environment/ServerEditor.js +7 -7
  60. package/build/src/elements/environment/ServerEditor.js.map +1 -1
  61. package/build/src/elements/environment/ServerEditor.styles.js +1 -1
  62. package/build/src/elements/environment/ServerEditor.styles.js.map +1 -1
  63. package/build/src/elements/http/BodyMultipartEditor.d.ts.map +1 -1
  64. package/build/src/elements/http/BodyMultipartEditor.js +4 -0
  65. package/build/src/elements/http/BodyMultipartEditor.js.map +1 -1
  66. package/build/src/elements/http/CertificateAdd.element.d.ts +1 -1
  67. package/build/src/elements/http/CertificateAdd.element.d.ts.map +1 -1
  68. package/build/src/elements/http/CertificateAdd.element.js +8 -8
  69. package/build/src/elements/http/CertificateAdd.element.js.map +1 -1
  70. package/build/src/elements/http/CertificateAdd.styles.js +1 -1
  71. package/build/src/elements/http/CertificateAdd.styles.js.map +1 -1
  72. package/build/src/elements/http/HttpAssertions.element.js +3 -3
  73. package/build/src/elements/http/HttpAssertions.element.js.map +1 -1
  74. package/build/src/elements/http/HttpFlows.element.js +3 -3
  75. package/build/src/elements/http/HttpFlows.element.js.map +1 -1
  76. package/build/src/elements/http/HttpFlowsUi.d.ts +1 -1
  77. package/build/src/elements/http/HttpFlowsUi.d.ts.map +1 -1
  78. package/build/src/elements/http/HttpFlowsUi.js +31 -31
  79. package/build/src/elements/http/HttpFlowsUi.js.map +1 -1
  80. package/build/src/elements/http/RequestConfigElement.d.ts +1 -1
  81. package/build/src/elements/http/RequestConfigElement.d.ts.map +1 -1
  82. package/build/src/elements/http/RequestConfigElement.js +7 -7
  83. package/build/src/elements/http/RequestConfigElement.js.map +1 -1
  84. package/build/src/elements/http/UrlParamsForm.d.ts +1 -1
  85. package/build/src/elements/http/UrlParamsForm.d.ts.map +1 -1
  86. package/build/src/elements/http/UrlParamsForm.js +1 -1
  87. package/build/src/elements/http/UrlParamsForm.js.map +1 -1
  88. package/build/src/elements/project/ProjectRunner.d.ts +1 -1
  89. package/build/src/elements/project/ProjectRunner.d.ts.map +1 -1
  90. package/build/src/elements/project/ProjectRunner.js +5 -5
  91. package/build/src/elements/project/ProjectRunner.js.map +1 -1
  92. package/build/src/md/input/Input.d.ts +0 -15
  93. package/build/src/md/input/Input.d.ts.map +1 -1
  94. package/build/src/md/input/Input.js +7 -42
  95. package/build/src/md/input/Input.js.map +1 -1
  96. package/build/src/md/list/internals/List.d.ts +7 -2
  97. package/build/src/md/list/internals/List.d.ts.map +1 -1
  98. package/build/src/md/list/internals/List.js +6 -0
  99. package/build/src/md/list/internals/List.js.map +1 -1
  100. package/build/src/md/list/internals/ListItem.styles.d.ts.map +1 -1
  101. package/build/src/md/list/internals/ListItem.styles.js +8 -0
  102. package/build/src/md/list/internals/ListItem.styles.js.map +1 -1
  103. package/build/src/md/listbox/internals/Listbox.d.ts +2 -2
  104. package/build/src/md/listbox/internals/Listbox.d.ts.map +1 -1
  105. package/build/src/md/listbox/internals/Listbox.js.map +1 -1
  106. package/build/src/md/text-area/internals/TextAreaElement.d.ts.map +1 -1
  107. package/build/src/md/text-area/internals/TextAreaElement.js +0 -5
  108. package/build/src/md/text-area/internals/TextAreaElement.js.map +1 -1
  109. package/build/src/md/text-area/ui-text-area.d.ts.map +1 -1
  110. package/build/src/md/text-area/ui-text-area.js +3 -2
  111. package/build/src/md/text-area/ui-text-area.js.map +1 -1
  112. package/build/src/md/text-field/internals/{TextFieldElement.d.ts → TextField.d.ts} +2 -2
  113. package/build/src/md/text-field/internals/TextField.d.ts.map +1 -0
  114. package/build/src/md/text-field/internals/{TextFieldElement.js → TextField.js} +2 -5
  115. package/build/src/md/text-field/internals/TextField.js.map +1 -0
  116. package/build/src/md/text-field/internals/{TextField.styles.d.ts → common.styles.d.ts} +1 -1
  117. package/build/src/md/text-field/internals/common.styles.d.ts.map +1 -0
  118. package/build/src/md/text-field/internals/{TextField.styles.js → common.styles.js} +8 -94
  119. package/build/src/md/text-field/internals/common.styles.js.map +1 -0
  120. package/build/src/md/text-field/internals/filled.styles.d.ts +3 -0
  121. package/build/src/md/text-field/internals/filled.styles.d.ts.map +1 -0
  122. package/build/src/md/text-field/internals/filled.styles.js +107 -0
  123. package/build/src/md/text-field/internals/filled.styles.js.map +1 -0
  124. package/build/src/md/text-field/internals/outlined.styles.d.ts +3 -0
  125. package/build/src/md/text-field/internals/outlined.styles.d.ts.map +1 -0
  126. package/build/src/md/text-field/internals/outlined.styles.js +43 -0
  127. package/build/src/md/text-field/internals/outlined.styles.js.map +1 -0
  128. package/build/src/md/text-field/ui-filled-text-field.d.ts +11 -0
  129. package/build/src/md/text-field/ui-filled-text-field.d.ts.map +1 -0
  130. package/build/src/md/text-field/ui-filled-text-field.js +28 -0
  131. package/build/src/md/text-field/ui-filled-text-field.js.map +1 -0
  132. package/build/src/md/text-field/ui-outlined-text-field.d.ts +11 -0
  133. package/build/src/md/text-field/ui-outlined-text-field.d.ts.map +1 -0
  134. package/build/src/md/text-field/ui-outlined-text-field.js +28 -0
  135. package/build/src/md/text-field/ui-outlined-text-field.js.map +1 -0
  136. package/build/src/types/input.d.ts +1 -1
  137. package/build/src/types/input.d.ts.map +1 -1
  138. package/build/src/types/input.js.map +1 -1
  139. package/demo/elements/authorization/oauth-authorize.html +4 -4
  140. package/demo/elements/authorization/oauth-authorize.ts +1 -1
  141. package/demo/elements/autocomplete/index.html +64 -0
  142. package/demo/elements/autocomplete/index.ts +171 -0
  143. package/demo/elements/http/body-editor.ts +3 -3
  144. package/demo/elements/index.html +15 -11
  145. package/demo/md/index.html +1 -1
  146. package/demo/md/inputs/input.html +10 -15
  147. package/demo/md/inputs/input.ts +389 -101
  148. package/demo/page.css +4 -0
  149. package/package.json +1 -1
  150. package/src/elements/authorization/ui/ApiKeyAuthorization.ts +7 -7
  151. package/src/elements/authorization/ui/Authorization.styles.ts +4 -4
  152. package/src/elements/authorization/ui/BasicAuthorization.ts +5 -5
  153. package/src/elements/authorization/ui/BearerAuthorization.ts +3 -3
  154. package/src/elements/authorization/ui/NtlmAuthorization.ts +7 -7
  155. package/src/elements/authorization/ui/OAuth2Authorization.ts +32 -27
  156. package/src/elements/authorization/ui/OidcAuthorization.ts +4 -4
  157. package/src/elements/autocomplete/autocomplete-input.ts +14 -0
  158. package/src/elements/autocomplete/internals/autocomplete.styles.ts +25 -0
  159. package/src/elements/autocomplete/internals/autocomplete.ts +599 -0
  160. package/src/elements/dialog/internals/DeleteCookieAction.element.ts +5 -5
  161. package/src/elements/dialog/internals/Rename.ts +3 -3
  162. package/src/elements/dialog/internals/SetCookieAction.element.ts +9 -9
  163. package/src/elements/environment/EnvironmentEditor.styles.ts +1 -1
  164. package/src/elements/environment/EnvironmentEditor.ts +3 -3
  165. package/src/elements/environment/ServerEditor.styles.ts +1 -1
  166. package/src/elements/environment/ServerEditor.ts +7 -7
  167. package/src/elements/http/BodyMultipartEditor.ts +4 -0
  168. package/src/elements/http/CertificateAdd.element.ts +8 -8
  169. package/src/elements/http/CertificateAdd.styles.ts +1 -1
  170. package/src/elements/http/HttpAssertions.element.ts +3 -3
  171. package/src/elements/http/HttpFlows.element.ts +3 -3
  172. package/src/elements/http/HttpFlowsUi.ts +31 -31
  173. package/src/elements/http/RequestConfigElement.ts +7 -7
  174. package/src/elements/http/UrlParamsForm.ts +1 -1
  175. package/src/elements/project/ProjectRunner.ts +5 -5
  176. package/src/md/input/Input.ts +6 -21
  177. package/src/md/list/internals/List.ts +14 -2
  178. package/src/md/list/internals/ListItem.styles.ts +8 -0
  179. package/src/md/listbox/internals/Listbox.ts +2 -2
  180. package/src/md/text-area/internals/TextAreaElement.ts +0 -5
  181. package/src/md/text-area/ui-text-area.ts +3 -2
  182. package/src/md/text-field/internals/{TextFieldElement.ts → TextField.ts} +1 -4
  183. package/src/md/text-field/internals/{TextField.styles.ts → common.styles.ts} +7 -93
  184. package/src/md/text-field/internals/filled.styles.ts +107 -0
  185. package/src/md/text-field/internals/outlined.styles.ts +43 -0
  186. package/src/md/text-field/ui-filled-text-field.ts +16 -0
  187. package/src/md/text-field/ui-outlined-text-field.ts +16 -0
  188. package/src/types/input.ts +0 -1
  189. package/test/elements/authorization/basic-method.test.ts +3 -3
  190. package/test/elements/authorization/bearer-method.test.ts +2 -2
  191. package/test/elements/authorization/ntlm-method.test.ts +4 -4
  192. package/test/elements/autocomplete/autocomplete-input.spec.ts +643 -0
  193. package/test/elements/http/BodyMultipartEditorElement.test.ts +15 -16
  194. package/test/elements/http/CertificateAdd.test.ts +11 -11
  195. package/test/elements/http/HttpAssertions.test.ts +9 -9
  196. package/test/elements/http/HttpFlows.test.ts +4 -4
  197. package/build/src/md/text-field/internals/TextField.styles.d.ts.map +0 -1
  198. package/build/src/md/text-field/internals/TextField.styles.js.map +0 -1
  199. package/build/src/md/text-field/internals/TextFieldElement.d.ts.map +0 -1
  200. package/build/src/md/text-field/internals/TextFieldElement.js.map +0 -1
  201. package/build/src/md/text-field/ui-text-field.d.ts +0 -11
  202. package/build/src/md/text-field/ui-text-field.d.ts.map +0 -1
  203. package/build/src/md/text-field/ui-text-field.js.map +0 -1
  204. package/src/md/text-field/ui-text-field.ts +0 -15
@@ -0,0 +1,599 @@
1
+ import { html, LitElement, PropertyValues, TemplateResult } from 'lit'
2
+ import { property, state } from 'lit/decorators.js'
3
+ import type UiListbox from '../../../md/listbox/internals/Listbox.js'
4
+ import type UiListItem from '../../../md/list/internals/ListItem.js'
5
+ import { bound } from '../../../decorators/bound.js'
6
+
7
+ /**
8
+ * An accessible and performant autocomplete component that enhances a text input with a list of suggestions.
9
+ *
10
+ * The `autocomplete-input` component provides a flexible way to add autocomplete functionality
11
+ * to any text input. It works by coordinating a slotted input element with a slotted `ui-listbox`
12
+ * containing suggestions.
13
+ *
14
+ * Key Features:
15
+ *
16
+ * - **Popover Management**: Automatically shows and hides the suggestions popover based on input focus
17
+ * and user interaction.
18
+ * - **Keyboard Navigation**: Allows users to navigate suggestions using ArrowUp, ArrowDown keys directly
19
+ * from the input. Enter selects a suggestion, and Escape closes the popover.
20
+ * The suggestion list itself never gains focus.
21
+ * - **Dynamic Filtering**: Filters the list of suggestions as the user types into the input. Items not matching
22
+ * the query are hidden (not removed).
23
+ * - By default, filtering checks `item.dataset.value` and then `item.textContent`.
24
+ * - If a `ui-list-item` has a `data-index` attribute (e.g., `data-index="name email"`), filtering will search
25
+ * within the specified `data-*` attributes (e.g., `data-name`, `data-email`).
26
+ * - **Mutation Awareness**: Reacts to changes in slotted suggestions, re-filtering them if necessary.
27
+ * - **Event-Driven**: Notifies the application via an `autocomplete` event when a suggestion is selected.
28
+ * The component itself does not modify the input's value, giving the application author full control.
29
+ *
30
+ * ### Accessibility
31
+ *
32
+ * The `autocomplete-input` component is designed with accessibility at its core:
33
+ * - **Keyboard Navigation**: Full keyboard support for navigating suggestions (ArrowUp, ArrowDown),
34
+ * selecting (Enter), and closing (Escape), all while focus remains on the input field.
35
+ * - **ARIA Attributes**: While the `autocomplete-input` manages ARIA attributes related to the popover's
36
+ * state, the slotted `ui-listbox` is responsible for its internal ARIA roles and states
37
+ * (e.g., `role="listbox"`, `aria-activedescendant`).
38
+ * - **Labeling Suggestions**: It is **crucial** for accessibility that the slotted `ui-listbox`
39
+ * has an `aria-label` attribute. This provides a descriptive name for the list of suggestions,
40
+ * which is announced by screen readers.
41
+ *
42
+ * The component uses CSS anchor positioning to place the suggestions popover relative to the input.
43
+ * Ensure your `ui-listbox` is styled appropriately for popover display (e.g., `popover="manual"`, `position-anchor`).
44
+ * The component will manage `showPopover()` and `hidePopover()` calls.
45
+ *
46
+ * @slot input - The input element that will be used for autocomplete. This element should be an `HTMLInputElement`
47
+ * or behave like one (have a `value` property and dispatch `input`, `focus`, `blur`, and `keydown`
48
+ * events).
49
+ * @slot suggestions - The `ui-listbox` element containing `ui-list-item` elements as suggestions.
50
+ * @slot anchor - An optional element that points to element that will be used as the anchor for the popover.
51
+ * This is useful if you want to position the suggestions relative to a different element than the input.
52
+ * If not provided, the input element will be used as the anchor.
53
+ * @slot Any additional content that should be rendered inside the component.
54
+ *
55
+ * @fires autocomplete - Dispatched when a suggestion is selected by the user (e.g., via click or Enter key).
56
+ * The `event.detail` object contains:
57
+ * - `item`: The selected `UiListItem` instance.
58
+ *
59
+ * @example
60
+ * ```html
61
+ * <autocomplete-input @autocomplete="${this.handleAutocompleteSelection}">
62
+ * <input slot="input" type="text" placeholder="Search fruits..." />
63
+ * <ui-listbox slot="suggestions">
64
+ * <ui-list-item data-value="apple">Apple</ui-list-item>
65
+ * <ui-list-item data-value="banana">Banana</ui-list-item>
66
+ * <ui-list-item data-value="cherry">Cherry</ui-list-item>
67
+ * </ui-listbox>
68
+ * <!-- With aria-label for accessibility -->
69
+ * <ui-listbox slot="suggestions" aria-label="Fruit suggestions">
70
+ * <ui-list-item data-value="apple">Apple</ui-list-item>
71
+ * <ui-list-item data-value="banana">Banana</ui-list-item>
72
+ * <ui-list-item data-value="cherry">Cherry</ui-list-item>
73
+ * </ui-listbox>
74
+ * </autocomplete-input>
75
+ *
76
+ * <script>
77
+ * // In your component/script
78
+ * handleAutocompleteSelection(event) {
79
+ * const selectedItem = event.detail.item;
80
+ * const inputElement = this.shadowRoot.querySelector('input[slot="input"]');
81
+ * if (inputElement) {
82
+ * inputElement.value = selectedItem.dataset.value || selectedItem.textContent;
83
+ * }
84
+ * console.log('Selected:', selectedItem.dataset.value);
85
+ * }
86
+ * </script>
87
+ * ```
88
+ *
89
+ * @example Dynamic filtering with `data-index`
90
+ * ```html
91
+ * <autocomplete-input @autocomplete="${this.handleUserSelection}">
92
+ * <input slot="input" type="text" placeholder="Search users..." />
93
+ * <ui-listbox slot="suggestions" aria-label="User suggestions">
94
+ * <ui-list-item data-id="1" data-name="Alice Wonderland" data-email="alice@example.com" data-index="name email">
95
+ * Alice Wonderland
96
+ * <span slot="supporting-text">alice@example.com</span>
97
+ * </ui-list-item>
98
+ * <ui-list-item data-id="2" data-name="Bob The Builder" data-email="bob@example.com" data-index="name email">
99
+ * Bob The Builder
100
+ * <span slot="supporting-text">bob@example.com</span>
101
+ * </ui-list-item>
102
+ * </ui-listbox>
103
+ * </autocomplete-input>
104
+ *
105
+ * <script>
106
+ * // In your component/script
107
+ * handleUserSelection(event) {
108
+ * const selectedItem = event.detail.item;
109
+ * const inputElement = this.shadowRoot.querySelector('input[slot="input"]');
110
+ * if (inputElement) {
111
+ * // You might want to display the name, but store the ID
112
+ * inputElement.value = selectedItem.dataset.name;
113
+ * }
114
+ * console.log('Selected user ID:', selectedItem.dataset.id);
115
+ * }
116
+ * </script>
117
+ * ```
118
+ */
119
+ export default class Autocomplete extends LitElement {
120
+ protected override createRenderRoot(): HTMLElement | DocumentFragment {
121
+ return this
122
+ }
123
+
124
+ /**
125
+ * The MutationObserver instance used to watch for changes in slotted children.
126
+ */
127
+ protected observer?: MutationObserver
128
+
129
+ /**
130
+ * The ID of the input element, generated if not provided.
131
+ * This is used for CSS anchoring and to ensure unique IDs for accessibility.
132
+ */
133
+ @state() protected accessor inputId = ''
134
+
135
+ /**
136
+ * The position area for the suggestions popover.
137
+ * This can be 'bottom' or 'top', depending on available space.
138
+ * Default is 'bottom'.
139
+ *
140
+ * Note, this is set dynamically based on available space
141
+ * and the position of the input element. This only sets the initial value.
142
+ *
143
+ * @attribute
144
+ * @type {'bottom' | 'top'}
145
+ * @default 'bottom'
146
+ */
147
+ @property() accessor positionArea: 'bottom' | 'top' = 'bottom'
148
+
149
+ /**
150
+ * The reference to the slotted input element.
151
+ * This should be an `HTMLInputElement` or behave like one.
152
+ */
153
+ protected inputRef?: HTMLInputElement | HTMLElement | null
154
+ /**
155
+ * The reference to the slotted suggestions element, which should be a `ui-listbox`.
156
+ * This is used to manage the suggestions popover and filtering.
157
+ */
158
+ protected suggestionsRef?: UiListbox | null
159
+
160
+ /**
161
+ * Checks if the suggestions popover is currently open.
162
+ */
163
+ get opened(): boolean {
164
+ return this.suggestionsRef?.matches(':popover-open') || false
165
+ }
166
+
167
+ override connectedCallback(): void {
168
+ super.connectedCallback()
169
+ this.observer = new MutationObserver(this.handleMutations.bind(this))
170
+ this.observer.observe(this, { childList: true })
171
+ if (!this.id) {
172
+ this.id = `autocomplete-${Math.random().toString(36).substring(2, 15)}`
173
+ }
174
+ }
175
+
176
+ override disconnectedCallback(): void {
177
+ super.disconnectedCallback()
178
+ this.observer?.disconnect()
179
+ this.observer = undefined
180
+ this.clearInputListeners()
181
+ this.clearSuggestionsListeners()
182
+ }
183
+
184
+ protected override firstUpdated(cp: PropertyValues): void {
185
+ super.firstUpdated(cp)
186
+ this.firstSetup()
187
+ }
188
+
189
+ /**
190
+ * Performs initial setup after the first update, ensuring that slotted input
191
+ * and suggestions elements are configured.
192
+ */
193
+ async firstSetup(): Promise<void> {
194
+ await this.updateComplete
195
+ const input = this.querySelector('[slot="input"]') as HTMLElement | null
196
+ const suggestions = this.querySelector('[slot="suggestions"]') as HTMLElement | null
197
+ if (input) {
198
+ this.setupInput(input)
199
+ }
200
+ if (suggestions) {
201
+ this.setupSuggestions(suggestions as UiListbox)
202
+ }
203
+ }
204
+
205
+ /**
206
+ * Handles mutations observed on the component's slotted children.
207
+ * This is used to set up or tear down input and suggestions elements when they are added or removed.
208
+ * @param mutations An array of MutationRecord objects.
209
+ */
210
+ protected handleMutations(mutations: MutationRecord[]): void {
211
+ for (const mutation of mutations) {
212
+ if (mutation.type === 'childList') {
213
+ for (const node of mutation.removedNodes) {
214
+ if (node instanceof HTMLElement) {
215
+ if (node.slot === 'input' && this.inputRef === node) {
216
+ this.clearInputListeners()
217
+ } else if (node.slot === 'suggestions' && this.suggestionsRef === node) {
218
+ this.clearSuggestionsListeners()
219
+ }
220
+ }
221
+ }
222
+ for (const node of mutation.addedNodes) {
223
+ if (node instanceof HTMLElement) {
224
+ if (node.slot === 'input') {
225
+ this.setupInput(node)
226
+ } else if (node.slot === 'suggestions') {
227
+ this.setupSuggestions(node as UiListbox)
228
+ }
229
+ }
230
+ }
231
+ }
232
+ }
233
+ }
234
+
235
+ /**
236
+ * Clears event listeners from the current input reference and resets it.
237
+ */
238
+ protected clearInputListeners(): void {
239
+ if (this.inputRef) {
240
+ this.inputRef.removeEventListener('focus', this.handleInputFocus)
241
+ this.inputRef.removeEventListener('input', this.handleInput)
242
+ this.inputRef.removeEventListener('keydown', this.handleKeydown as EventListener)
243
+ this.inputRef.removeEventListener('blur', this.handleInputBlur)
244
+ }
245
+ this.inputRef = null
246
+ }
247
+
248
+ /**
249
+ * Clears event listeners from the current suggestions reference and resets it.
250
+ */
251
+ protected clearSuggestionsListeners(): void {
252
+ if (this.suggestionsRef) {
253
+ this.suggestionsRef.removeEventListener('select', this.handleSuggestionSelect as EventListener)
254
+ // If ui-listbox uses a slot for its items, listen to its slotchange
255
+ this.suggestionsRef.removeEventListener('itemschange', this.handleSuggestionsSlotChange)
256
+ if (this.suggestionsRef.matches(':popover-open')) {
257
+ this.suggestionsRef.hidePopover()
258
+ }
259
+ }
260
+ this.suggestionsRef = null
261
+ }
262
+
263
+ /**
264
+ * Sets up the slotted input element.
265
+ * Assigns an ID if necessary, sets up CSS anchoring, and attaches event listeners.
266
+ * @param input The HTMLElement to be used as the input.
267
+ */
268
+ protected setupInput(input: HTMLElement): void {
269
+ this.clearInputListeners() // Clear any old listeners
270
+ if (!input.id) {
271
+ this.inputId = `autocomplete-input-${Math.random().toString(36).substring(2, 15)}`
272
+ input.id = this.inputId
273
+ } else {
274
+ this.inputId = input.id
275
+ }
276
+ const anchorElement = this.querySelector('[slot="anchor"]') as HTMLElement | null
277
+ // Ensure CSS anchor positioning can work
278
+ const anchor = anchorElement || input
279
+ anchor.style.setProperty('anchor-name', `--${this.inputId}`)
280
+
281
+ this.inputRef = input as HTMLInputElement // Assuming it behaves like an input
282
+ this.inputRef.addEventListener('focus', this.handleInputFocus)
283
+ this.inputRef.addEventListener('input', this.handleInput)
284
+ this.inputRef.addEventListener('keydown', this.handleKeydown as EventListener)
285
+ this.inputRef.addEventListener('blur', this.handleInputBlur)
286
+
287
+ // Apply initial filtering if suggestions are already present
288
+ if (this.suggestionsRef) {
289
+ this.suggestionsRef.style.setProperty('position-anchor', `--${this.inputId}`)
290
+ this.filterSuggestions((this.inputRef as HTMLInputElement).value || '')
291
+ }
292
+ }
293
+
294
+ /**
295
+ * Sets up the slotted suggestions element (assumed to be a `UiListbox`).
296
+ * Configures popover behavior, CSS anchoring, and attaches event listeners.
297
+ * @param suggestionsElement The `UiListbox` element to be used for suggestions.
298
+ */
299
+ protected setupSuggestions(suggestionsElement: UiListbox): void {
300
+ this.clearSuggestionsListeners() // Clear any old listeners
301
+
302
+ this.suggestionsRef = suggestionsElement
303
+ this.suggestionsRef.popover = 'manual'
304
+ this.suggestionsRef.tabIndex = -1 // Prevent direct focus
305
+
306
+ if (this.inputId) {
307
+ this.suggestionsRef.style.setProperty('position-anchor', `--${this.inputId}`)
308
+ }
309
+
310
+ this.suggestionsRef.addEventListener('select', this.handleSuggestionSelect as EventListener)
311
+ // The `List` dispatches `itemschange` when the slot changes, so we can listen to it.
312
+ this.suggestionsRef.addEventListener('itemschange', this.handleSuggestionsSlotChange)
313
+
314
+ // Initial filter
315
+ this.filterSuggestions(this.inputRef ? (this.inputRef as HTMLInputElement).value : '')
316
+ }
317
+
318
+ /**
319
+ * Handles the focus event on the input element.
320
+ * Opens the suggestions popover if there are items to display.
321
+ */
322
+ @bound
323
+ protected handleInputFocus(): void {
324
+ const items = this.suggestionsRef?.items
325
+ if (!items || items.length === 0) {
326
+ // If no suggestions, do not open
327
+ return
328
+ }
329
+ const active = items.find((item) => !item.hidden)
330
+ if (active) {
331
+ this.openSuggestions()
332
+ this.filterSuggestions(this.inputRef ? (this.inputRef as HTMLInputElement).value : '')
333
+ }
334
+ }
335
+
336
+ /**
337
+ * Handles the input event on the input element.
338
+ * Filters suggestions based on the input query and opens/closes the popover accordingly.
339
+ * @param event The input event.
340
+ */
341
+ @bound
342
+ protected handleInput(event: Event): void {
343
+ this.openSuggestions()
344
+ const query = (event.target as HTMLInputElement).value
345
+ this.filterSuggestions(query)
346
+ }
347
+
348
+ /**
349
+ * Handles keydown events on the input element for navigating and selecting suggestions.
350
+ * - ArrowDown/ArrowUp: Navigates the suggestion list.
351
+ * - Enter: Selects the highlighted suggestion.
352
+ * - Escape: Closes the suggestions popover.
353
+ * @param event The keyboard event.
354
+ */
355
+ @bound
356
+ protected handleKeydown(event: KeyboardEvent): void {
357
+ if (!this.suggestionsRef) return
358
+
359
+ const { key } = event
360
+ const isSuggestionsOpen = this.suggestionsRef.matches(':popover-open')
361
+
362
+ if (['ArrowDown', 'ArrowUp'].includes(key)) {
363
+ event.preventDefault()
364
+ if (!isSuggestionsOpen) {
365
+ this.openSuggestions()
366
+ // Give popover a moment to open before trying to highlight
367
+ requestAnimationFrame(() => this.navigateList(key))
368
+ } else {
369
+ this.navigateList(key)
370
+ }
371
+ } else if (key === 'Enter') {
372
+ if (isSuggestionsOpen && this.suggestionsRef.highlightListItem) {
373
+ event.preventDefault()
374
+ this.suggestionsRef.notifySelect(this.suggestionsRef.highlightListItem)
375
+ }
376
+ } else if (key === 'Escape') {
377
+ if (isSuggestionsOpen) {
378
+ event.preventDefault()
379
+ this.closeSuggestions()
380
+ }
381
+ }
382
+ }
383
+
384
+ /**
385
+ * Navigates the suggestion list based on the pressed key.
386
+ * @param key The key that was pressed (ArrowDown or ArrowUp).
387
+ */
388
+ protected navigateList(key: string): void {
389
+ if (!this.suggestionsRef) return
390
+ switch (key) {
391
+ case 'ArrowDown':
392
+ this.suggestionsRef.highlightNext()
393
+ break
394
+ case 'ArrowUp':
395
+ this.suggestionsRef.highlightPrevious()
396
+ break
397
+ // Don't handle Home or End keys here, as they break the usability of the input.
398
+ }
399
+ }
400
+
401
+ /**
402
+ * Handles the blur event on the input element.
403
+ * Closes the suggestions popover if focus moves outside the autocomplete component.
404
+ */
405
+ @bound
406
+ protected handleInputBlur(): void {
407
+ // We use the manual popover mode, so we need to close suggestions
408
+ // when the input loses focus, but only the current active element is not part of the suggestions.
409
+ requestAnimationFrame(() => {
410
+ if (
411
+ !this.inputRef?.contains(document.activeElement) &&
412
+ (!this.suggestionsRef || !this.suggestionsRef.contains(document.activeElement))
413
+ ) {
414
+ this.closeSuggestions()
415
+ }
416
+ })
417
+ }
418
+
419
+ /**
420
+ * Handles the `select` event dispatched by the `ui-listbox` when a suggestion is chosen.
421
+ * Dispatches an `autocomplete` event and closes the popover.
422
+ * @param event The custom event from `ui-listbox`.
423
+ */
424
+ @bound
425
+ protected handleSuggestionSelect(event: CustomEvent): void {
426
+ event.stopPropagation()
427
+ const selectedItem = event.detail.item as UiListItem
428
+ this.dispatchEvent(
429
+ new CustomEvent('autocomplete', {
430
+ detail: { item: selectedItem },
431
+ bubbles: false,
432
+ composed: false,
433
+ })
434
+ )
435
+ this.closeSuggestions()
436
+ }
437
+
438
+ /**
439
+ * Handles the `itemschange` event dispatched by the `ui-listbox` when its slotted items change.
440
+ * Re-filters the suggestions.
441
+ */
442
+ @bound
443
+ protected handleSuggestionsSlotChange(): void {
444
+ const value = this.inputRef ? (this.inputRef as HTMLInputElement).value : ''
445
+ if (!value) {
446
+ return
447
+ }
448
+ this.openSuggestions()
449
+ this.filterSuggestions(value)
450
+ }
451
+
452
+ /**
453
+ * Opens the suggestions popover if it's not already open and there are visible items.
454
+ */
455
+ protected openSuggestions(): void {
456
+ const popover = this.suggestionsRef
457
+ if (!popover || popover.matches(':popover-open')) {
458
+ return
459
+ }
460
+ // we need to open the popover first to make any measurements
461
+ popover.showPopover()
462
+ const anchor = (this.querySelector('[slot="anchor"]') as HTMLElement | null) || this.inputRef
463
+ if (!anchor) {
464
+ return
465
+ }
466
+ this.positionArea = this.decidePositionArea(popover, anchor)
467
+ }
468
+
469
+ /**
470
+ * Decides the position area for the popover based on available space.
471
+ * It checks if there is enough space below or above the anchor element and decides accordingly.
472
+ *
473
+ * We need to do this because we set the popover height to `100%`, `-webkit-fill-available`, or `-moz-available`
474
+ * and it makes the popover to always open at the bottom, even if there is no space. The `position-try-fallbacks`
475
+ * will not work in this case, because from its perspective there is always enough space, even if that will cause
476
+ * the popover to have a height of just a few pixels.
477
+ *
478
+ * @param popover The popover element to position.
479
+ * @param anchor The anchor element relative to which the popover will be positioned.
480
+ * @returns 'top' or 'bottom' based on available space.
481
+ */
482
+ protected decidePositionArea(popover: HTMLElement, anchor: HTMLElement): 'top' | 'bottom' {
483
+ let newArea: 'top' | 'bottom' = 'bottom'
484
+ // Get bounding rectangles
485
+ const anchorRect = anchor.getBoundingClientRect()
486
+ const viewportHeight = window.innerHeight
487
+
488
+ const spaceBelow = viewportHeight - anchorRect.bottom
489
+ const spaceAbove = anchorRect.top
490
+
491
+ // Estimate a typical/minimum height the popover might need to be useful
492
+ const popoverThresholdHeight = popover.scrollHeight || 150
493
+ if (spaceBelow < popoverThresholdHeight && spaceAbove > spaceBelow && spaceAbove > popoverThresholdHeight) {
494
+ // Not enough space below, but more (and sufficient) space above
495
+ newArea = 'top'
496
+ } else if (spaceBelow < popoverThresholdHeight && spaceAbove > spaceBelow) {
497
+ // Not enough space below, and space above is more than space below (even if not "sufficient")
498
+ newArea = 'top'
499
+ } else {
500
+ newArea = 'bottom' // Default to bottom if enough space or if top is worse
501
+ }
502
+ return newArea
503
+ }
504
+
505
+ /**
506
+ * Closes the suggestions popover if it's open and clears any highlighted item.
507
+ */
508
+ protected closeSuggestions(): void {
509
+ if (this.suggestionsRef && this.suggestionsRef.matches(':popover-open')) {
510
+ this.suggestionsRef.hidePopover()
511
+ }
512
+ }
513
+
514
+ /**
515
+ * Filters the suggestions based on the provided query.
516
+ * Hides items that do not match and manages the highlighted item state.
517
+ * @param query The search query string.
518
+ */
519
+ protected filterSuggestions(query: string): void {
520
+ if (!this.suggestionsRef) {
521
+ return
522
+ }
523
+
524
+ const lowerCaseQuery = query.toLowerCase().trim()
525
+ let firstVisibleItem: UiListItem | null = null
526
+
527
+ // The `items` getter in `List.ts` (parent of UiListbox) correctly gets assigned elements.
528
+ const items = this.suggestionsRef.items as UiListItem[]
529
+
530
+ for (const item of items) {
531
+ let matches = false
532
+ if (lowerCaseQuery === '') {
533
+ matches = true
534
+ } else if (item.dataset.index) {
535
+ const indexFields = item.dataset.index.split(' ').filter(Boolean)
536
+ for (const field of indexFields) {
537
+ const valueToSearch = item.dataset[field] || ''
538
+ if (valueToSearch.toLowerCase().includes(lowerCaseQuery)) {
539
+ matches = true
540
+ break
541
+ }
542
+ }
543
+ } else {
544
+ const valueToSearch = item.dataset.value || item.textContent || ''
545
+ matches = valueToSearch.toLowerCase().includes(lowerCaseQuery)
546
+ }
547
+ item.hidden = !matches
548
+ if (matches && !firstVisibleItem) {
549
+ firstVisibleItem = item
550
+ }
551
+ }
552
+
553
+ // If the currently highlighted item is now hidden, try to highlight the first visible one or clear.
554
+ if (this.suggestionsRef.highlightListItem?.hidden) {
555
+ // the highlightListItem clears the highlighted item if it is not passed an argument.
556
+ this.suggestionsRef?.highlightItem(firstVisibleItem)
557
+ }
558
+ if (!firstVisibleItem) {
559
+ // Close if no items are visible
560
+ this.closeSuggestions()
561
+ }
562
+ }
563
+
564
+ protected override render(): TemplateResult {
565
+ const { id, positionArea } = this
566
+ return html`
567
+ <style>
568
+ #${id} {
569
+ display: inline-block;
570
+
571
+ [popover] {
572
+ border: none;
573
+ margin: 0;
574
+ position-area: ${positionArea};
575
+ position-try-fallbacks:
576
+ flip-block,
577
+ flip-inline,
578
+ flip-block flip-inline;
579
+ width: anchor-size(width);
580
+
581
+ box-shadow: var(--md-sys-elevation-1);
582
+ border-radius: var(--md-sys-shape-corner-medium);
583
+
584
+ overflow: auto;
585
+ /* We try 100% and then vendor options which are more accurate */
586
+ height: 100%;
587
+ height: -webkit-fill-available;
588
+ height: -moz-available;
589
+ max-height: max-content;
590
+ }
591
+
592
+ [popover]:not(:popover-open) {
593
+ display: none;
594
+ }
595
+ }
596
+ </style>
597
+ `
598
+ }
599
+ }
@@ -4,7 +4,7 @@ import { live } from 'lit/directives/live.js'
4
4
  import { ClassInfo, classMap } from 'lit/directives/class-map.js'
5
5
  import UiDialog from '../../../md/dialog/internals/Dialog.js'
6
6
  import type Input from '../../../md/input/Input.js'
7
- import '../../../md/text-field/ui-text-field.js'
7
+ import '../../../md/text-field/ui-filled-text-field.js'
8
8
  import {
9
9
  DeleteCookieStep,
10
10
  type IDeleteCookieStep,
@@ -74,7 +74,7 @@ export default class DeleteCookieAction extends UiDialog {
74
74
  return html`
75
75
  <div class="content">
76
76
  <div class="param-row">
77
- <ui-text-field
77
+ <ui-filled-text-field
78
78
  name="name"
79
79
  label="Cookie name"
80
80
  type="text"
@@ -82,10 +82,10 @@ export default class DeleteCookieAction extends UiDialog {
82
82
  @change="${this.handleNameChange}"
83
83
  class="input"
84
84
  supportingText="Leave empty to delete all cookies for the URL."
85
- ></ui-text-field>
85
+ ></ui-filled-text-field>
86
86
  </div>
87
87
  <div class="param-row">
88
- <ui-text-field
88
+ <ui-filled-text-field
89
89
  name="url"
90
90
  label="Cookie URL"
91
91
  type="url"
@@ -93,7 +93,7 @@ export default class DeleteCookieAction extends UiDialog {
93
93
  @change="${this.handleUrlChange}"
94
94
  class="input"
95
95
  supportingText="When the URL is not set then the request URL is used."
96
- ></ui-text-field>
96
+ ></ui-filled-text-field>
97
97
  </div>
98
98
  </div>
99
99
  `
@@ -7,7 +7,7 @@ import type Input from '../../../md/input/Input.js'
7
7
 
8
8
  import '../../../md/button/ui-text-button.js'
9
9
  import '../../../md/icons/ui-icon.js'
10
- import '../../../md/text-field/ui-text-field.js'
10
+ import '../../../md/text-field/ui-filled-text-field.js'
11
11
 
12
12
  /**
13
13
  * A dialog that requests the user for the new name.
@@ -94,7 +94,7 @@ export default class RenameDialogElement extends UiDialog {
94
94
  const value = this.newValue || this.name || ''
95
95
  return html`
96
96
  <div class="content">
97
- <ui-text-field
97
+ <ui-filled-text-field
98
98
  class="file-name"
99
99
  type="text"
100
100
  name="name"
@@ -104,7 +104,7 @@ export default class RenameDialogElement extends UiDialog {
104
104
  @keydown="${this.handleInputKeyDown}"
105
105
  .value="${live(value)}"
106
106
  label="Enter new name"
107
- ></ui-text-field>
107
+ ></ui-filled-text-field>
108
108
  </div>
109
109
  `
110
110
  }