focus-trapping.vue 1.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748
  1. <template>
  2. <kk-button text @click="dialogVisible = true">
  3. click to open the Dialog
  4. </kk-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. <kk-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. <kk-button @click="dialogVisible = false">Cancel</kk-button>
  22. <kk-button type="primary" @click="dialogVisible = false">
  23. Confirm
  24. </kk-button>
  25. </span>
  26. </template>
  27. </kk-dialog>
  28. </template>
  29. <script lang="ts" setup>
  30. import { ref } from 'vue'
  31. import { KkButton, KkDialog } from 'kankan-components'
  32. import { ElInput } from 'element-plus'
  33. const dialogVisible = ref(false)
  34. const inputRef = ref<InstanceType<typeof ElInput>>()
  35. const handleCloseAutoFocus = () => {
  36. inputRef.value?.focus()
  37. }
  38. </script>
  39. <style scoped>
  40. .dialog-footer button:first-child {
  41. margin-right: 10px;
  42. }
  43. </style>