Part Overrides with Custom Elements
A part override can be the name of a custom element you've registered, rather than a function. That suits a site that already builds its interface from custom elements, or a part that needs its own state and behaviour, such as a badge with a tooltip. Comments draws your element in the part's place and keeps it updated with the part's data, and everything else about the thread stays as it was.
A Verified Badge
Suppose some commenters carry a verified badge, which a site adds on the server with the plugin's Comments::EVENT_DEFINE_BADGES event, and you want a tick and a tooltip on it. This element draws every badge, keeping Comments' own badge classes so its stylesheet still styles them:
class CommentBadge extends HTMLElement {
// Comments sets each of the part's context keys as a property, every time the part is drawn.
set badge(badge) {
const verified = badge.type === 'verified';
this.className = `comments__badge comments__badge--${badge.type.replace(/[^\w-]/g, '')}`;
this.textContent = verified ? `✓ ${badge.label}` : badge.label;
this.title = verified ? 'This commenter has bought from us.' : '';
}
}
customElements.define('acme-comment-badge', CommentBadge);document.querySelector('verbb-comments').parts = { badge: 'acme-comment-badge' };Comments creates the element once for each badge and, each time the badge is drawn, sets every key of the part's context on it as a property, such as badge, comment, viewer, labels, actions, controlAttributes, html and defaultRender. A setter, as above, or a library such as Lit that turns properties into renders, lets the element follow along. The element sits in the thread's light DOM, so Comments' stylesheet and yours reach it. Draw any text from a comment or a commenter as text, as textContent does, never as HTML.
Simpler Overrides as Functions
A custom element is worth registering when the part has state or behaviour of its own. For markup alone, a function is shorter and needs no registration:
document.querySelector('verbb-comments').parts = {
badge: ({ badge, html, defaultRender }) => badge.type === 'verified'
? html`<span class="badge badge-success">✓ ${badge.label}</span>`
: defaultRender(),
};What an Override Is Given
A function is given the part's context as its argument, and a custom element gets each key as a property, with the part's own data from the table below.
| Key | What it is |
|---|---|
labels | The thread's translated wording, keyed as Configuring Threads lists. |
actions | For a comment's parts, vote(vote), reply(), edit(), delete(), report(), copyLink(), moderate(action), toggleReplies() and loadReplies(), bound to the comment. The thread's own parts get an empty object. |
controlAttributes(name) | The attributes of one of the ten controls with your themeConfig applied, for a button or link your override draws. |
html | lit-html's template tag, which escapes everything it inserts. |
defaultRender() | Comments' own markup for the part, to wrap or fall back to. |
A comment's parts also get comment, the comment's data in the data API's shape, permissions, what the person reading may do with it (such as canReply, canVote and their own vote), and viewer, who's reading.
If an override throws, the error is logged in the browser console and Comments' own markup is drawn in its place, so the rest of the thread keeps working.
Overriding the Whole Comment
A function override of the comment part replaces the comment's list item along with everything in it. It has to return an <li>, because it sits in the thread's list, and it has to draw form(), where Reply and Edit open their form, and replies(), or those stop working. Overriding the smaller parts inside a comment, such as meta, content and actions, keeps all of that for you, and a framework component override of comment draws inside Comments' own list item.
The Parts
| Part | Group | What it draws | Its own context |
|---|---|---|---|
header | Thread | The heading with the count, the sort options, Follow and a moderator’s summary of held and reported comments | viewer, conversation and state |
empty | Thread | What a thread with no comments says | viewer, conversation and state |
showButton | Thread | The Show comments button for the button load mode | viewer, conversation and state |
loading | Thread | The loading line while the thread’s data is on its way | viewer, conversation and state |
notice | Thread | Why nobody can post, such as a closed thread’s notice | notice, the text, with viewer, conversation and state |
comment | Comment | The whole comment | comment, permissions, viewer, conversation, actions, and form() and replies(), which the override must draw |
avatar | Comment | The author’s avatar | comment, permissions, viewer, conversation and actions |
authorName | Comment | The author’s name | comment, permissions, viewer, conversation and actions |
badge | Comment | One of the author’s badges, once for each | badge (its type and label), with the comment’s context |
meta | Comment | The date and permalink, inline custom fields and the edited note | comment, permissions, viewer, conversation and actions |
content | Comment | The comment’s text | comment, permissions, viewer, conversation and actions |
fields | Comment | The custom fields shown under the text, as a labelled list | fields, the comment’s entries shown under its text, with the comment’s context |
actions | Comment | Votes, Reply and the ··· menu | comment, permissions, viewer, conversation and actions |
review | Comment | A moderator’s review box on a held or reported comment | moderation, the comment’s moderation details, with the comment’s context |
placeholder | Comment | What stands in for a comment that’s no longer shown but still has replies | comment, permissions, viewer, conversation and actions |
join | Editor | The sign-in card a guest sees in place of the comment box | viewer, conversation and state |
postingAs | Editor | “Posting as” and the member’s badges, above a member’s comment box | viewer |
formFields | Editor | The custom fields the form asks for, once for those above the text and once for those below it | fields, the descriptions of the fields in this group, and position, above or below |
The comment form itself, the comment box, the rich text editor and the report form can't be overridden, because they carry spam protection, the editor's loading and keyboard focus. Their look is reached through styling, and custom fields on the form and the comment through field overrides.
Removing Something
An override can return nothing, but to remove a feature, turn it off in the plugin's settings first. A setting also stops the server offering the feature, so hiding the vote buttons with an empty actions override would still let anyone vote through the data API, whereas setting Interaction Mode to None removes voting altogether.
In Twig
A thread Craft renders can take custom elements for its field overrides through craft.comments.render(), because the mount point carries their names as data. Part overrides, which can be functions, are given in JavaScript through Comments.configure(), with the element's name in place of a function:
document.addEventListener('DOMContentLoaded', () => {
window.Comments.configure({ parts: { badge: 'acme-comment-badge' } });
});