Skip to content

Commit a988c44

Browse files
authored
fix(menu): make child and parent menu service types more precise (#2696)
* fix(menu): make child and parent menu service typing stricter * docs(changeset): make child and parent menu service types more precise
1 parent 70143c1 commit a988c44

3 files changed

Lines changed: 22 additions & 8 deletions

File tree

.changeset/silver-stars-cross.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"@zag-js/menu": patch
3+
---
4+
5+
Make child and parent menu service types more precise

packages/machines/menu/src/menu.machine.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { createGuards, createMachine, type Scope, type Service } from "@zag-js/core"
1+
import { createGuards, createMachine, type Scope } from "@zag-js/core"
22
import { trackDismissableElement } from "@zag-js/dismissable"
33
import {
44
addDomEvent,
@@ -16,7 +16,7 @@ import { getPlacement, getPlacementSide, type Placement } from "@zag-js/popper"
1616
import { getElementPolygon, isPointInPolygon, type Point } from "@zag-js/rect-utils"
1717
import { isEqual } from "@zag-js/utils"
1818
import * as dom from "./menu.dom"
19-
import type { MenuSchema, MenuService } from "./menu.types"
19+
import type { ChildMenuService, MenuSchema, ParentMenuService } from "./menu.types"
2020

2121
const { not, and, or } = createGuards<MenuSchema>()
2222

@@ -838,7 +838,7 @@ export const machine = createMachine<MenuSchema>({
838838
},
839839
})
840840

841-
function closeRootMenu(ctx: { parent: Service<MenuSchema> | null }) {
841+
function closeRootMenu(ctx: { parent: ParentMenuService | null }) {
842842
let parent = ctx.parent
843843
while (parent && parent.context.get("isSubmenu")) {
844844
parent = parent.refs.get("parent")
@@ -851,7 +851,7 @@ function isWithinPolygon(polygon: Point[] | null, point: Point) {
851851
return isPointInPolygon(polygon, point)
852852
}
853853

854-
function resolveItemId(children: Record<string, MenuService>, value: string | null, scope: Scope) {
854+
function resolveItemId(children: Record<string, ChildMenuService>, value: string | null, scope: Scope) {
855855
const hasChildren = Object.keys(children).length > 0
856856
if (!value) return null
857857
if (!hasChildren) {

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

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -142,8 +142,8 @@ export interface MenuSchema {
142142
highlightedId: string | null
143143
}
144144
refs: {
145-
parent: Service<MenuSchema> | null
146-
children: Record<string, Service<MenuSchema>>
145+
parent: ParentMenuService | null
146+
children: Record<string, ChildMenuService>
147147
typeaheadState: TypeaheadState
148148
positioningOverride: Partial<PositioningOptions>
149149
}
@@ -162,6 +162,15 @@ export type MenuService = Service<MenuSchema>
162162

163163
export type MenuMachine = Machine<MenuSchema>
164164

165+
/**
166+
* Minimal interface required for child menu relationship.
167+
*/
168+
export type ChildMenuService = Pick<MenuService, "prop" | "send" | "scope">
169+
/**
170+
* Minimal interface required for parent menu relationship.
171+
*/
172+
export type ParentMenuService = Pick<MenuService, "prop" | "send" | "refs" | "context">
173+
165174
/* -----------------------------------------------------------------------------
166175
* Component API
167176
* -----------------------------------------------------------------------------*/
@@ -284,11 +293,11 @@ export interface MenuApi<T extends PropTypes = PropTypes> {
284293
/**
285294
* Function to register a parent menu. This is used for submenus
286295
*/
287-
setParent: (parent: MenuService) => void
296+
setParent: (parent: ParentMenuService) => void
288297
/**
289298
* Function to register a child menu. This is used for submenus
290299
*/
291-
setChild: (child: MenuService) => void
300+
setChild: (child: ChildMenuService) => void
292301
/**
293302
* Function to reposition the popover
294303
*/

0 commit comments

Comments
 (0)