Replies: 1 comment
-
|
Thanks for taking the time to create this detailed issue. IMHO I think the proposed solution might be too narrow and could fall short in practice. For example:
I'm not making decisions for Zag.js, but from an API design perspective, I think it would be better to delegate this logic to userland. Something like: const service = useMachine(menu.machine, {
// Option 1: Return filtered/weighted results
onQuery: (items: MenuItem[], searchString: string) => MenuItem[]
// Option 2: Custom match function (same as current internal logic)
onMatch: (valueText: string, query: string) => boolean
})Option 1 would allow you to implement sophisticated filtering with weighted results, fuzzy matching, etc. Option 2 keeps it simple and just lets you override the matching logic. Either way, the goal would be to provide flexibility without baking specific assumptions into the core library. This would support your language switcher use case while also handling hyphenated terms, fuzzy search, and any other custom matching logic developers might need. |
Beta Was this translation helpful? Give feedback.
Uh oh!
There was an error while loading. Please reload this page.
-
Feature Request: Support Word-Boundary Matching in Menu Typeahead Search
🎯 Current Behavior
The typeahead search feature in the Menu component currently only matches characters at the beginning of the entire
string. It doesn't support matching at word boundaries within the search text.
Example
Given these menu items:
Current behavior:
c✅ matches "Chinese"z❌ doesn't match "Zhongwen" (even though it exists in the text)n❌ doesn't match "Nihongo"e❌ doesn't match "Español"d❌ doesn't match "Deutsch"💡 Use Case
This limitation creates poor UX for international/multi-language applications, where users naturally expect to
search using their native language terminology.
Real-World Scenario: Language Switcher
In a language selection menu, users from different regions have different mental models:
cORzjORnkORhsORegORdUser expectation:
z(for Zhongwen/拼音)n(for Nihongo/ローマ字)e(for Español)d(for Deutsch)Current limitation: Only the first letter of the entire string works, forcing us to always put the English name
first, which feels unnatural for non-English speakers.
✅ Proposed Solution
Enhance the
matchfunction inpackages/utilities/dom-query/src/searchable.tsto support word-boundary matching:Current Implementation
Proposed Implementation
How It Works
Given
data-valuetext="Chinese Zhongwen":["Chinese", "Zhongwen"]c→ matches "Chinese" ✅z→ matches "Zhongwen" ✅zh→ matches "Zhongwen" ✅chi→ matches "Chinese" ✅🎨 Configuration Example
With this change, developers can configure menu items like this:
✨ Benefits
🔍 Technical Details
Affected File
packages/utilities/dom-query/src/searchable.ts- Thematchfunction around line 28Build Output
packages/utilities/dom-query/dist/index.js(CommonJS)packages/utilities/dom-query/dist/index.mjs(ESM)Testing Considerations
data-valuetextattributes📊 Impact Assessment
Who Benefits
Who Is Affected
data-valuetextwill see new behavior🚀 Demo
I've implemented and tested this solution in my local fork with a working language switcher that includes 11 languages
(English, Chinese, Japanese, Korean, Spanish, German, French, Portuguese, Italian, Dutch, and Polish). The word-boundary
matching works seamlessly across all these languages.
💪 Contribution
I'm happy to contribute a PR if this feature aligns with the project's goals.
Thank you for considering this enhancement! This would significantly improve UX for international users. 🌍
Beta Was this translation helpful? Give feedback.
All reactions