@octane-xplat/context-menu 0.0.0-nightly.g2e22171
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/LICENSE +21 -0
- package/README.md +127 -0
- package/package.json +87 -0
- package/platforms/android/include.gradle +15 -0
- package/platforms/android/java/com/octanexplat/contextmenu/XplatContextMenuProvider.kt +158 -0
- package/platforms/ios/src/XplatContextMenuProvider.swift +123 -0
- package/src/AppKitContextMenu.macos.tsrx +38 -0
- package/src/ContextMenu.web.tsrx +157 -0
- package/src/MaterialContextMenu.android.tsrx +126 -0
- package/src/SwiftUIContextMenu.ios.tsrx +117 -0
- package/src/android/index.ts +6 -0
- package/src/android/types.ts +26 -0
- package/src/index.ts +3 -0
- package/src/ios/index.ts +2 -0
- package/src/ios/types.ts +25 -0
- package/src/macos/index.ts +2 -0
- package/src/macos/types.ts +24 -0
- package/src/web/index.ts +2 -0
- package/src/web/types.ts +21 -0
- package/types/android/index.d.ts +22 -0
- package/types/index.d.ts +1 -0
- package/types/ios/index.d.ts +20 -0
- package/types/macos/index.d.ts +19 -0
- package/types/web/index.d.ts +22 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
The MIT License (MIT)
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 Alec Larson
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in
|
|
13
|
+
all copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
|
21
|
+
THE SOFTWARE.
|
package/README.md
ADDED
|
@@ -0,0 +1,127 @@
|
|
|
1
|
+
# `@octane-xplat/context-menu`
|
|
2
|
+
|
|
3
|
+
Platform-specific context menus for Octane xplat apps: a SwiftUI
|
|
4
|
+
`.contextMenu` on iOS, a Material 3 `DropdownMenu` anchored to a hosted
|
|
5
|
+
trigger on Android, an in-page right-click menu on web, and a real
|
|
6
|
+
`NSMenu` on the backing view on macOS.
|
|
7
|
+
|
|
8
|
+
There is no shared context-menu component. Install the package in the app
|
|
9
|
+
that renders a control, then import `SwiftUIContextMenu` from
|
|
10
|
+
`@octane-xplat/context-menu/ios`, `MaterialContextMenu` from
|
|
11
|
+
`@octane-xplat/context-menu/android`, `ContextMenu` from
|
|
12
|
+
`@octane-xplat/context-menu/web`, or `AppKitContextMenu` from
|
|
13
|
+
`@octane-xplat/context-menu/macos` in the matching platform-suffixed file.
|
|
14
|
+
|
|
15
|
+
```tsx
|
|
16
|
+
/** @jsxImportSource @nativescript-community/octane */
|
|
17
|
+
// SwiftUIContextMenuExample.ios.tsx
|
|
18
|
+
import { SwiftUIContextMenu } from '@octane-xplat/context-menu/ios'
|
|
19
|
+
import { Text } from '@octane-xplat/ui'
|
|
20
|
+
|
|
21
|
+
export function Menu() {
|
|
22
|
+
return (
|
|
23
|
+
<SwiftUIContextMenu
|
|
24
|
+
trigger={() => <Text>Trip actions</Text>}
|
|
25
|
+
items={[{ id: 'rename', title: 'Rename' }]}
|
|
26
|
+
onItemSelected={(id) => console.log(id)}
|
|
27
|
+
/>
|
|
28
|
+
)
|
|
29
|
+
}
|
|
30
|
+
```
|
|
31
|
+
|
|
32
|
+
```tsx
|
|
33
|
+
/** @jsxImportSource @nativescript-community/octane */
|
|
34
|
+
// MaterialContextMenuExample.android.tsx
|
|
35
|
+
import { MaterialContextMenu } from '@octane-xplat/context-menu/android'
|
|
36
|
+
import { Text } from '@octane-xplat/ui'
|
|
37
|
+
|
|
38
|
+
export function Menu() {
|
|
39
|
+
return (
|
|
40
|
+
<MaterialContextMenu
|
|
41
|
+
activation="singlePress"
|
|
42
|
+
trigger={() => <Text>Trip actions</Text>}
|
|
43
|
+
items={[{ id: 'rename', title: 'Rename' }]}
|
|
44
|
+
onItemSelected={(id) => console.log(id)}
|
|
45
|
+
/>
|
|
46
|
+
)
|
|
47
|
+
}
|
|
48
|
+
```
|
|
49
|
+
|
|
50
|
+
```tsx
|
|
51
|
+
// ContextMenuExample.web.tsx
|
|
52
|
+
import { ContextMenu } from '@octane-xplat/context-menu/web'
|
|
53
|
+
import { Text } from '@octane-xplat/ui'
|
|
54
|
+
|
|
55
|
+
export function Menu() {
|
|
56
|
+
return (
|
|
57
|
+
<ContextMenu
|
|
58
|
+
activation="singlePress"
|
|
59
|
+
trigger={() => <Text>Trip actions</Text>}
|
|
60
|
+
items={[{ id: 'rename', title: 'Rename' }]}
|
|
61
|
+
onItemSelected={(id) => console.log(id)}
|
|
62
|
+
/>
|
|
63
|
+
)
|
|
64
|
+
}
|
|
65
|
+
```
|
|
66
|
+
|
|
67
|
+
```tsx
|
|
68
|
+
// AppKitContextMenuExample.macos.tsx
|
|
69
|
+
import { AppKitContextMenu } from '@octane-xplat/context-menu/macos'
|
|
70
|
+
import { Text } from '@octane-xplat/ui'
|
|
71
|
+
|
|
72
|
+
export function Menu() {
|
|
73
|
+
return (
|
|
74
|
+
<AppKitContextMenu
|
|
75
|
+
trigger={() => <Text>Trip actions</Text>}
|
|
76
|
+
items={[{ id: 'rename', title: 'Rename' }]}
|
|
77
|
+
onItemSelected={(id) => console.log(id)}
|
|
78
|
+
/>
|
|
79
|
+
)
|
|
80
|
+
}
|
|
81
|
+
```
|
|
82
|
+
|
|
83
|
+
Menu items are plain data (`{id, title, destructive, disabled, divider}`),
|
|
84
|
+
reported back through `onItemSelected(id)`. The iOS entry also accepts an
|
|
85
|
+
optional `preview` render function (iOS 16+):
|
|
86
|
+
|
|
87
|
+
```tsx
|
|
88
|
+
/** @jsxImportSource @nativescript-community/octane */
|
|
89
|
+
// Actions.ios.tsx
|
|
90
|
+
import { SwiftUIContextMenu } from '@octane-xplat/context-menu/ios'
|
|
91
|
+
import { Text } from '@octane-xplat/ui'
|
|
92
|
+
|
|
93
|
+
export function Actions() {
|
|
94
|
+
return (
|
|
95
|
+
<SwiftUIContextMenu
|
|
96
|
+
trigger={() => <Text>Trip actions</Text>}
|
|
97
|
+
preview={() => <Text>Kyoto trip</Text>}
|
|
98
|
+
items={[{ id: 'delete', title: 'Delete', destructive: true, divider: true }]}
|
|
99
|
+
onItemSelected={(id) => console.log(id)}
|
|
100
|
+
/>
|
|
101
|
+
)
|
|
102
|
+
}
|
|
103
|
+
```
|
|
104
|
+
|
|
105
|
+
- iOS: long-press activation (platform native); optional `preview` render fn
|
|
106
|
+
(iOS 16+).
|
|
107
|
+
- Android: `activation` prop — `'longPress'` (default) or `'singlePress'`,
|
|
108
|
+
detected at parent level so interactive children in the trigger keep
|
|
109
|
+
their own taps.
|
|
110
|
+
- Web: right-click (`longPress`) or click (`singlePress`) opens the menu at
|
|
111
|
+
the pointer.
|
|
112
|
+
- macOS: right-click — the NSMenu is the view's `menu`, so AppKit owns
|
|
113
|
+
activation and disabled-item dimming; the trigger renders inline.
|
|
114
|
+
|
|
115
|
+
## How the native menus work
|
|
116
|
+
|
|
117
|
+
Unlike props-only leaves (picker, date-picker), this leaf exercises subtree
|
|
118
|
+
embedding: `trigger` is a render fn whose octane output mounts into a
|
|
119
|
+
detached root, and the native menu wraps that subtree — SwiftUI resolves it
|
|
120
|
+
through `NativeScriptViewFactory` (the same factory the swift-ui plugin's
|
|
121
|
+
`NativeScriptView` uses), and Android through the leaf's
|
|
122
|
+
`XplatViewRegistry` + `AndroidView`.
|
|
123
|
+
|
|
124
|
+
The native implementations are adapted from `@expo/ui` (MIT,
|
|
125
|
+
`packages/expo-ui` sdk-57): `ios/ContextMenu/ContextMenu.swift` and
|
|
126
|
+
`android/.../ui/menu/DropdownMenu*.kt`. Attribution headers sit on the
|
|
127
|
+
ported files.
|
package/package.json
ADDED
|
@@ -0,0 +1,87 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@octane-xplat/context-menu",
|
|
3
|
+
"version": "0.0.0-nightly.g2e22171",
|
|
4
|
+
"description": "Platform-specific context menu controls for Octane xplat",
|
|
5
|
+
"repository": {
|
|
6
|
+
"type": "git",
|
|
7
|
+
"url": "https://github.com/octane-xplat/octane-xplat",
|
|
8
|
+
"directory": "packages/context-menu"
|
|
9
|
+
},
|
|
10
|
+
"files": [
|
|
11
|
+
"types",
|
|
12
|
+
"src",
|
|
13
|
+
"platforms",
|
|
14
|
+
"!src/**/*.test.*",
|
|
15
|
+
"!src/**/*.spec.*"
|
|
16
|
+
],
|
|
17
|
+
"type": "module",
|
|
18
|
+
"exports": {
|
|
19
|
+
".": {
|
|
20
|
+
"types": "./types/index.d.ts",
|
|
21
|
+
"web": "./src/index.ts",
|
|
22
|
+
"native": "./src/index.ts",
|
|
23
|
+
"default": "./src/index.ts"
|
|
24
|
+
},
|
|
25
|
+
"./ios": {
|
|
26
|
+
"types": "./types/ios/index.d.ts",
|
|
27
|
+
"native": "./src/ios/index.ts"
|
|
28
|
+
},
|
|
29
|
+
"./android": {
|
|
30
|
+
"types": "./types/android/index.d.ts",
|
|
31
|
+
"native": "./src/android/index.ts"
|
|
32
|
+
},
|
|
33
|
+
"./web": {
|
|
34
|
+
"types": "./types/web/index.d.ts",
|
|
35
|
+
"web": "./src/web/index.ts"
|
|
36
|
+
},
|
|
37
|
+
"./macos": {
|
|
38
|
+
"types": "./types/macos/index.d.ts",
|
|
39
|
+
"macos": "./src/macos/index.ts"
|
|
40
|
+
}
|
|
41
|
+
},
|
|
42
|
+
"publishConfig": {
|
|
43
|
+
"access": "public"
|
|
44
|
+
},
|
|
45
|
+
"dependencies": {
|
|
46
|
+
"@nativescript/jetpack-compose": "1.0.0-beta.0",
|
|
47
|
+
"@nativescript/swift-ui": "4.0.2",
|
|
48
|
+
"@octane-xplat/lib": "0.0.0-nightly.g2e22171"
|
|
49
|
+
},
|
|
50
|
+
"devDependencies": {
|
|
51
|
+
"@nativescript-community/octane": "0.2.5",
|
|
52
|
+
"@nativescript/core": "9.1.3",
|
|
53
|
+
"@octane-xplat/macos-renderer": "0.0.0-nightly.g2e22171",
|
|
54
|
+
"@octanejs/vite-plugin": "0.1.61",
|
|
55
|
+
"octane": "0.11.0",
|
|
56
|
+
"tsrx-typegen": "0.3.0",
|
|
57
|
+
"typescript": "5.9.3",
|
|
58
|
+
"vite": "8.3.0"
|
|
59
|
+
},
|
|
60
|
+
"peerDependencies": {
|
|
61
|
+
"@nativescript-community/octane": ">=0.2.1 <1",
|
|
62
|
+
"@nativescript/core": ">=9.1.0 <10",
|
|
63
|
+
"octane": ">=0.1.51 <1"
|
|
64
|
+
},
|
|
65
|
+
"peerDependenciesMeta": {
|
|
66
|
+
"@nativescript-community/octane": {
|
|
67
|
+
"optional": true
|
|
68
|
+
},
|
|
69
|
+
"@nativescript/core": {
|
|
70
|
+
"optional": true
|
|
71
|
+
},
|
|
72
|
+
"octane": {
|
|
73
|
+
"optional": true
|
|
74
|
+
}
|
|
75
|
+
},
|
|
76
|
+
"nativescript": {
|
|
77
|
+
"platforms": {
|
|
78
|
+
"android": "9.1.0",
|
|
79
|
+
"ios": "9.1.0"
|
|
80
|
+
}
|
|
81
|
+
},
|
|
82
|
+
"scripts": {
|
|
83
|
+
"test:packed": "node tests/packed-consumer.mjs",
|
|
84
|
+
"pack:check": "tsrx-typegen --pack-check",
|
|
85
|
+
"typegen": "tsc -p tsconfig.types.json"
|
|
86
|
+
}
|
|
87
|
+
}
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
apply plugin: 'org.jetbrains.kotlin.plugin.compose'
|
|
2
|
+
|
|
3
|
+
android {
|
|
4
|
+
buildFeatures {
|
|
5
|
+
compose true
|
|
6
|
+
}
|
|
7
|
+
kotlinOptions {
|
|
8
|
+
jvmTarget = '17'
|
|
9
|
+
}
|
|
10
|
+
}
|
|
11
|
+
|
|
12
|
+
dependencies {
|
|
13
|
+
implementation platform('androidx.compose:compose-bom:2024.12.01')
|
|
14
|
+
implementation 'androidx.compose.material3:material3'
|
|
15
|
+
}
|
|
@@ -0,0 +1,158 @@
|
|
|
1
|
+
// Adapted from @expo/ui (MIT) — Copyright 2025-present 650 Industries.
|
|
2
|
+
// Upstream: expo/packages/expo-ui/android/.../ui/menu/DropdownMenu.kt,
|
|
3
|
+
// DropdownMenuItem.kt, DropdownMenuRecords.kt (sdk-57).
|
|
4
|
+
// The ExpoModules glue (ComposeProps records, slot views, ModifierRegistry)
|
|
5
|
+
// is replaced by the @nativescript/jetpack-compose provider contract:
|
|
6
|
+
// updateData(Map) + onEvent. The trigger child is a registered
|
|
7
|
+
// NativeScript view hosted through AndroidView — XplatViewRegistry is the
|
|
8
|
+
// leaf-side counterpart of iOS's NativeScriptViewFactory-by-id.
|
|
9
|
+
|
|
10
|
+
package com.octanexplat.contextmenu
|
|
11
|
+
|
|
12
|
+
import android.view.View
|
|
13
|
+
import android.view.ViewGroup
|
|
14
|
+
import androidx.compose.foundation.layout.Box
|
|
15
|
+
import androidx.compose.material3.DropdownMenu
|
|
16
|
+
import androidx.compose.material3.DropdownMenuItem
|
|
17
|
+
import androidx.compose.material3.HorizontalDivider
|
|
18
|
+
import androidx.compose.material3.MaterialTheme
|
|
19
|
+
import androidx.compose.material3.Text
|
|
20
|
+
import androidx.compose.runtime.Composable
|
|
21
|
+
import androidx.compose.runtime.getValue
|
|
22
|
+
import androidx.compose.runtime.mutableStateOf
|
|
23
|
+
import androidx.compose.runtime.setValue
|
|
24
|
+
import androidx.compose.ui.platform.ComposeView
|
|
25
|
+
import androidx.compose.ui.viewinterop.AndroidView
|
|
26
|
+
import org.json.JSONObject
|
|
27
|
+
import java.lang.ref.WeakReference
|
|
28
|
+
|
|
29
|
+
/** id → NativeScript view registry, callable directly from JS
|
|
30
|
+
* (`com.octanexplat.contextmenu.XplatViewRegistry.register`). The leaf's
|
|
31
|
+
* TSRX wrapper registers the trigger host's android.view.View before the
|
|
32
|
+
* ComposeView reads it back inside AndroidView. Weak refs — JS owns the
|
|
33
|
+
* view's real lifecycle and unregisters on unmount. */
|
|
34
|
+
object XplatViewRegistry {
|
|
35
|
+
private val views = HashMap<String, WeakReference<View>>()
|
|
36
|
+
|
|
37
|
+
@JvmStatic
|
|
38
|
+
fun register(id: String, view: View) {
|
|
39
|
+
views[id] = WeakReference(view)
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
@JvmStatic
|
|
43
|
+
fun unregister(id: String) {
|
|
44
|
+
views.remove(id)
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
@JvmStatic
|
|
48
|
+
fun get(id: String): View? = views[id]?.get()
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
private data class ContextMenuItemSpec(
|
|
52
|
+
val id: String,
|
|
53
|
+
val title: String,
|
|
54
|
+
val destructive: Boolean,
|
|
55
|
+
val disabled: Boolean,
|
|
56
|
+
val divider: Boolean,
|
|
57
|
+
)
|
|
58
|
+
|
|
59
|
+
private data class ContextMenuUiState(
|
|
60
|
+
val triggerId: String = "",
|
|
61
|
+
val expanded: Boolean = false,
|
|
62
|
+
val items: List<ContextMenuItemSpec> = emptyList(),
|
|
63
|
+
)
|
|
64
|
+
|
|
65
|
+
private fun Any?.asBoolean(): Boolean = when (this) {
|
|
66
|
+
is Boolean -> this
|
|
67
|
+
is Number -> toInt() != 0
|
|
68
|
+
else -> false
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
private fun Any?.asList(): List<Any?> = when (this) {
|
|
72
|
+
is org.json.JSONArray -> (0 until length()).map { get(it) }
|
|
73
|
+
is List<*> -> this
|
|
74
|
+
else -> emptyList()
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
private fun Any?.field(key: String): Any? = when (this) {
|
|
78
|
+
is org.json.JSONObject -> if (has(key)) get(key) else null
|
|
79
|
+
is Map<*, *> -> this[key]
|
|
80
|
+
else -> null
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
private fun parseItems(raw: Any?): List<ContextMenuItemSpec> {
|
|
84
|
+
return raw.asList().mapNotNull { entry ->
|
|
85
|
+
entry ?: return@mapNotNull null
|
|
86
|
+
ContextMenuItemSpec(
|
|
87
|
+
id = entry.field("id") as? String ?: return@mapNotNull null,
|
|
88
|
+
title = entry.field("title") as? String ?: "",
|
|
89
|
+
destructive = entry.field("destructive").asBoolean(),
|
|
90
|
+
disabled = entry.field("disabled").asBoolean(),
|
|
91
|
+
divider = entry.field("divider").asBoolean(),
|
|
92
|
+
)
|
|
93
|
+
}
|
|
94
|
+
}
|
|
95
|
+
|
|
96
|
+
@Composable
|
|
97
|
+
private fun ContextMenuContent(state: ContextMenuUiState, onDismiss: () -> Unit, onSelect: (String) -> Unit) {
|
|
98
|
+
Box {
|
|
99
|
+
val triggerId = state.triggerId
|
|
100
|
+
if (triggerId.isNotEmpty()) {
|
|
101
|
+
AndroidView(
|
|
102
|
+
factory = { ctx ->
|
|
103
|
+
XplatViewRegistry.get(triggerId) ?: View(ctx).apply {
|
|
104
|
+
layoutParams = ViewGroup.LayoutParams(0, 0)
|
|
105
|
+
}
|
|
106
|
+
},
|
|
107
|
+
)
|
|
108
|
+
}
|
|
109
|
+
|
|
110
|
+
// Expanded is controlled: activation lives in the NativeScript touch
|
|
111
|
+
// layer (the embedded subtree consumes pointer input before Compose
|
|
112
|
+
// gesture detection can resolve it), dismissal is reported back so
|
|
113
|
+
// the JS side can close the loop.
|
|
114
|
+
DropdownMenu(
|
|
115
|
+
expanded = state.expanded,
|
|
116
|
+
onDismissRequest = onDismiss,
|
|
117
|
+
) {
|
|
118
|
+
state.items.forEach { item ->
|
|
119
|
+
if (item.divider) {
|
|
120
|
+
HorizontalDivider()
|
|
121
|
+
}
|
|
122
|
+
DropdownMenuItem(
|
|
123
|
+
text = { Text(item.title) },
|
|
124
|
+
enabled = !item.disabled,
|
|
125
|
+
onClick = {
|
|
126
|
+
onSelect(item.id)
|
|
127
|
+
},
|
|
128
|
+
)
|
|
129
|
+
}
|
|
130
|
+
}
|
|
131
|
+
}
|
|
132
|
+
}
|
|
133
|
+
|
|
134
|
+
class XplatContextMenuProvider {
|
|
135
|
+
private var state by mutableStateOf(ContextMenuUiState())
|
|
136
|
+
var onEvent: ((Any) -> Unit)? = null
|
|
137
|
+
|
|
138
|
+
fun generateComposeView(view: ComposeView) {
|
|
139
|
+
view.setContent {
|
|
140
|
+
MaterialTheme {
|
|
141
|
+
ContextMenuContent(
|
|
142
|
+
state = state,
|
|
143
|
+
onDismiss = { onEvent?.invoke(JSONObject().put("dismissed", true)) },
|
|
144
|
+
onSelect = { id -> onEvent?.invoke(JSONObject().put("item", id)) },
|
|
145
|
+
)
|
|
146
|
+
}
|
|
147
|
+
}
|
|
148
|
+
}
|
|
149
|
+
|
|
150
|
+
fun updateData(input: Map<Any, Any>) {
|
|
151
|
+
val data = input["data"] as? Map<*, *> ?: input
|
|
152
|
+
state = ContextMenuUiState(
|
|
153
|
+
triggerId = data["triggerId"] as? String ?: "",
|
|
154
|
+
expanded = data["expanded"].asBoolean(),
|
|
155
|
+
items = parseItems(data["items"]),
|
|
156
|
+
)
|
|
157
|
+
}
|
|
158
|
+
}
|
|
@@ -0,0 +1,123 @@
|
|
|
1
|
+
// Adapted from @expo/ui (MIT) — Copyright 2025-present 650 Industries.
|
|
2
|
+
// Upstream: expo/packages/expo-ui/ios/ContextMenu/ContextMenu.swift (sdk-57).
|
|
3
|
+
// The ExpoModules glue (children slot views, EventDispatcher) is replaced by
|
|
4
|
+
// the @nativescript/swift-ui provider contract: the activation element is a
|
|
5
|
+
// UIViewRepresentable resolved by id from NativeScriptViewFactory (the same
|
|
6
|
+
// lookup the plugin's own NativeScriptView uses — re-declared here because
|
|
7
|
+
// the plugin's struct is module-internal), and menu content arrives as
|
|
8
|
+
// serialized item data through updateData(NSDictionary) + onEvent.
|
|
9
|
+
|
|
10
|
+
import SwiftUI
|
|
11
|
+
|
|
12
|
+
private struct ContextMenuItemSpec: Identifiable {
|
|
13
|
+
let id: String
|
|
14
|
+
let title: String
|
|
15
|
+
let destructive: Bool
|
|
16
|
+
let disabled: Bool
|
|
17
|
+
let divider: Bool
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
private final class ContextMenuModel: ObservableObject {
|
|
21
|
+
@Published var triggerId: String = ""
|
|
22
|
+
@Published var previewId: String = ""
|
|
23
|
+
@Published var items: [ContextMenuItemSpec] = []
|
|
24
|
+
var onSelect: ((String) -> Void)?
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
// Same shape as the plugin's NativeScriptViewRepresentable: a unique
|
|
28
|
+
// instance id (registered id + uuid) resolved through the core-owned
|
|
29
|
+
// NativeScriptViewFactory, which the JS-side SwiftUIManager hooks up.
|
|
30
|
+
private struct HostedNativeScriptView: UIViewRepresentable {
|
|
31
|
+
let id: String
|
|
32
|
+
|
|
33
|
+
init(id: String) {
|
|
34
|
+
self.id = id + "-" + UUID().uuidString
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
func makeUIView(context: Context) -> UIView {
|
|
38
|
+
return NativeScriptViewFactory.shared?.getViewById(id) ?? UIView()
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
func updateUIView(_ uiView: UIView, context: Context) {}
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
@available(iOS 14.0, *)
|
|
45
|
+
private struct ContextMenuContent: View {
|
|
46
|
+
@ObservedObject var model: ContextMenuModel
|
|
47
|
+
|
|
48
|
+
private var menuItems: some View {
|
|
49
|
+
ForEach(model.items) { item in
|
|
50
|
+
if item.divider {
|
|
51
|
+
Divider()
|
|
52
|
+
}
|
|
53
|
+
Button(role: item.destructive ? .destructive : nil) {
|
|
54
|
+
model.onSelect?(item.id)
|
|
55
|
+
} label: {
|
|
56
|
+
Text(item.title)
|
|
57
|
+
}
|
|
58
|
+
.disabled(item.disabled)
|
|
59
|
+
}
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
var body: some View {
|
|
63
|
+
if model.triggerId.isEmpty {
|
|
64
|
+
EmptyView()
|
|
65
|
+
} else if #available(iOS 16.0, tvOS 16.0, *), !model.previewId.isEmpty {
|
|
66
|
+
HostedNativeScriptView(id: model.triggerId)
|
|
67
|
+
.contextMenu(menuItems: { menuItems }, preview: {
|
|
68
|
+
HostedNativeScriptView(id: model.previewId)
|
|
69
|
+
})
|
|
70
|
+
} else {
|
|
71
|
+
HostedNativeScriptView(id: model.triggerId)
|
|
72
|
+
.contextMenu(menuItems: { menuItems })
|
|
73
|
+
}
|
|
74
|
+
}
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
@objc(XplatContextMenuProvider)
|
|
78
|
+
public final class XplatContextMenuProvider: UIViewController, SwiftUIProvider {
|
|
79
|
+
private let model = ContextMenuModel()
|
|
80
|
+
|
|
81
|
+
public required init() {
|
|
82
|
+
super.init(nibName: nil, bundle: nil)
|
|
83
|
+
model.onSelect = { [weak self] id in
|
|
84
|
+
self?.onEvent?(["item": id])
|
|
85
|
+
}
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
public required init?(coder: NSCoder) {
|
|
89
|
+
super.init(coder: coder)
|
|
90
|
+
model.onSelect = { [weak self] id in
|
|
91
|
+
self?.onEvent?(["item": id])
|
|
92
|
+
}
|
|
93
|
+
}
|
|
94
|
+
|
|
95
|
+
public override func viewDidLoad() {
|
|
96
|
+
super.viewDidLoad()
|
|
97
|
+
if #available(iOS 14.0, *) {
|
|
98
|
+
setupSwiftUIView(content: ContextMenuContent(model: model))
|
|
99
|
+
}
|
|
100
|
+
}
|
|
101
|
+
|
|
102
|
+
public func updateData(data: NSDictionary) {
|
|
103
|
+
if let triggerId = data["triggerId"] as? String {
|
|
104
|
+
model.triggerId = triggerId
|
|
105
|
+
}
|
|
106
|
+
if let previewId = data["previewId"] as? String {
|
|
107
|
+
model.previewId = previewId
|
|
108
|
+
}
|
|
109
|
+
if let items = data["items"] as? [NSDictionary] {
|
|
110
|
+
model.items = items.map { raw in
|
|
111
|
+
ContextMenuItemSpec(
|
|
112
|
+
id: raw["id"] as? String ?? "",
|
|
113
|
+
title: raw["title"] as? String ?? "",
|
|
114
|
+
destructive: (raw["destructive"] as? NSNumber)?.boolValue ?? false,
|
|
115
|
+
disabled: (raw["disabled"] as? NSNumber)?.boolValue ?? false,
|
|
116
|
+
divider: (raw["divider"] as? NSNumber)?.boolValue ?? false,
|
|
117
|
+
)
|
|
118
|
+
}
|
|
119
|
+
}
|
|
120
|
+
}
|
|
121
|
+
|
|
122
|
+
public var onEvent: ((NSDictionary) -> Void)?
|
|
123
|
+
}
|
|
@@ -0,0 +1,38 @@
|
|
|
1
|
+
/** @jsxImportSource @octane-xplat/macos-renderer */
|
|
2
|
+
import { useEffect, useRef } from 'octane'
|
|
3
|
+
import type { AppKitContextMenuProps } from './macos/types'
|
|
4
|
+
|
|
5
|
+
const host = (): any => (globalThis as any).__xplatAppKit ?? {}
|
|
6
|
+
|
|
7
|
+
/** macOS context menu — the trigger renders inline as this element's
|
|
8
|
+
* children and a real NSMenu is attached to the backing NSView's `menu`,
|
|
9
|
+
* so AppKit drives right-click activation and disabled items. Items are
|
|
10
|
+
* serialized data; selection reports through `onItemSelected`. */
|
|
11
|
+
export function AppKitContextMenu(props: AppKitContextMenuProps) @{
|
|
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
|
|
17
|
+
|
|
18
|
+
useEffect(() => {
|
|
19
|
+
const view = anchor.current
|
|
20
|
+
const attach = host().attachContextMenu
|
|
21
|
+
if (!view || typeof attach !== 'function') return
|
|
22
|
+
return attach(
|
|
23
|
+
view,
|
|
24
|
+
{ items: latest.current.items ?? [] },
|
|
25
|
+
(id: string) => latest.current.onItemSelected?.(id),
|
|
26
|
+
)
|
|
27
|
+
}, [props.items])
|
|
28
|
+
|
|
29
|
+
<flexboxlayout
|
|
30
|
+
ref={(v: any) => { anchor.current = v }}
|
|
31
|
+
id={props.id}
|
|
32
|
+
className={props.className}
|
|
33
|
+
style={props.style}
|
|
34
|
+
accessibilityLabel={props.accessibilityLabel}
|
|
35
|
+
>
|
|
36
|
+
{props.trigger()}
|
|
37
|
+
</flexboxlayout>;
|
|
38
|
+
}
|
|
@@ -0,0 +1,157 @@
|
|
|
1
|
+
/** @jsxImportSource octane */
|
|
2
|
+
import { Fragment, useLayoutEffect, useRef, useState } from 'octane'
|
|
3
|
+
import type { ContextMenuProps } from './web/types'
|
|
4
|
+
|
|
5
|
+
/** Web context menu: the trigger subtree renders inline; right-click (or
|
|
6
|
+
* long-press via the `activation` prop's 'singlePress' = click) opens an
|
|
7
|
+
* in-page menu at the pointer. */
|
|
8
|
+
export function ContextMenu(props: ContextMenuProps) @{
|
|
9
|
+
const [menu, setMenu] = useState<{ x: number; y: number } | null>(null)
|
|
10
|
+
const [active, setActive] = useState(0)
|
|
11
|
+
const listRef = useRef<HTMLUListElement | null>(null)
|
|
12
|
+
const items = props.items ?? []
|
|
13
|
+
const enabled = items
|
|
14
|
+
.map((item, index) => (item.disabled ? -1 : index))
|
|
15
|
+
.filter((index) => index >= 0)
|
|
16
|
+
|
|
17
|
+
const openAt = (e: any) => {
|
|
18
|
+
e.preventDefault?.()
|
|
19
|
+
setActive(enabled[0] ?? 0)
|
|
20
|
+
setMenu({ x: e.clientX ?? 0, y: e.clientY ?? 0 })
|
|
21
|
+
}
|
|
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
|
+
|
|
71
|
+
<span
|
|
72
|
+
id={props.id}
|
|
73
|
+
className={props.className}
|
|
74
|
+
style={{ display: 'inline-block', ...(props.style ?? {}) }}
|
|
75
|
+
onContextMenu={props.activation === 'singlePress' ? undefined : openAt}
|
|
76
|
+
onClick={props.activation === 'singlePress' ? openAt : undefined}
|
|
77
|
+
aria-label={props.accessibilityLabel}
|
|
78
|
+
>
|
|
79
|
+
{props.trigger()}
|
|
80
|
+
{menu && (
|
|
81
|
+
<>
|
|
82
|
+
{/* Events from the backdrop and the menu must not bubble to the
|
|
83
|
+
opener handlers — dismissal stays final. */}
|
|
84
|
+
<span
|
|
85
|
+
style={{ position: 'fixed', inset: 0, zIndex: 999 }}
|
|
86
|
+
onClick={(e: any) => {
|
|
87
|
+
e.stopPropagation?.()
|
|
88
|
+
setMenu(null)
|
|
89
|
+
}}
|
|
90
|
+
onContextMenu={(e: any) => {
|
|
91
|
+
e.preventDefault?.()
|
|
92
|
+
e.stopPropagation?.()
|
|
93
|
+
setMenu(null)
|
|
94
|
+
}}
|
|
95
|
+
/>
|
|
96
|
+
<ul
|
|
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}
|
|
108
|
+
style={{
|
|
109
|
+
position: 'fixed',
|
|
110
|
+
left: menu.x,
|
|
111
|
+
top: menu.y,
|
|
112
|
+
zIndex: 1000,
|
|
113
|
+
margin: 0,
|
|
114
|
+
padding: '4px 0',
|
|
115
|
+
listStyle: 'none',
|
|
116
|
+
background: 'var(--menu-bg, Canvas)',
|
|
117
|
+
color: 'var(--menu-fg, CanvasText)',
|
|
118
|
+
border: '1px solid color-mix(in srgb, CanvasText 20%, transparent)',
|
|
119
|
+
borderRadius: 8,
|
|
120
|
+
boxShadow: '0 6px 24px rgb(0 0 0 / 0.18)',
|
|
121
|
+
minWidth: 160,
|
|
122
|
+
outline: 'none',
|
|
123
|
+
}}
|
|
124
|
+
>
|
|
125
|
+
{items.map((item, index) => (
|
|
126
|
+
<Fragment key={item.id}>
|
|
127
|
+
{item.divider && (
|
|
128
|
+
<li
|
|
129
|
+
aria-hidden="true"
|
|
130
|
+
style={{ borderTop: '1px solid color-mix(in srgb, CanvasText 15%, transparent)', margin: '4px 0' }}
|
|
131
|
+
/>
|
|
132
|
+
)}
|
|
133
|
+
<li
|
|
134
|
+
role="menuitem"
|
|
135
|
+
aria-disabled={item.disabled || undefined}
|
|
136
|
+
aria-current={index === active || undefined}
|
|
137
|
+
style={{
|
|
138
|
+
padding: '6px 12px',
|
|
139
|
+
cursor: item.disabled ? 'default' : 'pointer',
|
|
140
|
+
opacity: item.disabled ? 0.5 : 1,
|
|
141
|
+
color: item.destructive ? 'crimson' : undefined,
|
|
142
|
+
background:
|
|
143
|
+
index === active
|
|
144
|
+
? 'color-mix(in srgb, CanvasText 10%, transparent)'
|
|
145
|
+
: undefined,
|
|
146
|
+
}}
|
|
147
|
+
onClick={() => choose(index)}
|
|
148
|
+
>
|
|
149
|
+
{item.title}
|
|
150
|
+
</li>
|
|
151
|
+
</Fragment>
|
|
152
|
+
))}
|
|
153
|
+
</ul>
|
|
154
|
+
</>
|
|
155
|
+
)}
|
|
156
|
+
</span>;
|
|
157
|
+
}
|
|
@@ -0,0 +1,126 @@
|
|
|
1
|
+
/** @jsxImportSource @nativescript-community/octane */
|
|
2
|
+
import { registerElement } from '@nativescript-community/octane'
|
|
3
|
+
import type { Attributes } from '@nativescript-community/octane/intrinsics'
|
|
4
|
+
import { ComposeDataDriver, JetpackCompose, registerJetpackCompose } from '@nativescript/jetpack-compose'
|
|
5
|
+
import { Application, GridLayout } from '@nativescript/core'
|
|
6
|
+
import { createPortal as createUniversalPortal, type UniversalRenderable } from 'octane/universal/native'
|
|
7
|
+
import { useEffect, useRef, useState } from 'octane'
|
|
8
|
+
import type { MaterialContextMenuProps } from './android/types'
|
|
9
|
+
|
|
10
|
+
declare const com: any
|
|
11
|
+
|
|
12
|
+
declare module '@nativescript-community/octane/intrinsics' {
|
|
13
|
+
interface NativeScriptElements {
|
|
14
|
+
xplatandroidcontextmenu: Attributes<typeof JetpackCompose>
|
|
15
|
+
}
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
registerElement('xplatandroidcontextmenu', JetpackCompose)
|
|
19
|
+
registerJetpackCompose('xplat-material-contextmenu', (owner: any) =>
|
|
20
|
+
new ComposeDataDriver(new com.octanexplat.contextmenu.XplatContextMenuProvider(), owner),
|
|
21
|
+
)
|
|
22
|
+
|
|
23
|
+
// Dash-free ids — the host-side representable appends "-<uuid>" and the
|
|
24
|
+
// registry lookup splits on the first dash.
|
|
25
|
+
let seq = 0
|
|
26
|
+
|
|
27
|
+
function TriggerRoot(props: { render: () => any }) @{
|
|
28
|
+
<>{props.render()}</>;
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
function createPortal(children: unknown, target: unknown) {
|
|
32
|
+
return createUniversalPortal(children as UniversalRenderable, target)
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
/** Renders a Material 3 DropdownMenu anchored to a NativeScript-hosted
|
|
36
|
+
* trigger subtree portaled from the declaring Octane tree. */
|
|
37
|
+
export function MaterialContextMenu(props: MaterialContextMenuProps) @{
|
|
38
|
+
const triggerId = useRef<string>('')
|
|
39
|
+
if (!triggerId.current) {
|
|
40
|
+
triggerId.current = `ctxmenu${++seq}`
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
const mounted = useRef<{ host: any } | null>(null)
|
|
44
|
+
const [portalHost, setPortalHost] = useState<GridLayout | null>(null)
|
|
45
|
+
const [expanded, setExpanded] = useState(false)
|
|
46
|
+
|
|
47
|
+
useEffect(() => {
|
|
48
|
+
const host = new GridLayout()
|
|
49
|
+
// Detached subtree — the plugin's generateNativeView shortcut passes
|
|
50
|
+
// `{}` as the native context, which fails to marshal on Android; the
|
|
51
|
+
// view factory needs a real android.content.Context to build its
|
|
52
|
+
// native view.
|
|
53
|
+
|
|
54
|
+
const context = (Application.android as any).foregroundActivity ?? (Application.android as any).startActivity
|
|
55
|
+
const nativeHost = host as any
|
|
56
|
+
|
|
57
|
+
nativeHost._setupAsRootView(context)
|
|
58
|
+
nativeHost.callLoaded()
|
|
59
|
+
com.octanexplat.contextmenu.XplatViewRegistry.register(triggerId.current, host.android)
|
|
60
|
+
mounted.current = { host }
|
|
61
|
+
|
|
62
|
+
// Gate triggerId on registration — the provider only composes the
|
|
63
|
+
// AndroidView host once triggerId is non-empty, so the factory must
|
|
64
|
+
// find the view in the registry on first read.
|
|
65
|
+
setPortalHost(host)
|
|
66
|
+
|
|
67
|
+
return () => {
|
|
68
|
+
com.octanexplat.contextmenu.XplatViewRegistry.unregister(triggerId.current)
|
|
69
|
+
mounted.current = null
|
|
70
|
+
nativeHost.callUnloaded?.()
|
|
71
|
+
nativeHost.removeChildren?.()
|
|
72
|
+
}
|
|
73
|
+
}, [])
|
|
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.
|
|
79
|
+
useEffect(() => {
|
|
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])
|
|
87
|
+
|
|
88
|
+
const items = props.items ?? []
|
|
89
|
+
|
|
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
|
+
</>;
|
|
126
|
+
}
|
|
@@ -0,0 +1,117 @@
|
|
|
1
|
+
/** @jsxImportSource @nativescript-community/octane */
|
|
2
|
+
import { registerElement } from '@nativescript-community/octane'
|
|
3
|
+
import type { Attributes } from '@nativescript-community/octane/intrinsics'
|
|
4
|
+
import { SwiftUI, UIDataDriver, registerSwiftUI, SwiftUIManager } from '@nativescript/swift-ui'
|
|
5
|
+
import { GridLayout } from '@nativescript/core'
|
|
6
|
+
import { getSwiftUIHostLifecycle, registerSwiftUIHost, unregisterSwiftUIHost } from '@octane-xplat/lib'
|
|
7
|
+
import { createPortal as createUniversalPortal, type UniversalRenderable } from 'octane/universal/native'
|
|
8
|
+
import { useEffect, useRef, useState } from 'octane'
|
|
9
|
+
import type { SwiftUIContextMenuProps } from './ios/types'
|
|
10
|
+
|
|
11
|
+
declare const XplatContextMenuProvider: any
|
|
12
|
+
declare const NativeScriptViewFactory: any
|
|
13
|
+
|
|
14
|
+
declare module '@nativescript-community/octane/intrinsics' {
|
|
15
|
+
interface NativeScriptElements {
|
|
16
|
+
xplatioscontextmenu: Attributes<typeof SwiftUI>
|
|
17
|
+
}
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
registerElement('xplatioscontextmenu', SwiftUI)
|
|
21
|
+
registerSwiftUI('xplat-swiftui-contextmenu', (owner: any) =>
|
|
22
|
+
new UIDataDriver(new XplatContextMenuProvider(), owner),
|
|
23
|
+
)
|
|
24
|
+
|
|
25
|
+
function createPortal(children: unknown, target: unknown) {
|
|
26
|
+
return createUniversalPortal(children as UniversalRenderable, target)
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
function ensureNativeScriptViewFactory(): void {
|
|
30
|
+
if (!NativeScriptViewFactory.shared) {
|
|
31
|
+
NativeScriptViewFactory.initShared()
|
|
32
|
+
}
|
|
33
|
+
}
|
|
34
|
+
|
|
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
|
+
)
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
function TriggerRoot(props: { render: () => any }) @{
|
|
51
|
+
<>{props.render()}</>;
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
let seq = 0
|
|
55
|
+
|
|
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. */
|
|
59
|
+
export function SwiftUIContextMenu(props: SwiftUIContextMenuProps) @{
|
|
60
|
+
const triggerId = useRef<string>('')
|
|
61
|
+
if (!triggerId.current) {
|
|
62
|
+
triggerId.current = `ctxmenu${++seq}`
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
const previewId = `${triggerId.current}preview`
|
|
66
|
+
const [portalHosts, setPortalHosts] = useState<{
|
|
67
|
+
trigger: GridLayout
|
|
68
|
+
preview: GridLayout
|
|
69
|
+
} | null>(null)
|
|
70
|
+
|
|
71
|
+
useEffect(() => {
|
|
72
|
+
const trigger = new GridLayout()
|
|
73
|
+
const preview = new GridLayout()
|
|
74
|
+
registerHosts(triggerId.current, trigger, previewId, preview)
|
|
75
|
+
setPortalHosts({ trigger, preview })
|
|
76
|
+
|
|
77
|
+
return () => {
|
|
78
|
+
unregisterSwiftUIHost(triggerId.current)
|
|
79
|
+
unregisterSwiftUIHost(previewId)
|
|
80
|
+
}
|
|
81
|
+
}, [])
|
|
82
|
+
|
|
83
|
+
const items = props.items ?? []
|
|
84
|
+
|
|
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
|
+
</>;
|
|
117
|
+
}
|
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
export type MaterialContextMenuActivation = 'longPress' | 'singlePress'
|
|
2
|
+
|
|
3
|
+
/** One row of serialized menu content carried over the bridge. */
|
|
4
|
+
export interface MaterialContextMenuItem {
|
|
5
|
+
/** Stable id reported to `onItemSelected`. */
|
|
6
|
+
id: string
|
|
7
|
+
title: string
|
|
8
|
+
destructive?: boolean
|
|
9
|
+
disabled?: boolean
|
|
10
|
+
/** Render a divider before this item. */
|
|
11
|
+
divider?: boolean
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
export interface MaterialContextMenuProps {
|
|
15
|
+
id?: string
|
|
16
|
+
className?: any
|
|
17
|
+
style?: any
|
|
18
|
+
/** Activation gesture. @default 'longPress' */
|
|
19
|
+
activation?: MaterialContextMenuActivation
|
|
20
|
+
items?: readonly MaterialContextMenuItem[]
|
|
21
|
+
/** Render fn producing the trigger subtree — portaled from the declaring
|
|
22
|
+
* Octane tree into the menu's AndroidView anchor. */
|
|
23
|
+
trigger: () => any
|
|
24
|
+
onItemSelected?: (id: string) => void
|
|
25
|
+
accessibilityLabel?: string
|
|
26
|
+
}
|
package/src/index.ts
ADDED
package/src/ios/index.ts
ADDED
package/src/ios/types.ts
ADDED
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
/** One row of serialized menu content carried over the bridge. */
|
|
2
|
+
export interface SwiftUIContextMenuItem {
|
|
3
|
+
/** Stable id reported to `onItemSelected`. */
|
|
4
|
+
id: string
|
|
5
|
+
title: string
|
|
6
|
+
/** Renders the item with .destructive role. */
|
|
7
|
+
destructive?: boolean
|
|
8
|
+
disabled?: boolean
|
|
9
|
+
/** Render a divider before this item. */
|
|
10
|
+
divider?: boolean
|
|
11
|
+
}
|
|
12
|
+
|
|
13
|
+
export interface SwiftUIContextMenuProps {
|
|
14
|
+
id?: string
|
|
15
|
+
className?: any
|
|
16
|
+
style?: any
|
|
17
|
+
items?: readonly SwiftUIContextMenuItem[]
|
|
18
|
+
/** Render fn producing the activation element — hosted inside the
|
|
19
|
+
* SwiftUI tree via the plugin's NativeScriptView-by-id factory. */
|
|
20
|
+
trigger: () => any
|
|
21
|
+
/** Optional preview shown while the menu opens (iOS 16+). */
|
|
22
|
+
preview?: () => any
|
|
23
|
+
onItemSelected?: (id: string) => void
|
|
24
|
+
accessibilityLabel?: string
|
|
25
|
+
}
|
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
/** One row of serialized menu content carried over the bridge. */
|
|
2
|
+
export interface AppKitContextMenuItem {
|
|
3
|
+
/** Stable id reported to `onItemSelected`. */
|
|
4
|
+
id: string
|
|
5
|
+
title: string
|
|
6
|
+
/** No AppKit destructive role — kept in the contract for parity; the
|
|
7
|
+
* item renders as a normal menu row. */
|
|
8
|
+
destructive?: boolean
|
|
9
|
+
disabled?: boolean
|
|
10
|
+
/** Render a divider before this item. */
|
|
11
|
+
divider?: boolean
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
export interface AppKitContextMenuProps {
|
|
15
|
+
id?: string
|
|
16
|
+
className?: any
|
|
17
|
+
style?: any
|
|
18
|
+
items?: readonly AppKitContextMenuItem[]
|
|
19
|
+
/** Render fn producing the trigger element — rendered inline; the NSMenu
|
|
20
|
+
* attaches to the host's backing NSView. */
|
|
21
|
+
trigger: () => any
|
|
22
|
+
onItemSelected?: (id: string) => void
|
|
23
|
+
accessibilityLabel?: string
|
|
24
|
+
}
|
package/src/web/index.ts
ADDED
package/src/web/types.ts
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
export type ContextMenuActivation = 'longPress' | 'singlePress'
|
|
2
|
+
|
|
3
|
+
export interface ContextMenuItem {
|
|
4
|
+
id: string
|
|
5
|
+
title: string
|
|
6
|
+
destructive?: boolean
|
|
7
|
+
disabled?: boolean
|
|
8
|
+
divider?: boolean
|
|
9
|
+
}
|
|
10
|
+
|
|
11
|
+
export interface ContextMenuProps {
|
|
12
|
+
id?: string
|
|
13
|
+
className?: any
|
|
14
|
+
style?: any
|
|
15
|
+
/** 'longPress' maps to right-click (contextmenu); 'singlePress' to click. */
|
|
16
|
+
activation?: ContextMenuActivation
|
|
17
|
+
items?: readonly ContextMenuItem[]
|
|
18
|
+
trigger: () => any
|
|
19
|
+
onItemSelected?: (id: string) => void
|
|
20
|
+
accessibilityLabel?: string
|
|
21
|
+
}
|
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
export type MaterialContextMenuActivation = 'longPress' | 'singlePress'
|
|
2
|
+
|
|
3
|
+
export interface MaterialContextMenuItem {
|
|
4
|
+
id: string
|
|
5
|
+
title: string
|
|
6
|
+
destructive?: boolean
|
|
7
|
+
disabled?: boolean
|
|
8
|
+
divider?: boolean
|
|
9
|
+
}
|
|
10
|
+
|
|
11
|
+
export interface MaterialContextMenuProps {
|
|
12
|
+
id?: string
|
|
13
|
+
className?: any
|
|
14
|
+
style?: any
|
|
15
|
+
activation?: MaterialContextMenuActivation
|
|
16
|
+
items?: readonly MaterialContextMenuItem[]
|
|
17
|
+
trigger: () => any
|
|
18
|
+
onItemSelected?: (id: string) => void
|
|
19
|
+
accessibilityLabel?: string
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
export declare function MaterialContextMenu(props: MaterialContextMenuProps): unknown
|
package/types/index.d.ts
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {}
|
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
export interface SwiftUIContextMenuItem {
|
|
2
|
+
id: string
|
|
3
|
+
title: string
|
|
4
|
+
destructive?: boolean
|
|
5
|
+
disabled?: boolean
|
|
6
|
+
divider?: boolean
|
|
7
|
+
}
|
|
8
|
+
|
|
9
|
+
export interface SwiftUIContextMenuProps {
|
|
10
|
+
id?: string
|
|
11
|
+
className?: any
|
|
12
|
+
style?: any
|
|
13
|
+
items?: readonly SwiftUIContextMenuItem[]
|
|
14
|
+
trigger: () => any
|
|
15
|
+
preview?: () => any
|
|
16
|
+
onItemSelected?: (id: string) => void
|
|
17
|
+
accessibilityLabel?: string
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
export declare function SwiftUIContextMenu(props: SwiftUIContextMenuProps): unknown
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
export interface AppKitContextMenuItem {
|
|
2
|
+
id: string
|
|
3
|
+
title: string
|
|
4
|
+
destructive?: boolean
|
|
5
|
+
disabled?: boolean
|
|
6
|
+
divider?: boolean
|
|
7
|
+
}
|
|
8
|
+
|
|
9
|
+
export interface AppKitContextMenuProps {
|
|
10
|
+
id?: string
|
|
11
|
+
className?: any
|
|
12
|
+
style?: any
|
|
13
|
+
items?: readonly AppKitContextMenuItem[]
|
|
14
|
+
trigger: () => any
|
|
15
|
+
onItemSelected?: (id: string) => void
|
|
16
|
+
accessibilityLabel?: string
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
export declare function AppKitContextMenu(props: AppKitContextMenuProps): unknown
|
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
export type ContextMenuActivation = 'longPress' | 'singlePress'
|
|
2
|
+
|
|
3
|
+
export interface ContextMenuItem {
|
|
4
|
+
id: string
|
|
5
|
+
title: string
|
|
6
|
+
destructive?: boolean
|
|
7
|
+
disabled?: boolean
|
|
8
|
+
divider?: boolean
|
|
9
|
+
}
|
|
10
|
+
|
|
11
|
+
export interface ContextMenuProps {
|
|
12
|
+
id?: string
|
|
13
|
+
className?: any
|
|
14
|
+
style?: any
|
|
15
|
+
activation?: ContextMenuActivation
|
|
16
|
+
items?: readonly ContextMenuItem[]
|
|
17
|
+
trigger: () => any
|
|
18
|
+
onItemSelected?: (id: string) => void
|
|
19
|
+
accessibilityLabel?: string
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
export declare function ContextMenu(props: ContextMenuProps): unknown
|