15 lines
390 B
JavaScript
15 lines
390 B
JavaScript
/**
|
|
* Svelte action: autofocus an element on mount.
|
|
* Usage: <input use:autofocus />
|
|
*/
|
|
export function autofocus(node) {
|
|
// Use requestAnimationFrame to ensure DOM is settled
|
|
requestAnimationFrame(() => {
|
|
node.focus()
|
|
// On mobile, also try after a slight delay for virtual keyboard
|
|
if ('ontouchstart' in window) {
|
|
setTimeout(() => node.focus(), 100)
|
|
}
|
|
})
|
|
}
|