Skip to content

Commit 1f3638a

Browse files
committed
fix(menu): avoid mixing framework agnostic and specific prop handling
1 parent 8f60309 commit 1f3638a

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,
@@ -23,6 +24,9 @@ import { parts } from "./menu.anatomy"
2324
import * as dom from "./menu.dom"
2425
import type { ItemProps, ItemState, MenuApi, MenuSchema, OptionItemProps, OptionItemState } from "./menu.types"
2526

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

@@ -62,11 +66,12 @@ export function connect<T extends PropTypes>(service: Service<MenuSchema>, norma
6266
}
6367
}
6468

65-
function getItemProps(props: ItemProps) {
69+
function getItemProps(props: ItemProps, normalized = true) {
6670
const { closeOnSelect, valueText, value } = props
6771
const itemState = getItemState(props)
6872
const id = dom.getItemId(scope, value)
69-
return normalize.element({
73+
74+
const itemProps = identityProps.element({
7075
...parts.item.attrs,
7176
id,
7277
role: "menuitem",
@@ -111,6 +116,8 @@ export function connect<T extends PropTypes>(service: Service<MenuSchema>, norma
111116
send({ type: "ITEM_CLICK", target, id, closeOnSelect })
112117
},
113118
})
119+
120+
return normalized ? normalize.element(itemProps) : itemProps
114121
}
115122

116123
return {
@@ -178,12 +185,13 @@ export function connect<T extends PropTypes>(service: Service<MenuSchema>, norma
178185
},
179186

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

185-
getTriggerProps() {
186-
return normalize.button({
193+
getTriggerProps(normalized = true) {
194+
const triggerProps = identityProps.button({
187195
...(isSubmenu ? parts.triggerItem.attrs : parts.trigger.attrs),
188196
"data-placement": context.get("currentPlacement"),
189197
type: "button",
@@ -257,6 +265,8 @@ export function connect<T extends PropTypes>(service: Service<MenuSchema>, norma
257265
}
258266
},
259267
})
268+
269+
return normalized ? normalize.button(triggerProps) : triggerProps
260270
},
261271

262272
getIndicatorProps() {

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

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -167,8 +167,8 @@ export type MenuMachine = Machine<MenuSchema>
167167
* -----------------------------------------------------------------------------*/
168168

169169
export interface Api {
170-
getItemProps: (props: ItemProps) => Record<string, any>
171-
getTriggerProps: () => Record<string, any>
170+
getItemProps: (props: ItemProps, normalized?: boolean) => Record<string, any>
171+
getTriggerProps: (normalized?: boolean) => Record<string, any>
172172
}
173173

174174
export interface ItemProps {
@@ -308,14 +308,14 @@ export interface MenuApi<T extends PropTypes = PropTypes> {
308308

309309
getContextTriggerProps: () => T["element"]
310310
getTriggerItemProps: <A extends Api>(childApi: A) => T["element"]
311-
getTriggerProps: () => T["button"]
311+
getTriggerProps: (normalized?: boolean) => T["button"]
312312
getIndicatorProps: () => T["element"]
313313
getPositionerProps: () => T["element"]
314314
getArrowProps: () => T["element"]
315315
getArrowTipProps: () => T["element"]
316316
getContentProps: () => T["element"]
317317
getSeparatorProps: () => T["element"]
318-
getItemProps: (options: ItemProps) => T["element"]
318+
getItemProps: (options: ItemProps, normalized?: boolean) => T["element"]
319319
getOptionItemProps: (option: OptionItemProps) => T["element"]
320320
getItemIndicatorProps: (option: ItemBaseProps) => T["element"]
321321
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)