Skip to content

Commit 13fb417

Browse files
committed
Default reactions block style based on avatar setting
The reactions block now defaults to the 'facepile' style if avatars are enabled and 'summary' if avatars are disabled, unless a style is explicitly set. The block registration also removes the 'facepile' style when avatars are disabled. This ensures consistent display behavior in both editor and frontend for auto-inserted blocks.
1 parent ce29dfd commit 13fb417

File tree

6 files changed

+43
-9
lines changed

6 files changed

+43
-9
lines changed

build/reactions/index.asset.php

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

build/reactions/index.js

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

build/reactions/render.php

Lines changed: 12 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

includes/class-blocks.php

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,18 @@ public static function register_blocks() {
8484
\register_block_type_from_metadata( ACTIVITYPUB_PLUGIN_DIR . '/build/extra-fields' );
8585
\register_block_type_from_metadata( ACTIVITYPUB_PLUGIN_DIR . '/build/follow-me' );
8686
\register_block_type_from_metadata( ACTIVITYPUB_PLUGIN_DIR . '/build/followers' );
87-
\register_block_type_from_metadata( ACTIVITYPUB_PLUGIN_DIR . '/build/reactions' );
87+
// Register reactions block, conditionally removing facepile style if avatars are disabled.
88+
$reactions_args = array();
89+
if ( ! \get_option( 'show_avatars', true ) ) {
90+
$reactions_args['styles'] = array(
91+
array(
92+
'name' => 'summary',
93+
'label' => __( 'Summary', 'activitypub' ),
94+
'isDefault' => true,
95+
),
96+
);
97+
}
98+
\register_block_type_from_metadata( ACTIVITYPUB_PLUGIN_DIR . '/build/reactions', $reactions_args );
8899

89100
\register_block_type_from_metadata(
90101
ACTIVITYPUB_PLUGIN_DIR . '/build/reply',

src/reactions/edit.js

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ export default function Edit( { attributes, setAttributes, __unstableLayoutClass
6666
const { showAvatars = true } = useOptions();
6767
const hasInitialized = useRef( false );
6868

69-
// On first render, if avatars are disabled and no style class is set, default to summary.
69+
// On first render, set default style based on avatar setting.
7070
useEffect( () => {
7171
if ( hasInitialized.current ) {
7272
return;
@@ -75,10 +75,11 @@ export default function Edit( { attributes, setAttributes, __unstableLayoutClass
7575

7676
// Only apply default if no style has been explicitly chosen yet.
7777
const hasStyleClass = className?.includes( 'is-style-' );
78-
if ( ! showAvatars && ! hasStyleClass ) {
78+
if ( ! hasStyleClass ) {
79+
const defaultStyle = showAvatars ? 'facepile' : 'summary';
7980
setAttributes( {
80-
className: clsx( className, 'is-style-summary' ),
81-
displayStyle: 'summary',
81+
className: clsx( className, `is-style-${ defaultStyle }` ),
82+
displayStyle: defaultStyle,
8283
} );
8384
}
8485
}, [ className, showAvatars, setAttributes ] );

src/reactions/render.php

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,17 @@
5151
// Generate a unique ID for the block.
5252
$block_id = 'activitypub-reactions-block-' . wp_unique_id();
5353

54-
// Determine display style - summary style hides avatars.
54+
/*
55+
* Determine display style - summary style hides avatars.
56+
* For auto-hooked blocks without explicit style, use avatar setting to determine style.
57+
*/
58+
$has_style_class = isset( $attributes['className'] ) && strpos( $attributes['className'], 'is-style-' ) !== false;
59+
if ( ! $has_style_class ) {
60+
$default_style = get_option( 'show_avatars', true ) ? 'facepile' : 'summary';
61+
$attributes['className'] = trim( ( $attributes['className'] ?? '' ) . ' is-style-' . $default_style );
62+
$attributes['displayStyle'] = $default_style;
63+
}
64+
5565
$is_summary = 'summary' === $attributes['displayStyle'];
5666
$show_avatars = ! $is_summary;
5767

@@ -233,6 +243,7 @@ class="reaction-label wp-element-button"
233243
$wrapper_attributes = get_block_wrapper_attributes(
234244
array(
235245
'id' => $block_id,
246+
'class' => $attributes['className'] ?? '',
236247
'data-wp-interactive' => 'activitypub/reactions',
237248
'data-wp-context' => wp_json_encode( $context, JSON_HEX_TAG | JSON_HEX_APOS | JSON_HEX_QUOT | JSON_HEX_AMP ),
238249
'data-wp-init' => 'callbacks.initReactions',

0 commit comments

Comments
 (0)