Skip to content

Commit f6056a8

Browse files
authored
Linked badges (#2132)
* Linked badges * Add rel attribute support
1 parent c30c97f commit f6056a8

File tree

4 files changed

+62
-2
lines changed

4 files changed

+62
-2
lines changed

.changeset/giant-sheep-bake.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'@cloudfour/patterns': minor
3+
---
4+
5+
Badges can now be links

src/components/badge/badge.scss

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,13 @@
1+
@use '../../compiled/tokens/scss/brightness';
2+
@use '../../compiled/tokens/scss/color';
3+
@use '../../compiled/tokens/scss/ease';
14
@use '../../compiled/tokens/scss/font-weight';
25
@use '../../compiled/tokens/scss/line-height';
36
@use '../../compiled/tokens/scss/size';
7+
@use '../../compiled/tokens/scss/transition';
48
@use '../../mixins/ms';
9+
@use '../../mixins/theme';
10+
@use 'sass:color' as sasscolor;
511
@use 'sass:math';
612

713
///
@@ -23,10 +29,31 @@
2329
font-weight: font-weight.$medium;
2430
line-height: line-height.$tighter;
2531
padding: 0 math.div(size.$padding-cell-horizontal, 2); // 2
32+
text-decoration: none;
33+
transition: filter transition.$slow ease.$out;
2634
vertical-align: middle;
2735
white-space: nowrap;
2836
}
2937

38+
///
39+
/// When badges are links, we want to saturate their appearance a bit and add
40+
/// some basic pointer effects.
41+
///
42+
a.c-badge {
43+
// I just wasn't happy with the light gray on its own, but I didn't think this
44+
// color was versatile enough to make available as a token.
45+
background-color: sasscolor.change(color.$brand-primary-lighter, $alpha: 0.2);
46+
color: var(--theme-color-text-action);
47+
48+
&:hover {
49+
filter: brightness(brightness.$control-brighten);
50+
}
51+
52+
&:active {
53+
filter: brightness(brightness.$control-dim);
54+
}
55+
}
56+
3057
.c-badge__content {
3158
&:first-child {
3259
padding-inline-start: math.div(size.$padding-cell-horizontal, 2);

src/components/badge/badge.stories.mdx

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ const badgeStory = (args) => {
1212
title="Components/Badge"
1313
argTypes={{
1414
label: { type: { name: 'string' } },
15+
href: { type: { name: 'string' } },
1516
icon: {
1617
options: ['', 'check', 'heart', 'pencil'],
1718
type: { name: 'string' },
@@ -34,17 +35,37 @@ Badges help provide just a smidge more context to repetitive or serialized conte
3435

3536
## With icon
3637

38+
An icon may be used to visually support the meaning of the badge.
39+
3740
<Canvas>
3841
<Story name="With icon" args={{ icon: 'check', label: 'Verified' }}>
3942
{(args) => badgeStory(args)}
4043
</Story>
4144
</Canvas>
4245

46+
## Linked
47+
48+
Badges may link to other content. For example, a badge representing an article's category might link to a list of all articles in that category.
49+
50+
Avoid using badges for critical links: Their appearance is too subtle to rely on for important calls to action.
51+
52+
<Canvas>
53+
<Story
54+
name="Linked"
55+
args={{ icon: 'paperclip', label: 'Attachment', href: '#', rel: 'tag' }}
56+
>
57+
{(args) => badgeStory(args)}
58+
</Story>
59+
</Canvas>
60+
4361
## Template Properties
4462

4563
- `class` (string)
64+
- `href` (string, optional): A URL for the badge to link to.
4665
- `icon` (string, optional): The name of one of [our icons](/docs/design-icons--page) to display.
4766
- `message` (string, default `'Label'`)
67+
- `rel` (string, optional): Specify the relationship of the linked URL.
68+
- `tag_name` (string, default `'span'` or `'a'` depending on presence of `href`)
4869

4970
## Template Blocks
5071

src/components/badge/badge.twig

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
{% set _tag_name = tag_name|default(href ? 'a' : 'span') %}
2+
13
{% set _extra_content %}
24
{% block extra %}
35
{% if icon %}
@@ -8,7 +10,12 @@
810
{% endblock %}
911
{% endset %}
1012

11-
<span class="c-badge{% if class %} {{class}}{% endif %}">
13+
<{{_tag_name}}
14+
class="c-badge{% if class %} {{class}}{% endif %}"
15+
{%- if _tag_name == 'a' %}
16+
href="{{href|default('#')}}"
17+
{% if rel %}rel="{{rel}}"{% endif %}
18+
{% endif -%}>
1219
{% if _extra_content|trim %}
1320
<span class="c-badge__extra">
1421
{{_extra_content}}
@@ -19,4 +26,4 @@
1926
{{label|default('Label')}}
2027
{% endblock %}
2128
</span>
22-
</span>
29+
</{{_tag_name}}>

0 commit comments

Comments
 (0)