Repository navigation
feat: support target and rel on LinkMenuItem - #354
jacobo-dominguez-wgu wants to merge 1 commit into
Conversation
|
Thanks for the pull request, @jacobo-dominguez-wgu! This repository is currently maintained by Once you've gone through the following steps feel free to tag them in a comment and let them know that your changes are ready for engineering review. 🔘 Get product approvalIf you haven't already, check this list to see if your contribution needs to go through the product review process.
🔘 Provide contextTo help your reviewers and other members of the community understand the purpose and larger context of your changes, feel free to add as much of the following information to the PR description as you can:
🔘 Get a green buildIf one or more checks are failing, continue working on your changes until this is no longer the case and your build turns green. DetailsWhere can I find more information?If you'd like to get more details on all aspects of the review process for open source pull requests (OSPRs), check out the following resources: When can I expect my changes to be merged?Our goal is to get community contributions seen and reviewed as efficiently as possible. However, the amount of time that it takes to review and merge a PR can vary significantly based on factors such as:
💡 As a result it may take up to several weeks or months to complete a review and merge your PR. |
| url?: string; | ||
| variant?: 'hyperlink' | 'navLink' | 'navDropdownItem' | 'dropdownItem'; | ||
| /** Where to open the link, such as `_blank` for a new tab. */ | ||
| target?: React.HTMLAttributeAnchorTarget; |
There was a problem hiding this comment.
I'd add screen-reader-only "opens in a new tab" text to the other three variants when target is _blank, with a new message in Shell.messages.ts.
Right now only hyperlink warns screen reader users, and the issue's own case is a dropdown item, which stays silent. shell/menus/LinkMenuItem.tsx:61-78
| // Default rel for new tabs to protect against reverse tabnabbing: without noopener, the | ||
| // opened page gets a window.opener handle and can redirect this one. | ||
| rel: rel ?? (target === '_blank' ? 'noopener noreferrer' : undefined), |
There was a problem hiding this comment.
I'd drop the default rel and pass through whatever the caller gives.
Supported browsers already treat _blank as noopener, so the default only adds noreferrer, which hides where the user came from, even on the same site. If parity with the header PR matters more, at least fix the comment.
| <Hyperlink {...linkProps}> | ||
| // Paragon types Hyperlink's target as '_blank' | '_self', but it passes any value through to | ||
| // the anchor. | ||
| <Hyperlink {...linkProps} target={target as ComponentProps<typeof Hyperlink>['target']}> |
There was a problem hiding this comment.
I'd move the cast into the shared link props and drop the extra prop on Hyperlink.
The extra prop overrides the spread, so a later change to how target is set would quietly skip this variant.
Description
Adds optional
targetandrelprops toLinkMenuItemso shell menu items can open in a new tab or a named window. This brings frontend-base up to the same behavior as the Studio header menu items in frontend-component-header openedx/frontend-component-header#687. Since that repo is in maintenance mode, the feature has to land here first.Part of: openedx/frontend-app-authoring#3052
Changes
target?: React.HTMLAttributeAnchorTargetandrel?: stringtoLinkMenuItemProps, each with a doc comment.relthe same way frontend-component-header does:rel ?? (target === '_blank' ? 'noopener noreferrer' : undefined). A comment explains that the default protects against reverse tabnabbing.target: neither attribute is rendered.target="_blank"withoutrel:rel="noopener noreferrer"is rendered.rel: used unchanged.target(e.g.studio-help): passed through, with norel.{ target, rel }aftergetLinkProps(finalUrl)for all four variants (hyperlink,navLink,navDropdownItem,dropdownItem).getLinkPropsis unchanged.Testing
npx jest shell/menus/LinkMenuItem.test.tsx: 31 of 31 pass.getByRole('link', { name }).role: accountRole) withtarget="_blank". This confirms that react-router'sLinkforwards both attributes to the<a>.eslintis clean on the changed files.npm run typecheckreports no errors in the changed files.Notes for reviewers
The
hyperlinkvariant differs from the header in three ways, all from Paragon'sHyperlink(v23.18.1):target="_self"when no target is set. This is already the case onmain._blanklinks, it addsnoopener noreferrerto any explicitrel, sorel="noopener"becomesnoopener noreferrer._blanklinks, it adds a new-tab icon and "in a new tab" screen-reader text to the link's accessible name.These are as safe or safer than the header's behavior. The tests record them in a per-variant
expectedtable.Type cast: Paragon types
Hyperlink'stargetas'_blank' | '_self', but any value reaches the anchor at runtime. TheHyperlinkcall site caststarget, with a comment explaining why.Compatibility: the change is additive and non-breaking under ADR 0018 (
docs/decisions/0018-app-breaking-changes.rst). Both props are optional, and callers that don't pass them render as before.Unchanged callers:
ProfileLinkMenuItemandHelpButtondon't forward or set the new props. Their behavior is unchanged.