focus-trapping.vue 1.1 KB

12345678910111213141516171819202122232425262728293031323334353637
  1. <template>
  2. <el-button text @click="dialogVisible = true">click to open the Dialog</el-button>
  3. <div>
  4. <p>Close dialog and the input will be focused</p>
  5. <el-input ref="inputRef" placeholder="Please input" />
  6. </div>
  7. <el-dialog v-model="dialogVisible" destroy-on-close title="Tips" width="30%" @close-auto-focus="handleCloseAutoFocus">
  8. <span>This is a message</span>
  9. <el-divider />
  10. <el-input placeholder="Initially focused" />
  11. <template #footer>
  12. <span class="dialog-footer">
  13. <el-button @click="dialogVisible = false">Cancel</el-button>
  14. <el-button type="primary" @click="dialogVisible = false">Confirm</el-button>
  15. </span>
  16. </template>
  17. </el-dialog>
  18. </template>
  19. <script lang="ts" setup>
  20. import { ref } from 'vue'
  21. import type { ElInput } from 'element-plus'
  22. const dialogVisible = ref(false)
  23. const inputRef = ref<InstanceType<typeof ElInput>>()
  24. const handleCloseAutoFocus = () => {
  25. inputRef.value?.focus()
  26. }
  27. </script>
  28. <style scoped>
  29. .dialog-footer button:first-child {
  30. margin-right: 10px;
  31. }
  32. </style>