Skip to content

Commit a1d7017

Browse files
authored
Add Blocks to Comments to make them more flexible
1 parent 5b14c5a commit a1d7017

File tree

5 files changed

+88
-61
lines changed

5 files changed

+88
-61
lines changed

.changeset/short-walls-hear.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+
Increase comment template flexibility using blocks.

src/components/comment/comment.stories.mdx

Lines changed: 27 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,18 @@ import { initCommentReplyForm } from './comment.ts';
55
import { createElasticTextArea } from '../input/elastic-textarea.ts';
66
import { useEffect } from '@storybook/client-api';
77
import { makeTwigInclude } from '../../make-twig-include';
8+
import authorDemo from './demo/author.twig';
9+
import memberDemo from './demo/member.twig';
10+
// See: https://github.com/webpack-contrib/raw-loader#examples
11+
// For now, it seems likely Storybook is pretty tied to Webpack, therefore, we are
12+
// okay with the following Webpack-specific raw loader syntax. It's better to leave
13+
// the ESLint rule enabled globally, and only thoughtfully disable as needed (e.g.
14+
// within a Storybook docs page and not within an actual component).
15+
// This can be revisited in the future if Storybook no longer relies on Webpack.
16+
// eslint-disable-next-line @cloudfour/import/no-webpack-loader-syntax
17+
import authorDemoSource from '!!raw-loader!./demo/author.twig';
18+
// eslint-disable-next-line @cloudfour/import/no-webpack-loader-syntax
19+
import memberDemoSource from '!!raw-loader!./demo/member.twig';
820
const tyler = {
921
name: 'Tyler Sticka',
1022
link: 'https://cloudfour.com/is/tyler',
@@ -42,12 +54,6 @@ const initCommentReplyForms = () => {
4254

4355
Displays a single comment in a comment thread, optionally with replies. Multiple comments can be displayed within [a Rhythm layout object](/docs/objects-rhythm--default-story).
4456

45-
## Status
46-
47-
This component is still a work in progress. The following features are still in development:
48-
49-
- Finalizing this pattern's blocks and properties for theme integration.
50-
5157
## Single
5258

5359
At minimum, a single comment consists of:
@@ -85,27 +91,24 @@ This information may be passed to the component as a `comment` object matching t
8591
</Story>
8692
</Canvas>
8793

88-
## Role badges
94+
## Author Title
8995

90-
It is helpful for context within a discussion to know when a commentor is the original post author or a Cloud Four team member. The mechanics of this feature are still in development, but these stories show how these roles should appear using [the Badge component](/docs/components-badge--basic).
96+
You can use the `author_title` block to pass in a custom author name or title.
97+
98+
It is helpful for context within a discussion to know when a commentor is the original post author or a Cloud Four team member. This can be achieved by passing in a name with [a Badge component](/docs/components-badge--basic) to the `author_title` block.
9199

92100
<Canvas>
93101
<Story
94102
name="Role: Author"
95103
parameters={{
96104
docs: {
97-
source: {
98-
code: makeTwigInclude('@cloudfour/components/comment/comment.twig', {
99-
comment: makeComment(),
100-
demo_post_author: true,
101-
}),
102-
},
105+
source: { code: authorDemoSource },
103106
},
104107
}}
105108
>
106109
{(args) => {
107110
useEffect(() => initCommentReplyForms());
108-
return template({
111+
return authorDemo({
109112
comment: makeComment(),
110113
allow_replies: args.allowReplies,
111114
demo_post_author: true,
@@ -122,17 +125,14 @@ It is helpful for context within a discussion to know when a commentor is the or
122125
parameters={{
123126
docs: {
124127
source: {
125-
code: makeTwigInclude('@cloudfour/components/comment/comment.twig', {
126-
comment: makeComment(),
127-
demo_cloud_four_member: true,
128-
}),
128+
code: memberDemoSource,
129129
},
130130
},
131131
}}
132132
>
133133
{(args) => {
134134
useEffect(() => initCommentReplyForms());
135-
return template({
135+
return memberDemo({
136136
comment: makeComment(),
137137
allow_replies: args.allowReplies,
138138
demo_cloud_four_member: true,
@@ -288,8 +288,6 @@ While it is theoretically possible to infinitely nest `children`, it's recommend
288288

289289
## Template Properties
290290

291-
Note: These template properties are not finalized, and may change in the future.
292-
293291
- `comment`: an object matching the structure of a [Timber comment](https://timber.github.io/docs/reference/timber-comment/)
294292
- `allow_replies`: A boolean property that controls whether to show a reply button and form
295293
- `logged_in_user`: [user object](https://timber.github.io/docs/reference/timber-user/#properties) of the type:
@@ -302,6 +300,13 @@ Note: These template properties are not finalized, and may change in the future.
302300
- `log_out_url`: URL used for log out link.
303301
- `source`: An optional object containing a `url` and `name` for the comment source.
304302

303+
## Template Blocks
304+
305+
- `header_content`: Use to over-write the content of the comment header
306+
- `heading_content`: Use to over-write the content of the comment heading
307+
- `author_title`: Use to over-write a portion of the comment heading content. Will be followed by a visually hidden span reading either `replied` or `said`. (Valid values could be `John Doe` or `John Doe (Post Author)`.)
308+
- `replies`: Use to pass in your own replies markup. This is useful if you need to modify other blocks within replies.
309+
305310
## JavaScript Instructions
306311

307312
You can run `initCommentReplyForm` to initialize a comment's reply form. Comments with reply forms will have the `js-comment-with-reply-form` class which you can use to query and initialize them.

src/components/comment/comment.twig

Lines changed: 21 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -13,41 +13,21 @@
1313
class="c-comment {{_comment_modifiers}}"
1414
id="comment-{{comment.ID}}"
1515
>
16-
<header class="c-comment__header">
16+
<header class="c-comment__header">
17+
{% block header_content %}
1718
<h{{_heading_depth}} class="c-comment__heading">
18-
{{comment.author.name}}
19-
{#
20-
TODO: Replace `demo_post_author` and `demo_cloud_four_member` with
21-
more meaningful blocks or properties once we have a better idea of how
22-
we'll integrate role badging based on actual comment data.
23-
#}
24-
{% if demo_post_author %}
25-
<span class="u-hidden-visually">(Article</span>
26-
{% embed '@cloudfour/components/badge/badge.twig' with {
27-
label: 'Author',
28-
icon: 'pencil',
29-
} only %}
30-
{% endembed %}
31-
<span class="u-hidden-visually">)</span>
32-
{% elseif demo_cloud_four_member %}
33-
<span class="u-hidden-visually">(Cloud Four</span>
34-
{% embed '@cloudfour/components/badge/badge.twig' with {
35-
label: 'Team'
36-
} only %}
37-
{% block extra %}
38-
{% include '@cloudfour/assets/brand/logo.svg.twig' with {
39-
class: 'c-icon',
40-
aria_hidden: 'true',
41-
} only %}
42-
{% endblock %}
43-
{% endembed %}
44-
<span class="u-hidden-visually">Member)</span>
45-
{% endif %}
46-
<span class="u-hidden-visually">
47-
{% if comment.is_child %}replied{% else %}said{% endif %}:
48-
</span>
19+
{% block heading_content %}
20+
{% block author_title %}
21+
{{comment.author.name}}
22+
{% endblock %}
23+
24+
<span class="u-hidden-visually">
25+
{% if comment.is_child %}replied{% else %}said{% endif %}:
26+
</span>
27+
{% endblock %}
4928
</h{{_heading_depth}}>
50-
</header>
29+
{% endblock %}
30+
</header>
5131
<div class="c-comment__object">
5232
{% include '@cloudfour/components/avatar/avatar.twig' with {
5333
src: comment.avatar,
@@ -137,12 +117,14 @@
137117
class: 'c-comment__replies'
138118
} %}
139119
{% block content %}
140-
{% for child in comment.children %}
141-
{% include '@cloudfour/components/comment/comment.twig' with {
142-
comment: child,
143-
heading_depth: _child_heading_depth,
144-
} only %}
145-
{% endfor %}
120+
{% block replies %}
121+
{% for child in comment.children %}
122+
{% include '@cloudfour/components/comment/comment.twig' with {
123+
comment: child,
124+
heading_depth: _child_heading_depth,
125+
} only %}
126+
{% endfor %}
127+
{% endblock %}
146128
{% endblock %}
147129
{% endembed %}
148130
{% endif %}
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
{% embed '@cloudfour/components/comment/comment.twig' with {
2+
comment: comment,
3+
logged_in_user: logged_in_user,
4+
allow_replies: allow_replies,
5+
} %}
6+
{% block author_title %}
7+
{{parent()}}
8+
<span class="u-hidden-visually">(Article</span>
9+
{% include '@cloudfour/components/badge/badge.twig' with {
10+
label: 'Author',
11+
icon: 'pencil',
12+
} only %}
13+
{% endblock %}
14+
{% endembed %}
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
{% embed '@cloudfour/components/comment/comment.twig' with {
2+
comment: comment,
3+
logged_in_user: logged_in_user,
4+
allow_replies: allow_replies,
5+
} %}
6+
{% block author_title %}
7+
{{parent()}}
8+
<span class="u-hidden-visually">(Cloud Four</span>
9+
{% embed '@cloudfour/components/badge/badge.twig' with {
10+
label: 'Team'
11+
} only %}
12+
{% block extra %}
13+
{% include '@cloudfour/assets/brand/logo.svg.twig' with {
14+
class: 'c-icon',
15+
aria_hidden: 'true',
16+
} only %}
17+
{% endblock %}
18+
{% endembed %}
19+
<span class="u-hidden-visually">Member)</span>
20+
{% endblock %}
21+
{% endembed %}

0 commit comments

Comments
 (0)