Code Monkey home page Code Monkey logo

Comments (3)

segunadebayo avatar segunadebayo commented on June 23, 2024

This is by design. The Menu uses virtual focus to navigate the items. Meaning the focus remains on the menu content all the time.

When an is selected, the onSelect is the best way to listen for selection. See the docs here
https://zagjs.com/components/react/menu#listening-for-item-selection

Links are a special case because they need to trigger navigation, hence the reason theyโ€™re manually clicked.

Use the merge props helper to combine the props of the content with your own custom handler

mergeProps(api.contentProps, { onKeyDown: โ€ฆ})

from zag.

erm1116 avatar erm1116 commented on June 23, 2024

@segunadebayo
I know that by passing onSelect to Menu, I can detect the user's selection.
However, this causes the processing to become redundant when handling each item is selected.
For example, let's assume there is a component like the following. Here, you want to call the API when either edit or delete is selected.

import * as menu from "@zag-js/menu"
import { useMachine, normalizeProps } from "@zag-js/react"
import { useId } from "react"

export function Menu() {
  const [state, send] = useMachine(menu.machine({ id: useId() }))

  const api = menu.connect(state, send, normalizeProps)

  return (
    <div>
      <button {...api.triggerProps}>
        Actions <span {...api.indicatorProps}>โ–พ</span>
      </button>
      <div {...api.positionerProps}>
        <ul {...api.contentProps}>
          <li {...api.getItemProps({ value: "edit" })}>Edit</li>
          <li {...api.getItemProps({ value: "delete" })}>Delete</li>
        </ul>
      </div>
    </div>
  )
}

For this, it is redundant to describe the processing with onSelect.

import * as menu from "@zag-js/menu"
import { useMachine, normalizeProps } from "@zag-js/react"
import { useId } from "react"

export function Menu() {
  const [state, send] = useMachine(menu.machine({ 
    id: useId(),
    onSelect(details) {
      switch(details.value) {
        case "edit":
          callEditAPI()
        case "delete":
          callDeleteAPI()
        // If there are more menu items, this would continue
      }
    }
  }))
  ...
}

So I think we should be able to pass props like onSelectItem to Item.
(Just like the Radix UI MenuItem's onSelect)

export function Menu() {
  ...
  return (
    <div>
      ...
      <div {...api.positionerProps}>
        <ul {...api.contentProps}>
          <li {...api.getItemProps({ value: "edit", onSelectItem: () => callEditAPI() })}>Edit</li>
          <li {...api.getItemProps({ value: "delete", onSelectItem: () => callDeleteAPI() })}>Delete</li>
        </ul>
      </div>
    </div>
  )
}

How do you think this??

from zag.

erm1116 avatar erm1116 commented on June 23, 2024

Perhaps I should have created the issue as a Feature Request instead of a Bug reporting

from zag.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.