Skip to content

Commit 0685411

Browse files
committed
fix(menu): avoid mixing framework agnostic and specific prop handling
1 parent 012540b commit 0685411

File tree

3 files changed

+21
-10
lines changed

3 files changed

+21
-10
lines changed

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

Lines changed: 16 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import type { Service } from "@zag-js/core"
22
import { mergeProps } from "@zag-js/core"
3+
import { createNormalizer } from "@zag-js/types"
34
import {
45
ariaAttr,
56
dataAttr,
@@ -22,6 +23,9 @@ import { parts } from "./menu.anatomy"
2223
import * as dom from "./menu.dom"
2324
import type { ItemProps, ItemState, MenuApi, MenuSchema, OptionItemProps, OptionItemState } from "./menu.types"
2425

26+
// We need to avoid mixing framework-agnostic logic with framework-specific prop handling
27+
const identityProps = createNormalizer<PropTypes>((v) => v)
28+
2529
export function connect<T extends PropTypes>(service: Service<MenuSchema>, normalize: NormalizeProps<T>): MenuApi<T> {
2630
const { context, send, state, computed, prop, scope } = service
2731

@@ -61,11 +65,12 @@ export function connect<T extends PropTypes>(service: Service<MenuSchema>, norma
6165
}
6266
}
6367

64-
function getItemProps(props: ItemProps) {
68+
function getItemProps(props: ItemProps, normalized = true) {
6569
const { closeOnSelect, valueText, value } = props
6670
const itemState = getItemState(props)
6771
const id = dom.getItemId(scope, value)
68-
return normalize.element({
72+
73+
const itemProps = identityProps.element({
6974
...parts.item.attrs,
7075
id,
7176
role: "menuitem",
@@ -110,6 +115,8 @@ export function connect<T extends PropTypes>(service: Service<MenuSchema>, norma
110115
send({ type: "ITEM_CLICK", target, id, closeOnSelect })
111116
},
112117
})
118+
119+
return normalized ? normalize.element(itemProps) : itemProps
113120
}
114121

115122
return {
@@ -177,12 +184,13 @@ export function connect<T extends PropTypes>(service: Service<MenuSchema>, norma
177184
},
178185

179186
getTriggerItemProps(childApi) {
180-
const triggerProps = childApi.getTriggerProps()
181-
return mergeProps(getItemProps({ value: triggerProps.id }), triggerProps) as T["element"]
187+
const triggerProps = childApi.getTriggerProps(false)
188+
const itemProps = getItemProps({ value: triggerProps.id }, false)
189+
return normalize.element(mergeProps(itemProps, triggerProps))
182190
},
183191

184-
getTriggerProps() {
185-
return normalize.button({
192+
getTriggerProps(normalized = true) {
193+
const triggerProps = identityProps.button({
186194
...(isSubmenu ? parts.triggerItem.attrs : parts.trigger.attrs),
187195
"data-placement": context.get("currentPlacement"),
188196
type: "button",
@@ -256,6 +264,8 @@ export function connect<T extends PropTypes>(service: Service<MenuSchema>, norma
256264
}
257265
},
258266
})
267+
268+
return normalized ? normalize.button(triggerProps) : triggerProps
259269
},
260270

261271
getIndicatorProps() {

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

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -176,8 +176,8 @@ export type ParentMenuService = Pick<MenuService, "prop" | "send" | "refs" | "co
176176
* -----------------------------------------------------------------------------*/
177177

178178
export interface Api {
179-
getItemProps: (props: ItemProps) => Record<string, any>
180-
getTriggerProps: () => Record<string, any>
179+
getItemProps: (props: ItemProps, normalized?: boolean) => Record<string, any>
180+
getTriggerProps: (normalized?: boolean) => Record<string, any>
181181
}
182182

183183
export interface ItemProps {
@@ -317,14 +317,14 @@ export interface MenuApi<T extends PropTypes = PropTypes> {
317317

318318
getContextTriggerProps: () => T["element"]
319319
getTriggerItemProps: <A extends Api>(childApi: A) => T["element"]
320-
getTriggerProps: () => T["button"]
320+
getTriggerProps: (normalized?: boolean) => T["button"]
321321
getIndicatorProps: () => T["element"]
322322
getPositionerProps: () => T["element"]
323323
getArrowProps: () => T["element"]
324324
getArrowTipProps: () => T["element"]
325325
getContentProps: () => T["element"]
326326
getSeparatorProps: () => T["element"]
327-
getItemProps: (options: ItemProps) => T["element"]
327+
getItemProps: (options: ItemProps, normalized?: boolean) => T["element"]
328328
getOptionItemProps: (option: OptionItemProps) => T["element"]
329329
getItemIndicatorProps: (option: ItemBaseProps) => T["element"]
330330
getItemTextProps: (option: ItemBaseProps) => T["element"]

packages/machines/tour/src/tour.connect.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,7 @@ export function connect<T extends PropTypes>(service: TourService, normalize: No
7171
send({ type: "STEPS.SET", value: next, src: "removeStep" })
7272
},
7373
updateStep(id, stepOverrides) {
74+
// Should mergeProps here merge the effect functions? It does not, it takes the last it finds.
7475
const next = steps.map((step) => (step.id === id ? mergeProps(step, stepOverrides) : step))
7576
send({ type: "STEPS.SET", value: next, src: "updateStep" })
7677
},

0 commit comments

Comments
 (0)