Skip to content

Commit d3545ae

Browse files
committed
docs(avatar): add README
1 parent 55b93e8 commit d3545ae

File tree

1 file changed

+71
-0
lines changed

1 file changed

+71
-0
lines changed

src/blocks/avatar/README.md

Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
# Avatar Block
2+
3+
A Gutenberg block that displays author avatar images with support for multiple authorship systems.
4+
5+
## Overview
6+
7+
The Avatar block renders circular author profile pictures for the current post. It automatically detects the authorship system in use and displays the appropriate avatar(s). When multiple authors are present, each author gets their own avatar image.
8+
9+
## Author resolution priority
10+
11+
The block follows the same priority as the [Byline block](../byline/README.md) to determine which avatars to display:
12+
13+
1. **Newspack Custom Bylines** - If the custom byline feature is enabled for the post (`_newspack_byline_active` meta) and the byline contains `[Author id="X"]` shortcodes, avatars are displayed for those authors. If the byline is active but contains only plain text (no shortcodes), no avatars are rendered.
14+
15+
2. **CoAuthors Plus** - If CAP is active and the post has co-authors, avatars are displayed for all of them. This works with both WordPress users and CAP Guest Authors.
16+
17+
3. **Default WordPress Author** - Falls back to the standard WordPress post author.
18+
19+
## Block attributes
20+
21+
| Attribute | Type | Default | Description |
22+
|-----------|------|---------|-------------|
23+
| `size` | number | `48` | Avatar image size in pixels. The actual image is fetched at 2x for retina displays. |
24+
| `linkToAuthorArchive` | boolean | `false` | Whether to wrap the avatar in a link to the author's archive page. |
25+
26+
## Block styles
27+
When CoAuthors Plus is active, a **Stacked** block style is registered. This overlaps avatars horizontally using negative margins and a radial mask, producing a compact, grouped layout for multi-author posts.
28+
29+
## Editor behavior
30+
31+
The editor uses two hooks to resolve avatar data:
32+
33+
- [usePostAuthors](./hooks.js) resolves the list of authors following the same priority as the PHP `get_avatar_authors()` method. Each author object includes an `avatarSrc` URL ready for rendering.
34+
35+
- [useUserAvatar](./hooks.js) provides the single WordPress post author avatar as a fallback when `usePostAuthors` returns an empty list.
36+
37+
For guest author avatars, the [useCoAuthors](../../shared/hooks/use-coauthors.js) hook fetches avatar URLs from the CAP REST endpoint (`/coauthors/v1/coauthors/{user_nicename}`). This is necessary because CAP's JS store strips avatar data. Results are cached at the module level by nicename, so adding a guest author displays their avatar immediately without requiring a post save.
38+
39+
In a Query Loop context, the block reads author data from the `newspack_author_info` REST field and extracts the author slug from `author_link` to fetch avatars from the same CAP endpoint.
40+
41+
## Availability
42+
43+
This block is only available in block themes. It replaces the core Avatar block in Newspack Block Theme patterns.
44+
45+
## Usage with block theme patterns
46+
47+
The block theme includes two post-meta patterns that use this block:
48+
49+
**Avatar, Byline, and Date on multiple lines** (48px avatar, stacked layout):
50+
```html
51+
<!-- wp:newspack/avatar {"size":48,"linkToAuthorArchive":true} /-->
52+
```
53+
Used by post header Style 1, which is the default for most single post templates (`single.html`, `large-image.html`, `wide-image.html`, `full-width-image.html`, `50-50-image.html`).
54+
55+
**Avatar, Byline, and Date on the same line** (24px avatar, compact horizontal layout):
56+
```html
57+
<!-- wp:newspack/avatar {"size":24,"linkToAuthorArchive":true} /-->
58+
```
59+
Used by post header Style 2, which is used in the `sidebar-layout.html` template.
60+
61+
Both patterns fall back to the core `avatar` and `post-author` blocks when the Newspack Plugin is not active.
62+
63+
## Styling
64+
65+
The block supports standard styles, and the avatar size is exposed as a `--avatar-size` CSS custom property on the wrapper, which is used by both the image sizing and the stacked style calculations.
66+
67+
## Related
68+
69+
- [Newspack Byline Block](../byline/README.md) - Displays author name attribution, designed to be used alongside this block.
70+
- [Newspack Bylines Feature](../../../includes/class-bylines.php) - The underlying custom bylines system.
71+
- [useCoAuthors hook](../../shared/hooks/use-coauthors.js) - Shared hook for CoAuthors Plus integration, used by both Avatar and Byline blocks.

0 commit comments

Comments
 (0)