import { useRef, useEffect } from 'react';
/**
* Hook to automatically scroll to bottom when content changes
*/
export function useAutoScroll<T extends HTMLElement>() {
const ref = useRef<T>(null);
useEffect(() => {
if (ref.current) {
ref.current.scrollTop = ref.current.scrollHeight;
}
});
return ref;
}
/**
* Hook to manage focus on an input element
*/
export function useAutoFocus<T extends HTMLElement>() {
const ref = useRef<T>(null);
useEffect(() => {
if (ref.current) {
ref.current.focus();
}
}, []);
return ref;
}
/**
* Hook to handle escape key press
*/
export function useEscapeKey(callback: () => void): void {
useEffect(() => {
const handleEscapeKey = (event: KeyboardEvent) => {
if (event.key === 'Escape') {
callback();
}
};
document.addEventListener('keydown', handleEscapeKey);
return () => document.removeEventListener('keydown', handleEscapeKey);
}, [callback]);
}