Description
The Anchor, also knows as Link
is used to navigate from one page to the next HTML page.
import { Link, Anchor } from '@dnb/eufemia'render(<Anchor href="/uilib/components/anchor">Accessible text</Anchor>)
Combine a Link with an Anchor
You can combine a meta framework link, with the Anchor. This way, all the framework provided features will still work, as well as the behavior of the Eufemia Anchor.
import Anchor from '@dnb/eufemia/components/Anchor'import { Link } from 'gatsby'render(<App><Anchor element={Link} to="/path">Link</Anchor></App>,)
Anchor hash
Some browsers like Chrome (behind a flag) does still not support animated anchor hash clicks when CSS scroll-behavior: smooth;
is set. To make it work, you can provide the scrollToHashHandler
helper function to the Anchor:
import Anchor, {scrollToHashHandler,} from '@dnb/eufemia/components/Anchor'render(<App><Anchor href="/path#hash-id" onClick={scrollToHashHandler}>Link</Anchor><div id="hash-id">element to scroll to</div></App>,)
Blank target
NB: If you only use a vanilla HTML anchor element including target="_blank"
then you have to ensure you add a title
attribute that includes Opens a new Window
or as a part of the text:
<atitle="Opens a new Window"target="_blank"href="https://"class="dnb-anchor">text (opens in new window)</a>
Demos
Anchor states
<Example data-visual-test="anchor-default"> <Anchor href="/uilib/components/anchor">Default Style</Anchor> </Example> <Example> <Anchor href="/uilib/components/anchor" className="dnb-anchor--hover"> Hover Style </Anchor> </Example> <Example> <Anchor href="/uilib/components/anchor" className="dnb-anchor--active"> Active Style </Anchor> </Example> <Example> <Anchor href="/uilib/components/anchor" className="dnb-anchor--focus"> Focus Style </Anchor> </Example>
Additional Anchor helper classes
To force a specific state of style, use the following classes to do so:
<ContrastExample data-visual-test="anchor-contrast"> <Anchor href="/uilib/components/anchor" className="dnb-anchor--contrast" > Contrast Style </Anchor> </ContrastExample> <Example> <Anchor href="/uilib/components/anchor" className="dnb-anchor--no-underline" > No underline </Anchor> </Example> <Example> <Anchor target="_blank" href="/uilib/components/anchor" className="dnb-anchor--no-icon" > Blank target without launch icon </Anchor> </Example> <Example data-visual-test="anchor-no-hover"> <Anchor href="/uilib/components/anchor" className="dnb-anchor--no-hover" > No hover </Anchor> </Example> <Example> <Anchor href="/uilib/components/anchor" className="dnb-anchor--no-radius" > No border-radius </Anchor> </Example> <Example> <Anchor href="/uilib/components/anchor" className="dnb-anchor--no-animation" > No animation </Anchor> </Example> <Example data-visual-test="anchor-no-style"> <Anchor href="/uilib/components/anchor" className="dnb-anchor--no-style" > Reset anchor style </Anchor> </Example> <Example> <button className="dnb-anchor">I'm a Button!</button> </Example> <Example data-visual-test="anchor-newline"> <Anchor href="/uilib/components/anchor"> Newline <br /> Newline </Anchor> </Example> <Example data-visual-test="anchor-skeleton"> <Anchor skeleton href="/uilib/components/anchor"> Skeleton </Anchor> </Example>
Anchor modifiers
.dnb-anchor--no-animation
No Animation.dnb-anchor--no-style
No Style.dnb-anchor--no-hover
No Hover.dnb-anchor--no-underline
No Underline
Anchor with icons
Icons can be added with the icon
and iconPosition
props. Normally by sending in the name if an icon, but it is also possible to send in html/react code (normally for custom svg).
<Example data-visual-test="anchor-icon-right"> <Anchor href="/uilib/components/anchor" icon="chevron_right" iconPosition="right" > Anchor with Icon right </Anchor> </Example> <Example data-visual-test="anchor-icon-left"> <Anchor href="/uilib/components/anchor" icon="question"> Anchor with Icon left </Anchor> </Example> <Example data-visual-test="anchor-icon-node"> <Anchor href="/uilib/components/anchor" icon={<IconPrimary icon="question" />} > Anchor with Icon left using a html/react element </Anchor> </Example> <Example data-visual-test="anchor-paragraph"> <P> text {'Â '} <Anchor href="/uilib/components/anchor" icon="bell" iconPosition="right" className="dnb-anchor--inline" > Inside a Paragraph </Anchor> {'Â '} text </P> </Example>
target="_blank"
Anchor with If the link opens a new window it will automatically get an icon to indicate this.
<Example data-visual-test="anchor-blank"> <Anchor target="_blank" href="/uilib/components/anchor"> Blank target with https </Anchor> </Example> <Example> <Anchor target="_blank" href="/uilib/components/anchor" icon="arrow_right" iconPosition="right" > Blank target with different launch icon </Anchor> </Example>
Unless the href contains :mailto
, :tel
or :sms
.
<Example> <Anchor target="_blank" href="mailto:john.doe@email.com"> Send a mail to: john.doe@email.com </Anchor> </Example> <Example> <Anchor target="_blank" href="tel:12345678"> Make a phone call to: 12345678 </Anchor> </Example> <Example> <Anchor target="_blank" href="sms:12345678"> Send an SMS to: 12345678 </Anchor> </Example>
Anchor in headings
<Example data-visual-test="anchor-heading"> <H2> <Anchor href="/uilib/components/anchor" icon="bell" iconPosition="right" > Inside Headings </Anchor>{' '} H2 </H2> </Example> <Example data-visual-test="anchor-heading-blank"> <H2> <Anchor target="_blank" href="/uilib/components/anchor"> Blank target in headings </Anchor>{' '} H2 </H2> </Example>
Anchor in Section
<Section spacing> <Anchor className="dnb-anchor--no-underline" href="https://dnb.no/"> Anchor in Section without underline </Anchor> </Section>