daisyui icon indicating copy to clipboard operation
daisyui copied to clipboard

bug: V5 Dialog does not put focus on first focusable element

Open mathieulesniak opened this issue 1 year ago β€’ 14 comments

What version of daisyUI are you using?

v5 beta 6

Which browsers are you seeing the problem on?

Chrome

Reproduction URL

https://codepen.io/mlesniak/pen/MYgxPxr

Describe your issue

Context: When opening a dialog html element, focus is automatically set to first focusable element in the dialog. In DaisyUI v5 beta 6, the first focusable element does not take focus once the modal is opened (JS way) In addition, trying to focus an input with JS does not seems to work either.

In v4, focus was ok when opening a modal : https://codepen.io/mlesniak/pen/ogvVaOR (whereas text field does not show blinking caret)

Steps to reproduce:

  • open the according codepen (v4 or v5)
  • click on button open modal in daisy UI
  • try to type something. Typed text should appear in modal input

mathieulesniak avatar Jan 31 '25 15:01 mathieulesniak

Thank you @mathieulesniak for reporting issues. It helps daisyUI a lot πŸ’š
I'll be working on issues one by one. I will help with this one as soon as a I find a solution.
In the meantime providing more details and reproduction links would be helpful.

github-actions[bot] avatar Jan 31 '25 15:01 github-actions[bot]

fixed in 5.0.0-beta.7

saadeghi avatar Feb 06 '25 21:02 saadeghi

It seems that this issue has reoccurred in 5.0.0-beta.9. It was working fine in beta.8.

ymmt07039 avatar Mar 08 '25 10:03 ymmt07039

Also experiencing this issue in 5.0.0.

The codepen for reproducing also still seems valid, you just have to replace the version, none the less should i open a new issue?

Conni2461 avatar Mar 11 '25 08:03 Conni2461

This effectively killed my cmd+k modal today...

delasource avatar Mar 12 '25 16:03 delasource

This can be easily seen on the demo of https://nexus.daisyui.com/dashboards/ecommerce - just click the search button on the top (it should automatically set focus to the input)

delasource avatar Mar 14 '25 10:03 delasource

I'm working on it

saadeghi avatar Mar 14 '25 10:03 saadeghi

seems to be fixed! πŸ‘ thank you

delasource avatar Mar 14 '25 20:03 delasource

@saadeghi I also have the same issue. I've tried in the latest version and it doesn't work.

Even calling element.focus() on the input field inside of the modal does not put the focus on the element.

Is there something especial that needs to be done?

neopointer avatar Apr 27 '25 09:04 neopointer

@neopointer

Is there something especial that needs to be done?

No.
This is fixed already. If it's not working for you, please share a minimal reproduction link so I can help.

saadeghi avatar Apr 27 '25 10:04 saadeghi

@saadeghi here's the reproducer with the latest version: https://codepen.io/neopointer/pen/OPPjvGq

Here's another version without daisy ui: https://codepen.io/neopointer/pen/XJJaqrM

I don't really know if it's a bug, but maybe I'm missing something.

neopointer avatar Apr 27 '25 10:04 neopointer

I'm experiencing the same "issue", not sure if I may be missing some understanding. I have a modal with an input with ID "project-name", it does not focus with autoFocus property (it did before I converted it to DaisyUI modal). I created a function to help ensure focus:

  const handleButtonClick = () => {
    modalRef.current?.showModal();
    const input = window.document.getElementById(
      "project-name"
    ) as HTMLInputElement;
    input.focus();
  };

Which doesn't focus the input either. But this does (after half a second):

  const handleButtonClick = () => {
    modalRef.current?.showModal();
    const input = window.document.getElementById(
      "project-name"
    ) as HTMLInputElement;
    window.setTimeout(() => {
      input.focus();
    }, 500);
  };

~~I have a feeling this is related to the modal's animation.~~ EDIT: Actually now I'm not so sure. Even doing setTimeout to 10ms works, so it might just be because the element is not visible. Either way would be nice if autofocus worked so I didn't have to do this weird workaround

callumbirks avatar May 01 '25 17:05 callumbirks

This issue is back again πŸ™ƒ
Apparently fixing https://github.com/saadeghi/daisyui/issues/3787 causes this issue.

πŸ€” Challenge: If visibility has a transition, the dialog inputs won't get focus, which is understandable: Transition is 0.3s but focus fires at 0s but it can't focus an invisible element.
πŸ‘ Solution, visibility must not have animation.
However there's a bug in Chrome:
πŸ‘Ž If visibility has no duration, it ignores translate transition.

In below video, see how disabling transition of visibility, breaks the transition of translate:

https://github.com/user-attachments/assets/b2a7f5c7-abef-48a2-a7af-0d30822db27d

Reproduction: https://play.tailwindcss.com/FZbfJGTluz?file=css

Let me know if you could find a solution ☹️

saadeghi avatar May 02 '25 16:05 saadeghi

I can also reproduce this on my side as of v5.0.35. I have a Pagefind UI inside a modal that is no longer autofocused after upgrading from Daisy UI 4.

bdmendes avatar May 04 '25 18:05 bdmendes

Hello everyone,

I have the same issue, and for what is worth, here is my css to fix it:

@layer utilities {
    .modal {
        transition:
            translate 0.3s ease-out,
            background-color 0.3s ease-out,
            opacity 0.1s ease-out;
        @starting-style {
            &.modal-open,
            &[open],
            &:target {
                visibility: visible;
            }
        }
    }
}

I simply removed the starting-style that had the visibility: hidden, and removed it from the transition.

It does seem to work as intended, but I haven't tested it extensively. It works for my usecase ! 🀞

Note: Only removing the starting-style visibility is not enough, I had to remove it from the transition as well.

fravan avatar Jun 13 '25 07:06 fravan

I just want to leave my +1 here. After updating from DaisyUI 4 the autofocus doesn't work anymore. The CSS workaround @fravan posted also worked for me. I'll use that in the meantime.

willianba avatar Jul 11 '25 02:07 willianba

@saadeghi would be great if it would be permanently fixed in the library :)

delasource avatar Aug 15 '25 08:08 delasource

@delasource Yes but: https://github.com/saadeghi/daisyui/issues/3440#issuecomment-2847662168

there's a bug in Chrome: If visibility has no duration, it ignores translate transition.

If anyone finds a workaround, let me know πŸ€·β€β™‚οΈ

saadeghi avatar Aug 15 '25 14:08 saadeghi

The workaround is:

.modal {
    visibility: visible !important;
}

(use at own risk :) )

delasource avatar Aug 15 '25 15:08 delasource

Removing visibility works but it makes the modal close without a transition

DanieII avatar Sep 05 '25 21:09 DanieII

@saadeghi I found a solution: https://play.tailwindcss.com/oT5V7KnQ0o?file=css Seems to work smooth even for .modal-end and .modal-bottom

Tested to work as expected on:

  • chrome desktop linux
  • firefox desktop linux
  • chrome android
  • safari ios (with the usual caveat that safari ios is trash and bottom modal stays under the keyboard)

Credits: https://chriskirknielsen.com/blog/prevent-jank-on-focused-elements-in-an-overflow-container/

.modal {
  overflow: clip;
  /*
  instead of
  overflow-x: hidden;
  overflow-y: hidden;
  */
}

.modal-box {
  /* add this to allow focus inside when showing */
  visibility: visible;
}

It also fixes the problem with .collapse in modal (#3835)

Updated example with collapse in modals: https://play.tailwindcss.com/nOguNii64h

pdanpdan avatar Oct 09 '25 19:10 pdanpdan

@pdanpdan Thank you πŸ™
Fixed in 5.1.30

saadeghi avatar Oct 09 '25 21:10 saadeghi

#3835 should also be closed

pdanpdan avatar Oct 09 '25 22:10 pdanpdan

@saadeghi I saw your comment https://github.com/saadeghi/daisyui/issues/3440#issuecomment-2847662168

I think now you can remove the visibility animation because the translate animation will work even if focused. (and then you can remove the newly added visible from modal-box)

pdanpdan avatar Oct 09 '25 22:10 pdanpdan