Skip to content

Commit 73eb5e6

Browse files
lorenzolewislorenzo-crabnebuladelucis
authored
Add LinkCard Component (#397)
Co-authored-by: Lorenzo Lewis <[email protected]> Co-authored-by: Chris Swithinbank <[email protected]>
1 parent bf91e27 commit 73eb5e6

File tree

5 files changed

+121
-0
lines changed

5 files changed

+121
-0
lines changed

.changeset/swift-kiwis-wash.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"@astrojs/starlight": minor
3+
---
4+
5+
Add `LinkCard` component

docs/src/content/docs/guides/components.mdx

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -125,6 +125,44 @@ Add the `stagger` attribute to shift the second column of cards vertically and a
125125

126126
:::
127127

128+
### Link Cards
129+
130+
Use the `<LinkCard>` component to link prominently to different pages.
131+
132+
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`.
133+
134+
Group multiple `<LinkCard>` components in `<CardGrid>` to display cards side-by-side when there’s enough space.
135+
136+
```mdx
137+
import { LinkCard, CardGrid } from '@astrojs/starlight/components';
138+
139+
<LinkCard
140+
title="Customizing Starlight"
141+
description="Learn how to make your Starlight site your own with custom styles, fonts, and more."
142+
href="/guides/customization/"
143+
/>
144+
145+
<CardGrid>
146+
<LinkCard title="Authoring Markdown" href="/guides/authoring-content/" />
147+
<LinkCard title="Components" href="/guides/components/" />
148+
</CardGrid>
149+
```
150+
151+
The above code generates the following on the page:
152+
153+
import { LinkCard } from '@astrojs/starlight/components';
154+
155+
<LinkCard
156+
title="Customizing Starlight"
157+
description="Learn how to make your Starlight site your own with custom styles, fonts, and more."
158+
href="/guides/customization/"
159+
/>
160+
161+
<CardGrid>
162+
<LinkCard title="Authoring Markdown" href="/guides/authoring-content/" />
163+
<LinkCard title="Components" href="/guides/components/" />
164+
</CardGrid>
165+
128166
### Icon
129167

130168
import { Icon } from '@astrojs/starlight/components';

packages/starlight/components.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,3 +3,4 @@ export { default as CardGrid } from './user-components/CardGrid.astro';
33
export { default as Icon } from './user-components/Icon.astro';
44
export { default as Tabs } from './user-components/Tabs.astro';
55
export { default as TabItem } from './user-components/TabItem.astro';
6+
export { default as LinkCard } from './user-components/LinkCard.astro';

packages/starlight/style/util.css

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,3 +41,15 @@
4141
display: block;
4242
}
4343
}
44+
45+
/*
46+
Flip an element around the y-axis when in an RTL context.
47+
Primarily useful for things where we can’t rely on writing direction like icons.
48+
49+
<Icon name="right-arrow" class="rtl:flip" />
50+
51+
In a LTR context: → In a RTL context: ←
52+
*/
53+
[dir='rtl'] .rtl\:flip:not(:where([dir='rtl'] [dir='ltr'] *)) {
54+
transform: matrix(-1, 0, 0, 1, 0, 0);
55+
}
Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
---
2+
import Icon from './Icon.astro';
3+
import type { HTMLAttributes } from 'astro/types';
4+
5+
interface Props extends Omit<HTMLAttributes<'a'>, 'title'> {
6+
title: string;
7+
description?: string;
8+
}
9+
10+
const { title, description, ...attributes } = Astro.props;
11+
---
12+
13+
<div>
14+
<a {...attributes}>
15+
<span class="flex stack">
16+
<span class="title" set:html={title} />
17+
{description && <span class="description" set:html={description} />}
18+
</span>
19+
<Icon name="right-arrow" size="1.333em" class="icon rtl:flip" />
20+
</a>
21+
</div>
22+
23+
<style>
24+
a {
25+
display: grid;
26+
grid-template-columns: 1fr auto;
27+
gap: 0.5rem;
28+
border: 1px solid var(--sl-color-gray-5);
29+
border-radius: 0.5rem;
30+
padding: 1rem;
31+
text-decoration: none;
32+
box-shadow: var(--sl-shadow-sm);
33+
}
34+
35+
.stack {
36+
flex-direction: column;
37+
gap: 0.5rem;
38+
}
39+
40+
.title {
41+
color: var(--sl-color-white);
42+
font-weight: 600;
43+
font-size: var(--sl-text-lg);
44+
line-height: var(--sl-line-height-headings);
45+
}
46+
47+
.description {
48+
color: var(--sl-color-gray-3);
49+
line-height: 1.5;
50+
}
51+
52+
.icon {
53+
color: var(--sl-color-gray-3);
54+
}
55+
56+
/* Hover state */
57+
a:hover {
58+
background: var(--sl-color-gray-7, var(--sl-color-gray-6));
59+
border-color: var(--sl-color-gray-2);
60+
}
61+
62+
a:hover .icon {
63+
color: var(--sl-color-white);
64+
}
65+
</style>

0 commit comments

Comments
 (0)