Git Product home page Git Product logo

Comments (5)

rgossiaux avatar rgossiaux commented on July 23, 2024 1

You can have class be a function which takes active as an argument:

<MenuItem href="/" class={({active}) => active ? 'active-class' : ''}>Link</MenuItem>

This isn't well-documented in the React docs, but you can see a preview of some docs I'm writing here: https://github.com/rgossiaux/svelte-headlessui/blob/initial-docs/src/routes/docs/general/important-concepts.svx

from svelte-headlessui.

rgossiaux avatar rgossiaux commented on July 23, 2024

Thanks for the great report with a REPL!

The upstream library has this same behavior, actually: https://codesandbox.io/s/frosty-jepsen-c120o Given that, this is probably a wontfix, but I'm curious what your use case is, since the first example in your REPL seems to work fine. You can still render the MenuItems as ul and wrap the individual MenuItems in <li> to get the same markup.

The reason this doesn't work is because the arrow keys focus the MenuItem components themselves, so in your second example, the <a> tags aren't focused (because they're children).

from svelte-headlessui.

snake-345 avatar snake-345 commented on July 23, 2024

Well, my particular case is to use HeadlessUI components with DaisyUi appearance. I have been able to use dropdown and menu components from DaisyUi and everything working well except keyboard navigation.

<li>
  <MenuItem href="/">Link</MenuItem>
</li>

This will solve the problem with working links by keyboard navigation but I can't show the active state with this solution. Because let:active will be accessible only in child elements of <MenuItem>.

In react HeadlessUi I would be able to make it like that:

<li>
  <Menu.Item>
    {({ active }) => (
      <a href="/" class={active ? 'active-class' : ''}>Link</a>
    )
  </Menu.Item>
</li>

And it will work perfectly because react HeadlessUi uses child <a> as the main element of MenuItem. But in svelte HeadlessUi the same approach will lead to a not working link when you press Enter.

Any ideas on how I can have fully working keyboard navigation with the ability to set active classes in svelte HeadlessUi?

from svelte-headlessui.

snake-345 avatar snake-345 commented on July 23, 2024

Fully working Codesandbox with an example which I want to reach in svelte HeadlessUi. Actually, it seems impossible with the current implementation.

from svelte-headlessui.

snake-345 avatar snake-345 commented on July 23, 2024

Thank you very much. Glad that there option to set active classes right in MenuItem. Definitely would be helpful to have this feature documented. =D

from svelte-headlessui.

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.