The Link component allows you to show links on the page, open them in a new window, and change the color.
Props
Usage guidelines
- For navigation within or directly accompanying a sentence.
- Directing users to another page or a different portion of the same page.
- Performing actions, such as "Save", "Cancel" or "Delete". Use Button instead.
- Submitting a form or opening a modal. Use Button instead.
Example
You should wrap Link
components inside of a Text
component to get the correct font & underline color.
Accessible Content
When providing the content for the link, avoid phrases like "click here" or "go to".
Permutations: tapStyle and hoverStyle
Variants
This example illustrates two custom navigation implementations to externally control the link navigation behavior of Link: setting a default logic with OnLinkNavigationProvider and a custom component logic with the onClick
prop.
If onNavigation
prop is passed to OnLinkNavigationProvider, it's passed down to all children links and sets a custom default link navigation behavior. onLinkNavigation
is a higher-order function: it takes named arguments (href
and target
) and returns an event handler function. In the component's onClick
event handler, the onClick
prop gets called first, followed by the function passed down by the OnLinkNavigationProvider.
Link's onClick
prop is an event handler function. This callback takes 2 named parameters: 'event', the 'onClick' event, and disableOnNavigation
, a callback function that disables any default custom navigation logic set by OnLinkNavigationProvider. disableOnNavigation
is only accessible in OnLinkNavigationProvider's children components with link behavior.
In this example, Link has a parent OnLinkNavigationProvider setting a default custom navigation logic. We can compare three navigation behaviors: (a) the default Link behavior (by disabling the OnLinkNavigationProvider's inherited logic), (b) the default custom behavior set by OnLinkNavigationProvider and (c) a custom behavior set on Link.
onClick
is used to disable the default custom logic set in OnLinkNavigationProvider and implement custom behavior at the component level. disableOnNavigation
and customOnNavigation
are both called first during the 'onClick' event.
If onNavigation
is a custom hook function, it can contain complex logic, including React hooks, to perform side effects.
In this example, both onNavigation
and customOnNavigation
functions execute the following actions:
- Disable the default link behavior
- Show an alert message
- Open a different URL in a new window
Both onNavigationClick
, inside onNavigation
, and onClick
have event access to preventDefault(). It could also be used to stopPropagation().
Related
OnLinkNavigationProvider
OnLinkNavigationProvider allows external link navigation control across all children components with link behavior.
See custom navigation variant for examples.