focus-trapping.vue 1.1 KB

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