Support displaying an icon in <LinkCard>
#970
Replies: 6 comments 7 replies
|
The LinkCard component doesn't seem to support icon insertion. How about using the |
|
You can do that, but the link target ends up only being the elements inside the card, whereas with LinkCard, the entire card acts like a button. The user experience is very different. |
|
+1 |
|
Cobbling together from the previous example, https://github.com/withastro/starlight/blob/main/packages/starlight/user-components/Card.astro and https://github.com/withastro/starlight/blob/main/packages/starlight/user-components/LinkCard.astro, I have come up with the following:
---
title: Cards
---
import { Card, CardGrid } from '@astrojs/starlight/components';
import IconLinkCard from "@components/IconLinkCard.astro";
## Activities
<CardGrid>
<IconLinkCard
title="Product"
href="/activities/#product"
icon="puzzle"
>
What this is.
</IconLinkCard>
<IconLinkCard
title="Development"
href="/activities/#development"
icon="pencil"
>
How it's made.
</IconLinkCard>
<IconLinkCard
title="Help"
href="/activities/#help"
icon="error"
>
Something is wrong.
</IconLinkCard>
<IconLinkCard
title="Contact"
href="/activities/#contact"
icon="open-book"
>
People, everywhere.
</IconLinkCard>
</CardGrid>
---
import { Icon } from '@astrojs/starlight/components';
import type { HTMLAttributes } from 'astro/types';
interface Props extends Omit<HTMLAttributes<'a'>, 'title'> {
icon?: keyof typeof Icon;
title: string;
}
const { icon, title, ...attributes } = Astro.props;
---
<article class="icon-link-card sl-flex">
<a {...attributes}>
<p class="title sl-flex">
{icon && <Icon name={icon} class="icon" size="1.5em" />}
<span class="title" set:html={title} />
{icon && <Icon name="right-arrow" size="1.5em" class="rtl:flip" />}
</p>
</a>
<div class="body"><slot /></div>
</article>
<style>
.icon-link-card {
--sl-card-border: var(--sl-color-purple);
--sl-card-bg: var(--sl-color-purple-low);
border: 1px solid var(--sl-color-gray-5);
background-color: var(--sl-color-black);
padding: clamp(1rem, calc(0.125rem + 3vw), 2.5rem);
flex-direction: column;
gap: clamp(0.5rem, calc(0.125rem + 1vw), 1rem);
}
.icon-link-card:nth-child(4n + 1) {
--sl-card-border: var(--sl-color-orange);
--sl-card-bg: var(--sl-color-orange-low);
}
.icon-link-card:nth-child(4n + 3) {
--sl-card-border: var(--sl-color-green);
--sl-card-bg: var(--sl-color-green-low);
}
.icon-link-card:nth-child(4n + 4) {
--sl-card-border: var(--sl-color-red);
--sl-card-bg: var(--sl-color-red-low);
}
.icon-link-card:nth-child(4n + 5) {
--sl-card-border: var(--sl-color-blue);
--sl-card-bg: var(--sl-color-blue-low);
}
a {
text-decoration: none;
line-height: var(--sl-line-height-headings);
}
.title {
font-weight: 600;
font-size: var(--sl-text-h4);
color: var(--sl-color-white);
line-height: var(--sl-line-height-headings);
gap: 1rem;
align-items: center;
}
.icon-link-card .icon {
border: 1px solid var(--sl-card-border);
background-color: var(--sl-card-bg);
padding: 0.2em;
border-radius: 0.25rem;
}
.icon-link-card .body {
margin: 0;
font-size: clamp(var(--sl-text-sm), calc(0.5rem + 1vw), var(--sl-text-body));
}
/* Hover state */
.icon-link-card:hover {
background: var(--sl-color-gray-7, var(--sl-color-gray-6));
border-color: var(--sl-color-gray-2);
}
.icon-link-card:hover .icon {
color: var(--sl-color-white);
}
</style>I wanted less configurability for the icon details. Surprisingly a size of Is this semantic enough for screen readers, with regards to #487? |
|
Hi everyone! I’ve just submitted a PR #3788 to address this. It includes full type safety for icons and parity for both MDX and Markdoc. Feel free to check out the deploy preview! |
|
Hi everyone! I’ve submitted a PR (#3788) to finally add icon support to LinkCard. I’d love to get your feedback on a design detail. In my current implementation, the custom icon and the default right arrow coexist (the icon is placed next to the title, while the arrow stays on the right as a navigation signifier). However, there's a suggestion to replace the right arrow with the custom icon when one is provided. I’d love to hear your thoughts: Personally, I think Option A maintains better consistency with the Card component and Starlight's navigation language.
|






Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
What version of
starlightare you using?latest
What is your idea?
It seems Card supports icon prop but LinkCard doesn't?
Maybe we should combine Card and LinkedCard?
Why is this feature necessary?
I think it's common to have a LinkedCard with an icon?
Do you have examples of this feature in other projects?
https://bugzpodder.github.io/pysandbox/ (I have a LinkedCard but no icon)
would it look good with this green icon?

Participation
All reactions