|
| 1 | +import { Attachment } from 'svelte/attachments'; |
| 2 | +import { focusMove } from './focus.js'; |
| 3 | + |
| 4 | +/** |
| 5 | + * Auto focus node when rendered. Useful for inputs |
| 6 | + */ |
| 7 | +export function autoFocus( |
| 8 | + options?: Parameters<typeof focusMove>['0'] |
| 9 | +): Attachment<HTMLElement | SVGElement> { |
| 10 | + // Delay by 5ms by default since Dialog/Drawer/Menu also call `focusMove` but with default `0ms` delay, and we want to focus last |
| 11 | + // Chrome works with `1ms`, but Firefox required `2ms` and Safari required `3ms`, so using `5ms` as a buffer |
| 12 | + return focusMove({ delay: 5, ...options }); |
| 13 | +} |
| 14 | + |
| 15 | +/** |
| 16 | + * Selects the text inside a text node when the node is focused |
| 17 | + */ |
| 18 | +export function selectOnFocus(): Attachment<HTMLInputElement | HTMLTextAreaElement> { |
| 19 | + return (node: HTMLInputElement | HTMLTextAreaElement) => { |
| 20 | + const handleFocus = (event: Event) => { |
| 21 | + node.select(); |
| 22 | + }; |
| 23 | + |
| 24 | + node.addEventListener('focus', handleFocus); |
| 25 | + |
| 26 | + return () => { |
| 27 | + node.removeEventListener('focus', handleFocus); |
| 28 | + }; |
| 29 | + }; |
| 30 | +} |
| 31 | + |
| 32 | +/** |
| 33 | + * Blurs the node when Escape is pressed |
| 34 | + */ |
| 35 | +export function blurOnEscape(): Attachment<HTMLInputElement | HTMLTextAreaElement> { |
| 36 | + return (node: HTMLInputElement | HTMLTextAreaElement) => { |
| 37 | + const handleKey = (event: Event) => { |
| 38 | + if (event instanceof KeyboardEvent && event.key === 'Escape') { |
| 39 | + node.blur(); |
| 40 | + } |
| 41 | + }; |
| 42 | + |
| 43 | + node.addEventListener('keydown', handleKey); |
| 44 | + |
| 45 | + return () => { |
| 46 | + node.removeEventListener('keydown', handleKey); |
| 47 | + }; |
| 48 | + }; |
| 49 | +} |
| 50 | + |
| 51 | +/** |
| 52 | + * Automatically resize textarea based on content |
| 53 | + * See: |
| 54 | + * - https://svelte.dev/repl/ead0f1fcd2d4402bbbd64eca1d665341?version=3.14.1 |
| 55 | + * - https://svelte.dev/repl/f1a7e24a08a54947bb4447f295c741fb?version=3.14.1 |
| 56 | + */ |
| 57 | +export function autoHeight(): Attachment<HTMLTextAreaElement> { |
| 58 | + return (node: HTMLTextAreaElement) => { |
| 59 | + function resize({ target }: { target: EventTarget | null }) { |
| 60 | + if (target instanceof HTMLElement) { |
| 61 | + target.style.height = '1px'; |
| 62 | + target.style.height = +target.scrollHeight + 'px'; |
| 63 | + } |
| 64 | + } |
| 65 | + |
| 66 | + node.style.overflow = 'hidden'; |
| 67 | + node.addEventListener('input', resize); |
| 68 | + |
| 69 | + // Resize initially |
| 70 | + resize({ target: node }); |
| 71 | + |
| 72 | + return () => { |
| 73 | + node.removeEventListener('input', resize); |
| 74 | + }; |
| 75 | + }; |
| 76 | +} |
| 77 | + |
| 78 | +/** |
| 79 | + * Debounce event handler (change, input, etc) |
| 80 | + */ |
| 81 | +export function debounceEvent( |
| 82 | + options?: { type: string; listener: (e: Event) => any; timeout?: number } | null |
| 83 | +): Attachment<HTMLInputElement | HTMLTextAreaElement> { |
| 84 | + return (node: HTMLInputElement | HTMLTextAreaElement) => { |
| 85 | + let lastTimeoutId: ReturnType<typeof setTimeout>; |
| 86 | + |
| 87 | + if (options) { |
| 88 | + const { type, listener, timeout } = options; |
| 89 | + |
| 90 | + function onEvent(e: Event) { |
| 91 | + clearTimeout(lastTimeoutId); |
| 92 | + lastTimeoutId = setTimeout(() => { |
| 93 | + listener(e); |
| 94 | + }, timeout ?? 300); |
| 95 | + } |
| 96 | + |
| 97 | + node.addEventListener(type, onEvent); |
| 98 | + |
| 99 | + return () => { |
| 100 | + node.removeEventListener(type, onEvent); |
| 101 | + clearTimeout(lastTimeoutId); |
| 102 | + }; |
| 103 | + } |
| 104 | + |
| 105 | + return () => { |
| 106 | + clearTimeout(lastTimeoutId); |
| 107 | + }; |
| 108 | + }; |
| 109 | +} |
0 commit comments