Skip to content

ColorPicker : Fix inconsistent HEX input clearing behavior#77912

Merged
mirka merged 5 commits into
WordPress:trunkfrom
dpmehta:fix/colorpicker-inconsistent-behavior
May 5, 2026
Merged

ColorPicker : Fix inconsistent HEX input clearing behavior#77912
mirka merged 5 commits into
WordPress:trunkfrom
dpmehta:fix/colorpicker-inconsistent-behavior

Conversation

@dpmehta
Copy link
Copy Markdown
Contributor

@dpmehta dpmehta commented May 4, 2026

What?

Closes #75243

This PR ensures that the ColorPicker component consistently resets to black when its HEX input field is cleared, regardless of whether the deletion happens character by character or in a single action (e.g., selecting all and deleting).

Why?

Previously, clearing the HEX input in a single action would trigger an early return in the handleChange function because of a falsy value check. This left the previously selected color active in the picker despite the input field being empty, causing user confusion. By strictly checking for undefined, we allow the empty string to be processed, resulting in a consistent fallback to black.

How?

The logic in packages/components/src/color-picker/hex-input.tsx was modified to change the early return condition from if ( ! nextValue ) to if ( nextValue === undefined ). This ensures that empty strings (which are falsy) are passed to colord(), which handles invalid/empty strings by defaulting to black. Unit tests in packages/components/src/color-picker/test/index.tsx were also updated to reflect the additional onChange call triggered by the field clearance.

Testing Instructions

  1. Open any block or component that uses the ColorPicker (e.g., a Paragraph block's text color).
  2. Select a custom color from the picker.
  3. Click into the HEX input field.
  4. Select the entire HEX value (e.g., Cmd+A or Ctrl+A) and press Backspace or Delete.
  5. Expected Result: The color picker should reset to black, and the input field should display #000000.
  6. Repeat the process but delete the HEX value character by character. Verify it also resets to black.

Testing Instructions for Keyboard

  1. Tab into the ColorPicker and navigate to the HEX input field.
  2. Type a custom HEX code (e.g., f00).
  3. Press Cmd+A (Mac) or Ctrl+A (Windows) to select the text.
  4. Press Backspace.
  5. Confirm the color picker updates visually to black and the input field updates to #000000.

Screenshots or screencast

Before Fix :

ColorPicker-issue.mov

After Fix :

after-fix.mov

Use of AI Tools

AI is used to draft PR description.

@github-actions github-actions Bot added the [Package] Components /packages/components label May 4, 2026
@dpmehta dpmehta marked this pull request as ready for review May 4, 2026 13:23
@dpmehta dpmehta requested review from a team and ajitbohra as code owners May 4, 2026 13:23
@github-actions
Copy link
Copy Markdown

github-actions Bot commented May 4, 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: dpmehta <mehtadev@git.wordpress.org>
Co-authored-by: mirka <0mirka00@git.wordpress.org>
Co-authored-by: ciampo <mciampini@git.wordpress.org>
Co-authored-by: Mustafabharmal <mustafabharmal@git.wordpress.org>

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

@mirka mirka added the [Type] Bug An existing feature does not function as intended label May 4, 2026
Copy link
Copy Markdown
Member

@mirka mirka left a comment

Choose a reason for hiding this comment

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

The fix looks good to me, thanks.

We'll also want a test to cover this, and also a changelog entry.

@dpmehta
Copy link
Copy Markdown
Contributor Author

dpmehta commented May 5, 2026

@mirka Thank you so much for review and suggestion, as per suggestion i have added relevant test case to cover this and also added CHANGELOG entry, Please have a look once 🙌.

@dpmehta dpmehta requested a review from mirka May 5, 2026 06:43
Copy link
Copy Markdown
Member

@mirka mirka left a comment

Choose a reason for hiding this comment

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

Looks good, thanks!

@mirka mirka enabled auto-merge (squash) May 5, 2026 12:10
@ciampo
Copy link
Copy Markdown
Contributor

ciampo commented May 5, 2026

@mirka performance checks are timing out (again), do you think we can skip CI checks and force merge?

@mirka mirka disabled auto-merge May 5, 2026 12:27
@mirka mirka merged commit 62d8820 into WordPress:trunk May 5, 2026
50 of 52 checks passed
@github-actions github-actions Bot added this to the Gutenberg 23.2 milestone May 5, 2026
@dpmehta
Copy link
Copy Markdown
Contributor Author

dpmehta commented May 5, 2026

Thank you so much @mirka and @ciampo for quick reply and review, appreciate it 🙌 🙇🏻

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

[Package] Components /packages/components [Type] Bug An existing feature does not function as intended

Projects

None yet

Development

Successfully merging this pull request may close these issues.

ColorPicker : Inconsistent behavior of default color when cleared one by one character and when cleared in one action

3 participants