@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.
package/README.md CHANGED
@@ -1,166 +1,5 @@
1
1
  # `@octane-xplat/context-menu`
2
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
- experimental `NSMenu` source path on native macOS. Native macOS activation,
7
- responder routing, keyboard/VoiceOver and installed-consumer qualification is
8
- pending.
3
+ [Documentation](https://octane-xplat.goddardai.org/package-context-menu).
9
4
 
10
- There is no shared context-menu component. Install the package in the app
11
- that renders a control, then import `SwiftUIContextMenu` from
12
- `@octane-xplat/context-menu/ios`, `MaterialContextMenu` from
13
- `@octane-xplat/context-menu/android`, `ContextMenu` from
14
- `@octane-xplat/context-menu/web`, or `AppKitContextMenu` from
15
- `@octane-xplat/context-menu/macos` in the matching platform-suffixed file.
16
-
17
- Install in your existing app with Bun. The macOS entry requires a
18
- [native AppKit app](https://octane-xplat.goddardai.org/macos-native); the renderer supplies
19
- its native menu attachment.
20
-
21
- ```sh
22
- bun add @octane-xplat/context-menu
23
- ```
24
-
25
- ```tsx
26
- /** @jsxImportSource @octane-xplat/jsx */
27
- // SwiftUIContextMenuExample.ios.tsx
28
- import { SwiftUIContextMenu } from '@octane-xplat/context-menu/ios'
29
- import { Text } from '@octane-xplat/ui'
30
-
31
- export function Menu() {
32
- return (
33
- <SwiftUIContextMenu
34
- trigger={() => <Text>Trip actions</Text>}
35
- items={[{ id: 'rename', title: 'Rename' }]}
36
- onItemSelected={(id) => console.log(id)}
37
- />
38
- )
39
- }
40
- ```
41
-
42
- ```tsx
43
- /** @jsxImportSource @octane-xplat/jsx */
44
- // MaterialContextMenuExample.android.tsx
45
- import { MaterialContextMenu } from '@octane-xplat/context-menu/android'
46
- import { Text } from '@octane-xplat/ui'
47
-
48
- export function Menu() {
49
- return (
50
- <MaterialContextMenu
51
- activation="singlePress"
52
- trigger={() => <Text>Trip actions</Text>}
53
- items={[{ id: 'rename', title: 'Rename' }]}
54
- onItemSelected={(id) => console.log(id)}
55
- />
56
- )
57
- }
58
- ```
59
-
60
- ```tsx
61
- // ContextMenuExample.web.tsx
62
- import { ContextMenu } from '@octane-xplat/context-menu/web'
63
- import { Text } from '@octane-xplat/ui'
64
-
65
- export function Menu() {
66
- return (
67
- <ContextMenu
68
- activation="singlePress"
69
- trigger={() => <Text>Trip actions</Text>}
70
- items={[{ id: 'rename', title: 'Rename' }]}
71
- onItemSelected={(id) => console.log(id)}
72
- />
73
- )
74
- }
75
- ```
76
-
77
- ```tsx
78
- // AppKitContextMenuExample.macos.tsx
79
- import { useState } from 'octane'
80
- import { AppKitContextMenu } from '@octane-xplat/context-menu/macos'
81
- import { Text, VStack } from '@octane-xplat/ui'
82
-
83
- export function Menu() {
84
- const [last, setLast] = useState('none')
85
- return (
86
- <VStack gap={2}>
87
- <AppKitContextMenu
88
- id="trip-actions"
89
- trigger={() => <Text>Trip actions</Text>}
90
- items={[{ id: 'rename', title: 'Rename' }]}
91
- onItemSelected={setLast}
92
- />
93
- <Text>{`Last action: ${last}`}</Text>
94
- </VStack>
95
- )
96
- }
97
- ```
98
-
99
- Menu items are plain data (`{id, title, destructive, disabled, divider}`),
100
- reported back through `onItemSelected(id)`. The iOS entry also accepts an
101
- optional `preview` render function (iOS 16+):
102
-
103
- ```tsx
104
- /** @jsxImportSource @octane-xplat/jsx */
105
- // Actions.ios.tsx
106
- import { SwiftUIContextMenu } from '@octane-xplat/context-menu/ios'
107
- import { Text } from '@octane-xplat/ui'
108
-
109
- export function Actions() {
110
- return (
111
- <SwiftUIContextMenu
112
- trigger={() => <Text>Trip actions</Text>}
113
- preview={() => <Text>Kyoto trip</Text>}
114
- items={[{ id: 'delete', title: 'Delete', destructive: true, divider: true }]}
115
- onItemSelected={(id) => console.log(id)}
116
- />
117
- )
118
- }
119
- ```
120
-
121
- - iOS: long-press activation (platform native); optional `preview` render fn
122
- (iOS 16+).
123
- - Android: `activation` prop — `'longPress'` (default) or `'singlePress'`,
124
- detected at parent level so interactive children in the trigger keep
125
- their own taps.
126
- - Web: right-click (`longPress`) or click (`singlePress`) opens the menu at
127
- the pointer.
128
- - macOS: right-click — the NSMenu is the view's `menu`, so AppKit owns
129
- activation and disabled-item dimming; the trigger renders inline.
130
-
131
- On native macOS, item IDs must be unique strings and titles must be strings.
132
- Changing menu content or its record `id` retires the old session; callback-only
133
- renders preserve it and use the current handler. Empty items remove this
134
- wrapper's menu. Invalid data or a missing native attachment shows a generic
135
- unavailable message while the trigger remains inline. Native child editing menus
136
- and nested wrappers keep their own menu slots. See the
137
- [native macOS lifecycle and remaining limits](../../docs/platform/context-menu.md#native-macos-menus).
138
-
139
- ```tsx
140
- // NoteActions.macos.tsx — use a record-specific identifier when reusing a row.
141
- import { AppKitContextMenu } from '@octane-xplat/context-menu/macos'
142
- import { Text } from '@octane-xplat/ui'
143
- export function NoteActions({ noteID }: { noteID: string }) {
144
- return <AppKitContextMenu id={`note-actions-${noteID}`} items={[{ id: 'rename', title: 'Rename' }]}
145
- trigger={() => <Text>Right-click this note</Text>} onItemSelected={(id) => console.log(noteID, id)} />
146
- }
147
- ```
148
-
149
- ## How the native menus work
150
-
151
- On iOS and Android this leaf exercises subtree embedding: `trigger` is a render
152
- fn whose octane output mounts into a
153
- detached root, and the native menu wraps that subtree — SwiftUI resolves it
154
- through `NativeScriptViewFactory` (the same factory the swift-ui plugin's
155
- `NativeScriptView` uses), and Android through the leaf's
156
- `XplatViewRegistry` + `AndroidView`.
157
- The macOS trigger stays in its original tree, with the menu attached only at
158
- its wrapper. AppKit's documented unhandled right-click propagation supplies
159
- ancestor lookup; actual custom-host routing remains unqualified.
160
-
161
- The native implementations are adapted from `@expo/ui` (MIT,
162
- `packages/expo-ui` sdk-57): `ios/ContextMenu/ContextMenu.swift` and
163
- `android/.../ui/menu/DropdownMenu*.kt`. Attribution headers sit on the
164
- ported files.
165
-
166
- Continue with [Platform context menus](https://octane-xplat.goddardai.org/context-menu).
5
+ [API reference](https://octane-xplat.goddardai.org/api-context-menu).
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@octane-xplat/context-menu",
3
- "version": "0.19.0",
3
+ "version": "0.20.0",
4
4
  "description": "Platform-specific context menu controls for Octane xplat",
5
5
  "repository": {
6
6
  "type": "git",
@@ -48,24 +48,24 @@
48
48
  "test:packed": "node tests/packed-consumer.mjs",
49
49
  "pack:check": "tsrx-typegen --pack-check",
50
50
  "typegen": "tsrx-typegen --target context-menu",
51
- "typecheck": "tsrx-tsc --noEmit -p tsconfig.types.json"
51
+ "typecheck": "tsc --runExternalCode --noEmit -p tsconfig.types.json"
52
52
  },
53
53
  "dependencies": {
54
54
  "@nativescript/jetpack-compose": "1.0.0-beta.0",
55
55
  "@nativescript/swift-ui": "4.0.2",
56
- "@octane-xplat/jsx": "0.19.0",
57
- "@octane-xplat/lib": "0.19.0"
56
+ "@octane-xplat/jsx": "0.20.0",
57
+ "@octane-xplat/lib": "0.20.0"
58
58
  },
59
59
  "devDependencies": {
60
60
  "@nativescript-community/octane": "0.2.5",
61
61
  "@nativescript/core": "9.1.3",
62
- "@octane-xplat/macos-renderer": "0.19.0",
62
+ "@octane-xplat/macos-appkit": "0.20.0",
63
63
  "@octanejs/vite-plugin": "0.5.0",
64
64
  "octane": "0.16.3",
65
- "tsrx-typegen": "0.5.0",
66
- "typescript": "5.9.3",
67
- "vite": "8.3.0",
68
- "@tsrx/typescript-plugin": "0.4.11"
65
+ "tsrx-typegen": "0.6.1",
66
+ "typescript": "7.1.0-beta",
67
+ "vite": "8.3.6",
68
+ "@tsrx/content-mapper": "0.1.2"
69
69
  },
70
70
  "peerDependencies": {
71
71
  "@nativescript-community/octane": ">=0.2.1 <1",
@@ -13,38 +13,40 @@ const host = (): any => (globalThis as any).__xplatAppKit ?? {}
13
13
  * so AppKit drives right-click activation and disabled items. Items are
14
14
  * serialized data; selection reports through `onItemSelected`. */
15
15
  export function AppKitContextMenu(props: AppKitContextMenuProps): unknown @{
16
- const latest = useRef(props)
17
- latest.current = props
18
- const [, rerender] = useState(0)
19
- const owner = useMemo(
20
- () =>
21
- createContextMenuOwner({
22
- props: () => latest.current,
23
- attach: () =>
24
- typeof host().attachContextMenu === 'function'
25
- ? (host().attachContextMenu as ContextMenuAttach)
26
- : undefined,
27
- status: () => rerender((n) => n + 1),
28
- }),
29
- [],
30
- )
16
+ const latest = useRef(props)
31
17
 
32
- useLayoutEffect(() => owner.update(), null)
33
- useEffect(() => () => owner.dispose(), [])
18
+ latest.current = props
34
19
 
35
- <masonview
36
- ref={owner.bind}
37
- id={props.id}
38
- className={cx('xplat-appkit-context-menu', props.className)}
39
- style={props.style}
40
- accessibilityLabel={props.accessibilityLabel}
41
- >
42
- {props.trigger()}
43
- @if (
44
- owner.getStatus() === 'invalid' ||
45
- owner.getStatus() === 'unavailable'
46
- ) {
47
- <label text="Menu actions are unavailable." accessibilityLiveRegion="polite" />
48
- }
49
- </masonview>
20
+ const [, rerender] = useState(0)
21
+ const owner = useMemo(
22
+ () =>
23
+ createContextMenuOwner({
24
+ props: () => latest.current,
25
+ attach: () =>
26
+ typeof host().attachContextMenu === 'function'
27
+ ? (host().attachContextMenu as ContextMenuAttach)
28
+ : undefined,
29
+ status: () => rerender((n) => n + 1),
30
+ }),
31
+ [],
32
+ )
33
+
34
+ useLayoutEffect(() => owner.update(), null)
35
+ useEffect(() => () => owner.dispose(), [])
36
+
37
+ <masonview
38
+ ref={owner.bind}
39
+ id={props.id}
40
+ className={cx('xplat-appkit-context-menu', props.className)}
41
+ style={props.style}
42
+ accessibilityLabel={props.accessibilityLabel}
43
+ >
44
+ {props.trigger()}
45
+ @if (
46
+ owner.getStatus() === 'invalid' ||
47
+ owner.getStatus() === 'unavailable'
48
+ ) {
49
+ <label text="Menu actions are unavailable." accessibilityLiveRegion="polite" />
50
+ }
51
+ </masonview>
50
52
  }
@@ -0,0 +1,2 @@
1
+ // Type contract only. The target build must select a platform implementation.
2
+ export declare const AppKitContextMenu: typeof import('./AppKitContextMenu.macos.tsrx').AppKitContextMenu
@@ -0,0 +1,2 @@
1
+ // Type contract only. The target build must select a platform implementation.
2
+ export declare const ContextMenu: typeof import('./ContextMenu.web.tsrx').ContextMenu
@@ -7,164 +7,170 @@ import type { ContextMenuProps } from './web/types'
7
7
  * long-press via the `activation` prop's 'singlePress' = click) opens an
8
8
  * in-page menu at the pointer. */
9
9
  export function ContextMenu(props: ContextMenuProps): JSX.Element @{
10
- const [menu, setMenu] = useState<{ x: number; y: number } | null>(null)
11
- const [active, setActive] = useState(0)
12
- const listRef = useRef<HTMLUListElement | null>(null)
13
- const items = props.items ?? []
14
- const enabled = items
15
- .map((item, index) => (item.disabled ? -1 : index))
16
- .filter((index) => index >= 0)
17
-
18
- const openAt = (e: any) => {
19
- e.preventDefault?.()
20
- setActive(enabled[0] ?? 0)
21
- setMenu({ x: e.clientX ?? 0, y: e.clientY ?? 0 })
22
- }
23
-
24
- const choose = (index: number) => {
25
- const item = items[index]
26
- if (!item || item.disabled) {
27
- return
28
- }
29
-
30
- setMenu(null)
31
- props.onItemSelected?.(item.id)
32
- }
33
-
34
- // Once the menu is mounted: clamp it into the viewport so every item
35
- // stays reachable, then focus it so the keyboard contract works.
36
- useLayoutEffect(() => {
37
- const el = listRef.current
38
- if (!menu || !el) {
39
- return
40
- }
41
-
42
- const x = Math.max(0, Math.min(menu.x, window.innerWidth - el.offsetWidth - 8))
43
- const y = Math.max(0, Math.min(menu.y, window.innerHeight - el.offsetHeight - 8))
44
- if (x !== menu.x || y !== menu.y) {
45
- setMenu({ x, y })
46
- return
47
- }
48
-
49
- el.focus()
50
- }, [menu])
51
-
52
- const onMenuKeyDown = (e: any) => {
53
- if (e.key === 'Escape') {
54
- e.preventDefault?.()
55
- e.stopPropagation?.()
56
- setMenu(null)
57
- return
58
- }
59
-
60
- if (!enabled.length) {
61
- return
62
- }
63
-
64
- const at = Math.max(0, enabled.indexOf(active))
65
- if (e.key === 'ArrowDown' || e.key === 'ArrowUp') {
66
- e.preventDefault?.()
67
- const step = e.key === 'ArrowDown' ? 1 : -1
68
- setActive(enabled[(at + step + enabled.length) % enabled.length])
69
- } else if (e.key === 'Home') {
70
- e.preventDefault?.()
71
- setActive(enabled[0])
72
- } else if (e.key === 'End') {
73
- e.preventDefault?.()
74
- setActive(enabled[enabled.length - 1])
75
- } else if (e.key === 'Enter' || e.key === ' ') {
76
- e.preventDefault?.()
77
- choose(active)
78
- }
79
- }
80
-
81
- <span
82
- id={props.id}
83
- className={props.className}
84
- style={{ display: 'inline-block', ...props.style }}
85
- onContextMenu={props.activation === 'singlePress' ? undefined : openAt}
86
- onClick={props.activation === 'singlePress' ? openAt : undefined}
87
- aria-label={props.accessibilityLabel}
88
- >
89
- {props.trigger()}
90
- {menu && (
91
- <>
92
- {/* Events from the backdrop and the menu must not bubble to the
10
+ const [menu, setMenu] = useState<{ x: number; y: number } | null>(null)
11
+ const [active, setActive] = useState(0)
12
+ const listRef = useRef<HTMLUListElement | null>(null)
13
+ const items = props.items ?? []
14
+ const enabled = items
15
+ .map((item, index) => (item.disabled ? -1 : index))
16
+ .filter((index) => index >= 0)
17
+
18
+ const openAt = (e: any) => {
19
+ e.preventDefault?.()
20
+ setActive(enabled[0] ?? 0)
21
+ setMenu({ x: e.clientX ?? 0, y: e.clientY ?? 0 })
22
+ }
23
+
24
+ const choose = (index: number) => {
25
+ const item = items[index]
26
+
27
+ if (!item || item.disabled) {
28
+ return
29
+ }
30
+
31
+ setMenu(null)
32
+ props.onItemSelected?.(item.id)
33
+ }
34
+
35
+ // Once the menu is mounted: clamp it into the viewport so every item
36
+ // stays reachable, then focus it so the keyboard contract works.
37
+ useLayoutEffect(() => {
38
+ const el = listRef.current
39
+
40
+ if (!menu || !el) {
41
+ return
42
+ }
43
+
44
+ const x = Math.max(0, Math.min(menu.x, window.innerWidth - el.offsetWidth - 8))
45
+ const y = Math.max(0, Math.min(menu.y, window.innerHeight - el.offsetHeight - 8))
46
+
47
+ if (x !== menu.x || y !== menu.y) {
48
+ setMenu({ x, y })
49
+ return
50
+ }
51
+
52
+ el.focus()
53
+ }, [menu])
54
+
55
+ const onMenuKeyDown = (e: any) => {
56
+ if (e.key === 'Escape') {
57
+ e.preventDefault?.()
58
+ e.stopPropagation?.()
59
+ setMenu(null)
60
+ return
61
+ }
62
+
63
+ if (!enabled.length) {
64
+ return
65
+ }
66
+
67
+ const at = Math.max(0, enabled.indexOf(active))
68
+
69
+ if (e.key === 'ArrowDown' || e.key === 'ArrowUp') {
70
+ e.preventDefault?.()
71
+
72
+ const step = e.key === 'ArrowDown' ? 1 : -1
73
+
74
+ setActive(enabled[(at + step + enabled.length) % enabled.length])
75
+ } else if (e.key === 'Home') {
76
+ e.preventDefault?.()
77
+ setActive(enabled[0])
78
+ } else if (e.key === 'End') {
79
+ e.preventDefault?.()
80
+ setActive(enabled[enabled.length - 1])
81
+ } else if (e.key === 'Enter' || e.key === ' ') {
82
+ e.preventDefault?.()
83
+ choose(active)
84
+ }
85
+ }
86
+
87
+ <span
88
+ id={props.id}
89
+ className={props.className}
90
+ style={{ display: 'inline-block', ...props.style }}
91
+ onContextMenu={props.activation === 'singlePress' ? undefined : openAt}
92
+ onClick={props.activation === 'singlePress' ? openAt : undefined}
93
+ aria-label={props.accessibilityLabel}
94
+ >
95
+ {props.trigger()}
96
+ {menu && (
97
+ <>
98
+ {/* Events from the backdrop and the menu must not bubble to the
93
99
  opener handlers — dismissal stays final. */}
94
- <span
95
- style={{ position: 'fixed', inset: 0, zIndex: 999 }}
96
- onClick={(e: any) => {
97
- e.stopPropagation?.()
98
- setMenu(null)
99
- }}
100
- onContextMenu={(e: any) => {
101
- e.preventDefault?.()
102
- e.stopPropagation?.()
103
- setMenu(null)
104
- }}
105
- />
106
- <ul
107
- role="menu"
108
- tabIndex={-1}
109
- ref={(el: any) => {
110
- listRef.current = el
111
- }}
112
- onClick={(e: any) => e.stopPropagation?.()}
113
- onContextMenu={(e: any) => {
114
- e.preventDefault?.()
115
- e.stopPropagation?.()
116
- }}
117
- onKeyDown={onMenuKeyDown}
118
- style={{
119
- position: 'fixed',
120
- left: menu.x,
121
- top: menu.y,
122
- zIndex: 1000,
123
- margin: 0,
124
- padding: '4px 0',
125
- listStyle: 'none',
126
- background: 'var(--menu-bg, Canvas)',
127
- color: 'var(--menu-fg, CanvasText)',
128
- border: '1px solid color-mix(in srgb, CanvasText 20%, transparent)',
129
- borderRadius: 8,
130
- boxShadow: '0 6px 24px rgb(0 0 0 / 0.18)',
131
- minWidth: 160,
132
- outline: 'none',
133
- }}
134
- >
135
- {items.map((item, index) => (
136
- <Fragment key={item.id}>
137
- {item.divider && (
138
- <li
139
- aria-hidden="true"
140
- style={{
141
- borderTop: '1px solid color-mix(in srgb, CanvasText 15%, transparent)',
142
- margin: '4px 0',
143
- }}
144
- />
145
- )}
146
- <li
147
- role="menuitem"
148
- aria-disabled={item.disabled || undefined}
149
- aria-current={index === active || undefined}
150
- style={{
151
- padding: '6px 12px',
152
- cursor: item.disabled ? 'default' : 'pointer',
153
- opacity: item.disabled ? 0.5 : 1,
154
- color: item.destructive ? 'crimson' : undefined,
155
- background:
156
- index === active
157
- ? 'color-mix(in srgb, CanvasText 10%, transparent)'
158
- : undefined,
159
- }}
160
- onClick={() => choose(index)}
161
- >
162
- {item.title}
163
- </li>
164
- </Fragment>
165
- ))}
166
- </ul>
167
- </>
168
- )}
169
- </span>
100
+ <span
101
+ style={{ position: 'fixed', inset: 0, zIndex: 999 }}
102
+ onClick={(e: any) => {
103
+ e.stopPropagation?.()
104
+ setMenu(null)
105
+ }}
106
+ onContextMenu={(e: any) => {
107
+ e.preventDefault?.()
108
+ e.stopPropagation?.()
109
+ setMenu(null)
110
+ }}
111
+ />
112
+ <ul
113
+ role="menu"
114
+ tabIndex={-1}
115
+ ref={(el: any) => {
116
+ listRef.current = el
117
+ }}
118
+ onClick={(e: any) => e.stopPropagation?.()}
119
+ onContextMenu={(e: any) => {
120
+ e.preventDefault?.()
121
+ e.stopPropagation?.()
122
+ }}
123
+ onKeyDown={onMenuKeyDown}
124
+ style={{
125
+ position: 'fixed',
126
+ left: menu.x,
127
+ top: menu.y,
128
+ zIndex: 1000,
129
+ margin: 0,
130
+ padding: '4px 0',
131
+ listStyle: 'none',
132
+ background: 'var(--menu-bg, Canvas)',
133
+ color: 'var(--menu-fg, CanvasText)',
134
+ border: '1px solid color-mix(in srgb, CanvasText 20%, transparent)',
135
+ borderRadius: 8,
136
+ boxShadow: '0 6px 24px rgb(0 0 0 / 0.18)',
137
+ minWidth: 160,
138
+ outline: 'none',
139
+ }}
140
+ >
141
+ {items.map((item, index) => (
142
+ <Fragment key={item.id}>
143
+ {item.divider && (
144
+ <li
145
+ aria-hidden="true"
146
+ style={{
147
+ borderTop: '1px solid color-mix(in srgb, CanvasText 15%, transparent)',
148
+ margin: '4px 0',
149
+ }}
150
+ />
151
+ )}
152
+ <li
153
+ role="menuitem"
154
+ aria-disabled={item.disabled || undefined}
155
+ aria-current={index === active || undefined}
156
+ style={{
157
+ padding: '6px 12px',
158
+ cursor: item.disabled ? 'default' : 'pointer',
159
+ opacity: item.disabled ? 0.5 : 1,
160
+ color: item.destructive ? 'crimson' : undefined,
161
+ background:
162
+ index === active
163
+ ? 'color-mix(in srgb, CanvasText 10%, transparent)'
164
+ : undefined,
165
+ }}
166
+ onClick={() => choose(index)}
167
+ >
168
+ {item.title}
169
+ </li>
170
+ </Fragment>
171
+ ))}
172
+ </ul>
173
+ </>
174
+ )}
175
+ </span>
170
176
  }