weather_app/src/lib/autofocus.js
2026-07-24 01:53:36 +00:00

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)
}
})
}