stash/ui/v2.5/src/utils/focus.ts
WithoutPants ec6acab2f4
Details operation toolbar (#4714)
* Add scene detail header
* Make common count button and add view count
* Add titles to play count and o count buttons
* Move rating from edit panel
* Include frame rate in header
* Remove redundant title/studio
* Improve numeric rating presentation
* Add star where there is no rating header
* Set rating on blur when click to edit
* Add star to numeric rating on gallery wall card
* Apply click to rate on movie page
* Apply click to rate to performer page
* Apply click to rate to studio page
* Fix rating number presentation on list tables
* Add data-value attributes
2024-04-17 10:29:36 +10:00

31 lines
766 B
TypeScript

import { useRef, useEffect } from "react";
const useFocus = () => {
const htmlElRef = useRef<HTMLInputElement | null>(null);
const setFocus = () => {
const currentEl = htmlElRef.current;
if (currentEl) {
currentEl.focus();
}
};
// eslint-disable-next-line no-undef
return [htmlElRef, setFocus] as const;
};
// focuses on the element only once on mount
export const useFocusOnce = (active?: boolean, override?: boolean) => {
const [htmlElRef, setFocus] = useFocus();
const focused = useRef(false);
useEffect(() => {
if ((!focused.current || override) && active) {
setFocus();
focused.current = true;
}
}, [setFocus, active, override]);
return [htmlElRef, setFocus] as const;
};
export default useFocus;