@octane-xplat/context-menu 0.19.0 → 0.20.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.
@@ -2,15 +2,15 @@
2
2
  import { registerElement } from '@nativescript-community/octane'
3
3
  import type { Attributes } from '@octane-xplat/jsx/intrinsics'
4
4
  import {
5
- ComposeDataDriver,
6
- JetpackCompose,
7
- registerJetpackCompose,
5
+ ComposeDataDriver,
6
+ JetpackCompose,
7
+ registerJetpackCompose,
8
8
  } from '@nativescript/jetpack-compose'
9
9
 
10
10
  import { Application, GridLayout } from '@nativescript/core'
11
11
  import {
12
- createPortal as createUniversalPortal,
13
- type UniversalRenderable,
12
+ createPortal as createUniversalPortal,
13
+ type UniversalRenderable,
14
14
  } from 'octane/universal/native'
15
15
 
16
16
  import { useEffect, useRef, useState } from 'octane'
@@ -19,23 +19,23 @@ import type { MaterialContextMenuProps } from './android/types'
19
19
  declare const com: any
20
20
 
21
21
  declare module '@octane-xplat/jsx/intrinsics' {
22
- interface NativeElements<TElement> {
23
- xplatandroidcontextmenu: Attributes<
24
- TElement,
25
- {
26
- composeId?: string
27
- data?: Record<string, unknown>
28
- onComposeEvent?: (event: { data?: unknown }) => void
29
- }
30
- >
31
- }
22
+ interface NativeElements<TElement> {
23
+ xplatandroidcontextmenu: Attributes<
24
+ TElement,
25
+ {
26
+ composeId?: string
27
+ data?: Record<string, unknown>
28
+ onComposeEvent?: (event: { data?: unknown }) => void
29
+ }
30
+ >
31
+ }
32
32
  }
33
33
 
34
34
  registerElement('xplatandroidcontextmenu', JetpackCompose)
35
35
  registerJetpackCompose(
36
- 'xplat-material-contextmenu',
37
- (owner: any) =>
38
- new ComposeDataDriver(new com.octanexplat.contextmenu.XplatContextMenuProvider(), owner),
36
+ 'xplat-material-contextmenu',
37
+ (owner: any) =>
38
+ new ComposeDataDriver(new com.octanexplat.contextmenu.XplatContextMenuProvider(), owner),
39
39
  )
40
40
 
41
41
  // Dash-free ids — the host-side representable appends "-<uuid>" and the
@@ -43,107 +43,111 @@ registerJetpackCompose(
43
43
  let seq = 0
44
44
 
45
45
  function TriggerRoot(props: { render: () => any }) @{
46
- <>{props.render()}</>
46
+ <>{props.render()}</>
47
47
  }
48
48
 
49
49
  function createPortal(children: unknown, target: unknown) {
50
- return createUniversalPortal(children as UniversalRenderable, target)
50
+ return createUniversalPortal(children as UniversalRenderable, target)
51
51
  }
52
52
 
53
53
  /** Renders a Material 3 DropdownMenu anchored to a NativeScript-hosted
54
54
  * trigger subtree portaled from the declaring Octane tree. */
55
55
  export function MaterialContextMenu(props: MaterialContextMenuProps): unknown @{
56
- const triggerId = useRef<string>('')
57
- if (!triggerId.current) {
58
- triggerId.current = `ctxmenu${++seq}`
59
- }
60
-
61
- const mounted = useRef<{ host: any } | null>(null)
62
- const [portalHost, setPortalHost] = useState<GridLayout | null>(null)
63
- const [expanded, setExpanded] = useState(false)
64
-
65
- useEffect(() => {
66
- const host = new GridLayout()
67
- // Detached subtree — the plugin's generateNativeView shortcut passes
68
- // `{}` as the native context, which fails to marshal on Android; the
69
- // view factory needs a real android.content.Context to build its
70
- // native view.
71
-
72
- const context =
73
- (Application.android as any).foregroundActivity ?? (Application.android as any).startActivity
74
-
75
- const nativeHost = host as any
76
-
77
- nativeHost._setupAsRootView(context)
78
- nativeHost.callLoaded()
79
- com.octanexplat.contextmenu.XplatViewRegistry.register(triggerId.current, host.android)
80
- mounted.current = { host }
81
-
82
- // Gate triggerId on registration — the provider only composes the
83
- // AndroidView host once triggerId is non-empty, so the factory must
84
- // find the view in the registry on first read.
85
- setPortalHost(host)
86
-
87
- return () => {
88
- com.octanexplat.contextmenu.XplatViewRegistry.unregister(triggerId.current)
89
- mounted.current = null
90
- nativeHost.callUnloaded?.()
91
- nativeHost.removeChildren?.()
92
- }
93
- }, [])
94
-
95
- // Activation is an NS gesture on the trigger host — the hosted subtree
96
- // consumes pointer input before Compose's gesture detectors could resolve
97
- // it, so expanded is pushed through data instead. Rebind when the
98
- // activation prop or the mounted host changes.
99
- useEffect(() => {
100
- const host = mounted.current?.host
101
- if (!portalHost || !host) {
102
- return
103
- }
104
-
105
- const open = () => setExpanded(true)
106
- const event = (props.activation ?? 'longPress') === 'singlePress' ? 'tap' : 'longPress'
107
- host.on(event, open)
108
- return () => host.off(event, open)
109
- }, [portalHost, props.activation])
110
-
111
- const items = props.items ?? []
112
-
113
- <>
114
- <xplatandroidcontextmenu
115
- id={props.id}
116
- className={props.className}
117
- style={props.style}
118
- accessibilityLabel={props.accessibilityLabel}
119
- composeId="xplat-material-contextmenu"
120
- data={{
121
- triggerId: portalHost ? triggerId.current : '',
122
- expanded: expanded ? 1 : 0,
123
- items: items.map((item) => ({
124
- id: item.id,
125
- title: item.title,
126
- // The bridge's dataSerialize throws on booleans; send numbers.
127
- destructive: item.destructive ? 1 : 0,
128
- disabled: item.disabled ? 1 : 0,
129
- divider: item.divider ? 1 : 0,
130
- })),
131
- }}
132
- onComposeEvent={(event: any) => {
133
- if (event.data?.dismissed) {
134
- setExpanded(false)
135
- return
136
- }
137
-
138
- const item = event.data?.item
139
- if (typeof item === 'string' && item) {
140
- setExpanded(false)
141
- props.onItemSelected?.(item)
142
- }
143
- }}
144
- />
145
- {portalHost && props.trigger
146
- ? createPortal(<TriggerRoot render={props.trigger} />, portalHost)
147
- : null}
148
- </>
56
+ const triggerId = useRef<string>('')
57
+
58
+ if (!triggerId.current) {
59
+ triggerId.current = `ctxmenu${++seq}`
60
+ }
61
+
62
+ const mounted = useRef<{ host: any } | null>(null)
63
+ const [portalHost, setPortalHost] = useState<GridLayout | null>(null)
64
+ const [expanded, setExpanded] = useState(false)
65
+
66
+ useEffect(() => {
67
+ const host = new GridLayout()
68
+ // Detached subtree — the plugin's generateNativeView shortcut passes
69
+ // `{}` as the native context, which fails to marshal on Android; the
70
+ // view factory needs a real android.content.Context to build its
71
+ // native view.
72
+
73
+ const context =
74
+ (Application.android as any).foregroundActivity ?? (Application.android as any).startActivity
75
+
76
+ const nativeHost = host as any
77
+
78
+ nativeHost._setupAsRootView(context)
79
+ nativeHost.callLoaded()
80
+ com.octanexplat.contextmenu.XplatViewRegistry.register(triggerId.current, host.android)
81
+ mounted.current = { host }
82
+
83
+ // Gate triggerId on registration — the provider only composes the
84
+ // AndroidView host once triggerId is non-empty, so the factory must
85
+ // find the view in the registry on first read.
86
+ setPortalHost(host)
87
+
88
+ return () => {
89
+ com.octanexplat.contextmenu.XplatViewRegistry.unregister(triggerId.current)
90
+ mounted.current = null
91
+ nativeHost.callUnloaded?.()
92
+ nativeHost.removeChildren?.()
93
+ }
94
+ }, [])
95
+
96
+ // Activation is an NS gesture on the trigger host — the hosted subtree
97
+ // consumes pointer input before Compose's gesture detectors could resolve
98
+ // it, so expanded is pushed through data instead. Rebind when the
99
+ // activation prop or the mounted host changes.
100
+ useEffect(() => {
101
+ const host = mounted.current?.host
102
+
103
+ if (!portalHost || !host) {
104
+ return
105
+ }
106
+
107
+ const open = () => setExpanded(true)
108
+ const event = (props.activation ?? 'longPress') === 'singlePress' ? 'tap' : 'longPress'
109
+
110
+ host.on(event, open)
111
+ return () => host.off(event, open)
112
+ }, [portalHost, props.activation])
113
+
114
+ const items = props.items ?? []
115
+
116
+ <>
117
+ <xplatandroidcontextmenu
118
+ id={props.id}
119
+ className={props.className}
120
+ style={props.style}
121
+ accessibilityLabel={props.accessibilityLabel}
122
+ composeId="xplat-material-contextmenu"
123
+ data={{
124
+ triggerId: portalHost ? triggerId.current : '',
125
+ expanded: expanded ? 1 : 0,
126
+ items: items.map((item) => ({
127
+ id: item.id,
128
+ title: item.title,
129
+ // The bridge's dataSerialize throws on booleans; send numbers.
130
+ destructive: item.destructive ? 1 : 0,
131
+ disabled: item.disabled ? 1 : 0,
132
+ divider: item.divider ? 1 : 0,
133
+ })),
134
+ }}
135
+ onComposeEvent={(event: any) => {
136
+ if (event.data?.dismissed) {
137
+ setExpanded(false)
138
+ return
139
+ }
140
+
141
+ const item = event.data?.item
142
+
143
+ if (typeof item === 'string' && item) {
144
+ setExpanded(false)
145
+ props.onItemSelected?.(item)
146
+ }
147
+ }}
148
+ />
149
+ {portalHost && props.trigger
150
+ ? createPortal(<TriggerRoot render={props.trigger} />, portalHost)
151
+ : null}
152
+ </>
149
153
  }
@@ -0,0 +1,2 @@
1
+ // Type contract only. The target build must select a platform implementation.
2
+ export declare const MaterialContextMenu: typeof import('./MaterialContextMenu.android.tsrx').MaterialContextMenu
@@ -4,14 +4,14 @@ import type { Attributes } from '@octane-xplat/jsx/intrinsics'
4
4
  import { SwiftUI, UIDataDriver, registerSwiftUI, SwiftUIManager } from '@nativescript/swift-ui'
5
5
  import { GridLayout } from '@nativescript/core'
6
6
  import {
7
- getSwiftUIHostLifecycle,
8
- registerSwiftUIHost,
9
- unregisterSwiftUIHost,
7
+ getSwiftUIHostLifecycle,
8
+ registerSwiftUIHost,
9
+ unregisterSwiftUIHost,
10
10
  } from '@octane-xplat/lib'
11
11
 
12
12
  import {
13
- createPortal as createUniversalPortal,
14
- type UniversalRenderable,
13
+ createPortal as createUniversalPortal,
14
+ type UniversalRenderable,
15
15
  } from 'octane/universal/native'
16
16
 
17
17
  import { useEffect, useRef, useState } from 'octane'
@@ -21,51 +21,51 @@ declare const XplatContextMenuProvider: any
21
21
  declare const NativeScriptViewFactory: any
22
22
 
23
23
  declare module '@octane-xplat/jsx/intrinsics' {
24
- interface NativeElements<TElement> {
25
- xplatioscontextmenu: Attributes<
26
- TElement,
27
- {
28
- swiftId?: string
29
- data?: Record<string, unknown>
30
- onSwiftUIEvent?: (event: { data?: unknown }) => void
31
- }
32
- >
33
- }
24
+ interface NativeElements<TElement> {
25
+ xplatioscontextmenu: Attributes<
26
+ TElement,
27
+ {
28
+ swiftId?: string
29
+ data?: Record<string, unknown>
30
+ onSwiftUIEvent?: (event: { data?: unknown }) => void
31
+ }
32
+ >
33
+ }
34
34
  }
35
35
 
36
36
  registerElement('xplatioscontextmenu', SwiftUI)
37
37
  registerSwiftUI(
38
- 'xplat-swiftui-contextmenu',
39
- (owner: any) => new UIDataDriver(new XplatContextMenuProvider(), owner),
38
+ 'xplat-swiftui-contextmenu',
39
+ (owner: any) => new UIDataDriver(new XplatContextMenuProvider(), owner),
40
40
  )
41
41
 
42
42
  function createPortal(children: unknown, target: unknown) {
43
- return createUniversalPortal(children as UniversalRenderable, target)
43
+ return createUniversalPortal(children as UniversalRenderable, target)
44
44
  }
45
45
 
46
46
  function ensureNativeScriptViewFactory(): void {
47
- if (!NativeScriptViewFactory.shared) {
48
- NativeScriptViewFactory.initShared()
49
- }
47
+ if (!NativeScriptViewFactory.shared) {
48
+ NativeScriptViewFactory.initShared()
49
+ }
50
50
  }
51
51
 
52
52
  function registerHosts(
53
- triggerId: string,
54
- trigger: GridLayout,
55
- previewId: string,
56
- preview: GridLayout,
53
+ triggerId: string,
54
+ trigger: GridLayout,
55
+ previewId: string,
56
+ preview: GridLayout,
57
57
  ): void {
58
- ensureNativeScriptViewFactory()
59
- registerSwiftUIHost(triggerId, { create: () => trigger })
60
- registerSwiftUIHost(previewId, { create: () => preview })
61
- SwiftUIManager.registerNativeScriptViews(
62
- { [triggerId]: true, [previewId]: true },
63
- getSwiftUIHostLifecycle(),
64
- )
58
+ ensureNativeScriptViewFactory()
59
+ registerSwiftUIHost(triggerId, { create: () => trigger })
60
+ registerSwiftUIHost(previewId, { create: () => preview })
61
+ SwiftUIManager.registerNativeScriptViews(
62
+ { [triggerId]: true, [previewId]: true },
63
+ getSwiftUIHostLifecycle(),
64
+ )
65
65
  }
66
66
 
67
67
  function TriggerRoot(props: { render: () => any }) @{
68
- <>{props.render()}</>
68
+ <>{props.render()}</>
69
69
  }
70
70
 
71
71
  let seq = 0
@@ -74,61 +74,64 @@ let seq = 0
74
74
  * from the declaring Octane tree into NativeScript hosts. Menu items arrive
75
75
  * as serialized data. */
76
76
  export function SwiftUIContextMenu(props: SwiftUIContextMenuProps): unknown @{
77
- const triggerId = useRef<string>('')
78
- if (!triggerId.current) {
79
- triggerId.current = `ctxmenu${++seq}`
80
- }
81
-
82
- const previewId = `${triggerId.current}preview`
83
- const [portalHosts, setPortalHosts] = useState<{
84
- trigger: GridLayout
85
- preview: GridLayout
86
- } | null>(null)
87
-
88
- useEffect(() => {
89
- const trigger = new GridLayout()
90
- const preview = new GridLayout()
91
- registerHosts(triggerId.current, trigger, previewId, preview)
92
- setPortalHosts({ trigger, preview })
93
-
94
- return () => {
95
- unregisterSwiftUIHost(triggerId.current)
96
- unregisterSwiftUIHost(previewId)
97
- }
98
- }, [])
99
-
100
- const items = props.items ?? []
101
-
102
- <>
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: portalHosts ? triggerId.current : '',
111
- previewId: portalHosts && props.preview ? previewId : '',
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
- />
127
- {portalHosts && props.trigger
128
- ? createPortal(<TriggerRoot render={props.trigger} />, portalHosts.trigger)
129
- : null}
130
- {portalHosts && props.preview
131
- ? createPortal(<TriggerRoot render={props.preview} />, portalHosts.preview)
132
- : null}
133
- </>
77
+ const triggerId = useRef<string>('')
78
+
79
+ if (!triggerId.current) {
80
+ triggerId.current = `ctxmenu${++seq}`
81
+ }
82
+
83
+ const previewId = `${triggerId.current}preview`
84
+ const [portalHosts, setPortalHosts] = useState<{
85
+ trigger: GridLayout
86
+ preview: GridLayout
87
+ } | null>(null)
88
+
89
+ useEffect(() => {
90
+ const trigger = new GridLayout()
91
+ const preview = new GridLayout()
92
+
93
+ registerHosts(triggerId.current, trigger, previewId, preview)
94
+ setPortalHosts({ trigger, preview })
95
+
96
+ return () => {
97
+ unregisterSwiftUIHost(triggerId.current)
98
+ unregisterSwiftUIHost(previewId)
99
+ }
100
+ }, [])
101
+
102
+ const items = props.items ?? []
103
+
104
+ <>
105
+ <xplatioscontextmenu
106
+ id={props.id}
107
+ className={props.className}
108
+ style={props.style}
109
+ accessibilityLabel={props.accessibilityLabel}
110
+ swiftId="xplat-swiftui-contextmenu"
111
+ data={{
112
+ triggerId: portalHosts ? triggerId.current : '',
113
+ previewId: portalHosts && props.preview ? previewId : '',
114
+ items: items.map((item) => ({
115
+ id: item.id,
116
+ title: item.title,
117
+ destructive: item.destructive ? 1 : 0,
118
+ disabled: item.disabled ? 1 : 0,
119
+ divider: item.divider ? 1 : 0,
120
+ })),
121
+ }}
122
+ onSwiftUIEvent={(event: any) => {
123
+ const item = event.data?.item
124
+
125
+ if (typeof item === 'string' && item) {
126
+ props.onItemSelected?.(item)
127
+ }
128
+ }}
129
+ />
130
+ {portalHosts && props.trigger
131
+ ? createPortal(<TriggerRoot render={props.trigger} />, portalHosts.trigger)
132
+ : null}
133
+ {portalHosts && props.preview
134
+ ? createPortal(<TriggerRoot render={props.preview} />, portalHosts.preview)
135
+ : null}
136
+ </>
134
137
  }
@@ -0,0 +1,2 @@
1
+ // Type contract only. The target build must select a platform implementation.
2
+ export declare const SwiftUIContextMenu: typeof import('./SwiftUIContextMenu.ios.tsrx').SwiftUIContextMenu
@@ -1,6 +1,6 @@
1
1
  export { MaterialContextMenu } from '../MaterialContextMenu.android.tsrx'
2
2
  export type {
3
- MaterialContextMenuActivation,
4
- MaterialContextMenuItem,
5
- MaterialContextMenuProps,
3
+ MaterialContextMenuActivation,
4
+ MaterialContextMenuItem,
5
+ MaterialContextMenuProps,
6
6
  } from './types'
@@ -3,35 +3,35 @@ export type MaterialContextMenuActivation = 'longPress' | 'singlePress'
3
3
 
4
4
  /** One row of serialized menu content carried over the bridge. */
5
5
  export interface MaterialContextMenuItem {
6
- /** Stable id reported to `onItemSelected`. */
7
- id: string
8
- /** Item display text. */
9
- title: string
10
- /** Marks the action destructive (red styling where supported). */
11
- destructive?: boolean
12
- /** Greys the item; not selectable. */
13
- disabled?: boolean
14
- /** Render a divider before this item. */
15
- divider?: boolean
6
+ /** Stable id reported to `onItemSelected`. */
7
+ id: string
8
+ /** Item display text. */
9
+ title: string
10
+ /** Marks the action destructive (red styling where supported). */
11
+ destructive?: boolean
12
+ /** Greys the item; not selectable. */
13
+ disabled?: boolean
14
+ /** Render a divider before this item. */
15
+ divider?: boolean
16
16
  }
17
17
 
18
18
  /** Android context-menu props. */
19
19
  export interface MaterialContextMenuProps {
20
- /** Element id passed through to the host element. */
21
- id?: string
22
- /** Class list applied to the element. */
23
- className?: any
24
- /** Inline style (string CSS or object). */
25
- style?: any
26
- /** Activation gesture. @default 'longPress' */
27
- activation?: MaterialContextMenuActivation
28
- /** Menu entries in display order. */
29
- items?: readonly MaterialContextMenuItem[]
30
- /** Render fn producing the trigger subtree — portaled from the declaring
31
- * Octane tree into the menu's AndroidView anchor. */
32
- trigger: () => any
33
- /** Fires with the selected item id. */
34
- onItemSelected?: (id: string) => void
35
- /** Spoken name for assistive tech. */
36
- accessibilityLabel?: string
20
+ /** Element id passed through to the host element. */
21
+ id?: string
22
+ /** Class list applied to the element. */
23
+ className?: any
24
+ /** Inline style (string CSS or object). */
25
+ style?: any
26
+ /** Activation gesture. @default 'longPress' */
27
+ activation?: MaterialContextMenuActivation
28
+ /** Menu entries in display order. */
29
+ items?: readonly MaterialContextMenuItem[]
30
+ /** Render fn producing the trigger subtree — portaled from the declaring
31
+ * Octane tree into the menu's AndroidView anchor. */
32
+ trigger: () => any
33
+ /** Fires with the selected item id. */
34
+ onItemSelected?: (id: string) => void
35
+ /** Spoken name for assistive tech. */
36
+ accessibilityLabel?: string
37
37
  }