Skip to content

Commit dd2ab25

Browse files
committed
chore: add listener fn to menu
1 parent e5ba28a commit dd2ab25

File tree

5 files changed

+32
-1
lines changed

5 files changed

+32
-1
lines changed

.changeset/cold-hornets-find.md

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"@zag-js/menu": patch
3+
---
4+
5+
Add `addItemListener` function to allow listening for dispatched custom event

packages/machines/menu/src/index.ts

+1
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ export type {
88
HighlightChangeDetails,
99
ItemGroupLabelProps,
1010
ItemGroupProps,
11+
ItemListenerProps,
1112
ItemProps,
1213
ItemState,
1314
MenuMachine as Machine,

packages/machines/menu/src/menu.connect.ts

+7
Original file line numberDiff line numberDiff line change
@@ -132,6 +132,13 @@ export function connect<T extends PropTypes>(service: Service<MenuSchema>, norma
132132
reposition(options = {}) {
133133
send({ type: "POSITIONING.SET", options })
134134
},
135+
addItemListener(props) {
136+
const node = dom.getItemEl(scope, props.id)
137+
if (!node) return
138+
const listener = () => props.onSelect?.()
139+
node.addEventListener(dom.itemSelectEvent, listener)
140+
return () => node.removeEventListener(dom.itemSelectEvent, listener)
141+
},
135142

136143
getContextTriggerProps() {
137144
return normalize.element({

packages/machines/menu/src/menu.dom.ts

+3-1
Original file line numberDiff line numberDiff line change
@@ -75,9 +75,11 @@ export const getOptionFromItemEl = (el: HTMLElement) => {
7575
}
7676
}
7777

78+
export const itemSelectEvent = "menu:select"
79+
7880
export function dispatchSelectionEvent(el: HTMLElement | null, value: string) {
7981
if (!el) return
8082
const win = getWindow(el)
81-
const event = new win.CustomEvent("menu:select", { detail: { value } })
83+
const event = new win.CustomEvent(itemSelectEvent, { detail: { value } })
8284
el.dispatchEvent(event)
8385
}

packages/machines/menu/src/menu.types.ts

+16
Original file line numberDiff line numberDiff line change
@@ -190,6 +190,17 @@ export interface ItemProps {
190190
closeOnSelect?: boolean | undefined
191191
}
192192

193+
export interface ItemListenerProps {
194+
/**
195+
* The id of the item. Can be obtained from the `getItemState` function.
196+
*/
197+
id: string
198+
/**
199+
* Function called when the item is selected
200+
*/
201+
onSelect?: VoidFunction
202+
}
203+
193204
export interface OptionItemProps extends Partial<ItemProps> {
194205
/**
195206
* Whether the option is checked
@@ -282,6 +293,11 @@ export interface MenuApi<T extends PropTypes = PropTypes> {
282293
* Returns the state of the menu item
283294
*/
284295
getItemState(props: ItemProps): ItemState
296+
/**
297+
* Setup the custom event listener for item selection event
298+
*/
299+
addItemListener(props: ItemListenerProps): VoidFunction | undefined
300+
285301
getContextTriggerProps(): T["element"]
286302
getTriggerItemProps<A extends Api>(childApi: A): T["element"]
287303
getTriggerProps(): T["button"]

0 commit comments

Comments
 (0)