Skip to content

Only add the Generate Alt Text button to selected image blocks#356

Merged
dkotter merged 3 commits intoWordPress:developfrom
dkotter:fix/alt-text-pattern
Apr 1, 2026
Merged

Only add the Generate Alt Text button to selected image blocks#356
dkotter merged 3 commits intoWordPress:developfrom
dkotter:fix/alt-text-pattern

Conversation

@dkotter
Copy link
Copy Markdown
Collaborator

@dkotter dkotter commented Mar 31, 2026

What?

Closes WordPress/ai-provider-for-openai#16

Ensure we add the alt text generation controls only to selected image blocks

Why?

As reported in the above issue, if you have an image block within a pattern and you toggle back and forth between the image and another block in the pattern, the alt text generation button ends up displaying weird (that issue mentions it duplicating but I wasn't able to reproduce that).

To fix this, we just need to ensure we only show our controls on selected image blocks.

How?

Instead of checking if the current block is an image, we also add a check to ensure that block is currently selected

Use of AI Tools

None

Testing Instructions

  1. Pull this branch down and run npm i && npm run build
  2. Turn on the Alt Text Generation Experiment
  3. Add an image into a post and with it selected, ensure you see a button in the sidebar to generate alt text
  4. Insert a pattern
  5. Ensure there's an image block within the pattern
  6. Select that image block and ensure you see the same button in the sidebar
  7. Using the Content controls in the pattern sidebar, click into another block within the pattern
  8. Ensure you no longer see the alt text controls
  9. Click back into the image block and ensure it shows again

Screenshots

Before After
Alt text controls showing in the pattern editor when they shouldn't Alt text controls not showing in the pattern editor
Open WordPress Playground Preview

…cks to ensure we don't show that when in the pattern editing mode
@dkotter dkotter added this to the 0.7.0 milestone Mar 31, 2026
@dkotter dkotter self-assigned this Mar 31, 2026
@github-actions
Copy link
Copy Markdown

github-actions bot commented Mar 31, 2026

The following accounts have interacted with this PR and/or linked issues. I will continue to update these lists as activity occurs. You can also manually ask me to refresh this list by adding the props-bot label.

If you're merging code through a pull request on GitHub, copy and paste the following into the bottom of the merge commit message.

Co-authored-by: dkotter <dkotter@git.wordpress.org>
Co-authored-by: gziolo <gziolo@git.wordpress.org>
Co-authored-by: abdullah1908 <abdullahramzan@git.wordpress.org>

To understand the WordPress project's expectations around crediting contributors, please review the Contributor Attribution page in the Core Handbook.

@codecov
Copy link
Copy Markdown

codecov bot commented Mar 31, 2026

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 57.70%. Comparing base (249ecb9) to head (04555a0).
⚠️ Report is 4 commits behind head on develop.

Additional details and impacted files
@@            Coverage Diff             @@
##             develop     #356   +/-   ##
==========================================
  Coverage      57.70%   57.70%           
  Complexity       617      617           
==========================================
  Files             46       46           
  Lines           3173     3173           
==========================================
  Hits            1831     1831           
  Misses          1342     1342           
Flag Coverage Δ
unit 57.70% <ø> (ø)

Flags with carried forward coverage won't be shown. Click here to find out more.

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

@dkotter dkotter requested a review from jeffpaul March 31, 2026 20:03
gziolo
gziolo previously approved these changes Apr 1, 2026
Copy link
Copy Markdown
Member

@gziolo gziolo left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The proposed change makes perfect sense.

Copy link
Copy Markdown
Member

@gziolo gziolo left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Minor suggestion: from a React standpoint, it might be better to always render <BlockEdit> in a stable position (inside the fragment) and only conditionally render <AltTextControls>. Right now, toggling isSelected and changing other conditions causes <BlockEdit> to shift between being a standalone element and a fragment child, which can trigger unnecessary unmount/remount cycles.

Something like:

const showControls =
    props.name === 'core/image' &&
    props.isSelected &&
    aiAltTextGenerationData?.enabled;

return (
    <>
        <BlockEdit { ...props } />
        { showControls && (
            <AltTextControls
                clientId={ props.clientId }
                attributes={ props.attributes }
                setAttributes={ props.setAttributes }
            />
        ) }
    </>
);

I haven't located a concrete bug caused by this here, but based on past experience, keeping the component tree structure stable avoids subtle re-mounting and therefore order issues. Not a blocker — just a refinement to consider as a follow-up.

… <BlockEdit> in the same place to avoid any mounting problems
@dkotter
Copy link
Copy Markdown
Collaborator Author

dkotter commented Apr 1, 2026

Minor suggestion: from a React standpoint, it might be better to always render <BlockEdit> in a stable position (inside the fragment) and only conditionally render <AltTextControls>. Right now, toggling isSelected and changing other conditions causes <BlockEdit> to shift between being a standalone element and a fragment child, which can trigger unnecessary unmount/remount cycles.

@gziolo That's a good idea, thanks for the feedback. Updated that here: 76baf57

@dkotter dkotter requested a review from gziolo April 1, 2026 15:05
@dkotter dkotter merged commit d479cf3 into WordPress:develop Apr 1, 2026
17 checks passed
@dkotter dkotter deleted the fix/alt-text-pattern branch April 1, 2026 17:02
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Duplicate "Regenerate Alt Text" button appears in Block Editor sidebar when navigating between blocks

2 participants