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(AppMenuItem): Make app menu external links open in new browser tab. #1022

Merged
merged 2 commits into from
Oct 6, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion lib/components/app/app-menu-item.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,9 @@ export default class AppMenuItem extends Component<Props, State> {
render(): JSX.Element {
const { icon, id, onClick, subItems, text, ...otherProps } = this.props
const { isExpanded } = this.state
const Element = otherProps.href ? 'a' : 'button'
const hasHref = !!otherProps.href
const isAbsolute = otherProps.href?.startsWith('http')
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If a url gets passed without an http should it still open a new browser tab?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Then it would be a relative URL (like the route viewer or another resource), but that's a good question.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Right now, relative URLs open in the same window per PR description.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good! One suggestion, but not needed:

Suggested change
const isAbsolute = otherProps.href?.startsWith('http')
const isAbsolute = hasHref && otherProps.href?.startsWith('http')

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It is not needed. That condition would be redundant, as otherProps.href?.startsWith(...) is null if otherProps.href is null, and can't be non-null unless otherProps.hrefis non null. Same for your other suggestion.

const Element = hasHref ? 'a' : 'button'
const containerId = `${id}-container`
return (
<>
Expand All @@ -80,6 +82,7 @@ export default class AppMenuItem extends Component<Props, State> {
onClick={subItems ? this._toggleSubmenu : onClick}
onKeyDown={this._handleKeyDown}
{...otherProps}
target={hasHref && isAbsolute ? '_blank' : undefined}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
target={hasHref && isAbsolute ? '_blank' : undefined}
target={isAbsolute ? '_blank' : undefined}

>
<span aria-hidden>{icon}</span>
<span>{text}</span>
Expand Down
Loading