Creating an external anchor component
This pages describes how I create a fancy
Even though this component does’t do much by itself, getting started with something this simple allows me to dive a bit deeper into technologies like
Description
I wanted a component that would:
- Be as close to easy to use as a normal <a />element.
- Display an icon to denoted is an external link so that you know you’re leaving the site.
- Open the link in another browser. I’m always taken out of the flow if I click a link and I lose my place in the article.
External Anchor (Text and Icon)
Using this:
<EA text="Svelte" href="https://svelte.dev" />
generates this:
External Anchor (Icon Only)
Using this:
<EA href="https://svelte.dev" />
generates this:
The reason why I wanted to include the icon only view will be for a future article.
Building the component
This is a very simple
<script lang="ts">
import FaExternalLinkAlt from "$lib/components/icons/ExternalLinkAlt.svelte";
export let text: string;
export let href: string;
</script>
<a {href} target="_blank">
{#if text}
{text}
{/if}
<div class="icon"><FaExternalLinkAlt /></div>
</a>
<style>
a {
text-decoration-color: none;
text-decoration-line: none;
text-decoration-style: none;
border-bottom-style: solid;
border-bottom-width: 1px;
border-bottom-color: lightgray;
}
a:hover {
cursor: pointer;
border-bottom-width: 2px;
}
.icon {
display: inline-block;
color: darkgrey;
width: 1rem;
height: 1rem;
transform: translateY(0.05em);
}
</style>
The script section
<script lang="ts">
import FaExternalLinkAlt from "$lib/components/icons/ExternalLinkAlt.svelte";
export let text: string;
export let url: string;
</script>
The lang="ts"
describes that this component uses the
The import FaExternalLinkAlt from "$lib/components/icons/ExternalLinkAlt.svelte";
imports the FaExternalLinkAlt
icon from the
The export let text: string
and export let url: string;
describe the component properties (input arguments). The text
argument is the
url
argument is the value for the href
attribute. The markup section
All
<a {href} target="_blank">
{#if text}
{text}
{/if}
<div class="icon"><FaExternalLinkAlt /></div>
</a>
The <a {href} target="_blank">
assigns the href
component input property to the
href
attribute value. The target="_blank">
sets the target of the
The {#if text}{text}{/if}
inserts the contents of the text
input property into the output if the text
input property contains a
The <div class="icon"><FaExternalLinkAlt /></div>
inserts the <FaExternalLinkAlt />
icon and wraps it in a
The style section
<style>
a {
text-decoration-color: none;
text-decoration-line: none;
text-decoration-style: none;
border-bottom-style: solid;
border-bottom-width: 1px;
border-bottom-color: lightgray;
}
a:hover {
cursor: pointer;
border-bottom-width: 2px;
}
.icon {
display: inline-block;
color: darkgrey;
width: 1rem;
height: 1rem;
transform: translateY(0.05em);
}
</style>
Where the
a {...}
, sets the none
which removes It also sets the
The
hover
selector, a:hover {...}
, configures the pointer
. This provides a bit of distinction when the element is under the cursor. The icon’s custom
.icon {...}
styles the It sets the The display: inline-block
sets the display value to allow the <FaExternalLinkAlt />
to be inline with the normal text flow.
The
The transform: translateY(0.05em);
translateY(0.1em)
function moves the icon vertically down a bit, which I think aligns icon a bit better wtih the text. Completed
Now you can just import the component and use it as listed in the examples at this beginning of this article.