Skip to content

Commit 87964a7

Browse files
committed
chore: add item indicator
1 parent b5b395c commit 87964a7

File tree

5 files changed

+69
-2
lines changed

5 files changed

+69
-2
lines changed
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
---
2+
"@zag-js/navigation-menu": minor
3+
---
4+
5+
Add `getItemIndicatorProps` part
6+
7+
```tsx
8+
<div {...api.getItemProps({ value: "products" })}>
9+
<button {...api.getTriggerProps({ value: "products" })}>Products</button>
10+
<div {...api.getItemIndicatorProps({ value: "products" })} />
11+
</div>
12+
```

packages/machines/navigation-menu/src/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ export type {
1111
ElementIds,
1212
ViewportProps,
1313
ItemProps,
14+
ItemState,
1415
LinkProps,
1516
ValueChangeDetails,
1617
} from "./navigation-menu.types"

packages/machines/navigation-menu/src/navigation-menu.anatomy.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ export const anatomy = createAnatomy("navigation-menu").parts(
1111
"link",
1212
"indicator",
1313
"indicatorTrack",
14+
"itemIndicator",
1415
"arrow",
1516
)
1617

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

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ import type { NormalizeProps, PropTypes } from "@zag-js/types"
1111
import { toPx } from "@zag-js/utils"
1212
import { parts } from "./navigation-menu.anatomy"
1313
import * as dom from "./navigation-menu.dom"
14-
import type { ItemProps, NavigationMenuApi, NavigationMenuService } from "./navigation-menu.types"
14+
import type { ItemProps, ItemState, NavigationMenuApi, NavigationMenuService } from "./navigation-menu.types"
1515

1616
export function connect<T extends PropTypes>(
1717
service: NavigationMenuService,
@@ -31,7 +31,7 @@ export function connect<T extends PropTypes>(
3131
const isViewportRendered = context.get("isViewportRendered")
3232
const preventTransition = value && !previousValue
3333

34-
function getItemState(props: ItemProps) {
34+
function getItemState(props: ItemProps): ItemState {
3535
const selected = value === props.value
3636
const wasSelected = !value && previousValue === props.value
3737
return {
@@ -420,5 +420,20 @@ export function connect<T extends PropTypes>(
420420
},
421421
})
422422
},
423+
424+
getItemState,
425+
426+
getItemIndicatorProps(props) {
427+
const itemState = getItemState(props)
428+
return normalize.element({
429+
...parts.itemIndicator.attrs,
430+
"aria-hidden": true,
431+
dir: prop("dir"),
432+
hidden: !itemState.selected,
433+
"data-state": itemState.selected ? "open" : "closed",
434+
"data-orientation": prop("orientation"),
435+
"data-value": props.value,
436+
})
437+
},
423438
}
424439
}

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

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -129,6 +129,41 @@ export interface ItemProps {
129129
disabled?: boolean | undefined
130130
}
131131

132+
export interface ItemState {
133+
/**
134+
* The id of the item element
135+
*/
136+
itemId: string
137+
/**
138+
* The id of the trigger element
139+
*/
140+
triggerId: string
141+
/**
142+
* The id of the trigger proxy element
143+
*/
144+
triggerProxyId: string
145+
/**
146+
* The id of the content element
147+
*/
148+
contentId: string
149+
/**
150+
* Whether the item is currently selected
151+
*/
152+
selected: boolean
153+
/**
154+
* Whether the item was previously selected (for animation)
155+
*/
156+
wasSelected: boolean
157+
/**
158+
* Whether the item's content is open
159+
*/
160+
open: boolean
161+
/**
162+
* Whether the item is disabled
163+
*/
164+
disabled: boolean
165+
}
166+
132167
export interface ArrowProps {
133168
/**
134169
* The value of the item
@@ -190,6 +225,7 @@ export interface NavigationMenuApi<T extends PropTypes = PropTypes> {
190225
getItemProps: (props: ItemProps) => T["element"]
191226
getIndicatorTrackProps: () => T["element"]
192227
getIndicatorProps: () => T["element"]
228+
getItemIndicatorProps: (props: ItemProps) => T["element"]
193229
getArrowProps: (props?: ArrowProps) => T["element"]
194230

195231
getTriggerProps: (props: ItemProps) => T["button"]
@@ -200,4 +236,6 @@ export interface NavigationMenuApi<T extends PropTypes = PropTypes> {
200236
getContentProps: (props: LinkProps) => T["element"]
201237
getViewportPositionerProps: (props?: ViewportProps) => T["element"]
202238
getViewportProps: (props?: ViewportProps) => T["element"]
239+
240+
getItemState: (props: ItemProps) => ItemState
203241
}

0 commit comments

Comments
 (0)