Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(circuit-ui): ColorInput paste and change events #2687

Merged
merged 5 commits into from
Oct 12, 2024

Conversation

matoous
Copy link
Contributor

@matoous matoous commented Sep 17, 2024

Improve ColorInput, specifically:

  • handle default value and value props properly
  • ensure color picker and hex color input are always synchronized
  • handle properly paste events with hex color

@matoous matoous self-assigned this Sep 17, 2024
Copy link

changeset-bot bot commented Sep 17, 2024

🦋 Changeset detected

Latest commit: db0b401

The changes in this PR will be included in the next version bump.

This PR includes changesets to release 1 package
Name Type
@sumup/circuit-ui Patch

Not sure what this means? Click here to learn what changesets are.

Click here if you're a maintainer who wants to add another changeset to this PR

Copy link

vercel bot commented Sep 17, 2024

The latest updates on your projects. Learn more about Vercel for Git ↗︎

Name Status Preview Comments Updated (UTC)
oss-circuit-ui ✅ Ready (Inspect) Visit Preview 💬 Add feedback Oct 12, 2024 11:54am

Copy link

codecov bot commented Sep 17, 2024

Codecov Report

Attention: Patch coverage is 96.15385% with 2 lines in your changes missing coverage. Please review.

Project coverage is 86.74%. Comparing base (14643b2) to head (db0b401).
Report is 1 commits behind head on main.

Files with missing lines Patch % Lines
...es/circuit-ui/components/ColorInput/ColorInput.tsx 96.15% 2 Missing ⚠️
Additional details and impacted files

Impacted file tree graph

@@            Coverage Diff             @@
##             main    #2687      +/-   ##
==========================================
+ Coverage   86.63%   86.74%   +0.11%     
==========================================
  Files         210      210              
  Lines       12000    12043      +43     
  Branches     1507     1520      +13     
==========================================
+ Hits        10396    10447      +51     
+ Misses       1551     1543       -8     
  Partials       53       53              
Files with missing lines Coverage Δ
...es/circuit-ui/components/ColorInput/ColorInput.tsx 98.10% <96.15%> (+7.66%) ⬆️

.changeset/beige-planes-shave.md Outdated Show resolved Hide resolved
Comment on lines +173 to +179
expect(colorPicker.value).toBe('#000000');
expect(colorInput.value).toBe('');
Copy link
Member

Choose a reason for hiding this comment

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

Why are the values different here?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

#000000 is the default for color picker, to my understanding it can never not have a value whereas the color input can be empty - meaning no color submitted.

packages/circuit-ui/components/ColorInput/ColorInput.tsx Outdated Show resolved Hide resolved

const pastedText = e.clipboardData.getData('text/plain').trim();

if (!pastedText || !/^#[0-9A-F]{6}$/i.test(pastedText)) {
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
if (!pastedText || !/^#[0-9A-F]{6}$/i.test(pastedText)) {
if (!pastedText || !/^#?[0-9A-F]{3,6}$/i.test(pastedText)) {

Copy link
Member

Choose a reason for hiding this comment

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

I think the # should be optional, but then you also need to add it when setting the value of the color picker input.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Adjusted for pasting without #. Allowing 3-char colors is a little bit more tricky because html color input only support 7-character strings (6 for color + #)1.

Footnotes

  1. https://developer.mozilla.org/en-US/docs/Web/HTML/Element/input/color

const onPickerColorChange: ChangeEventHandler<InputElement> = (e) => {
setCurrentColor(e.target.value);
if (colorInputRef.current) {
colorInputRef.current.value = e.target.value.replace('#', '');
Copy link
Member

Choose a reason for hiding this comment

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

Might be worth to extract the formatting logic for the color input and color picker values into reusable functions.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Tried and honestly didn't find it more readable. I think with just # even these inline calls should be understandable as long as all of them happen inside ColorInput. Happy to adjust though if you have specific suggestion how.

Improve ColorInput, specifically:

- handle default value and value props properly
- ensure color picker and hex color input are always synchronized
- handle properly paste events with hex color
Copy link
Member

@connor-baer connor-baer left a comment

Choose a reason for hiding this comment

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

Nice, thank you! 🙌

@matoous
Copy link
Contributor Author

matoous commented Oct 12, 2024

@connor-baer can I merge? Or anything left? 🙈

@connor-baer
Copy link
Member

@matoous Feel free to merge :shipit:

@matoous matoous merged commit 2e85454 into main Oct 12, 2024
13 of 14 checks passed
@matoous matoous deleted the md/color-input-fixups branch October 12, 2024 12:41
@connor-baer connor-baer mentioned this pull request Oct 12, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants