@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.
@@ -1,190 +1,213 @@
1
1
  import type { AppKitContextMenuItem, AppKitContextMenuProps } from './macos/types'
2
2
 
3
3
  export type ContextMenuAttach = (
4
- view: object,
5
- options: { items: readonly AppKitContextMenuItem[] },
6
- onSelect: (id: string | undefined) => void,
4
+ view: object,
5
+ options: { items: readonly AppKitContextMenuItem[] },
6
+ onSelect: (id: string | undefined) => void,
7
7
  ) => (() => void) | null
8
8
 
9
9
  export type ContextMenuStatus = 'ready' | 'empty' | 'invalid' | 'unavailable'
10
10
 
11
11
  /** Copy primitive content so in-place item changes cannot rewrite an old menu. */
12
12
  function menuSnapshot(
13
- items: readonly AppKitContextMenuItem[] = [],
13
+ items: readonly AppKitContextMenuItem[] = [],
14
14
  ): AppKitContextMenuItem[] | null {
15
- const ids = new Set<string>()
16
- try {
17
- return items.map((item) => {
18
- if (typeof item.id !== 'string' || typeof item.title !== 'string' || ids.has(item.id)) {
19
- throw Error('invalid menu')
20
- }
21
- ids.add(item.id)
22
- return {
23
- id: item.id,
24
- title: item.title,
25
- disabled: !!item.disabled,
26
- divider: !!item.divider,
27
- destructive: !!item.destructive,
28
- }
29
- })
30
- } catch {
31
- return null
32
- }
15
+ const ids = new Set<string>()
16
+
17
+ try {
18
+ return items.map((item) => {
19
+ if (typeof item.id !== 'string' || typeof item.title !== 'string' || ids.has(item.id)) {
20
+ throw Error('invalid menu')
21
+ }
22
+
23
+ ids.add(item.id)
24
+ return {
25
+ id: item.id,
26
+ title: item.title,
27
+ disabled: !!item.disabled,
28
+ divider: !!item.divider,
29
+ destructive: !!item.destructive,
30
+ }
31
+ })
32
+ } catch {
33
+ return null
34
+ }
33
35
  }
34
36
 
35
37
  const sameMenuItems = (a: readonly AppKitContextMenuItem[], b: readonly AppKitContextMenuItem[]) =>
36
- a.length === b.length &&
37
- a.every((item, i) => {
38
- const other = b[i]
39
- return (
40
- item.id === other.id &&
41
- item.title === other.title &&
42
- item.disabled === other.disabled &&
43
- item.divider === other.divider &&
44
- item.destructive === other.destructive
45
- )
46
- })
38
+ a.length === b.length &&
39
+ a.every((item, i) => {
40
+ const other = b[i]
41
+
42
+ return (
43
+ item.id === other.id &&
44
+ item.title === other.title &&
45
+ item.disabled === other.disabled &&
46
+ item.divider === other.divider &&
47
+ item.destructive === other.destructive
48
+ )
49
+ })
47
50
 
48
51
  /** Current native menu session; callback-only renders preserve its tracking menu. */
49
52
  export function createContextMenuOwner(deps: {
50
- props(): AppKitContextMenuProps
51
- attach(): ContextMenuAttach | undefined
52
- status?(status: ContextMenuStatus): void
53
+ props(): AppKitContextMenuProps
54
+ attach(): ContextMenuAttach | undefined
55
+ status?(status: ContextMenuStatus): void
53
56
  }): {
54
- update: () => void
55
- bind(next: object | null): void
56
- dispose(): void
57
- getStatus: () => ContextMenuStatus
57
+ update: () => void
58
+ bind(next: object | null): void
59
+ dispose(): void
60
+ getStatus: () => ContextMenuStatus
58
61
  } {
59
- type Session = {
60
- view: object
61
- scope: string | undefined
62
- items: AppKitContextMenuItem[]
63
- ready: boolean
64
- selecting: boolean
65
- detach?: () => void
66
- }
67
- let view: object | null = null,
68
- session: Session | null = null,
69
- disposed = false
70
- let status: ContextMenuStatus = 'empty'
71
- const report = (next: ContextMenuStatus) => {
72
- if (!disposed && next !== status) {
73
- status = next
74
- deps.status?.(next)
75
- }
76
- }
77
- const detach = () => {
78
- const old = session
79
- session = null
80
- try {
81
- old?.detach?.()
82
- } catch {}
83
- }
84
- const update = () => {
85
- if (disposed || !view) {
86
- return
87
- }
88
- const props = deps.props(),
89
- items = menuSnapshot(props.items)
90
- if (!items) {
91
- detach()
92
- report('invalid')
93
- return
94
- }
95
- if (!items.length) {
96
- detach()
97
- report('empty')
98
- return
99
- }
100
- if (
101
- session &&
102
- session.view === view &&
103
- session.scope === props.id &&
104
- sameMenuItems(session.items, items)
105
- ) {
106
- return
107
- }
108
- detach()
109
- const attach = deps.attach()
110
- if (!attach) {
111
- report('unavailable')
112
- return
113
- }
114
- const owner: Session = { view, scope: props.id, items, ready: false, selecting: false }
115
- session = owner
116
- try {
117
- const cleanup = attach(view, { items }, (id) => {
118
- if (
119
- disposed ||
120
- session !== owner ||
121
- !owner.ready ||
122
- owner.selecting ||
123
- typeof id !== 'string'
124
- ) {
125
- return
126
- }
127
- const current = deps.props(),
128
- currentItems = menuSnapshot(current.items)
129
- if (
130
- !currentItems ||
131
- current.id !== owner.scope ||
132
- !sameMenuItems(owner.items, currentItems)
133
- ) {
134
- update()
135
- return
136
- }
137
- const item = currentItems.find((item) => item.id === id)
138
- if (!item || item.disabled) {
139
- return
140
- }
141
- owner.selecting = true
142
- try {
143
- current.onItemSelected?.(id)
144
- } finally {
145
- owner.selecting = false
146
- }
147
- })
148
-
149
- if (disposed || session !== owner) {
150
- cleanup?.()
151
- return
152
- }
153
-
154
- if (typeof cleanup !== 'function') {
155
- session = null
156
- report('unavailable')
157
- return
158
- }
159
-
160
- owner.detach = cleanup
161
- owner.ready = true
162
- report('ready')
163
- } catch {
164
- if (session === owner) {
165
- detach()
166
- report('unavailable')
167
- }
168
- }
169
- }
170
- return {
171
- update,
172
- bind(next: object | null) {
173
- if (!disposed && next !== view) {
174
- detach()
175
- view = next
176
- if (view) {
177
- update()
178
- }
179
- }
180
- },
181
- dispose() {
182
- if (!disposed) {
183
- disposed = true
184
- detach()
185
- view = null
186
- }
187
- },
188
- getStatus: () => status,
189
- }
62
+ type Session = {
63
+ view: object
64
+ scope: string | undefined
65
+ items: AppKitContextMenuItem[]
66
+ ready: boolean
67
+ selecting: boolean
68
+ detach?: () => void
69
+ }
70
+
71
+ let view: object | null = null,
72
+ session: Session | null = null,
73
+ disposed = false
74
+
75
+ let status: ContextMenuStatus = 'empty'
76
+ const report = (next: ContextMenuStatus) => {
77
+ if (!disposed && next !== status) {
78
+ status = next
79
+ deps.status?.(next)
80
+ }
81
+ }
82
+
83
+ const detach = () => {
84
+ const old = session
85
+
86
+ session = null
87
+ try {
88
+ old?.detach?.()
89
+ } catch {}
90
+ }
91
+
92
+ const update = () => {
93
+ if (disposed || !view) {
94
+ return
95
+ }
96
+
97
+ const props = deps.props(),
98
+ items = menuSnapshot(props.items)
99
+
100
+ if (!items) {
101
+ detach()
102
+ report('invalid')
103
+ return
104
+ }
105
+
106
+ if (!items.length) {
107
+ detach()
108
+ report('empty')
109
+ return
110
+ }
111
+
112
+ if (
113
+ session &&
114
+ session.view === view &&
115
+ session.scope === props.id &&
116
+ sameMenuItems(session.items, items)
117
+ ) {
118
+ return
119
+ }
120
+
121
+ detach()
122
+
123
+ const attach = deps.attach()
124
+
125
+ if (!attach) {
126
+ report('unavailable')
127
+ return
128
+ }
129
+
130
+ const owner: Session = { view, scope: props.id, items, ready: false, selecting: false }
131
+
132
+ session = owner
133
+ try {
134
+ const cleanup = attach(view, { items }, (id) => {
135
+ if (
136
+ disposed ||
137
+ session !== owner ||
138
+ !owner.ready ||
139
+ owner.selecting ||
140
+ typeof id !== 'string'
141
+ ) {
142
+ return
143
+ }
144
+
145
+ const current = deps.props(),
146
+ currentItems = menuSnapshot(current.items)
147
+
148
+ if (
149
+ !currentItems ||
150
+ current.id !== owner.scope ||
151
+ !sameMenuItems(owner.items, currentItems)
152
+ ) {
153
+ update()
154
+ return
155
+ }
156
+
157
+ const item = currentItems.find((item) => item.id === id)
158
+
159
+ if (!item || item.disabled) {
160
+ return
161
+ }
162
+
163
+ owner.selecting = true
164
+ try {
165
+ current.onItemSelected?.(id)
166
+ } finally {
167
+ owner.selecting = false
168
+ }
169
+ })
170
+
171
+ if (disposed || session !== owner) {
172
+ cleanup?.()
173
+ return
174
+ }
175
+
176
+ if (typeof cleanup !== 'function') {
177
+ session = null
178
+ report('unavailable')
179
+ return
180
+ }
181
+
182
+ owner.detach = cleanup
183
+ owner.ready = true
184
+ report('ready')
185
+ } catch {
186
+ if (session === owner) {
187
+ detach()
188
+ report('unavailable')
189
+ }
190
+ }
191
+ }
192
+
193
+ return {
194
+ update,
195
+ bind(next: object | null) {
196
+ if (!disposed && next !== view) {
197
+ detach()
198
+ view = next
199
+ if (view) {
200
+ update()
201
+ }
202
+ }
203
+ },
204
+ dispose() {
205
+ if (!disposed) {
206
+ disposed = true
207
+ detach()
208
+ view = null
209
+ }
210
+ },
211
+ getStatus: () => status,
212
+ }
190
213
  }
package/src/ios/types.ts CHANGED
@@ -1,34 +1,34 @@
1
1
  /** One row of serialized menu content carried over the bridge. */
2
2
  export interface SwiftUIContextMenuItem {
3
- /** Stable id reported to `onItemSelected`. */
4
- id: string
5
- /** Item display text. */
6
- title: string
7
- /** Renders the item with .destructive role. */
8
- destructive?: boolean
9
- /** Greys the item; not selectable. */
10
- disabled?: boolean
11
- /** Render a divider before this item. */
12
- divider?: boolean
3
+ /** Stable id reported to `onItemSelected`. */
4
+ id: string
5
+ /** Item display text. */
6
+ title: string
7
+ /** Renders the item with .destructive role. */
8
+ destructive?: boolean
9
+ /** Greys the item; not selectable. */
10
+ disabled?: boolean
11
+ /** Render a divider before this item. */
12
+ divider?: boolean
13
13
  }
14
14
 
15
15
  /** iOS context-menu props (SwiftUI contextMenu). */
16
16
  export interface SwiftUIContextMenuProps {
17
- /** Element id passed through to the host element. */
18
- id?: string
19
- /** Class list applied to the element. */
20
- className?: any
21
- /** Inline style (string CSS or object). */
22
- style?: any
23
- /** Menu entries in display order. */
24
- items?: readonly SwiftUIContextMenuItem[]
25
- /** Render fn producing the activation element — hosted inside the
26
- * SwiftUI tree via the plugin's NativeScriptView-by-id factory. */
27
- trigger: () => any
28
- /** Optional preview shown while the menu opens (iOS 16+). */
29
- preview?: () => any
30
- /** Fires with the selected item id. */
31
- onItemSelected?: (id: string) => void
32
- /** Spoken name for assistive tech. */
33
- accessibilityLabel?: string
17
+ /** Element id passed through to the host element. */
18
+ id?: string
19
+ /** Class list applied to the element. */
20
+ className?: any
21
+ /** Inline style (string CSS or object). */
22
+ style?: any
23
+ /** Menu entries in display order. */
24
+ items?: readonly SwiftUIContextMenuItem[]
25
+ /** Render fn producing the activation element — hosted inside the
26
+ * SwiftUI tree via the plugin's NativeScriptView-by-id factory. */
27
+ trigger: () => any
28
+ /** Optional preview shown while the menu opens (iOS 16+). */
29
+ preview?: () => any
30
+ /** Fires with the selected item id. */
31
+ onItemSelected?: (id: string) => void
32
+ /** Spoken name for assistive tech. */
33
+ accessibilityLabel?: string
34
34
  }
@@ -1,33 +1,33 @@
1
1
  /** One row of serialized menu content carried over the bridge. */
2
2
  export interface AppKitContextMenuItem {
3
- /** Stable id reported to `onItemSelected`; unique within this menu. */
4
- id: string
5
- /** Item display text. */
6
- title: string
7
- /** No AppKit destructive role — kept in the contract for parity; the
8
- * item renders as a normal menu row. */
9
- destructive?: boolean
10
- /** Greys the item; not selectable. */
11
- disabled?: boolean
12
- /** Render a divider before this item. */
13
- divider?: boolean
3
+ /** Stable id reported to `onItemSelected`; unique within this menu. */
4
+ id: string
5
+ /** Item display text. */
6
+ title: string
7
+ /** No AppKit destructive role — kept in the contract for parity; the
8
+ * item renders as a normal menu row. */
9
+ destructive?: boolean
10
+ /** Greys the item; not selectable. */
11
+ disabled?: boolean
12
+ /** Render a divider before this item. */
13
+ divider?: boolean
14
14
  }
15
15
 
16
16
  /** macOS context-menu props (NSMenu). */
17
17
  export interface AppKitContextMenuProps {
18
- /** Native view identifier. Changing it retires this record's menu session. */
19
- id?: string
20
- /** Class list applied to the element. */
21
- className?: any
22
- /** Inline style (string CSS or object). */
23
- style?: any
24
- /** Menu entries in display order. */
25
- items?: readonly AppKitContextMenuItem[]
26
- /** Render fn producing the trigger element — rendered inline; the NSMenu
27
- * attaches to the host's backing NSView. */
28
- trigger: () => any
29
- /** Current enabled item selections; retired/replaced menu actions are ignored. */
30
- onItemSelected?: (id: string) => void
31
- /** Spoken name for assistive tech. */
32
- accessibilityLabel?: string
18
+ /** Native view identifier. Changing it retires this record's menu session. */
19
+ id?: string
20
+ /** Class list applied to the element. */
21
+ className?: any
22
+ /** Inline style (string CSS or object). */
23
+ style?: any
24
+ /** Menu entries in display order. */
25
+ items?: readonly AppKitContextMenuItem[]
26
+ /** Render fn producing the trigger element — rendered inline; the NSMenu
27
+ * attaches to the host's backing NSView. */
28
+ trigger: () => any
29
+ /** Current enabled item selections; retired/replaced menu actions are ignored. */
30
+ onItemSelected?: (id: string) => void
31
+ /** Spoken name for assistive tech. */
32
+ accessibilityLabel?: string
33
33
  }
package/src/web/types.ts CHANGED
@@ -3,34 +3,34 @@ export type ContextMenuActivation = 'longPress' | 'singlePress'
3
3
 
4
4
  /** One menu entry or separator. */
5
5
  export interface ContextMenuItem {
6
- /** Element id passed through to the host element. */
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 as a separator instead of an item. */
15
- divider?: boolean
6
+ /** Element id passed through to the host element. */
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 as a separator instead of an item. */
15
+ divider?: boolean
16
16
  }
17
17
 
18
18
  /** Web context-menu props (composed web menu). */
19
19
  export interface ContextMenuProps {
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
- /** 'longPress' maps to right-click (contextmenu); 'singlePress' to click. */
27
- activation?: ContextMenuActivation
28
- /** Menu entries in display order. */
29
- items?: readonly ContextMenuItem[]
30
- /** Content that hosts the context menu (long-press/right-click). */
31
- trigger: () => any
32
- /** Fires with the selected item id. */
33
- onItemSelected?: (id: string) => void
34
- /** Spoken name for assistive tech. */
35
- 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
+ /** 'longPress' maps to right-click (contextmenu); 'singlePress' to click. */
27
+ activation?: ContextMenuActivation
28
+ /** Menu entries in display order. */
29
+ items?: readonly ContextMenuItem[]
30
+ /** Content that hosts the context menu (long-press/right-click). */
31
+ trigger: () => any
32
+ /** Fires with the selected item id. */
33
+ onItemSelected?: (id: string) => void
34
+ /** Spoken name for assistive tech. */
35
+ accessibilityLabel?: string
36
36
  }