Skip to content
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: Add badge to LinkCard component #712

Open
wants to merge 5 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/six-dolphins-wonder.md
@@ -0,0 +1,5 @@
---
'@astrojs/starlight': patch
---

Add badge to `LinkCard` component
6 changes: 3 additions & 3 deletions docs/src/content/docs/guides/components.mdx
Expand Up @@ -129,7 +129,7 @@ Add the `stagger` attribute to shift the second column of cards vertically and a

Use the `<LinkCard>` component to link prominently to different pages.

A `<LinkCard>` requires a `title` and an [`href`](https://developer.mozilla.org/en-US/docs/Web/HTML/Element/a#href) attribute. You can optionally include a short `description` or other link attributes such as `target`.
A `<LinkCard>` requires a `title` and an [`href`](https://developer.mozilla.org/en-US/docs/Web/HTML/Element/a#href) attribute. You can optionally include a short `description`, a [badge](/guides/sidebar/#badges), and other link attributes such as `target`.

Group multiple `<LinkCard>` components in `<CardGrid>` to display cards side-by-side when there’s enough space.

Expand All @@ -144,7 +144,7 @@ import { LinkCard, CardGrid } from '@astrojs/starlight/components';

<CardGrid>
<LinkCard title="Authoring Markdown" href="/guides/authoring-content/" />
<LinkCard title="Components" href="/guides/components/" />
<LinkCard title="Sidebar Navigation" badge="New" href="/guides/sidebar/" />
</CardGrid>
```

Expand All @@ -160,7 +160,7 @@ import { LinkCard } from '@astrojs/starlight/components';

<CardGrid>
<LinkCard title="Authoring Markdown" href="/guides/authoring-content/" />
<LinkCard title="Components" href="/guides/components/" />
<LinkCard title="Sidebar Navigation" badge="New" href="/guides/sidebar/" />
</CardGrid>

### Icon
Expand Down
24 changes: 20 additions & 4 deletions packages/starlight/user-components/LinkCard.astro
@@ -1,20 +1,36 @@
---
import Icon from './Icon.astro';
import Badge from '../components/Badge.astro';
import type { Badge as BadgeType } from '../schemas/badge';
import { BadgeConfigSchema } from '../schemas/badge';
lorenzolewis marked this conversation as resolved.
Show resolved Hide resolved
import type { HTMLAttributes } from 'astro/types';

interface Props extends Omit<HTMLAttributes<'a'>, 'title'> {
title: string;
badge: BadgeType | undefined;
lorenzolewis marked this conversation as resolved.
Show resolved Hide resolved
description?: string;
}

const { title, description, ...attributes } = Astro.props;
const { title, badge, description, ...attributes } = Astro.props;

const badgeData = BadgeConfigSchema().parse(badge);
lorenzolewis marked this conversation as resolved.
Show resolved Hide resolved
---

<div>
<span class="sl-flex stack">
<a {...attributes}>
<span class="title" set:html={title} />
</a>
<span>
<a {...attributes}>
<span class="title" set:html={title} />
</a>
{
badge && (
<>
{' '}
<Badge {...badgeData} />
</>
)
}
</span>
{description && <span class="description" set:html={description} />}
</span>
<Icon name="right-arrow" size="1.333em" class="icon rtl:flip" />
Expand Down