@rdlabo/capacitor-native-ui 0.0.0-stage → 2.0.0-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.
Files changed (261) hide show
  1. package/LICENSE +21 -0
  2. package/Package.swift +23 -0
  3. package/README.md +66 -2
  4. package/RdlaboCapacitorNativeUi.podspec +17 -0
  5. package/android/build.gradle +69 -0
  6. package/android/proguard-rules.pro +0 -0
  7. package/android/src/main/AndroidManifest.xml +1 -0
  8. package/android/src/main/java/dev/rdlabo/capacitor/nativeui/NativeUI.kt +51 -0
  9. package/android/src/main/java/dev/rdlabo/capacitor/nativeui/NativeUIPlugin.kt +192 -0
  10. package/android/src/main/java/dev/rdlabo/capacitor/nativeui/NativeUISnapshot.kt +57 -0
  11. package/dist/_virtual/_rolldown/runtime.js +13 -0
  12. package/dist/core.d.ts +4 -0
  13. package/dist/core.js +3 -0
  14. package/dist/native/activation.d.ts +14 -0
  15. package/dist/native/activation.d.ts.map +1 -0
  16. package/dist/native/activation.js +305 -0
  17. package/dist/native/activation.js.map +1 -0
  18. package/dist/native/components/index.d.ts +19 -0
  19. package/dist/native/components/index.d.ts.map +1 -0
  20. package/dist/native/components/index.js +38 -0
  21. package/dist/native/components/index.js.map +1 -0
  22. package/dist/native/components/ion-back-button.d.ts +10 -0
  23. package/dist/native/components/ion-back-button.d.ts.map +1 -0
  24. package/dist/native/components/ion-back-button.js +21 -0
  25. package/dist/native/components/ion-back-button.js.map +1 -0
  26. package/dist/native/components/ion-button.d.ts +10 -0
  27. package/dist/native/components/ion-button.d.ts.map +1 -0
  28. package/dist/native/components/ion-button.js +25 -0
  29. package/dist/native/components/ion-button.js.map +1 -0
  30. package/dist/native/components/ion-buttons.d.ts +11 -0
  31. package/dist/native/components/ion-buttons.d.ts.map +1 -0
  32. package/dist/native/components/ion-buttons.js +30 -0
  33. package/dist/native/components/ion-buttons.js.map +1 -0
  34. package/dist/native/components/ion-fab.d.ts +10 -0
  35. package/dist/native/components/ion-fab.d.ts.map +1 -0
  36. package/dist/native/components/ion-fab.js +132 -0
  37. package/dist/native/components/ion-fab.js.map +1 -0
  38. package/dist/native/components/ion-menu-button.d.ts +13 -0
  39. package/dist/native/components/ion-menu-button.d.ts.map +1 -0
  40. package/dist/native/components/ion-menu-button.js +44 -0
  41. package/dist/native/components/ion-menu-button.js.map +1 -0
  42. package/dist/native/components/ion-segment.d.ts +11 -0
  43. package/dist/native/components/ion-segment.d.ts.map +1 -0
  44. package/dist/native/components/ion-segment.js +30 -0
  45. package/dist/native/components/ion-segment.js.map +1 -0
  46. package/dist/native/components/ion-tab-bar.d.ts +11 -0
  47. package/dist/native/components/ion-tab-bar.d.ts.map +1 -0
  48. package/dist/native/components/ion-tab-bar.js +50 -0
  49. package/dist/native/components/ion-tab-bar.js.map +1 -0
  50. package/dist/native/components/searchable-tabs.d.ts +20 -0
  51. package/dist/native/components/searchable-tabs.d.ts.map +1 -0
  52. package/dist/native/components/searchable-tabs.js +413 -0
  53. package/dist/native/components/searchable-tabs.js.map +1 -0
  54. package/dist/native/definitions.d.ts +237 -0
  55. package/dist/native/definitions.d.ts.map +1 -0
  56. package/dist/native/definitions.js +0 -0
  57. package/dist/native/extensions.d.ts +31 -0
  58. package/dist/native/extensions.d.ts.map +1 -0
  59. package/dist/native/extensions.js +122 -0
  60. package/dist/native/extensions.js.map +1 -0
  61. package/dist/native/foldable/fold-state.d.ts +5 -0
  62. package/dist/native/foldable/fold-state.d.ts.map +1 -0
  63. package/dist/native/foldable/fold-state.js +15 -0
  64. package/dist/native/foldable/fold-state.js.map +1 -0
  65. package/dist/native/foldable/types.d.ts +12 -0
  66. package/dist/native/foldable/types.d.ts.map +1 -0
  67. package/dist/native/foldable/types.js +0 -0
  68. package/dist/native/html.d.ts +9 -0
  69. package/dist/native/html.d.ts.map +1 -0
  70. package/dist/native/html.js +197 -0
  71. package/dist/native/html.js.map +1 -0
  72. package/dist/native/index.d.ts +10 -0
  73. package/dist/native/index.d.ts.map +1 -0
  74. package/dist/native/index.js +19 -0
  75. package/dist/native/index.js.map +1 -0
  76. package/dist/native/legacy-layout.d.ts +5 -0
  77. package/dist/native/legacy-layout.d.ts.map +1 -0
  78. package/dist/native/legacy-layout.js +27 -0
  79. package/dist/native/legacy-layout.js.map +1 -0
  80. package/dist/native/overlays/content.d.ts +4 -0
  81. package/dist/native/overlays/content.d.ts.map +1 -0
  82. package/dist/native/overlays/content.js +51 -0
  83. package/dist/native/overlays/content.js.map +1 -0
  84. package/dist/native/overlays/controller.d.ts +8 -0
  85. package/dist/native/overlays/controller.d.ts.map +1 -0
  86. package/dist/native/overlays/controller.js +285 -0
  87. package/dist/native/overlays/controller.js.map +1 -0
  88. package/dist/native/overlays/dialog-animation.d.ts +9 -0
  89. package/dist/native/overlays/dialog-animation.d.ts.map +1 -0
  90. package/dist/native/overlays/dialog-animation.js +53 -0
  91. package/dist/native/overlays/dialog-animation.js.map +1 -0
  92. package/dist/native/overlays/dismiss-animation.d.ts +4 -0
  93. package/dist/native/overlays/dismiss-animation.d.ts.map +1 -0
  94. package/dist/native/overlays/dismiss-animation.js +18 -0
  95. package/dist/native/overlays/dismiss-animation.js.map +1 -0
  96. package/dist/native/overlays/focus.d.ts +5 -0
  97. package/dist/native/overlays/focus.d.ts.map +1 -0
  98. package/dist/native/overlays/focus.js +59 -0
  99. package/dist/native/overlays/focus.js.map +1 -0
  100. package/dist/native/overlays/popover.d.ts +12 -0
  101. package/dist/native/overlays/popover.d.ts.map +1 -0
  102. package/dist/native/overlays/popover.js +110 -0
  103. package/dist/native/overlays/popover.js.map +1 -0
  104. package/dist/native/overlays/styles.d.ts +11 -0
  105. package/dist/native/overlays/styles.d.ts.map +1 -0
  106. package/dist/native/overlays/styles.js +167 -0
  107. package/dist/native/overlays/styles.js.map +1 -0
  108. package/dist/native/prehide.d.ts +8 -0
  109. package/dist/native/prehide.d.ts.map +1 -0
  110. package/dist/native/prehide.js +238 -0
  111. package/dist/native/prehide.js.map +1 -0
  112. package/dist/native/runtime.d.ts +7 -0
  113. package/dist/native/runtime.d.ts.map +1 -0
  114. package/dist/native/runtime.js +776 -0
  115. package/dist/native/runtime.js.map +1 -0
  116. package/dist/native/shared/bridge.d.ts +6 -0
  117. package/dist/native/shared/bridge.d.ts.map +1 -0
  118. package/dist/native/shared/bridge.js +18 -0
  119. package/dist/native/shared/bridge.js.map +1 -0
  120. package/dist/native/shared/candidate.d.ts +25 -0
  121. package/dist/native/shared/candidate.d.ts.map +1 -0
  122. package/dist/native/shared/candidate.js +106 -0
  123. package/dist/native/shared/candidate.js.map +1 -0
  124. package/dist/native/shared/clipping.d.ts +5 -0
  125. package/dist/native/shared/clipping.d.ts.map +1 -0
  126. package/dist/native/shared/clipping.js +26 -0
  127. package/dist/native/shared/clipping.js.map +1 -0
  128. package/dist/native/shared/control-options.d.ts +6 -0
  129. package/dist/native/shared/control-options.d.ts.map +1 -0
  130. package/dist/native/shared/control-options.js +17 -0
  131. package/dist/native/shared/control-options.js.map +1 -0
  132. package/dist/native/shared/crossfade.d.ts +12 -0
  133. package/dist/native/shared/crossfade.d.ts.map +1 -0
  134. package/dist/native/shared/crossfade.js +67 -0
  135. package/dist/native/shared/crossfade.js.map +1 -0
  136. package/dist/native/shared/diagnostics.d.ts +4 -0
  137. package/dist/native/shared/diagnostics.d.ts.map +1 -0
  138. package/dist/native/shared/diagnostics.js +18 -0
  139. package/dist/native/shared/diagnostics.js.map +1 -0
  140. package/dist/native/shared/dom.d.ts +46 -0
  141. package/dist/native/shared/dom.d.ts.map +1 -0
  142. package/dist/native/shared/dom.js +180 -0
  143. package/dist/native/shared/dom.js.map +1 -0
  144. package/dist/native/shared/events.d.ts +15 -0
  145. package/dist/native/shared/events.d.ts.map +1 -0
  146. package/dist/native/shared/events.js +17 -0
  147. package/dist/native/shared/events.js.map +1 -0
  148. package/dist/native/shared/icons.d.ts +8 -0
  149. package/dist/native/shared/icons.d.ts.map +1 -0
  150. package/dist/native/shared/icons.js +61 -0
  151. package/dist/native/shared/icons.js.map +1 -0
  152. package/dist/native/shared/modal.d.ts +10 -0
  153. package/dist/native/shared/modal.d.ts.map +1 -0
  154. package/dist/native/shared/modal.js +160 -0
  155. package/dist/native/shared/modal.js.map +1 -0
  156. package/dist/native/shared/projection-id.d.ts +4 -0
  157. package/dist/native/shared/projection-id.d.ts.map +1 -0
  158. package/dist/native/shared/projection-id.js +7 -0
  159. package/dist/native/shared/projection-id.js.map +1 -0
  160. package/dist/native/shared/toolbar-options.d.ts +13 -0
  161. package/dist/native/shared/toolbar-options.d.ts.map +1 -0
  162. package/dist/native/shared/toolbar-options.js +28 -0
  163. package/dist/native/shared/toolbar-options.js.map +1 -0
  164. package/dist/native/shared/vertical-bars-geometry.d.ts +10 -0
  165. package/dist/native/shared/vertical-bars-geometry.d.ts.map +1 -0
  166. package/dist/native/shared/vertical-bars-geometry.js +85 -0
  167. package/dist/native/shared/vertical-bars-geometry.js.map +1 -0
  168. package/dist/native/startup.d.ts +8 -0
  169. package/dist/native/startup.d.ts.map +1 -0
  170. package/dist/native/startup.js +70 -0
  171. package/dist/native/startup.js.map +1 -0
  172. package/dist/native/vertical-bars-web-search.d.ts +10 -0
  173. package/dist/native/vertical-bars-web-search.d.ts.map +1 -0
  174. package/dist/native/vertical-bars-web-search.js +90 -0
  175. package/dist/native/vertical-bars-web-search.js.map +1 -0
  176. package/dist/native/vertical-bars-web.d.ts +5 -0
  177. package/dist/native/vertical-bars-web.d.ts.map +1 -0
  178. package/dist/native/vertical-bars-web.js +422 -0
  179. package/dist/native/vertical-bars-web.js.map +1 -0
  180. package/dist/native-integration/index.d.ts +25 -0
  181. package/dist/native-integration/index.d.ts.map +1 -0
  182. package/dist/native-integration/index.js +33 -0
  183. package/dist/native-integration/index.js.map +1 -0
  184. package/dist/vertical-bars.d.ts +6 -0
  185. package/dist/vertical-bars.js +5 -0
  186. package/docs/appearance.md +45 -0
  187. package/docs/configuration.md +219 -0
  188. package/docs/framework-independent.md +107 -0
  189. package/docs/ionic-vertical-bars.md +173 -0
  190. package/docs/migration.md +184 -0
  191. package/docs/native-ui-extensions.md +394 -0
  192. package/docs/native-ui.md +528 -0
  193. package/docs/setup.md +224 -0
  194. package/docs/vertical-bars.md +431 -0
  195. package/ios/Sources/NativeUIPlugin/Components/NativeUIButton.swift +71 -0
  196. package/ios/Sources/NativeUIPlugin/Components/NativeUIButtons.swift +24 -0
  197. package/ios/Sources/NativeUIPlugin/Components/NativeUIComponents.swift +19 -0
  198. package/ios/Sources/NativeUIPlugin/Components/NativeUIFab.swift +80 -0
  199. package/ios/Sources/NativeUIPlugin/Components/NativeUISearchController.swift +439 -0
  200. package/ios/Sources/NativeUIPlugin/Components/NativeUISegment.swift +91 -0
  201. package/ios/Sources/NativeUIPlugin/Components/NativeUITabBar.swift +218 -0
  202. package/ios/Sources/NativeUIPlugin/Components/NativeUIVerticalBars.swift +590 -0
  203. package/ios/Sources/NativeUIPlugin/Components/NativeUIVerticalSearch.swift +118 -0
  204. package/ios/Sources/NativeUIPlugin/Extensions/NativeUI.swift +178 -0
  205. package/ios/Sources/NativeUIPlugin/Extensions/NativeUIRendering.swift +38 -0
  206. package/ios/Sources/NativeUIPlugin/NativeUIPlugin.swift +615 -0
  207. package/ios/Sources/NativeUIPlugin/Overlays/NativeUIOverlayController.swift +119 -0
  208. package/ios/Sources/NativeUIPlugin/Overlays/NativeUIOverlayHost.swift +75 -0
  209. package/ios/Sources/NativeUIPlugin/Shared/NativeUIControlRendering.swift +21 -0
  210. package/ios/Sources/NativeUIPlugin/Shared/NativeUICrossfade.swift +35 -0
  211. package/ios/Sources/NativeUIPlugin/Shared/NativeUIHost.swift +8 -0
  212. package/ios/Sources/NativeUIPlugin/Shared/NativeUISnapshot.swift +161 -0
  213. package/ios/Sources/NativeUIPlugin/Shared/NativeUIVerticalBarsGeometry.swift +52 -0
  214. package/ios/Tests/NativeUIPluginTests/NativeUIExtensionTests.swift +165 -0
  215. package/ios/Tests/NativeUIPluginTests/NativeUISnapshotTests.swift +805 -0
  216. package/package.json +89 -4
  217. package/src/core.ts +14 -0
  218. package/src/native/activation.ts +385 -0
  219. package/src/native/components/index.ts +45 -0
  220. package/src/native/components/ion-back-button.ts +22 -0
  221. package/src/native/components/ion-button.ts +25 -0
  222. package/src/native/components/ion-buttons.ts +37 -0
  223. package/src/native/components/ion-fab.ts +151 -0
  224. package/src/native/components/ion-menu-button.ts +37 -0
  225. package/src/native/components/ion-segment.ts +27 -0
  226. package/src/native/components/ion-tab-bar.ts +50 -0
  227. package/src/native/components/searchable-tabs.ts +499 -0
  228. package/src/native/definitions.ts +274 -0
  229. package/src/native/extensions.ts +176 -0
  230. package/src/native/foldable/fold-state.ts +10 -0
  231. package/src/native/foldable/types.ts +4 -0
  232. package/src/native/html.ts +199 -0
  233. package/src/native/index.ts +32 -0
  234. package/src/native/legacy-layout.ts +21 -0
  235. package/src/native/overlays/content.ts +51 -0
  236. package/src/native/overlays/controller.ts +349 -0
  237. package/src/native/overlays/dialog-animation.ts +53 -0
  238. package/src/native/overlays/dismiss-animation.ts +15 -0
  239. package/src/native/overlays/focus.ts +64 -0
  240. package/src/native/overlays/popover.ts +120 -0
  241. package/src/native/overlays/styles.ts +181 -0
  242. package/src/native/prehide.ts +285 -0
  243. package/src/native/runtime.ts +979 -0
  244. package/src/native/shared/bridge.ts +18 -0
  245. package/src/native/shared/candidate.ts +162 -0
  246. package/src/native/shared/clipping.ts +24 -0
  247. package/src/native/shared/control-options.ts +20 -0
  248. package/src/native/shared/crossfade.ts +67 -0
  249. package/src/native/shared/diagnostics.ts +13 -0
  250. package/src/native/shared/dom.ts +304 -0
  251. package/src/native/shared/events.ts +14 -0
  252. package/src/native/shared/icons.ts +57 -0
  253. package/src/native/shared/modal.ts +157 -0
  254. package/src/native/shared/projection-id.ts +2 -0
  255. package/src/native/shared/toolbar-options.ts +49 -0
  256. package/src/native/shared/vertical-bars-geometry.ts +80 -0
  257. package/src/native/startup.ts +68 -0
  258. package/src/native/vertical-bars-web-search.ts +86 -0
  259. package/src/native/vertical-bars-web.ts +500 -0
  260. package/src/native-integration/index.ts +49 -0
  261. package/src/vertical-bars.ts +12 -0
@@ -0,0 +1,32 @@
1
+ // Compatibility Ionic entrypoint. Standard HTML consumers can import /core without Ionic.
2
+ import { startNativeUI as activate } from './startup';
3
+ import type { NativeUIOptions, VerticalControlAreaOptions, VerticalControlAreaHandle } from './definitions';
4
+ import { setVerticalControlAreaPlacement } from './legacy-layout';
5
+ export { setVerticalControlAreaPlacement } from './legacy-layout';
6
+ export { configureNativeTransition, NativeUI } from './activation';
7
+ export { defineNativeUI } from './extensions';
8
+ export type { NativeUIExtension, NativeUIValue, NativeUIReadContext, NativeUIImageReference } from './extensions';
9
+ export type {
10
+ NativeUIVerticalBarsOptions,
11
+ NativeUIComponent,
12
+ NativeUIControls,
13
+ ControlProjection,
14
+ IonicProjectionOptions,
15
+ NativeUIHandle,
16
+ NativeUIOptions,
17
+ NativeUIStatus,
18
+ NativeUISuspension,
19
+ VerticalBarEdge,
20
+ VerticalBarPlacement,
21
+ VerticalControlAreaHandle,
22
+ VerticalControlAreaOptions,
23
+ WebViewMetrics,
24
+ } from './definitions';
25
+
26
+ export const enableNativeUI = (options: NativeUIOptions = {}) =>
27
+ activate(options, async (...args) => (await import('./overlays/controller')).createOverlayController(...args));
28
+
29
+ export const enableVerticalControlArea = async (options: VerticalControlAreaOptions = {}): Promise<VerticalControlAreaHandle> => {
30
+ const handle = await enableNativeUI({ ...options, controls: options.controls ?? 'verticalOnly' });
31
+ return { ...handle, setPlacement: setVerticalControlAreaPlacement };
32
+ };
@@ -0,0 +1,21 @@
1
+ import type { VerticalBarEdge, VerticalBarPlacement } from './definitions';
2
+ const physicalVerticalBarEdge = (edge: Exclude<VerticalBarEdge, null>, rtl: boolean) => ((edge === 'leading') !== rtl ? 'left' : 'right');
3
+ const elementRtl = (element: Element): boolean => element.closest('[dir]')?.getAttribute('dir') === 'rtl';
4
+
5
+ /**
6
+ * @deprecated Use the theme setVerticalBarsLayout helper. nativeEdge is ignored; device metrics determine availability.
7
+ * Legacy Web layout adapter; does not report native availability.
8
+ * Pass `rtl` when the document direction is known; otherwise the nearest `dir` attribute is used.
9
+ */
10
+ export const setVerticalControlAreaPlacement = (placement: VerticalBarEdge | VerticalBarPlacement, rtl?: boolean): void => {
11
+ if (typeof document === 'undefined') return;
12
+ const app = document.querySelector<HTMLElement>('ion-app');
13
+ if (!app) throw new Error('Vertical Control Area requires ion-app');
14
+ const { edge, inset = 0 } = placement && typeof placement === 'object' ? placement : { edge: placement, inset: 0 };
15
+ app.removeAttribute('data-native-ui-vertical-bars-suspended');
16
+ app.classList.toggle('ios-theme-vertical-bars', edge !== null);
17
+ app.classList.toggle('ios-theme-vertical-bars-left', edge !== null && physicalVerticalBarEdge(edge, rtl ?? elementRtl(app)) === 'left');
18
+ if (edge && Number.isFinite(inset) && inset > 0) app.style.setProperty('--ios-theme-vertical-bars-native-inset', `${inset}px`);
19
+ else app.style.removeProperty('--ios-theme-vertical-bars-native-inset');
20
+ document.defaultView?.dispatchEvent(new Event('nativeUIRefresh'));
21
+ };
@@ -0,0 +1,51 @@
1
+ import { sheetText } from './styles';
2
+
3
+ /** Constructed stylesheets cannot cross documents with their nodes. */
4
+ export const moveContent = (content: HTMLElement[], destination: HTMLElement): (() => void) => {
5
+ const home = content.map((element) => ({ element, parent: element.parentNode!, next: element.nextSibling }));
6
+ const roots: { root: ShadowRoot; css: string; sheets: CSSStyleSheet[] }[] = [];
7
+ const collect = (node: Element) => {
8
+ if (node.shadowRoot) {
9
+ roots.push({
10
+ root: node.shadowRoot,
11
+ css: sheetText(node.shadowRoot.adoptedStyleSheets),
12
+ sheets: [...node.shadowRoot.adoptedStyleSheets],
13
+ });
14
+ for (const child of Array.from(node.shadowRoot.children)) collect(child);
15
+ }
16
+ for (const child of Array.from(node.children)) collect(child);
17
+ };
18
+ content.forEach(collect);
19
+ for (const element of content) destination.append(destination.ownerDocument.adoptNode(element));
20
+ const styles = roots
21
+ .filter(({ css }) => css)
22
+ .map(({ root, css }) => {
23
+ const style = destination.ownerDocument.createElement('style');
24
+ style.textContent = css;
25
+ root.append(style);
26
+ return style;
27
+ });
28
+ return () => {
29
+ styles.forEach((style) => style.remove());
30
+ for (const { element, parent, next } of home) {
31
+ const connected = element.isConnected;
32
+ parent.ownerDocument!.adoptNode(element);
33
+ // Ionic may already have destroyed the Angular component on dismiss.
34
+ if (connected) parent.insertBefore(element, next?.parentNode === parent ? next : null);
35
+ }
36
+ for (const { root, css, sheets } of roots) {
37
+ // WebKit re-associates adopted sheets with the new document across
38
+ // adoptNode, so they may no longer match this root; fall back to the
39
+ // inline copy instead of aborting the remaining restore.
40
+ try {
41
+ root.adoptedStyleSheets = sheets;
42
+ } catch {
43
+ if (css) {
44
+ const style = root.ownerDocument.createElement('style');
45
+ style.textContent = css;
46
+ root.append(style);
47
+ }
48
+ }
49
+ }
50
+ };
51
+ };
@@ -0,0 +1,349 @@
1
+ import type { NativeUIOptions, NativeUIPlugin } from '../definitions';
2
+ import { bounded, boundedListener } from '../shared/bridge';
3
+ import { isExcluded, isPermanentlyExcluded, isNativeUIDisabled } from '../shared/dom';
4
+ import { activeElement, trapFocus } from './focus';
5
+ import { relayPopover, popoverPresentation, verticalBarsPopoverAnchor } from './popover';
6
+ import { nativeDialogAnimation } from './dialog-animation';
7
+ import { relayLayoutSupported, relayStyles } from './styles';
8
+ import { moveContent } from './content';
9
+ import { validVerticalBarFrame } from '../shared/vertical-bars-geometry';
10
+
11
+ type Presentation = { kind: 'popover'; animated: boolean };
12
+ type Overlay = HTMLIonPopoverElement;
13
+ const lifecycle = (overlay: Overlay, phase: string) =>
14
+ `ion${overlay.localName.slice(4, 5).toUpperCase()}${overlay.localName.slice(5)}${phase}`;
15
+
16
+ interface Connection {
17
+ id: string;
18
+ overlay: Overlay;
19
+ dismissed: boolean;
20
+ win?: Window;
21
+ focus: Element | null;
22
+ stopStyles?: () => void;
23
+ releaseProjection?: () => void;
24
+ stopContent?: () => void;
25
+ stopFocus?: () => void;
26
+ stopEvents?: () => void;
27
+ stopChildEvents?: () => void;
28
+ stopExclusions?: () => void;
29
+ closing?: Promise<void>;
30
+ nativeClosing?: Promise<void>;
31
+ stopAnimation?: () => void;
32
+ hideAnimation?: () => void;
33
+ stopLifecycle?: () => void;
34
+ }
35
+
36
+ /** Ionic owns lifecycle and dismissal; only one overlay is relayed at a time. */
37
+ export const createOverlayController = async (
38
+ doc: Document,
39
+ plugin: NativeUIPlugin,
40
+ options: NativeUIOptions,
41
+ retainProjection: (deferActivation?: (id: string) => boolean) => () => void,
42
+ nativeVerticalBars: () => boolean,
43
+ ) => {
44
+ let current: Connection | undefined;
45
+ let opening = Promise.resolve();
46
+ let sequence = 0;
47
+ let stopped = false;
48
+ const suspensions = new Set<symbol>();
49
+ const events = ['Modal', 'Popover', 'Alert', 'ActionSheet', 'Loading', 'Picker', 'Toast'].map((name) => `ion${name}WillPresent`);
50
+ const cleanup = async (connection: Connection) => {
51
+ // Every teardown step is isolated and bounded: a throwing or hanging step
52
+ // must not strand the retained page projections or the child window.
53
+ const stage = async (label: string, run?: () => void | Promise<void>) => {
54
+ try {
55
+ await bounded(Promise.resolve(run?.()));
56
+ } catch (error) {
57
+ console.error(`Native overlay teardown failed at ${label}; continuing cleanup anyway.`, error);
58
+ }
59
+ };
60
+ try {
61
+ await stage('events', connection.stopEvents);
62
+ await stage('lifecycle', connection.stopLifecycle);
63
+ await stage('childEvents', connection.stopChildEvents);
64
+ await stage('exclusions', connection.stopExclusions);
65
+ await stage('focus', connection.stopFocus);
66
+ // A timed-out dismissal must not strand the relayed content in the child window.
67
+ await connection.nativeClosing?.catch((error) => {
68
+ console.error('Native overlay dismissal did not complete; restoring the Web content anyway.', error);
69
+ });
70
+ await stage('content', connection.stopContent);
71
+ await stage('animation', connection.stopAnimation);
72
+ await stage('styles', connection.stopStyles);
73
+ await stage('close', () => bounded(plugin.closeOverlay({ id: connection.id })));
74
+ } finally {
75
+ // The page projections must be released even if teardown throws above.
76
+ connection.releaseProjection?.();
77
+ connection.releaseProjection = undefined;
78
+ try {
79
+ connection.win?.close();
80
+ } catch (error) {
81
+ console.error('Native overlay window was already gone.', error);
82
+ }
83
+ if (current === connection) current = undefined;
84
+ const focus = connection.focus as HTMLElement | null;
85
+ if (focus?.isConnected && focus.tabIndex >= 0) focus.focus({ preventScroll: true });
86
+ }
87
+ };
88
+ const release = (connection: Connection): Promise<void> => (connection.closing ??= cleanup(connection));
89
+ const restoreWeb = async () => {
90
+ if (current) await release(current);
91
+ };
92
+ const excluded = (overlay: HTMLElement) => isPermanentlyExcluded(overlay) || isExcluded(overlay) || isNativeUIDisabled(overlay);
93
+ const connect = async (connection: Connection, ready: Promise<void>, presentation: Presentation) => {
94
+ const { overlay } = connection;
95
+ const gone = () =>
96
+ current !== connection ||
97
+ stopped ||
98
+ suspensions.size ||
99
+ connection.dismissed ||
100
+ !overlay.isConnected ||
101
+ !nativeVerticalBars() ||
102
+ excluded(overlay);
103
+ // Keep the source invisible while the native toolbar prepares its popover.
104
+ connection.hideAnimation?.();
105
+ try {
106
+ // Prepare the child window concurrently with Ionic's content initialization.
107
+ const staging = (async () => {
108
+ await bounded(
109
+ plugin.prepareOverlay({
110
+ id: connection.id,
111
+ }),
112
+ );
113
+ if (gone()) return undefined;
114
+ const win = doc.defaultView!.open(`about:blank#${connection.id}`, '_blank');
115
+ if (!win) throw new Error('Native overlay window unavailable');
116
+ connection.win = win;
117
+ // The requested blank page replaces the initial document created by window.open.
118
+ // WebKit percent-encodes the '#' marker in the popup URL; compare decoded.
119
+ // Adopt nodes only after that navigation; otherwise WebKit discards the relay.
120
+ if (decodeURIComponent(win.document.URL) !== `about:blank#${connection.id}` || win.document.readyState !== 'complete')
121
+ await bounded(new Promise<void>((resolve) => win.addEventListener('load', () => resolve(), { once: true })));
122
+ if (gone()) return undefined;
123
+ // about:blank is a quirks-mode document in WebKit. Initialize standards
124
+ // mode before adopting live content: otherwise percentage button heights
125
+ // can resolve against the entire popover viewport.
126
+ win.document.open();
127
+ win.document.write('<!doctype html><html><head></head><body></body></html>');
128
+ win.document.close();
129
+ connection.stopChildEvents = observe(win.document);
130
+ const environment = relayStyles(doc, win.document, overlay, () => {
131
+ if (connection.stopContent) void release(connection).catch(console.error);
132
+ });
133
+ connection.stopStyles = environment.stop;
134
+ await bounded(environment.ready);
135
+ if (gone()) return undefined;
136
+ return { destination: environment.destination, win };
137
+ })();
138
+ // The staging join is awaited below; settle early rejections quietly.
139
+ staging.catch(() => {});
140
+ // Ionic initializes its gestures after didPresent. Let that turn finish before adopting their content.
141
+ // A stalled presentation must not park the opening queue forever.
142
+ await bounded(ready);
143
+ // Placement is captured after Ionic has laid out the overlay.
144
+ await bounded(new Promise<void>((resolve) => doc.defaultView!.requestAnimationFrame(() => resolve())));
145
+ const staged = await staging;
146
+ if (gone() || !staged) {
147
+ await release(connection);
148
+ return;
149
+ }
150
+ const focused = activeElement(doc) as HTMLElement | null;
151
+ const nativePresentation = popoverPresentation(overlay);
152
+ const content = relayPopover(overlay, staged.destination);
153
+ connection.stopContent = content.stop;
154
+ if (!relayLayoutSupported(overlay) || !content.matchesSourceAppearance()) {
155
+ await release(connection);
156
+ return;
157
+ }
158
+ connection.hideAnimation?.();
159
+ // Ionic does not dismiss a presented overlay when its page is hidden or
160
+ // destroyed; without this a popover could retain the page projections
161
+ // forever and freeze every later control update.
162
+ const exclusions = new MutationObserver(() => {
163
+ if (!overlay.isConnected || !nativeVerticalBars() || excluded(overlay) || isNativeUIDisabled(content.root))
164
+ void release(connection).catch(console.error);
165
+ });
166
+ const attributes = { attributes: true, attributeFilter: ['class', 'data-native-ui', 'hidden', 'inert'] };
167
+ for (let node: HTMLElement | null = overlay; node; node = node.parentElement) {
168
+ exclusions.observe(node, attributes);
169
+ exclusions.observe(node, { childList: true });
170
+ }
171
+ exclusions.observe(content.root, { ...attributes, subtree: true });
172
+ connection.stopExclusions = () => exclusions.disconnect();
173
+ connection.stopFocus = trapFocus(
174
+ content.root,
175
+ () =>
176
+ !connection.dismissed &&
177
+ (!('focusTrap' in overlay) || overlay.focusTrap !== false) &&
178
+ !overlay.classList.contains('ion-disable-focus-trap'),
179
+ );
180
+ const close = () => {
181
+ void release(connection).catch(console.error);
182
+ };
183
+ const escape = (event: KeyboardEvent) => {
184
+ if (event.key === 'Escape' && overlay.backdropDismiss) void overlay.dismiss(undefined, 'backdrop');
185
+ };
186
+ staged.win.document.addEventListener('keydown', escape);
187
+ overlay.addEventListener(lifecycle(overlay, 'DidDismiss'), close, { once: true });
188
+ connection.stopEvents = () => {
189
+ staged.win.document.removeEventListener('keydown', escape);
190
+ overlay.removeEventListener(lifecycle(overlay, 'DidDismiss'), close);
191
+ };
192
+ // SwiftUI owns the only visible presentation animation.
193
+ await bounded(
194
+ plugin.presentOverlay({
195
+ id: connection.id,
196
+ presentation: { ...nativePresentation, animated: presentation.animated },
197
+ }),
198
+ );
199
+ (focused?.isConnected && focused.ownerDocument === staged.win.document ? focused : content.root).focus({ preventScroll: true });
200
+ } catch (error) {
201
+ await release(connection);
202
+ console.error('Native overlay projection failed; keeping Ionic content in the source WebView.', error);
203
+ }
204
+ };
205
+ const present = (event: Event) => {
206
+ const overlay = event.target as Overlay;
207
+ if (current) {
208
+ const connection = current;
209
+ const fromChild = overlay.ownerDocument === connection.win?.document;
210
+ if (fromChild) {
211
+ // A child-document overlay may be a sibling of the relayed content.
212
+ // Move it before cleanup closes that document; keep the live node,
213
+ // component handlers and adopted shadow styles in the source WebView.
214
+ moveContent([overlay], doc.querySelector<HTMLElement>('ion-app') ?? doc.body);
215
+ }
216
+ current = undefined;
217
+ opening = opening.then(() => release(connection)).catch(console.error);
218
+ // The original event path still belongs to the child document. Let the
219
+ // source runtime observe presentation before releasing retained controls.
220
+ if (fromChild) doc.dispatchEvent(new CustomEvent('nativeUIOverlayAdopted', { detail: overlay }));
221
+ if (!connection.dismissed) return;
222
+ // A didDismiss handler may immediately open the next popover while SwiftUI is still closing this one.
223
+ connection.stopAnimation?.();
224
+ connection.stopAnimation = undefined;
225
+ }
226
+ const enabled =
227
+ overlay.localName === 'ion-popover' &&
228
+ overlay.mode !== 'md' &&
229
+ !overlay.classList.contains('md') &&
230
+ options.ionic?.verticalBars?.popover === true &&
231
+ nativeVerticalBars() &&
232
+ verticalBarsPopoverAnchor(overlay) !== undefined;
233
+ if (stopped || suspensions.size || !enabled || excluded(overlay)) {
234
+ opening = opening.then(restoreWeb).catch(console.error);
235
+ return;
236
+ }
237
+ const connection: Connection = { id: `ios-theme-overlay-${++sequence}`, overlay, dismissed: false, focus: activeElement(doc) };
238
+ const presentation: Presentation = { kind: 'popover', animated: overlay.animated };
239
+ current = connection;
240
+ // Retain synchronously, before the scheduled projection update can retire the rail.
241
+ const anchorId = verticalBarsPopoverAnchor(overlay);
242
+ connection.releaseProjection = retainProjection(
243
+ (id) =>
244
+ id === anchorId &&
245
+ current === connection &&
246
+ !!connection.nativeClosing &&
247
+ !stopped &&
248
+ !suspensions.size &&
249
+ nativeVerticalBars() &&
250
+ !excluded(overlay),
251
+ );
252
+ const closed = () => {
253
+ connection.dismissed = true;
254
+ };
255
+ overlay.addEventListener(lifecycle(overlay, 'DidDismiss'), closed, { once: true });
256
+ let resolveReady!: () => void;
257
+ const readyPromise = new Promise<void>((resolve) => {
258
+ resolveReady = resolve;
259
+ });
260
+ const ready = () => {
261
+ resolveReady();
262
+ };
263
+ const closeNative = () =>
264
+ (connection.nativeClosing ??= bounded(plugin.dismissOverlay({ id: connection.id, animated: presentation.animated })));
265
+ const viewport = `${win.innerWidth}:${win.innerHeight}`;
266
+ const anchor = bounded(plugin.getVerticalBarsGeometry({ viewportWidth: win.innerWidth, ids: [anchorId!] }))
267
+ .then(({ items }) => {
268
+ const frame = items[anchorId!];
269
+ return frame && validVerticalBarFrame(frame) && viewport === `${win.innerWidth}:${win.innerHeight}` ? frame : undefined;
270
+ })
271
+ .catch(() => undefined);
272
+ const animation = nativeDialogAnimation(overlay, closeNative, anchor);
273
+ connection.stopAnimation = animation.stop;
274
+ connection.hideAnimation = animation.hide;
275
+ animation.hide();
276
+ overlay.addEventListener(lifecycle(overlay, 'DidPresent'), ready, { once: true });
277
+ connection.stopLifecycle = () => overlay.removeEventListener(lifecycle(overlay, 'DidDismiss'), closed);
278
+ opening = opening
279
+ .then(() => connect(connection, readyPromise, presentation))
280
+ .catch(console.error)
281
+ .finally(() => {
282
+ overlay.removeEventListener(lifecycle(overlay, 'DidPresent'), ready);
283
+ });
284
+ };
285
+ const menuOpen = (event: Event) => {
286
+ if (!event.composedPath().some((node): node is HTMLElement => node instanceof HTMLElement && node.matches('ion-menu'))) return;
287
+ // Menus render in the source document below the native overlay; release the relay.
288
+ const connection = current;
289
+ current = undefined;
290
+ if (connection) opening = opening.then(() => release(connection)).catch(console.error);
291
+ };
292
+ const observe = (target: Document) => {
293
+ for (const name of events) target.addEventListener(name, present);
294
+ target.addEventListener('ionWillOpen', menuOpen);
295
+ return () => {
296
+ for (const name of events) target.removeEventListener(name, present);
297
+ target.removeEventListener('ionWillOpen', menuOpen);
298
+ };
299
+ };
300
+ const invalidate = () => {
301
+ const connection = current;
302
+ current = undefined;
303
+ if (connection) opening = opening.then(() => release(connection)).catch(console.error);
304
+ };
305
+ // Retention must not freeze an obsolete layout or a departed page.
306
+ const win = doc.defaultView!;
307
+ const visibility = () => {
308
+ if (doc.hidden) invalidate();
309
+ };
310
+ const listener = await boundedListener(
311
+ plugin.addListener('overlay', (event) => {
312
+ if (event.id !== current?.id) return;
313
+ if (event.action === 'dismiss' && current.overlay.backdropDismiss)
314
+ void current.overlay.dismiss(undefined, 'backdrop').catch(console.error);
315
+ }),
316
+ );
317
+ win.addEventListener('resize', invalidate);
318
+ doc.addEventListener('ionViewWillLeave', invalidate);
319
+ doc.addEventListener('visibilitychange', visibility);
320
+ const stopEvents = observe(doc);
321
+ return {
322
+ async suspend() {
323
+ const lease = Symbol();
324
+ suspensions.add(lease);
325
+ await opening;
326
+ await restoreWeb();
327
+ return () => {
328
+ suspensions.delete(lease);
329
+ };
330
+ },
331
+ async destroy() {
332
+ stopped = true;
333
+ stopEvents();
334
+ win.removeEventListener('resize', invalidate);
335
+ doc.removeEventListener('ionViewWillLeave', invalidate);
336
+ doc.removeEventListener('visibilitychange', visibility);
337
+ try {
338
+ await opening;
339
+ await restoreWeb();
340
+ } finally {
341
+ try {
342
+ await bounded(plugin.stopOverlays());
343
+ } finally {
344
+ await bounded(listener.remove());
345
+ }
346
+ }
347
+ },
348
+ };
349
+ };
@@ -0,0 +1,53 @@
1
+ import type { Animation } from '@ionic/core';
2
+ import { createAnimation } from '@ionic/core/components/index.js';
3
+ import { iosPopoverEnterAnimation } from '@rdlabo/ionic-theme-utils';
4
+ import type { Frame } from '../definitions';
5
+ import { dismissAnimation } from './dismiss-animation';
6
+
7
+ export type Dialog = HTMLIonPopoverElement;
8
+
9
+ /** Prepare the source's final layout while SwiftUI owns visible motion. */
10
+ export const nativeDialogAnimation = (overlay: Dialog, close: () => Promise<void>, anchor: Promise<Frame | undefined>) => {
11
+ const enterAnimation = overlay.enterAnimation;
12
+ const builder = enterAnimation ?? iosPopoverEnterAnimation;
13
+ overlay.enterAnimation = (base, options) => {
14
+ const animation = createAnimation();
15
+ const play = animation.play;
16
+ animation.play = async () => {
17
+ const frame = await anchor;
18
+ const proxy = overlay.ownerDocument.createElement('ion-button');
19
+ if (frame) proxy.getBoundingClientRect = () => new DOMRect(frame.x, frame.y, frame.width, frame.height);
20
+ // The real trigger is display:none while projected. Use only a detached
21
+ // measurement proxy; never replace or mutate the application's button.
22
+ const positioned = frame ? { ...options, trigger: proxy } : options;
23
+ animation.addAnimation(builder(base, positioned));
24
+ const finishWithoutMotion = (item: Animation) => {
25
+ item.duration(0).delay(0);
26
+ item.childAnimations.forEach(finishWithoutMotion);
27
+ };
28
+ finishWithoutMotion(animation);
29
+ // Ionic owns this wrapper and destroys its children on dismissal.
30
+ await play({ sync: true });
31
+ };
32
+ return animation;
33
+ };
34
+ const leaveAnimation = overlay.leaveAnimation;
35
+ const opacity = overlay.style.opacity;
36
+ const event = 'ionPopoverWillDismiss';
37
+ const dismiss = () => {
38
+ const closing = close();
39
+ overlay.leaveAnimation = () => dismissAnimation(closing);
40
+ };
41
+ overlay.addEventListener(event, dismiss);
42
+ return {
43
+ hide() {
44
+ overlay.style.opacity = '0';
45
+ },
46
+ stop() {
47
+ overlay.removeEventListener(event, dismiss);
48
+ overlay.enterAnimation = enterAnimation;
49
+ overlay.leaveAnimation = leaveAnimation;
50
+ overlay.style.opacity = opacity;
51
+ },
52
+ };
53
+ };
@@ -0,0 +1,15 @@
1
+ import { createAnimation } from '@ionic/core/components/index.js';
2
+
3
+ /** Let Ionic finish dismissal after the native presentation has closed. */
4
+ export const dismissAnimation = (closing: Promise<void>) => {
5
+ const animation = createAnimation();
6
+ const play = animation.play;
7
+ animation.play = async (options) => {
8
+ // A native failure must not suppress Ionic's didDismiss event/data.
9
+ await closing.catch((error) => {
10
+ console.error('Native popover dismissal failed; completing the Ionic lifecycle.', error);
11
+ });
12
+ await play(options);
13
+ };
14
+ return animation;
15
+ };
@@ -0,0 +1,64 @@
1
+ export const activeElement = (doc: Document): Element | null => {
2
+ let element = doc.activeElement;
3
+ while (element?.shadowRoot?.activeElement) element = element.shadowRoot.activeElement;
4
+ return element;
5
+ };
6
+
7
+ const focusableElements = (root: HTMLElement): HTMLElement[] => {
8
+ const elements: HTMLElement[] = [];
9
+ const win = root.ownerDocument.defaultView!;
10
+ const collect = (scope: HTMLElement | ShadowRoot) => {
11
+ for (const node of Array.from(scope.querySelectorAll<HTMLElement>('*'))) {
12
+ if (node.inert || node.closest('[inert]') || (node as HTMLButtonElement).disabled) continue;
13
+ const before = elements.length;
14
+ if (node.shadowRoot) collect(node.shadowRoot);
15
+ const css = win.getComputedStyle(node);
16
+ if (
17
+ elements.length === before &&
18
+ node.tabIndex >= 0 &&
19
+ !node.hidden &&
20
+ css.display !== 'none' &&
21
+ css.visibility !== 'hidden' &&
22
+ node.getClientRects().length
23
+ )
24
+ elements.push(node);
25
+ }
26
+ };
27
+ collect(root);
28
+ return elements
29
+ .map((element, index) => ({ element, index }))
30
+ .sort((a, b) => (a.element.tabIndex || Infinity) - (b.element.tabIndex || Infinity) || a.index - b.index)
31
+ .map(({ element }) => element);
32
+ };
33
+
34
+ export const trapFocus = (root: HTMLElement, enabled: () => boolean): (() => void) => {
35
+ const doc = root.ownerDocument;
36
+ const previous = root.getAttribute('tabindex');
37
+ root.tabIndex = -1;
38
+ let last: Element | null;
39
+ const onFocus = (event: FocusEvent) => {
40
+ if (!enabled()) return;
41
+ if (event.composedPath().includes(root)) {
42
+ last = activeElement(doc);
43
+ return;
44
+ }
45
+ const elements = focusableElements(root);
46
+ (elements.find((element) => element === last) ?? elements[0] ?? root).focus({ preventScroll: true });
47
+ };
48
+ const onKey = (event: KeyboardEvent) => {
49
+ if (event.key !== 'Tab' || event.altKey || event.ctrlKey || event.metaKey || !enabled()) return;
50
+ const elements = focusableElements(root);
51
+ const index = elements.findIndex((element) => element === activeElement(doc));
52
+ const next = event.shiftKey ? (index <= 0 ? elements.length - 1 : index - 1) : (index + 1) % elements.length;
53
+ event.preventDefault();
54
+ (elements[next] ?? root).focus();
55
+ };
56
+ doc.addEventListener('focusin', onFocus, true);
57
+ doc.addEventListener('keydown', onKey, true);
58
+ return () => {
59
+ doc.removeEventListener('focusin', onFocus, true);
60
+ doc.removeEventListener('keydown', onKey, true);
61
+ if (previous === null) root.removeAttribute('tabindex');
62
+ else root.setAttribute('tabindex', previous);
63
+ };
64
+ };