-
Notifications
You must be signed in to change notification settings - Fork 72
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
feat: introduce as
prop for Hyperlink
#3082
Merged
adamstankiewicz
merged 1 commit into
release-22.x
from
ags/hyperlink-react-router-support
Jan 31, 2025
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,15 +1,19 @@ | ||
import React from 'react'; | ||
import React, { forwardRef } from 'react'; | ||
import PropTypes from 'prop-types'; | ||
import classNames from 'classnames'; | ||
import { | ||
type BsPrefixRefForwardingComponent as ComponentWithAsProp, | ||
type BsPrefixProps, | ||
} from 'react-bootstrap/esm/helpers'; | ||
import { defineMessages, useIntl } from 'react-intl'; | ||
import { Launch } from '../../icons'; | ||
import Icon from '../Icon'; | ||
// @ts-ignore | ||
import { customPropTypeRequirement } from '../utils/propTypes/utils'; | ||
|
||
export const HYPER_LINK_EXTERNAL_LINK_ALT_TEXT = 'in a new tab'; | ||
export const HYPER_LINK_EXTERNAL_LINK_TITLE = 'Opens in a new tab'; | ||
|
||
interface Props extends Omit<React.ComponentPropsWithRef<'a'>, 'href' | 'target'> { | ||
export interface HyperlinkProps extends BsPrefixProps, Omit<React.ComponentPropsWithRef<'a'>, 'href' | 'target'> { | ||
/** specifies the URL */ | ||
destination: string; | ||
destination?: string; | ||
/** Content of the hyperlink */ | ||
children: React.ReactNode; | ||
/** Custom class names for the hyperlink */ | ||
|
@@ -24,22 +28,42 @@ interface Props extends Omit<React.ComponentPropsWithRef<'a'>, 'href' | 'target' | |
isInline?: boolean; | ||
/** specify if we need to show launch Icon. By default, it will be visible. */ | ||
showLaunchIcon?: boolean; | ||
/** specifies where the link should open. The default behavior is `_self`, which means that the URL will be | ||
* loaded into the same browsing context as the current one. | ||
* If the target is `_blank` (opening a new window) `rel='noopener'` will be added to the anchor tag to prevent | ||
* any potential [reverse tabnabbing attack](https://www.owasp.org/index.php/Reverse_Tabnabbing). | ||
*/ | ||
target?: '_blank' | '_self'; | ||
} | ||
|
||
const Hyperlink = React.forwardRef<HTMLAnchorElement, Props>(({ | ||
export type HyperlinkType = ComponentWithAsProp<'a', HyperlinkProps>; | ||
|
||
const messages = defineMessages({ | ||
externalLinkAltText: { | ||
id: 'Hyperlink.externalLinkAltText', | ||
defaultMessage: 'in a new tab', | ||
}, | ||
externalLinkTitle: { | ||
id: 'Hyperlink.externalLinkTitle', | ||
defaultMessage: 'Opens in a new tab', | ||
}, | ||
}); | ||
|
||
const Hyperlink = forwardRef<HTMLAnchorElement, HyperlinkProps>(({ | ||
as: Component = 'a', | ||
className, | ||
destination, | ||
children, | ||
target, | ||
target = '_self', | ||
onClick, | ||
externalLinkAlternativeText, | ||
externalLinkTitle, | ||
variant, | ||
isInline, | ||
showLaunchIcon, | ||
variant = 'default', | ||
isInline = false, | ||
showLaunchIcon = true, | ||
...attrs | ||
}, ref) => { | ||
const intl = useIntl(); | ||
let externalLinkIcon; | ||
|
||
if (target === '_blank') { | ||
|
@@ -63,11 +87,11 @@ const Hyperlink = React.forwardRef<HTMLAnchorElement, Props>(({ | |
externalLinkIcon = ( | ||
<span | ||
className="pgn__hyperlink__external-icon" | ||
title={externalLinkTitle} | ||
title={externalLinkTitle || intl.formatMessage(messages.externalLinkTitle)} | ||
> | ||
<Icon | ||
src={Launch} | ||
screenReaderText={externalLinkAlternativeText} | ||
screenReaderText={externalLinkAlternativeText || intl.formatMessage(messages.externalLinkAltText)} | ||
style={{ height: '1em', width: '1em' }} | ||
data-testid="hyperlink-icon" | ||
/> | ||
|
@@ -76,8 +100,13 @@ const Hyperlink = React.forwardRef<HTMLAnchorElement, Props>(({ | |
} | ||
} | ||
|
||
const additionalProps: Record<string, any> = { ...attrs }; | ||
if (destination) { | ||
additionalProps.href = destination; | ||
} | ||
|
||
return ( | ||
<a | ||
<Component | ||
ref={ref} | ||
className={classNames( | ||
'pgn__hyperlink', | ||
|
@@ -88,31 +117,27 @@ const Hyperlink = React.forwardRef<HTMLAnchorElement, Props>(({ | |
}, | ||
className, | ||
)} | ||
href={destination} | ||
target={target} | ||
onClick={onClick} | ||
{...attrs} | ||
{...additionalProps} | ||
> | ||
{children} | ||
{externalLinkIcon} | ||
</a> | ||
</Component> | ||
); | ||
}); | ||
|
||
Hyperlink.defaultProps = { | ||
className: undefined, | ||
target: '_self', | ||
onClick: () => {}, | ||
externalLinkAlternativeText: HYPER_LINK_EXTERNAL_LINK_ALT_TEXT, | ||
externalLinkTitle: HYPER_LINK_EXTERNAL_LINK_TITLE, | ||
variant: 'default', | ||
isInline: false, | ||
showLaunchIcon: true, | ||
}; | ||
|
||
Hyperlink.propTypes = { | ||
/** specifies the URL */ | ||
destination: PropTypes.string.isRequired, | ||
/** specifies the component element type to render for the hyperlink */ | ||
// @ts-ignore | ||
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. [inform] It's unclear why TS throws an error on the |
||
as: PropTypes.elementType, | ||
/** specifies the URL; required iff `as` prop is a standard anchor tag */ | ||
destination: customPropTypeRequirement( | ||
PropTypes.string, | ||
({ as }: { as: React.ElementType }) => as && as === 'a', | ||
// "[`destination` is required when]..." | ||
'the `as` prop is a standard anchor element (i.e., "a")', | ||
), | ||
/** Content of the hyperlink */ | ||
// @ts-ignore | ||
children: PropTypes.node.isRequired, | ||
|
@@ -138,4 +163,19 @@ Hyperlink.propTypes = { | |
showLaunchIcon: PropTypes.bool, | ||
}; | ||
|
||
Hyperlink.defaultProps = { | ||
as: 'a', | ||
className: undefined, | ||
destination: undefined, | ||
externalLinkAlternativeText: undefined, | ||
externalLinkTitle: undefined, | ||
isInline: false, | ||
onClick: undefined, | ||
showLaunchIcon: true, | ||
target: '_self', | ||
variant: 'default', | ||
}; | ||
|
||
Hyperlink.displayName = 'Hyperlink'; | ||
|
||
export default Hyperlink; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
It looks like
onClick
is still something we have an example for on the docs site https://deploy-preview-3082--paragon-openedx.netlify.app/components/hyperlink/#with-onclickI think it might be worth keeping this test to cover the off chance someone accidentally removes
onClick={onClick}
from theHyperlink
component.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.
I removed this test case, as its behavior is technically already covered within the "renders Hyperlink" test case, i.e.: