@octane-xplat/context-menu 0.18.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.
Files changed (38) hide show
  1. package/README.md +2 -124
  2. package/package.json +92 -86
  3. package/src/AppKitContextMenu.macos.css +1 -0
  4. package/src/AppKitContextMenu.macos.tsrx +41 -21
  5. package/src/AppKitContextMenu.tsrx +2 -0
  6. package/src/ContextMenu.tsrx +2 -0
  7. package/src/ContextMenu.web.tsrx +168 -149
  8. package/src/MaterialContextMenu.android.tsrx +128 -101
  9. package/src/MaterialContextMenu.tsrx +2 -0
  10. package/src/SwiftUIContextMenu.ios.tsrx +104 -84
  11. package/src/SwiftUIContextMenu.tsrx +2 -0
  12. package/src/android/index.ts +3 -3
  13. package/src/android/types.ts +29 -18
  14. package/src/context-menu-owner.macos.ts +213 -0
  15. package/src/ios/types.ts +28 -19
  16. package/src/macos/types.ts +27 -18
  17. package/src/web/types.ts +29 -14
  18. package/types/generated/.tsrx-typegen-manifest.json +19 -0
  19. package/types/generated/AppKitContextMenu.macos.d.ts +8 -0
  20. package/types/generated/ContextMenu.web.d.ts +7 -0
  21. package/types/generated/MaterialContextMenu.android.d.ts +16 -0
  22. package/types/generated/SwiftUIContextMenu.ios.d.ts +17 -0
  23. package/types/generated/android/index.d.ts +2 -0
  24. package/types/generated/android/types.d.ts +35 -0
  25. package/types/generated/context-menu-owner.macos.d.ts +16 -0
  26. package/types/generated/index.d.ts +1 -0
  27. package/types/generated/ios/index.d.ts +2 -0
  28. package/types/generated/ios/types.d.ts +33 -0
  29. package/types/generated/macos/index.d.ts +2 -0
  30. package/types/generated/macos/types.d.ts +32 -0
  31. package/types/generated/web/index.d.ts +2 -0
  32. package/types/generated/web/types.d.ts +34 -0
  33. package/LICENSE +0 -21
  34. package/types/android/index.d.ts +0 -22
  35. package/types/index.d.ts +0 -1
  36. package/types/ios/index.d.ts +0 -20
  37. package/types/macos/index.d.ts +0 -19
  38. package/types/web/index.d.ts +0 -22
@@ -1,157 +1,176 @@
1
- /** @jsxImportSource octane */
1
+ /** @jsxImportSource @octane-xplat/jsx/dom */
2
+ import type { JSX } from 'octane/jsx-runtime'
2
3
  import { Fragment, useLayoutEffect, useRef, useState } from 'octane'
3
4
  import type { ContextMenuProps } from './web/types'
4
5
 
5
6
  /** Web context menu: the trigger subtree renders inline; right-click (or
6
7
  * long-press via the `activation` prop's 'singlePress' = click) opens an
7
8
  * 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
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
+
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
83
99
  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>;
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>
157
176
  }
@@ -1,23 +1,41 @@
1
- /** @jsxImportSource @nativescript-community/octane */
1
+ /** @jsxImportSource @octane-xplat/jsx */
2
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'
3
+ import type { Attributes } from '@octane-xplat/jsx/intrinsics'
4
+ import {
5
+ ComposeDataDriver,
6
+ JetpackCompose,
7
+ registerJetpackCompose,
8
+ } from '@nativescript/jetpack-compose'
9
+
5
10
  import { Application, GridLayout } from '@nativescript/core'
6
- import { createPortal as createUniversalPortal, type UniversalRenderable } from 'octane/universal/native'
11
+ import {
12
+ createPortal as createUniversalPortal,
13
+ type UniversalRenderable,
14
+ } from 'octane/universal/native'
15
+
7
16
  import { useEffect, useRef, useState } from 'octane'
8
17
  import type { MaterialContextMenuProps } from './android/types'
9
18
 
10
19
  declare const com: any
11
20
 
12
- declare module '@nativescript-community/octane/intrinsics' {
13
- interface NativeScriptElements {
14
- xplatandroidcontextmenu: Attributes<typeof JetpackCompose>
15
- }
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
+ }
16
32
  }
17
33
 
18
34
  registerElement('xplatandroidcontextmenu', JetpackCompose)
19
- registerJetpackCompose('xplat-material-contextmenu', (owner: any) =>
20
- new ComposeDataDriver(new com.octanexplat.contextmenu.XplatContextMenuProvider(), owner),
35
+ registerJetpackCompose(
36
+ 'xplat-material-contextmenu',
37
+ (owner: any) =>
38
+ new ComposeDataDriver(new com.octanexplat.contextmenu.XplatContextMenuProvider(), owner),
21
39
  )
22
40
 
23
41
  // Dash-free ids — the host-side representable appends "-<uuid>" and the
@@ -25,102 +43,111 @@ registerJetpackCompose('xplat-material-contextmenu', (owner: any) =>
25
43
  let seq = 0
26
44
 
27
45
  function TriggerRoot(props: { render: () => any }) @{
28
- <>{props.render()}</>;
46
+ <>{props.render()}</>
29
47
  }
30
48
 
31
49
  function createPortal(children: unknown, target: unknown) {
32
- return createUniversalPortal(children as UniversalRenderable, target)
50
+ return createUniversalPortal(children as UniversalRenderable, target)
33
51
  }
34
52
 
35
53
  /** Renders a Material 3 DropdownMenu anchored to a NativeScript-hosted
36
54
  * 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
- </>;
55
+ export function MaterialContextMenu(props: MaterialContextMenuProps): unknown @{
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
+ </>
126
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