Skip to content

Commit 9b34589

Browse files
authored
Integrate reply Comment Form into Comment pattern (#1423)
The primary focus of this PR is integrating the Comment Form for replies into the Comment pattern. While I was working on this I made some changes to the Comment Form component and updated the code examples for all the Comment stories.
1 parent 3b84118 commit 9b34589

13 files changed

+495
-53
lines changed

.changeset/grumpy-dancers-jump.md

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'@cloudfour/patterns': minor
3+
---
4+
5+
Add reply forms to comments

src/components/comment-form/comment-form.stories.mdx

+31-5
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ const tyler = {
2424

2525
# Comment Form
2626

27-
The form used to add a comment to an article.
27+
The form used to add a comment to an article. Including the `heading_id` and `help_text_id` properties will provide assistive technologies additional context.
2828

2929
<Canvas>
3030
<Story
@@ -34,7 +34,11 @@ The form used to add a comment to an article.
3434
docs: {
3535
source: {
3636
code: makeTwigInclude(
37-
'@cloudfour/components/comment-form/comment-form.twig'
37+
'@cloudfour/components/comment-form/comment-form.twig',
38+
{
39+
heading_id: 'leave-a-comment',
40+
help_text_id: 'leave-a-comment-help-text',
41+
}
3842
),
3943
},
4044
},
@@ -51,12 +55,14 @@ The form used to add a comment to an article.
5155
logged_in_user: isLoggedIn ? tyler : undefined,
5256
log_out_url: '/logout',
5357
is_reply: isReply,
58+
heading_id: 'leave-a-comment',
59+
help_text_id: 'leave-a-comment-help-text',
5460
});
5561
}}
5662
</Story>
5763
</Canvas>
5864

59-
There is also a reply version of this component (`is_reply: true`), used to reply to an existing comment:
65+
There is also a reply version of this component (`is_reply: true`), used to reply to an existing comment. (You'll also want to modify `heading_tag`, `heading_id`, `heading_class`, and `heading_text`. When using the [Comment pattern](/docs/components-comment--with-reply-button) this will be done automatically.)
6066

6167
<Canvas>
6268
<Story
@@ -67,7 +73,16 @@ There is also a reply version of this component (`is_reply: true`), used to repl
6773
source: {
6874
code: makeTwigInclude(
6975
'@cloudfour/components/comment-form/comment-form.twig',
70-
{ logged_in_user: tyler, log_out_url: '/logout', is_reply: true }
76+
{
77+
logged_in_user: tyler,
78+
log_out_url: '/logout',
79+
is_reply: true,
80+
heading_id: 'reply-to-comment-100',
81+
heading_tag: 'h4',
82+
heading_text: 'Reply to John Doe',
83+
heading_class: 'u-hidden-visually',
84+
main_label: 'Reply',
85+
}
7186
),
7287
},
7388
},
@@ -84,6 +99,11 @@ There is also a reply version of this component (`is_reply: true`), used to repl
8499
logged_in_user: isLoggedIn ? tyler : undefined,
85100
log_out_url: '/logout',
86101
is_reply: isReply,
102+
heading_id: 'reply-to-comment-100',
103+
heading_tag: 'h4',
104+
heading_text: 'Reply to John Doe',
105+
heading_class: 'u-hidden-visually',
106+
main_label: 'Reply',
87107
});
88108
}}
89109
</Story>
@@ -92,6 +112,12 @@ There is also a reply version of this component (`is_reply: true`), used to repl
92112
## Template Properties
93113

94114
- `class`: Append a class to the root element.
115+
- `heading_id`: A unique ID for your heading element. This will be used as the accessible name for the form.
116+
- `heading_tag`: The tag for your heading (defaults to `h2`).
117+
- `heading_text`: The text for your heading.
118+
- `heading_class`: An optional class for your heading.
119+
- `heading_id`: A unique ID for your help text. This will be used as the accessible description for the reply textarea.
120+
- `main_label`: The label for the primary message textarea. Defaults to `message`
95121
- `logged_in_user`: [user object](https://timber.github.io/docs/reference/timber-user/#properties) of the type:
96122

97123
```ts
@@ -107,4 +133,4 @@ There is also a reply version of this component (`is_reply: true`), used to repl
107133

108134
## JavaScript
109135

110-
The Comment Form component includes the [Elastic Textarea component](http://localhost:6006/?path=/docs/components-input--elastic-textarea). See the Elastic Textarea component for JavaScript setup details.
136+
The Comment Form component includes the [Elastic Textarea component](/docs/components-input--elastic-textarea). See the Elastic Textarea component for JavaScript setup details.

src/components/comment-form/comment-form.twig

+25-6
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,34 @@
11
<form
22
class="c-comment-form{% if class %} {{class}}{% endif %}"
3+
{% if heading_id %}aria-labelledby="{{ heading_id }}"{% endif %}
34
{%- if action %}action="{{ action }}"{% endif -%}
45
{%- if method %}method="{{ method }}"{% endif -%}
56
>
67
{% block prompt %}
7-
<h2>Leave a Comment</h2>
8-
<p>Please be kind, courteous and constructive. You may use simple HTML or <a href="https://en.support.wordpress.com/markdown-quick-reference">Markdown</a> in your comments. All fields are required.</p>
8+
{% set _heading_tag = heading_tag|default('h2') %}
9+
<{{_heading_tag}}
10+
{% if heading_id %}id="{{ heading_id }}"{% endif %}
11+
{% if heading_class %}class="{{ heading_class }}"{% endif %}
12+
>
13+
{{ heading_text|default("Leave a Comment") }}
14+
</{{_heading_tag}}>
15+
<p {% if help_text_id %}id="{{ help_text_id }}"{% endif %}>
16+
Please be kind, courteous and constructive.
17+
You may use simple HTML or
18+
<a href="https://en.support.wordpress.com/markdown-quick-reference">Markdown</a>
19+
in your comments.
20+
All fields are required.
21+
</p>
922
{% endblock %}
10-
{% embed '@cloudfour/objects/form-group/form-group.twig' with { label: 'Message' } only %}
23+
{% embed '@cloudfour/objects/form-group/form-group.twig' with { label: main_label|default('Message') } %}
1124
{% block control %}
12-
{% include '@cloudfour/components/input/input.twig' with { type: 'textarea', name: 'comment', required: true, class: 'c-input--elastic js-elastic-textarea' } only %}
25+
{% include '@cloudfour/components/input/input.twig' with {
26+
type: 'textarea',
27+
name: 'comment',
28+
required: true,
29+
class: 'c-input--elastic js-elastic-textarea',
30+
'aria-describedby': help_text_id
31+
} only %}
1332
{% endblock %}
1433
{% endembed %}
1534
{% if logged_in_user and log_out_url %}
@@ -34,10 +53,10 @@
3453
{% embed '@cloudfour/objects/button-group/button-group.twig' only %}
3554
{% block content %}
3655
{% include '@cloudfour/components/button/button.twig' with { label: 'Submit Reply' } only %}
37-
{% include '@cloudfour/components/button/button.twig' with { label: 'Cancel', class: 'c-button--secondary' } only %}
56+
{% include '@cloudfour/components/button/button.twig' with { label: 'Cancel', class: 'c-button--secondary js-cancel-reply' } only %}
3857
{% endblock %}
3958
{% endembed %}
4059
{% else %}
4160
{% include '@cloudfour/components/button/button.twig' with { label: 'Submit Comment' } only %}
4261
{% endif %}
43-
</div>
62+
</form>

src/components/comment/comment.scss

+14
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323

2424
/// Display a vertical line if the comment contains a reply thread to connect
2525
/// those comments visually with their parent.
26+
.c-comment.is-replying::after,
2627
.c-comment--thread::after {
2728
border-radius: size.$border-radius-full;
2829
content: '';
@@ -42,6 +43,15 @@
4243
}
4344
}
4445

46+
/// Comment reply forms are hidden until someone starts a reply.
47+
.c-comment__reply-form {
48+
display: none;
49+
}
50+
51+
.c-comment.is-replying .c-comment__reply-form {
52+
display: block;
53+
}
54+
4555
/// Named object for consistency with the Media component.
4656
.c-comment__object {
4757
grid-area: object;
@@ -124,6 +134,10 @@
124134
// Visually centers the child avatars with the indentation of the parent
125135
// comment. Without this, the replies feel nested too far to the right.
126136
margin-left: math.div(size.$square-avatar-small, -2);
137+
}
138+
139+
.c-comment__replies,
140+
.c-comment__reply-form {
127141
// Since the replies are likely wrapped in a Rhythm object intended to inherit
128142
// the parent rhythm, we can use that custom property to inherit that same
129143
// spacing between the meta and replies.

0 commit comments

Comments
 (0)