Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: modify incorrect selected style in commandItem #6267

Open
wants to merge 4 commits into
base: main
Choose a base branch
from

Conversation

usernamebuzz
Copy link

Description

Changes

  • Fixed the issue #6129 where the first item was automatically selected when opening the Command menu
  • Improved the visual feedback for selected and hovered items
  • Implemented a more intuitive hover behavior using CSS-only solution

Technical Details

  1. Added data-selected={false} to CommandItem to prevent automatic selection behavior from cmdk library
  2. Implemented group hover pattern using Tailwind's named groups (group/cmd)
  3. Modified CommandItem styling to:
    /* Base selected state */
    .selected { background-color: accent; }
    
    /* Remove background when group is hovered */
    .group-hover:bg-transparent
    
    /* Force background on direct hover */
    .hover:!bg-accent

The solution uses pure CSS instead of React state management for better performance and simpler implementation.

Before

  • First item was automatically selected on menu open
  • Selected item's background persisted even when hovering other items
  • Inconsistent visual feedback between selected and hovered states

After

  • No automatic selection on menu open
  • Selected item's background disappears when hovering the menu
  • Clear visual distinction between selected and hovered states
  • Smooth transition between states
스크린샷 2025-01-04 오후 5 06 04

Implementation Notes

  • Used Tailwind's named group feature (group/cmd) to prevent potential group styling conflicts
  • Leveraged the ! modifier in Tailwind to ensure hover styles take precedence
  • Maintained existing accessibility features and keyboard navigation

Testing Steps

  1. Open the Command menu
    • Verify no item is automatically selected
  2. Select an item
    • Confirm the background color is applied
  3. Hover over the menu
    • Check if the selected item's background disappears
  4. Hover over different items
    • Verify only the hovered item shows background color
  5. Move mouse out of the menu
    • Confirm the selected item's background reappears

Copy link

vercel bot commented Jan 4, 2025

@usernamebuzz is attempting to deploy a commit to the shadcn-pro Team on Vercel.

A member of the Team first needs to authorize it.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant