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

The input box in the modal will automatically gain focus #3062

Open
Jevin0 opened this issue Jan 10, 2025 · 1 comment
Open

The input box in the modal will automatically gain focus #3062

Jevin0 opened this issue Jan 10, 2025 · 1 comment
Labels
bug Something isn't working triage

Comments

@Jevin0
Copy link

Jevin0 commented Jan 10, 2025

Environment

nuxt: 3.15.1

Version

v2.20.0

Reproduction

Description

<UModal>
  <UInput v-model="state.name" placeholder=".." />
  <UInput v-model="state.name1" placeholder="..." />
</UModal>

When a modal contains one or more input boxes, the first input box will always get focus automatically.
If I manually add a close button in the upper right corner, it will trigger a validation when I click it. This is not good. How can I avoid the input box getting focus when opening the modal?

Additional context

Image

Logs

@Jevin0 Jevin0 added bug Something isn't working triage labels Jan 10, 2025
@maxsteinwand
Copy link
Contributor

https://headlessui.com/v1/vue/dialog#managing-initial-focus

You can use a ref on a hidden div to change the initial focus. Sadly directly passing null or undefined will not work as this will then automatically use the first element.

<script setup lang="ts">
const isOpen = ref(false);
const hiddenRef = ref(undefined);
</script>

<template>
  <div>
    <UButton label="Open" @click="isOpen = true" />
    <UModal v-model="isOpen" :initial-focus="hiddenRef">
    <div ref="hiddenRef" style="display: none" />
      <div class="p-4">
        <UInput />
      </div>
    </UModal>
  </div>
</template>

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working triage
Projects
None yet
Development

No branches or pull requests

2 participants