-
Notifications
You must be signed in to change notification settings - Fork 15
fix(menu): correctly identify menu item link with same URL as web page #693
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
base: main
Are you sure you want to change the base?
Changes from 2 commits
a5c22a9
0a3cd4f
a2da6ed
82c5cbd
33dbe71
374bc72
0a234c9
56e5179
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -33,6 +33,15 @@ type MenuItemProps = { | |
isSelected?: boolean; | ||
}; | ||
|
||
/** | ||
* [1] For a disabled link to be valid, it must have: | ||
* - `aria-disabled="true"` | ||
* - `role="link"`, or `role="menuitem"` if within a menu | ||
* - an `undefined` `href` | ||
* | ||
* @example <a role="link" aria-disabled="true">Learn something!</a> | ||
* @see https://www.scottohara.me/blog/2021/05/28/disabled-links.html | ||
*/ | ||
const Item = ({ item, getItemProps, focusedValue, isSelected }: MenuItemProps) => { | ||
const itemProps = getItemProps({ item }); | ||
|
||
|
@@ -60,7 +69,14 @@ const Item = ({ item, getItemProps, focusedValue, isSelected }: MenuItemProps) = | |
{itemProps.href ? ( | ||
<a | ||
{...(itemProps as AnchorHTMLAttributes<HTMLAnchorElement>)} | ||
className="w-full rounded-sm outline-offset-0 transition-none border-width-none" | ||
href={item.disabled ? undefined : itemProps.href} | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This |
||
className={classNames( | ||
' w-full rounded-sm outline-offset-0 transition-none border-width-none', | ||
{ | ||
'text-grey-400': item.disabled, | ||
'cursor-default': item.disabled | ||
} | ||
)} | ||
> | ||
{itemChildren} | ||
{!!item.isExternal && ( | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -155,7 +155,7 @@ export const useMenu = <T extends HTMLElement = HTMLElement, M extends HTMLEleme | |
); | ||
|
||
const isItemSelected = useCallback( | ||
(value: string, type?: string, name?: string): boolean | undefined => { | ||
(value: string, type?: string, name?: string, href?: string): boolean | undefined => { | ||
let isSelected; | ||
|
||
if (type === 'checkbox') { | ||
|
@@ -164,6 +164,10 @@ export const useMenu = <T extends HTMLElement = HTMLElement, M extends HTMLEleme | |
const match = controlledSelectedItems.filter(item => item.name === name)[0]; | ||
|
||
isSelected = match?.value === value; | ||
} else if (href) { | ||
const current = controlledSelectedItems[0]; | ||
|
||
isSelected = current?.value === value; | ||
} | ||
|
||
return isSelected; | ||
|
@@ -218,10 +222,10 @@ export const useMenu = <T extends HTMLElement = HTMLElement, M extends HTMLEleme | |
); | ||
|
||
const getSelectedItems = useCallback( | ||
({ value, type, name, label, selected }: IMenuItemBase) => { | ||
({ value, type, name, label, selected, href }: IMenuItemBase) => { | ||
let changes: ISelectedItem[] | null = [...controlledSelectedItems]; | ||
|
||
if (!type) return null; | ||
if (!(type || href)) return null; | ||
|
||
const selectedItem = { | ||
value, | ||
|
@@ -236,7 +240,7 @@ export const useMenu = <T extends HTMLElement = HTMLElement, M extends HTMLEleme | |
} else { | ||
changes.push(selectedItem); | ||
} | ||
} else if (type === 'radio') { | ||
} else if (type === 'radio' || href) { | ||
const index = changes.findIndex(item => item.name === name); | ||
|
||
if (index > -1) { | ||
|
@@ -428,15 +432,17 @@ export const useMenu = <T extends HTMLElement = HTMLElement, M extends HTMLEleme | |
}, [onChange]); | ||
|
||
const handleItemClick = useCallback( | ||
(item: IMenuItemBase) => { | ||
(event: React.MouseEvent, item: IMenuItemBase) => { | ||
let changeType = StateChangeTypes.MenuItemClick; | ||
const { isNext, isPrevious } = item; | ||
const { isNext, isPrevious, href, selected } = item; | ||
const isTransitionItem = isNext || isPrevious; | ||
|
||
if (isNext) { | ||
changeType = StateChangeTypes.MenuItemClickNext; | ||
} else if (isPrevious) { | ||
changeType = StateChangeTypes.MenuItemClickPrevious; | ||
} else if (href && selected) { | ||
event.preventDefault(); | ||
} | ||
|
||
const nextSelection = getSelectedItems(item); | ||
|
@@ -815,7 +821,7 @@ export const useMenu = <T extends HTMLElement = HTMLElement, M extends HTMLEleme | |
itemRole = 'menuitemcheckbox'; | ||
} | ||
|
||
const selected = isItemSelected(value, type, name); | ||
const selected = isItemSelected(value, type, name, href); | ||
|
||
/** | ||
* The "select" of useSelection isn't | ||
|
@@ -829,27 +835,36 @@ export const useMenu = <T extends HTMLElement = HTMLElement, M extends HTMLEleme | |
'data-garden-container-id': 'containers.menu.item', | ||
'data-garden-container-version': PACKAGE_VERSION, | ||
'aria-selected': undefined, | ||
'aria-checked': selected, | ||
'aria-disabled': itemDisabled, | ||
role: itemRole === null ? undefined : itemRole, | ||
href, | ||
onClick, | ||
onKeyDown, | ||
onMouseEnter, | ||
...other | ||
}; | ||
|
||
if (href && isExternal) { | ||
elementProps.target = '_blank'; | ||
elementProps.rel = 'noopener noreferrer'; | ||
} | ||
if (href) { | ||
/** | ||
* Validation | ||
*/ | ||
if (isNext || isPrevious || type) { | ||
anchorItemError(item); | ||
} | ||
|
||
/** | ||
* Validation | ||
*/ | ||
elementProps.href = href; | ||
|
||
if (!itemDisabled) { | ||
if (isExternal) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Should probably be named |
||
elementProps.target = '_blank'; | ||
elementProps.rel = 'noopener noreferrer'; | ||
} | ||
|
||
if (href && (isNext || isPrevious || type)) { | ||
anchorItemError(item); | ||
if (selected) { | ||
elementProps['aria-current'] = 'page'; | ||
} | ||
} | ||
} else { | ||
elementProps['aria-checked'] = selected; | ||
} | ||
|
||
if (itemDisabled) { | ||
|
@@ -859,8 +874,8 @@ export const useMenu = <T extends HTMLElement = HTMLElement, M extends HTMLEleme | |
const itemProps = getElementProps({ | ||
value: value as any, | ||
...elementProps, | ||
onClick: composeEventHandlers(onClick, () => | ||
handleItemClick({ ...item, label, selected, isNext, isPrevious }) | ||
onClick: composeEventHandlers(onClick, (e: React.MouseEvent) => | ||
handleItemClick(e, { ...item, label, selected, isNext, isPrevious }) | ||
), | ||
onKeyDown: composeEventHandlers(onKeyDown, (e: React.KeyboardEvent) => | ||
handleItemKeyDown(e, { ...item, label, selected, isNext, isPrevious }) | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is there an item treatment that can be done for anchor items if
selected
is true? It would be nice to see a visual change here in the containers storybook (as opposed toreact-components
where we don't want any visual artifact).