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: add field modal does not close on first close button click #1455

Open
wants to merge 2 commits into
base: main
Choose a base branch
from

Conversation

BohdanOne
Copy link
Contributor

Resolves: DT-2345

Description

When adding a new field in a a page or custom type in slice-machine the edit modal won't close on the first X button click.
This is happening because Label input is being auto-focused.

onMouseDown added to the close button will execute close function before the focus is being hijacked by input.

Checklist

  • If my changes require tests, I added them.
  • If my changes affect backward compatibility, it has been discussed.
  • If my changes require an update to the CONTRIBUTING.md guide, I updated it.

Preview

BEFORE

Nagranie.z.ekranu.2024-09-11.o.12.16.24.mov

AFTER

Nagranie.z.ekranu.2024-09-11.o.12.30.53.mov

How to QA 1

Footnotes

  1. Please use these labels when submitting a review:
    ❓ #ask: Ask a question.
    💡 #idea: Suggest an idea.
    ⚠️ #issue: Strongly suggest a change.
    🎉 #nice: Share a compliment.

@BohdanOne BohdanOne requested a review from a team as a code owner September 11, 2024 10:32
Copy link

linear bot commented Sep 11, 2024

Copy link

vercel bot commented Sep 11, 2024

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

Name Status Preview Updated (UTC)
slice-machine ✅ Ready (Inspect) Visit Preview Sep 11, 2024 1:01pm

@@ -234,6 +234,8 @@ const EditModal = ({ close, data, fields, onSave, zoneType }) => {
/>
<Close // eslint-disable-next-line @typescript-eslint/no-unsafe-argument, @typescript-eslint/no-unsafe-call, @typescript-eslint/no-unsafe-assignment, @typescript-eslint/no-unsafe-assignment, @typescript-eslint/no-unsafe-argument, @typescript-eslint/no-unsafe-assignment, @typescript-eslint/no-unsafe-assignment
onClick={close}
// eslint-disable-next-line @typescript-eslint/no-unsafe-assignment
Copy link
Contributor

@dani-mp dani-mp Sep 11, 2024

Choose a reason for hiding this comment

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

Let's add this comment at the top of the file so we don't have to add it in each line. We said we were going to do this but if we never start we'll never do it!

@@ -234,6 +234,8 @@ const EditModal = ({ close, data, fields, onSave, zoneType }) => {
/>
<Close // eslint-disable-next-line @typescript-eslint/no-unsafe-argument, @typescript-eslint/no-unsafe-call, @typescript-eslint/no-unsafe-assignment, @typescript-eslint/no-unsafe-assignment, @typescript-eslint/no-unsafe-argument, @typescript-eslint/no-unsafe-assignment, @typescript-eslint/no-unsafe-assignment
onClick={close}
// eslint-disable-next-line @typescript-eslint/no-unsafe-assignment
onMouseDown={close} // will execute the close function before Label input hijacks the focus
Copy link
Contributor

Choose a reason for hiding this comment

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

I wonder if this is the best way to solve this, it looks like a hack. This is a common scenario on the web (clicking a button while another element has the focus). We rarely have to duplicate the on-click handler to fix this, and with this, we're executing the close function twice (it might be ok for this particular scenario, but it is still not ideal).

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I'm not 100% sure about what is exact root of the problem, it's most likely related with re-render happening when input is validated, and my idea was that after the re-render focus is brought back to input (if I disable autofocus, button works without any problem).
I agree the solution looks like a hack, but couldn't find any other working (might try to spend some more time on it).

@jomifepe
Copy link
Contributor

jomifepe commented Sep 11, 2024

Small comment on the description: This doesn't seem to be caused by the label autofocus, the same happens if you open the modal, focus an input and try to close the modal with the X button. This is may be related to the re-render that happens on input blur.

@BohdanOne
Copy link
Contributor Author

Small comment on the description: This doesn't seem to be caused by the label autofocus, the same happens if you open the modal, focus an input and try to close the modal with the X button. This is may be related to the re-render that happens on input blur, that validates the form.
I'm not 100% sure what is exactly the problem, and yes I also think is related to the re-render.
What I'm sure about is that the issue is not present when there is no autofocus at all.

@jomifepe
Copy link
Contributor

Small comment on the description: This doesn't seem to be caused by the label autofocus, the same happens if you open the modal, focus an input and try to close the modal with the X button. This is may be related to the re-render that happens on input blur, that validates the form.
I'm not 100% sure what is exactly the problem, and yes I also think is related to the re-render.
What I'm sure about is that the issue is not present when there is no autofocus at all.

That's not the result I'm getting when I disabled the autofocus 😅

Kapture.2024-09-11.at.12.37.32.mp4

@BohdanOne
Copy link
Contributor Author

BohdanOne commented Sep 11, 2024

Small comment on the description: This doesn't seem to be caused by the label autofocus, the same happens if you open the modal, focus an input and try to close the modal with the X button. This is may be related to the re-render that happens on input blur, that validates the form.
I'm not 100% sure what is exactly the problem, and yes I also think is related to the re-render.
What I'm sure about is that the issue is not present when there is no autofocus at all.

That's not the result I'm getting when I disabled the autofocus 😅

Kapture.2024-09-11.at.12.37.32.mp4

How do you disabled it? (I just removed the passed autofocus prop) Ok, I see it, I didn't try to click on the input when autofocus was disabled 🤦

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.

3 participants