@octane-xplat/context-menu 0.14.0 → 0.15.0

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/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@octane-xplat/context-menu",
3
- "version": "0.14.0",
3
+ "version": "0.15.0",
4
4
  "description": "Platform-specific context menu controls for Octane xplat",
5
5
  "repository": {
6
6
  "type": "git",
@@ -8,30 +8,35 @@
8
8
  "directory": "packages/context-menu"
9
9
  },
10
10
  "files": [
11
- "dist",
12
11
  "types",
13
12
  "src",
14
- "platforms"
13
+ "platforms",
14
+ "!src/**/*.test.*",
15
+ "!src/**/*.spec.*"
15
16
  ],
16
17
  "type": "module",
17
18
  "exports": {
18
19
  ".": {
19
20
  "types": "./types/index.d.ts",
20
- "web": "./dist/native/index.js",
21
- "native": "./dist/native/index.js",
22
- "default": "./dist/native/index.js"
21
+ "web": "./src/index.ts",
22
+ "native": "./src/index.ts",
23
+ "default": "./src/index.ts"
23
24
  },
24
25
  "./ios": {
25
26
  "types": "./types/ios/index.d.ts",
26
- "native": "./dist/native/ios/index.js"
27
+ "native": "./src/ios/index.ts"
27
28
  },
28
29
  "./android": {
29
30
  "types": "./types/android/index.d.ts",
30
- "native": "./dist/native/android/index.js"
31
+ "native": "./src/android/index.ts"
31
32
  },
32
33
  "./web": {
33
34
  "types": "./types/web/index.d.ts",
34
- "web": "./dist/web/web/index.js"
35
+ "web": "./src/web/index.ts"
36
+ },
37
+ "./macos": {
38
+ "types": "./types/macos/index.d.ts",
39
+ "macos": "./src/macos/index.ts"
35
40
  }
36
41
  },
37
42
  "publishConfig": {
@@ -39,14 +44,16 @@
39
44
  },
40
45
  "dependencies": {
41
46
  "@nativescript/jetpack-compose": "1.0.0-beta.0",
42
- "@nativescript/swift-ui": "4.0.2"
47
+ "@nativescript/swift-ui": "4.0.2",
48
+ "@octane-xplat/lib": "0.15.0"
43
49
  },
44
50
  "devDependencies": {
45
- "@nativescript-community/octane": "0.2.4",
51
+ "@nativescript-community/octane": "0.2.5",
46
52
  "@nativescript/core": "9.1.3",
47
- "@octanejs/vite-plugin": "0.1.60",
48
- "octane": "0.5.0",
49
- "tsrx-typegen": "0.2.2",
53
+ "@octane-xplat/macos-renderer": "0.15.0",
54
+ "@octanejs/vite-plugin": "0.1.61",
55
+ "octane": "0.11.0",
56
+ "tsrx-typegen": "0.3.0",
50
57
  "typescript": "5.9.3",
51
58
  "vite": "8.3.0"
52
59
  },
@@ -73,8 +80,8 @@
73
80
  }
74
81
  },
75
82
  "scripts": {
76
- "build": "vite build && vite build --mode native && tsc -p tsconfig.types.json",
77
83
  "test:packed": "node tests/packed-consumer.mjs",
78
- "pack:check": "tsrx-typegen --pack-check"
84
+ "pack:check": "tsrx-typegen --pack-check",
85
+ "typegen": "tsc -p tsconfig.types.json"
79
86
  }
80
87
  }
@@ -10,6 +10,10 @@ const host = (): any => (globalThis as any).__xplatAppKit ?? {}
10
10
  * serialized data; selection reports through `onItemSelected`. */
11
11
  export function AppKitContextMenu(props: AppKitContextMenuProps) @{
12
12
  const anchor = useRef<any>(null)
13
+ // Selection must reach the latest handler — re-attaching the NSMenu on
14
+ // every callback change would churn the view's `menu` subtree.
15
+ const latest = useRef(props)
16
+ latest.current = props
13
17
 
14
18
  useEffect(() => {
15
19
  const view = anchor.current
@@ -17,8 +21,8 @@ export function AppKitContextMenu(props: AppKitContextMenuProps) @{
17
21
  if (!view || typeof attach !== 'function') return
18
22
  return attach(
19
23
  view,
20
- { items: props.items ?? [] },
21
- (id: string) => props.onItemSelected?.(id),
24
+ { items: latest.current.items ?? [] },
25
+ (id: string) => latest.current.onItemSelected?.(id),
22
26
  )
23
27
  }, [props.items])
24
28
 
@@ -27,6 +31,7 @@ export function AppKitContextMenu(props: AppKitContextMenuProps) @{
27
31
  id={props.id}
28
32
  className={props.className}
29
33
  style={props.style}
34
+ accessibilityLabel={props.accessibilityLabel}
30
35
  >
31
36
  {props.trigger()}
32
37
  </flexboxlayout>;
@@ -1,5 +1,5 @@
1
1
  /** @jsxImportSource octane */
2
- import { useState } from 'octane'
2
+ import { Fragment, useLayoutEffect, useRef, useState } from 'octane'
3
3
  import type { ContextMenuProps } from './web/types'
4
4
 
5
5
  /** Web context menu: the trigger subtree renders inline; right-click (or
@@ -7,12 +7,67 @@ import type { ContextMenuProps } from './web/types'
7
7
  * in-page menu at the pointer. */
8
8
  export function ContextMenu(props: ContextMenuProps) @{
9
9
  const [menu, setMenu] = useState<{ x: number; y: number } | null>(null)
10
+ const [active, setActive] = useState(0)
11
+ const listRef = useRef<HTMLUListElement | null>(null)
10
12
  const items = props.items ?? []
13
+ const enabled = items
14
+ .map((item, index) => (item.disabled ? -1 : index))
15
+ .filter((index) => index >= 0)
16
+
11
17
  const openAt = (e: any) => {
12
18
  e.preventDefault?.()
19
+ setActive(enabled[0] ?? 0)
13
20
  setMenu({ x: e.clientX ?? 0, y: e.clientY ?? 0 })
14
21
  }
15
22
 
23
+ const choose = (index: number) => {
24
+ const item = items[index]
25
+ if (!item || item.disabled) return
26
+ setMenu(null)
27
+ props.onItemSelected?.(item.id)
28
+ }
29
+
30
+ // Once the menu is mounted: clamp it into the viewport so every item
31
+ // stays reachable, then focus it so the keyboard contract works.
32
+ useLayoutEffect(() => {
33
+ const el = listRef.current
34
+ if (!menu || !el) return
35
+ const x = Math.max(0, Math.min(menu.x, window.innerWidth - el.offsetWidth - 8))
36
+ const y = Math.max(0, Math.min(menu.y, window.innerHeight - el.offsetHeight - 8))
37
+ if (x !== menu.x || y !== menu.y) {
38
+ setMenu({ x, y })
39
+ return
40
+ }
41
+
42
+ el.focus()
43
+ }, [menu])
44
+
45
+ const onMenuKeyDown = (e: any) => {
46
+ if (e.key === 'Escape') {
47
+ e.preventDefault?.()
48
+ e.stopPropagation?.()
49
+ setMenu(null)
50
+ return
51
+ }
52
+
53
+ if (!enabled.length) return
54
+ const at = Math.max(0, enabled.indexOf(active))
55
+ if (e.key === 'ArrowDown' || e.key === 'ArrowUp') {
56
+ e.preventDefault?.()
57
+ const step = e.key === 'ArrowDown' ? 1 : -1
58
+ setActive(enabled[(at + step + enabled.length) % enabled.length])
59
+ } else if (e.key === 'Home') {
60
+ e.preventDefault?.()
61
+ setActive(enabled[0])
62
+ } else if (e.key === 'End') {
63
+ e.preventDefault?.()
64
+ setActive(enabled[enabled.length - 1])
65
+ } else if (e.key === 'Enter' || e.key === ' ') {
66
+ e.preventDefault?.()
67
+ choose(active)
68
+ }
69
+ }
70
+
16
71
  <span
17
72
  id={props.id}
18
73
  className={props.className}
@@ -24,16 +79,32 @@ export function ContextMenu(props: ContextMenuProps) @{
24
79
  {props.trigger()}
25
80
  {menu && (
26
81
  <>
82
+ {/* Events from the backdrop and the menu must not bubble to the
83
+ opener handlers — dismissal stays final. */}
27
84
  <span
28
85
  style={{ position: 'fixed', inset: 0, zIndex: 999 }}
29
- onClick={() => setMenu(null)}
86
+ onClick={(e: any) => {
87
+ e.stopPropagation?.()
88
+ setMenu(null)
89
+ }}
30
90
  onContextMenu={(e: any) => {
31
91
  e.preventDefault?.()
92
+ e.stopPropagation?.()
32
93
  setMenu(null)
33
94
  }}
34
95
  />
35
96
  <ul
36
97
  role="menu"
98
+ tabIndex={-1}
99
+ ref={(el: any) => {
100
+ listRef.current = el
101
+ }}
102
+ onClick={(e: any) => e.stopPropagation?.()}
103
+ onContextMenu={(e: any) => {
104
+ e.preventDefault?.()
105
+ e.stopPropagation?.()
106
+ }}
107
+ onKeyDown={onMenuKeyDown}
37
108
  style={{
38
109
  position: 'fixed',
39
110
  left: menu.x,
@@ -48,10 +119,11 @@ export function ContextMenu(props: ContextMenuProps) @{
48
119
  borderRadius: 8,
49
120
  boxShadow: '0 6px 24px rgb(0 0 0 / 0.18)',
50
121
  minWidth: 160,
122
+ outline: 'none',
51
123
  }}
52
124
  >
53
- {items.map((item) => (
54
- <>
125
+ {items.map((item, index) => (
126
+ <Fragment key={item.id}>
55
127
  {item.divider && (
56
128
  <li
57
129
  aria-hidden="true"
@@ -61,21 +133,22 @@ export function ContextMenu(props: ContextMenuProps) @{
61
133
  <li
62
134
  role="menuitem"
63
135
  aria-disabled={item.disabled || undefined}
136
+ aria-current={index === active || undefined}
64
137
  style={{
65
138
  padding: '6px 12px',
66
139
  cursor: item.disabled ? 'default' : 'pointer',
67
140
  opacity: item.disabled ? 0.5 : 1,
68
141
  color: item.destructive ? 'crimson' : undefined,
142
+ background:
143
+ index === active
144
+ ? 'color-mix(in srgb, CanvasText 10%, transparent)'
145
+ : undefined,
69
146
  }}
70
- onClick={() => {
71
- if (item.disabled) return
72
- setMenu(null)
73
- props.onItemSelected?.(item.id)
74
- }}
147
+ onClick={() => choose(index)}
75
148
  >
76
149
  {item.title}
77
150
  </li>
78
- </>
151
+ </Fragment>
79
152
  ))}
80
153
  </ul>
81
154
  </>
@@ -3,9 +3,8 @@ import { registerElement } from '@nativescript-community/octane'
3
3
  import type { Attributes } from '@nativescript-community/octane/intrinsics'
4
4
  import { ComposeDataDriver, JetpackCompose, registerJetpackCompose } from '@nativescript/jetpack-compose'
5
5
  import { Application, GridLayout } from '@nativescript/core'
6
- import { createNativeScriptRoot } from '@nativescript-community/octane'
6
+ import { createPortal as createUniversalPortal, type UniversalRenderable } from 'octane/universal/native'
7
7
  import { useEffect, useRef, useState } from 'octane'
8
- import type { UniversalComponent } from 'octane/universal'
9
8
  import type { MaterialContextMenuProps } from './android/types'
10
9
 
11
10
  declare const com: any
@@ -29,17 +28,20 @@ function TriggerRoot(props: { render: () => any }) @{
29
28
  <>{props.render()}</>;
30
29
  }
31
30
 
31
+ function createPortal(children: unknown, target: unknown) {
32
+ return createUniversalPortal(children as UniversalRenderable, target)
33
+ }
34
+
32
35
  /** Renders a Material 3 DropdownMenu anchored to a NativeScript-hosted
33
- * trigger subtree. The trigger mounts into a detached octane root whose
34
- * android.view.View the provider embeds through AndroidView. */
36
+ * trigger subtree portaled from the declaring Octane tree. */
35
37
  export function MaterialContextMenu(props: MaterialContextMenuProps) @{
36
38
  const triggerId = useRef<string>('')
37
39
  if (!triggerId.current) {
38
40
  triggerId.current = `ctxmenu${++seq}`
39
41
  }
40
42
 
41
- const mounted = useRef<{ host: any; root: any } | null>(null)
42
- const [ready, setReady] = useState(false)
43
+ const mounted = useRef<{ host: any } | null>(null)
44
+ const [portalHost, setPortalHost] = useState<GridLayout | null>(null)
43
45
  const [expanded, setExpanded] = useState(false)
44
46
 
45
47
  useEffect(() => {
@@ -54,72 +56,71 @@ export function MaterialContextMenu(props: MaterialContextMenuProps) @{
54
56
 
55
57
  nativeHost._setupAsRootView(context)
56
58
  nativeHost.callLoaded()
57
- const root = createNativeScriptRoot(host)
58
- root.render(TriggerRoot as unknown as UniversalComponent, { render: props.trigger })
59
59
  com.octanexplat.contextmenu.XplatViewRegistry.register(triggerId.current, host.android)
60
- mounted.current = { host, root }
61
- // Activation is an NS gesture on the trigger host — the hosted
62
- // subtree consumes pointer input before Compose's gesture detectors
63
- // could resolve it, so expanded is pushed through data instead.
64
- const open = () => setExpanded(true)
65
-
66
- if ((props.activation ?? 'longPress') === 'singlePress') {
67
- host.on('tap', open)
68
- } else {
69
- host.on('longPress', open)
70
- }
60
+ mounted.current = { host }
71
61
 
72
62
  // Gate triggerId on registration — the provider only composes the
73
63
  // AndroidView host once triggerId is non-empty, so the factory must
74
64
  // find the view in the registry on first read.
75
- setReady(true)
65
+ setPortalHost(host)
76
66
 
77
67
  return () => {
78
68
  com.octanexplat.contextmenu.XplatViewRegistry.unregister(triggerId.current)
79
- nativeHost.off()
80
- root.unmount?.()
81
69
  mounted.current = null
82
- setReady(false)
70
+ nativeHost.callUnloaded?.()
71
+ nativeHost.removeChildren?.()
83
72
  }
84
73
  }, [])
85
74
 
75
+ // Activation is an NS gesture on the trigger host — the hosted subtree
76
+ // consumes pointer input before Compose's gesture detectors could resolve
77
+ // it, so expanded is pushed through data instead. Rebind when the
78
+ // activation prop or the mounted host changes.
86
79
  useEffect(() => {
87
- mounted.current?.root?.render(TriggerRoot as unknown as UniversalComponent, {
88
- render: props.trigger,
89
- })
90
- }, [props.trigger])
80
+ const host = mounted.current?.host
81
+ if (!portalHost || !host) return
82
+ const open = () => setExpanded(true)
83
+ const event = (props.activation ?? 'longPress') === 'singlePress' ? 'tap' : 'longPress'
84
+ host.on(event, open)
85
+ return () => host.off(event, open)
86
+ }, [portalHost, props.activation])
91
87
 
92
88
  const items = props.items ?? []
93
89
 
94
- <xplatandroidcontextmenu
95
- id={props.id}
96
- className={props.className}
97
- style={props.style}
98
- accessibilityLabel={props.accessibilityLabel}
99
- composeId="xplat-material-contextmenu"
100
- data={{
101
- triggerId: ready ? triggerId.current : '',
102
- expanded: expanded ? 1 : 0,
103
- items: items.map((item) => ({
104
- id: item.id,
105
- title: item.title,
106
- // The bridge's dataSerialize throws on booleans; send numbers.
107
- destructive: item.destructive ? 1 : 0,
108
- disabled: item.disabled ? 1 : 0,
109
- divider: item.divider ? 1 : 0,
110
- })),
111
- }}
112
- onComposeEvent={(event: any) => {
113
- if (event.data?.dismissed) {
114
- setExpanded(false)
115
- return
116
- }
117
-
118
- const item = event.data?.item
119
- if (typeof item === 'string' && item) {
120
- setExpanded(false)
121
- props.onItemSelected?.(item)
122
- }
123
- }}
124
- />;
90
+ <>
91
+ <xplatandroidcontextmenu
92
+ id={props.id}
93
+ className={props.className}
94
+ style={props.style}
95
+ accessibilityLabel={props.accessibilityLabel}
96
+ composeId="xplat-material-contextmenu"
97
+ data={{
98
+ triggerId: portalHost ? triggerId.current : '',
99
+ expanded: expanded ? 1 : 0,
100
+ items: items.map((item) => ({
101
+ id: item.id,
102
+ title: item.title,
103
+ // The bridge's dataSerialize throws on booleans; send numbers.
104
+ destructive: item.destructive ? 1 : 0,
105
+ disabled: item.disabled ? 1 : 0,
106
+ divider: item.divider ? 1 : 0,
107
+ })),
108
+ }}
109
+ onComposeEvent={(event: any) => {
110
+ if (event.data?.dismissed) {
111
+ setExpanded(false)
112
+ return
113
+ }
114
+
115
+ const item = event.data?.item
116
+ if (typeof item === 'string' && item) {
117
+ setExpanded(false)
118
+ props.onItemSelected?.(item)
119
+ }
120
+ }}
121
+ />
122
+ {portalHost && props.trigger
123
+ ? createPortal(<TriggerRoot render={props.trigger} />, portalHost)
124
+ : null}
125
+ </>;
125
126
  }
@@ -3,12 +3,13 @@ import { registerElement } from '@nativescript-community/octane'
3
3
  import type { Attributes } from '@nativescript-community/octane/intrinsics'
4
4
  import { SwiftUI, UIDataDriver, registerSwiftUI, SwiftUIManager } from '@nativescript/swift-ui'
5
5
  import { GridLayout } from '@nativescript/core'
6
- import { createNativeScriptRoot } from '@nativescript-community/octane'
6
+ import { getSwiftUIHostLifecycle, registerSwiftUIHost, unregisterSwiftUIHost } from '@octane-xplat/lib'
7
+ import { createPortal as createUniversalPortal, type UniversalRenderable } from 'octane/universal/native'
7
8
  import { useEffect, useRef, useState } from 'octane'
8
- import type { UniversalComponent, UniversalRoot } from 'octane/universal'
9
9
  import type { SwiftUIContextMenuProps } from './ios/types'
10
10
 
11
11
  declare const XplatContextMenuProvider: any
12
+ declare const NativeScriptViewFactory: any
12
13
 
13
14
  declare module '@nativescript-community/octane/intrinsics' {
14
15
  interface NativeScriptElements {
@@ -21,42 +22,29 @@ registerSwiftUI('xplat-swiftui-contextmenu', (owner: any) =>
21
22
  new UIDataDriver(new XplatContextMenuProvider(), owner),
22
23
  )
23
24
 
24
- // Host views keyed by dash-free registry id — the provider's representable
25
- // appends "-<uuid>" and the factory looks the host up by the first segment.
26
- const hosts = new Map<string, GridLayout>()
27
- const roots = new Map<string, UniversalRoot>()
28
-
29
- function mountSubtree(id: string, render: () => any): void {
30
- let host = hosts.get(id)
31
- if (!host) {
32
- host = new GridLayout()
33
- hosts.set(id, host)
34
- // Register the id the first time the host exists — the factory's
35
- // lifecycle resolves it back out of this map.
36
- SwiftUIManager.registerNativeScriptViews(
37
- { [id]: true },
38
- {
39
- create: (fullId: string) => hosts.get(fullId.split('-')[0]) as any,
40
- destroy: (fullId: string) => {
41
- hosts.delete(fullId.split('-')[0])
42
- },
43
- } as any,
44
- )
45
- }
25
+ function createPortal(children: unknown, target: unknown) {
26
+ return createUniversalPortal(children as UniversalRenderable, target)
27
+ }
46
28
 
47
- let root = roots.get(id)
48
- if (!root) {
49
- root = createNativeScriptRoot(host)
50
- roots.set(id, root)
29
+ function ensureNativeScriptViewFactory(): void {
30
+ if (!NativeScriptViewFactory.shared) {
31
+ NativeScriptViewFactory.initShared()
51
32
  }
52
-
53
- root.render(TriggerRoot as unknown as UniversalComponent, { render })
54
33
  }
55
34
 
56
- function unmountSubtree(id: string): void {
57
- roots.get(id)?.unmount?.()
58
- roots.delete(id)
59
- hosts.delete(id)
35
+ function registerHosts(
36
+ triggerId: string,
37
+ trigger: GridLayout,
38
+ previewId: string,
39
+ preview: GridLayout,
40
+ ): void {
41
+ ensureNativeScriptViewFactory()
42
+ registerSwiftUIHost(triggerId, { create: () => trigger })
43
+ registerSwiftUIHost(previewId, { create: () => preview })
44
+ SwiftUIManager.registerNativeScriptViews(
45
+ { [triggerId]: true, [previewId]: true },
46
+ getSwiftUIHostLifecycle(),
47
+ )
60
48
  }
61
49
 
62
50
  function TriggerRoot(props: { render: () => any }) @{
@@ -65,63 +53,65 @@ function TriggerRoot(props: { render: () => any }) @{
65
53
 
66
54
  let seq = 0
67
55
 
68
- /** Renders a SwiftUI .contextMenu whose activation element is a detached
69
- * octane subtree embedded through the plugin's NativeScriptView-by-id
70
- * factory. Menu items arrive as serialized data. */
56
+ /** Renders a SwiftUI .contextMenu whose trigger and optional preview portal
57
+ * from the declaring Octane tree into NativeScript hosts. Menu items arrive
58
+ * as serialized data. */
71
59
  export function SwiftUIContextMenu(props: SwiftUIContextMenuProps) @{
72
60
  const triggerId = useRef<string>('')
73
61
  if (!triggerId.current) {
74
62
  triggerId.current = `ctxmenu${++seq}`
75
63
  }
76
64
 
77
- const [ready, setReady] = useState(false)
65
+ const previewId = `${triggerId.current}preview`
66
+ const [portalHosts, setPortalHosts] = useState<{
67
+ trigger: GridLayout
68
+ preview: GridLayout
69
+ } | null>(null)
78
70
 
79
71
  useEffect(() => {
80
- mountSubtree(triggerId.current, props.trigger)
81
- // Gate triggerId on registration — the representable resolves the id
82
- // through NativeScriptViewFactory on first composition.
83
- setReady(true)
72
+ const trigger = new GridLayout()
73
+ const preview = new GridLayout()
74
+ registerHosts(triggerId.current, trigger, previewId, preview)
75
+ setPortalHosts({ trigger, preview })
84
76
 
85
77
  return () => {
86
- unmountSubtree(triggerId.current)
87
- setReady(false)
78
+ unregisterSwiftUIHost(triggerId.current)
79
+ unregisterSwiftUIHost(previewId)
88
80
  }
89
81
  }, [])
90
82
 
91
- useEffect(() => {
92
- mountSubtree(triggerId.current, props.trigger)
93
- }, [props.trigger])
94
-
95
- useEffect(() => {
96
- if (props.preview) {
97
- mountSubtree(`${triggerId.current}preview`, props.preview)
98
- }
99
- }, [props.preview])
100
-
101
83
  const items = props.items ?? []
102
84
 
103
- <xplatioscontextmenu
104
- id={props.id}
105
- className={props.className}
106
- style={props.style}
107
- accessibilityLabel={props.accessibilityLabel}
108
- swiftId="xplat-swiftui-contextmenu"
109
- data={{
110
- triggerId: ready ? triggerId.current : '',
111
- previewId: ready && props.preview ? `${triggerId.current}preview` : '',
112
- items: items.map((item) => ({
113
- id: item.id,
114
- title: item.title,
115
- destructive: item.destructive ? 1 : 0,
116
- disabled: item.disabled ? 1 : 0,
117
- divider: item.divider ? 1 : 0,
118
- })),
119
- }}
120
- onSwiftUIEvent={(event: any) => {
121
- const item = event.data?.item
122
- if (typeof item === 'string' && item) {
123
- props.onItemSelected?.(item)
124
- }
125
- }}
126
- />;
85
+ <>
86
+ <xplatioscontextmenu
87
+ id={props.id}
88
+ className={props.className}
89
+ style={props.style}
90
+ accessibilityLabel={props.accessibilityLabel}
91
+ swiftId="xplat-swiftui-contextmenu"
92
+ data={{
93
+ triggerId: portalHosts ? triggerId.current : '',
94
+ previewId: portalHosts && props.preview ? previewId : '',
95
+ items: items.map((item) => ({
96
+ id: item.id,
97
+ title: item.title,
98
+ destructive: item.destructive ? 1 : 0,
99
+ disabled: item.disabled ? 1 : 0,
100
+ divider: item.divider ? 1 : 0,
101
+ })),
102
+ }}
103
+ onSwiftUIEvent={(event: any) => {
104
+ const item = event.data?.item
105
+ if (typeof item === 'string' && item) {
106
+ props.onItemSelected?.(item)
107
+ }
108
+ }}
109
+ />
110
+ {portalHosts && props.trigger
111
+ ? createPortal(<TriggerRoot render={props.trigger} />, portalHosts.trigger)
112
+ : null}
113
+ {portalHosts && props.preview
114
+ ? createPortal(<TriggerRoot render={props.preview} />, portalHosts.preview)
115
+ : null}
116
+ </>;
127
117
  }
@@ -18,8 +18,8 @@ export interface MaterialContextMenuProps {
18
18
  /** Activation gesture. @default 'longPress' */
19
19
  activation?: MaterialContextMenuActivation
20
20
  items?: readonly MaterialContextMenuItem[]
21
- /** Render fn producing the trigger subtree — hosted inside the menu's
22
- * anchor via AndroidView. */
21
+ /** Render fn producing the trigger subtree — portaled from the declaring
22
+ * Octane tree into the menu's AndroidView anchor. */
23
23
  trigger: () => any
24
24
  onItemSelected?: (id: string) => void
25
25
  accessibilityLabel?: string
@@ -1,103 +0,0 @@
1
- import { createNativeScriptRoot, defineUniversalComponent, hookSlots, registerElement, universalPlan, universalValue, useEffect, useRef, useState } from "@nativescript-community/octane";
2
- import { Application, GridLayout } from "@nativescript/core";
3
- import { ComposeDataDriver, JetpackCompose, registerJetpackCompose } from "@nativescript/jetpack-compose";
4
- //#region src/MaterialContextMenu.android.tsrx
5
- var _hs$ = /* @__PURE__ */ hookSlots(6);
6
- var _h$0 = /* @__PURE__ */ Symbol(_hs$);
7
- var _h$1 = /* @__PURE__ */ Symbol(_hs$ + 1);
8
- var _h$2 = /* @__PURE__ */ Symbol(_hs$ + 2);
9
- var _h$3 = /* @__PURE__ */ Symbol(_hs$ + 3);
10
- var _h$4 = /* @__PURE__ */ Symbol(_hs$ + 4);
11
- var _h$5 = /* @__PURE__ */ Symbol(_hs$ + 5);
12
- var __octaneUniversalPlan0 = universalPlan("nativescript", {
13
- "kind": "range",
14
- "children": [{
15
- "kind": "slot",
16
- "slot": 0
17
- }]
18
- });
19
- var __octaneUniversalPlan1 = universalPlan("nativescript", {
20
- "kind": "host",
21
- "type": "xplatandroidcontextmenu",
22
- "props": { "composeId": "xplat-material-contextmenu" },
23
- "bindings": [
24
- ["id", 0],
25
- ["className", 1],
26
- ["style", 2],
27
- ["accessibilityLabel", 3],
28
- ["data", 4],
29
- ["onComposeEvent", 5]
30
- ]
31
- });
32
- registerElement("xplatandroidcontextmenu", JetpackCompose);
33
- registerJetpackCompose("xplat-material-contextmenu", (owner) => new ComposeDataDriver(new com.octanexplat.contextmenu.XplatContextMenuProvider(), owner));
34
- var seq = 0;
35
- var TriggerRoot = defineUniversalComponent("nativescript", function TriggerRoot(props) {
36
- return universalValue(__octaneUniversalPlan0, [props.render()]);
37
- }, { "module": "@nativescript-community/octane" });
38
- var MaterialContextMenu = defineUniversalComponent("nativescript", function MaterialContextMenu(props) {
39
- const triggerId = useRef("", _h$0);
40
- if (!triggerId.current) triggerId.current = `ctxmenu${++seq}`;
41
- const mounted = useRef(null, _h$1);
42
- const [ready, setReady] = useState(false, _h$2);
43
- const [expanded, setExpanded] = useState(false, _h$3);
44
- useEffect(() => {
45
- const host = new GridLayout();
46
- const context = Application.android.foregroundActivity ?? Application.android.startActivity;
47
- const nativeHost = host;
48
- nativeHost._setupAsRootView(context);
49
- nativeHost.callLoaded();
50
- const root = createNativeScriptRoot(host);
51
- root.render(TriggerRoot, { render: props.trigger });
52
- com.octanexplat.contextmenu.XplatViewRegistry.register(triggerId.current, host.android);
53
- mounted.current = {
54
- host,
55
- root
56
- };
57
- const open = () => setExpanded(true);
58
- if ((props.activation ?? "longPress") === "singlePress") host.on("tap", open);
59
- else host.on("longPress", open);
60
- setReady(true);
61
- return () => {
62
- com.octanexplat.contextmenu.XplatViewRegistry.unregister(triggerId.current);
63
- nativeHost.off();
64
- root.unmount?.();
65
- mounted.current = null;
66
- setReady(false);
67
- };
68
- }, [], _h$4);
69
- useEffect(() => {
70
- mounted.current?.root?.render(TriggerRoot, { render: props.trigger });
71
- }, [props.trigger], _h$5);
72
- const items = props.items ?? [];
73
- return universalValue(__octaneUniversalPlan1, [
74
- props.id,
75
- props.className,
76
- props.style,
77
- props.accessibilityLabel,
78
- {
79
- triggerId: ready ? triggerId.current : "",
80
- expanded: expanded ? 1 : 0,
81
- items: items.map((item) => ({
82
- id: item.id,
83
- title: item.title,
84
- destructive: item.destructive ? 1 : 0,
85
- disabled: item.disabled ? 1 : 0,
86
- divider: item.divider ? 1 : 0
87
- }))
88
- },
89
- (event) => {
90
- if (event.data?.dismissed) {
91
- setExpanded(false);
92
- return;
93
- }
94
- const item = event.data?.item;
95
- if (typeof item === "string" && item) {
96
- setExpanded(false);
97
- props.onItemSelected?.(item);
98
- }
99
- }
100
- ]);
101
- }, { "module": "@nativescript-community/octane" });
102
- //#endregion
103
- export { MaterialContextMenu };
@@ -1,105 +0,0 @@
1
- import { createNativeScriptRoot, defineUniversalComponent, hookSlots, registerElement, universalPlan, universalValue, useEffect, useRef, useState } from "@nativescript-community/octane";
2
- import { SwiftUI, SwiftUIManager, UIDataDriver, registerSwiftUI } from "@nativescript/swift-ui";
3
- import { GridLayout } from "@nativescript/core";
4
- //#region src/SwiftUIContextMenu.ios.tsrx
5
- var _hs$ = /* @__PURE__ */ hookSlots(5);
6
- var _h$0 = /* @__PURE__ */ Symbol(_hs$);
7
- var _h$1 = /* @__PURE__ */ Symbol(_hs$ + 1);
8
- var _h$2 = /* @__PURE__ */ Symbol(_hs$ + 2);
9
- var _h$3 = /* @__PURE__ */ Symbol(_hs$ + 3);
10
- var _h$4 = /* @__PURE__ */ Symbol(_hs$ + 4);
11
- var __octaneUniversalPlan0 = universalPlan("nativescript", {
12
- "kind": "range",
13
- "children": [{
14
- "kind": "slot",
15
- "slot": 0
16
- }]
17
- });
18
- var __octaneUniversalPlan1 = universalPlan("nativescript", {
19
- "kind": "host",
20
- "type": "xplatioscontextmenu",
21
- "props": { "swiftId": "xplat-swiftui-contextmenu" },
22
- "bindings": [
23
- ["id", 0],
24
- ["className", 1],
25
- ["style", 2],
26
- ["accessibilityLabel", 3],
27
- ["data", 4],
28
- ["onSwiftUIEvent", 5]
29
- ]
30
- });
31
- registerElement("xplatioscontextmenu", SwiftUI);
32
- registerSwiftUI("xplat-swiftui-contextmenu", (owner) => new UIDataDriver(new XplatContextMenuProvider(), owner));
33
- var hosts = /* @__PURE__ */ new Map();
34
- var roots = /* @__PURE__ */ new Map();
35
- function mountSubtree(id, render) {
36
- let host = hosts.get(id);
37
- if (!host) {
38
- host = new GridLayout();
39
- hosts.set(id, host);
40
- SwiftUIManager.registerNativeScriptViews({ [id]: true }, {
41
- create: (fullId) => hosts.get(fullId.split("-")[0]),
42
- destroy: (fullId) => {
43
- hosts.delete(fullId.split("-")[0]);
44
- }
45
- });
46
- }
47
- let root = roots.get(id);
48
- if (!root) {
49
- root = createNativeScriptRoot(host);
50
- roots.set(id, root);
51
- }
52
- root.render(TriggerRoot, { render });
53
- }
54
- function unmountSubtree(id) {
55
- roots.get(id)?.unmount?.();
56
- roots.delete(id);
57
- hosts.delete(id);
58
- }
59
- var TriggerRoot = defineUniversalComponent("nativescript", function TriggerRoot(props) {
60
- return universalValue(__octaneUniversalPlan0, [props.render()]);
61
- }, { "module": "@nativescript-community/octane" });
62
- var seq = 0;
63
- var SwiftUIContextMenu = defineUniversalComponent("nativescript", function SwiftUIContextMenu(props) {
64
- const triggerId = useRef("", _h$0);
65
- if (!triggerId.current) triggerId.current = `ctxmenu${++seq}`;
66
- const [ready, setReady] = useState(false, _h$1);
67
- useEffect(() => {
68
- mountSubtree(triggerId.current, props.trigger);
69
- setReady(true);
70
- return () => {
71
- unmountSubtree(triggerId.current);
72
- setReady(false);
73
- };
74
- }, [], _h$2);
75
- useEffect(() => {
76
- mountSubtree(triggerId.current, props.trigger);
77
- }, [props.trigger], _h$3);
78
- useEffect(() => {
79
- if (props.preview) mountSubtree(`${triggerId.current}preview`, props.preview);
80
- }, [props.preview], _h$4);
81
- const items = props.items ?? [];
82
- return universalValue(__octaneUniversalPlan1, [
83
- props.id,
84
- props.className,
85
- props.style,
86
- props.accessibilityLabel,
87
- {
88
- triggerId: ready ? triggerId.current : "",
89
- previewId: ready && props.preview ? `${triggerId.current}preview` : "",
90
- items: items.map((item) => ({
91
- id: item.id,
92
- title: item.title,
93
- destructive: item.destructive ? 1 : 0,
94
- disabled: item.disabled ? 1 : 0,
95
- divider: item.divider ? 1 : 0
96
- }))
97
- },
98
- (event) => {
99
- const item = event.data?.item;
100
- if (typeof item === "string" && item) props.onItemSelected?.(item);
101
- }
102
- ]);
103
- }, { "module": "@nativescript-community/octane" });
104
- //#endregion
105
- export { SwiftUIContextMenu };
@@ -1,2 +0,0 @@
1
- import { MaterialContextMenu } from "../MaterialContextMenu.android.tsrx.js";
2
- export { MaterialContextMenu };
File without changes
@@ -1,2 +0,0 @@
1
- import { SwiftUIContextMenu } from "../SwiftUIContextMenu.ios.tsrx.js";
2
- export { SwiftUIContextMenu };
@@ -1,8 +0,0 @@
1
- {
2
- "version": 1,
3
- "buildId": "dc23e660-8325-4916-bab2-b32789bfd2dc",
4
- "mode": "production",
5
- "capabilities": {
6
- "independentHydration": false
7
- }
8
- }
@@ -1,101 +0,0 @@
1
- import { Fragment, __s, bag11, child, clone, createElement, createScopedElement, createScopedValue, delegateEvents, hookSlots, positionalChildren, setAriaAttribute, setEventHandler, setStyle, sibling, template, useState } from "octane";
2
- import { bindSignalAttribute, bindSignalChild, compilerMemoRegion, enableSignalBindings, hookMemoCreate, hookMemoPublishInvariant, setPlainAttribute } from "octane/internal/client";
3
- //#region src/ContextMenu.web.tsrx
4
- enableSignalBindings(1, true);
5
- delegateEvents(["click", "contextmenu"]);
6
- var _t$0 = /* @__PURE__ */ template("<span><!><!></span>");
7
- var _hs$ = /* @__PURE__ */ hookSlots(1);
8
- var ContextMenu = /* @__PURE__ */ __s(function ContextMenu(props, __s, __extra) {
9
- const __memoRegion = compilerMemoRegion(__s, _hs$ + 0);
10
- let __hk = __memoRegion.hooks;
11
- if (__hk === void 0) __memoRegion.hooks = __hk = hookMemoCreate(1);
12
- const [menu, setMenu] = useState(null, _hs$ + 0);
13
- const items = props.items ?? [];
14
- const openAt = __hk[0] ?? hookMemoPublishInvariant(__hk, 0, (e) => {
15
- e.preventDefault?.();
16
- setMenu({
17
- x: e.clientX ?? 0,
18
- y: e.clientY ?? 0
19
- });
20
- });
21
- let _b = __s.slots[0];
22
- if (_b === void 0) {
23
- let _m1, _m5, _m8;
24
- const _root = clone(_t$0);
25
- _m1 = _root;
26
- setEventHandler(_root, "$$contextmenu", props.activation === "singlePress" ? void 0 : openAt);
27
- setEventHandler(_root, "$$click", props.activation === "singlePress" ? openAt : void 0);
28
- const _el0 = child(_root);
29
- _m5 = _el0;
30
- _m8 = sibling(_el0, 1);
31
- _b = bag11(__s, _root, void 0, _m1, void 0, void 0, void 0, _m5, null, void 0, _m8, null, void 0);
32
- } else {
33
- setEventHandler(_b.b, "$$contextmenu", props.activation === "singlePress" ? void 0 : openAt);
34
- setEventHandler(_b.b, "$$click", props.activation === "singlePress" ? openAt : void 0);
35
- }
36
- _b.a = bindSignalAttribute(__s, _b.a, _b.b, "id", props.id, "b:d5b82f97", setPlainAttribute);
37
- _b.c = bindSignalAttribute(__s, _b.c, _b.b, "class", props.className, "b:722f753c", "class");
38
- {
39
- const _v = {
40
- display: "inline-block",
41
- ...props.style ?? {}
42
- };
43
- if (_b.d !== _v) {
44
- setStyle(_b.b, _v, _b.d);
45
- _b.d = _v;
46
- }
47
- }
48
- _b.e = bindSignalAttribute(__s, _b.e, _b.b, "aria-label", props.accessibilityLabel, "b:a27ad92a", setAriaAttribute);
49
- _b.l = bindSignalChild(__s, _b.l, 1, _b.b, props.trigger(), "b:6a5a30ce", _b.f, true, void 0, void 0, void 0);
50
- _b.m = bindSignalChild(__s, _b.m, 2, _b.b, menu && createScopedElement(Fragment, {}, () => positionalChildren([createScopedValue(() => createElement("span", {
51
- style: {
52
- position: "fixed",
53
- inset: 0,
54
- zIndex: 999
55
- },
56
- onClick: () => setMenu(null),
57
- onContextMenu: (e) => {
58
- e.preventDefault?.();
59
- setMenu(null);
60
- }
61
- })), createScopedValue(() => createScopedElement("ul", {
62
- role: "menu",
63
- style: {
64
- position: "fixed",
65
- left: menu.x,
66
- top: menu.y,
67
- zIndex: 1e3,
68
- margin: 0,
69
- padding: "4px 0",
70
- listStyle: "none",
71
- background: "var(--menu-bg, Canvas)",
72
- color: "var(--menu-fg, CanvasText)",
73
- border: "1px solid color-mix(in srgb, CanvasText 20%, transparent)",
74
- borderRadius: 8,
75
- boxShadow: "0 6px 24px rgb(0 0 0 / 0.18)",
76
- minWidth: 160
77
- }
78
- }, () => items.map((item) => createScopedElement(Fragment, {}, () => positionalChildren([item.divider && createScopedValue(() => createElement("li", {
79
- "aria-hidden": "true",
80
- style: {
81
- borderTop: "1px solid color-mix(in srgb, CanvasText 15%, transparent)",
82
- margin: "4px 0"
83
- }
84
- })), createScopedValue(() => createScopedElement("li", {
85
- role: "menuitem",
86
- "aria-disabled": item.disabled || void 0,
87
- style: {
88
- padding: "6px 12px",
89
- cursor: item.disabled ? "default" : "pointer",
90
- opacity: item.disabled ? .5 : 1,
91
- color: item.destructive ? "crimson" : void 0
92
- },
93
- onClick: () => {
94
- if (item.disabled) return;
95
- setMenu(null);
96
- props.onItemSelected?.(item.id);
97
- }
98
- }, () => item.title))])))))])), "b:fff1b948", _b.i, true, void 0, void 0, void 0);
99
- });
100
- //#endregion
101
- export { ContextMenu };
@@ -1,8 +0,0 @@
1
- {
2
- "version": 1,
3
- "buildId": "ff7e0eb0-e1ff-4ede-b798-2d9ec70fb4f9",
4
- "mode": "production",
5
- "capabilities": {
6
- "independentHydration": false
7
- }
8
- }
@@ -1,2 +0,0 @@
1
- import { ContextMenu } from "../ContextMenu.web.tsrx.js";
2
- export { ContextMenu };