Skip to content

19 - 切换焦点指令 #2957

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

Open
Gilfoyle-sy opened this issue Apr 7, 2025 · 0 comments
Open

19 - 切换焦点指令 #2957

Gilfoyle-sy opened this issue Apr 7, 2025 · 0 comments

Comments

@Gilfoyle-sy
Copy link

<script setup lang="ts">
import { ref } from 'vue'
import type { DirectiveBinding } from 'vue'

const state = ref(false)

/**
 * Implement the custom directive
 * Make sure the input element focuses/blurs when the 'state' is toggled
 *
 */
const VFocus = {
  mounted: (el: HTMLElement) => el.focus(),
  updated: (el: HTMLElement, binding: DirectiveBinding) => {
    void (binding.value ? el.focus() : el.blur())
  },
}

setInterval(() => {
  state.value = !state.value
}, 2000)
</script>

<template>
  <input v-focus="state" type="text" />
</template>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

1 participant