Skip to content

Commit 88df223

Browse files
authored
Radio component (#2200)
* Radio component * Support radios in Jetpack contact forms
1 parent 8c4a2c6 commit 88df223

File tree

11 files changed

+290
-118
lines changed

11 files changed

+290
-118
lines changed

.changeset/moody-bottles-agree.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+
Style radio inputs in Jetpack contact forms.

.changeset/poor-icons-tan.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+
Add Radio component to style `input type="radio"` elements.

src/components/checkbox/checkbox.stories.mdx

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,12 +11,11 @@ import './demo/styles.scss';
1111

1212
# Checkbox
1313

14-
`c-checkbox` may be applied to any `<input type="checkbox">` element. They are larger than native checkboxes and more visually consistent with our [input](/docs/components-input--text-elements) and [button](/docs/components-button--elements) components.
14+
`c-checkbox` may be applied to any `<input type="checkbox">` element. They are larger than native checkboxes and more visually consistent with our [radio](/docs/components-radio--Enabled), [input](/docs/components-input--text-elements) and [button](/docs/components-button--elements) components.
1515

1616
<Canvas>
1717
<Story
1818
name="Enabled"
19-
height="120px"
2019
args={{ disabled: false }}
2120
parameters={{
2221
docs: {
@@ -42,7 +41,6 @@ Checkboxes appear more subtle and less actionable when the `disabled` attribute
4241
<Canvas>
4342
<Story
4443
name="Disabled"
45-
height="120px"
4644
args={{ disabled: true }}
4745
parameters={{
4846
docs: {

src/components/checkbox/checkbox.twig

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,11 @@
1+
{#
2+
This all might be a little overengineered? We're basically recreating all of
3+
the HTML attributes manually. See the radio component for another example.
4+
5+
Leaving in for now since I'm not really interested in another breaking change
6+
so soon after our last one.
7+
#}
8+
19
{% set attributes %}
210
type="checkbox"
311
class="c-checkbox{% if class %} {{ class }}{% endif %}"
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
@use '../../../mixins/ms';
2+
3+
.demo-radio-labels {
4+
display: grid;
5+
grid-gap: ms.step(1);
6+
}
7+
8+
.demo-radio-label {
9+
display: grid;
10+
grid-gap: ms.step(-2);
11+
grid-template-columns: auto 1fr;
12+
user-select: none;
13+
}
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
<div class="demo-radio-labels">
2+
{% for i in 1..3 %}
3+
<label class="demo-radio-label">
4+
{% include '@cloudfour/components/radio/radio.twig' with {
5+
checked: loop.first,
6+
name: disabled ? 'demo-radio-disabled' : 'demo-radio-enabled',
7+
} %}
8+
Option {{i}}
9+
</label>
10+
{% endfor %}
11+
</div>

src/components/radio/radio.scss

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
@use '../../mixins/toggle-input';
2+
3+
.c-radio {
4+
@include toggle-input.radio;
5+
}
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
import { Canvas, Meta, Story } from '@storybook/addon-docs';
2+
import withLabelDemo from './demo/with-label.twig';
3+
import './demo/styles.scss';
4+
5+
<Meta
6+
title="Components/Radio"
7+
argTypes={{
8+
disabled: { type: { name: 'boolean' } },
9+
}}
10+
/>
11+
12+
# Radio
13+
14+
`c-radio` may be applied to any `<input type="radio">` element. They are larger than native radio elements and more visually consistent with our [checkbox](/docs/components-checkbox--Enabled), [input](/docs/components-input--text-elements) and [button](/docs/components-button--elements) components.
15+
16+
<Canvas>
17+
<Story
18+
name="Enabled"
19+
parameters={{
20+
docs: {
21+
source: {
22+
code: `{% include '@cloudfour/components/radio/radio.twig' with {
23+
name: 'demo-radio-enabled',
24+
value: 'example',
25+
checked: true,
26+
} only %}`,
27+
},
28+
},
29+
}}
30+
>
31+
{(args) => withLabelDemo(args)}
32+
</Story>
33+
</Canvas>
34+
35+
Radios appear more subtle and less actionable when the `disabled` attribute is set.
36+
37+
<Canvas>
38+
<Story
39+
name="Disabled"
40+
args={{ disabled: true }}
41+
parameters={{
42+
docs: {
43+
source: {
44+
code: `{% include '@cloudfour/components/radio/radio.twig' with {
45+
name: 'demo-radio-disabled',
46+
value: 'example',
47+
checked: true,
48+
disabled: true,
49+
} only %}`,
50+
},
51+
},
52+
}}
53+
>
54+
{(args) => withLabelDemo(args)}
55+
</Story>
56+
</Canvas>

src/components/radio/radio.twig

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
{#
2+
This all might be a little overengineered? Leaving in for now for consistency
3+
with the checkbox component.
4+
#}
5+
6+
{% set attributes %}
7+
type="radio"
8+
class="c-radio{% if class %} {{ class }}{% endif %}"
9+
{% for attribute_name in [
10+
'value',
11+
'id',
12+
'name',
13+
'autocomplete'
14+
] %}
15+
{% set attribute_value = attribute(_context, attribute_name) %}
16+
{% if attribute_value %}{{ attribute_name }}="{{ attribute_value }}"{% endif %}
17+
{% endfor %}
18+
{% for attribute_name in [
19+
'checked',
20+
'required',
21+
'disabled'
22+
] %}
23+
{% set attribute_value = attribute(_context, attribute_name) %}
24+
{% if attribute_value %}{{ attribute_name }}{% endif %}
25+
{% endfor %}
26+
{% endset %}
27+
28+
<input {{ attributes }}>

0 commit comments

Comments
 (0)