Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Enhance the logic for choosing which element to focus initially #52

Merged
merged 1 commit into from
Jan 5, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 17 additions & 10 deletions src/user-interface.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ const UserInterface = ({
const [copyToClipboardTimeoutId, setCopyToClipboardTimeoutId] =
useState<ReturnType<typeof setTimeout> | null>(null);
const [pendingFillInPassword, setPendingFillInPassword] = useState(false);
const domainRef = useRef<HTMLInputElement>(null);
const universalPasswordRef = useRef<HTMLInputElement>(null);

// eslint-disable-next-line react-hooks/exhaustive-deps -- We need to debounce this function.
Expand All @@ -63,12 +64,17 @@ const UserInterface = ({
);

useEffect(() => {
const inputElement = universalPasswordRef.current;
const domainElement = domainRef.current;
const universalPasswordElement = universalPasswordRef.current;

if (inputElement !== null) {
inputElement.focus();
if (initialDomain === null) {
if (domainElement !== null) {
domainElement.focus();
}
} else if (universalPasswordElement !== null) {
universalPasswordElement.focus();
}
}, []);
}, [initialDomain]);

useEffect(() => {
updateGeneratedPassword(domain, universalPassword);
Expand All @@ -77,20 +83,20 @@ const UserInterface = ({
const onResetDomain = useCallback((): void => {
setDomain(initialDomainOrEmpty);

const inputElement = universalPasswordRef.current;
const universalPasswordElement = universalPasswordRef.current;

if (inputElement !== null) {
inputElement.focus();
if (universalPasswordElement !== null) {
universalPasswordElement.focus();
}
}, [initialDomainOrEmpty]);

const onToggleUniversalPasswordHidden = useCallback((): void => {
setIsUniversalPasswordHidden(!isUniversalPasswordHidden);

const inputElement = universalPasswordRef.current;
const universalPasswordElement = universalPasswordRef.current;

if (inputElement !== null) {
inputElement.focus();
if (universalPasswordElement !== null) {
universalPasswordElement.focus();
}
}, [isUniversalPasswordHidden]);

Expand Down Expand Up @@ -169,6 +175,7 @@ const UserInterface = ({
monospace={false}
onChange={setDomain}
placeholder="example.com"
ref={domainRef}
updating={false}
value={domain}
/>
Expand Down
Loading