Skip to content

Commit

Permalink
Add detect keyboard open
Browse files Browse the repository at this point in the history
  • Loading branch information
andrea-zecevic committed Jan 28, 2025
1 parent 21ecf73 commit b4fdc03
Show file tree
Hide file tree
Showing 3 changed files with 59 additions and 1 deletion.
41 changes: 41 additions & 0 deletions apps/app/src/hooks/useDetectKeyboardOpen.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
import { useEffect, useState } from 'react';

const useDetectKeyboardOpen = (
minKeyboardHeight = 300,
defaultValue = false,
) => {
const [isKeyboardOpen, setIsKeyboardOpen] = useState(defaultValue);

useEffect(() => {
const listener = () => {
const viewportHeight =
window.visualViewport && window.visualViewport.height !== null
? window.visualViewport.height
: window.innerHeight;

const newState =
window.screen.height - minKeyboardHeight > viewportHeight;
if (isKeyboardOpen !== newState) {
setIsKeyboardOpen(newState);
}
};

if (typeof window.visualViewport !== 'undefined') {
window.visualViewport?.addEventListener('resize', listener);
} else {
window.addEventListener('resize', listener);
}

return () => {
if (typeof window.visualViewport !== 'undefined') {
window.visualViewport?.removeEventListener('resize', listener);
} else {
window.removeEventListener('resize', listener);
}
};
}, [isKeyboardOpen, minKeyboardHeight]);

return isKeyboardOpen;
};

export default useDetectKeyboardOpen;
11 changes: 11 additions & 0 deletions apps/app/src/pages/LoginPage/LoginPage.module.scss
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,7 @@
opacity: 0.6;
text-decoration: none;
align-self: flex-end;
margin-top: -16px;
}

.buttonContainer {
Expand All @@ -122,6 +123,16 @@
margin-top: auto;
transition: transform 0.3s ease;

@media (max-width: $breakpoint-tablet) {
&.keyboardOpen {
transform: translateY(-140px);
}
&:not(.keyboardOpen) {
transform: translateY(0);
transition: transform 0.3s ease-in-out;
}
}

@media (min-width: $breakpoint-tablet) {
margin: 0 auto;
min-width: 470px;
Expand Down
8 changes: 7 additions & 1 deletion apps/app/src/pages/LoginPage/LoginPage.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,16 @@ import closeIcon from '../../assets/icons/close-icon.svg';
import Button from '../../components/Button';
import googleIcon from '../../assets/icons/google.svg';
import { RouteNames } from '../../router/routes';
import useDetectKeyboardOpen from '../../hooks/useDetectKeyboardOpen';

export const LoginPage = () => {
const [email, setEmail] = useState('');
const [password, setPassword] = useState('');
const [emailError, setEmailError] = useState('');
const [passwordError, setPasswordError] = useState('');

const isKeyboardOpen = useDetectKeyboardOpen(400, false);

const validateInputs = () => {
let isValid = true;
const emailRegex = /^[^\s@]+@[^\s@]+\.[^\s@]+$/;
Expand Down Expand Up @@ -74,7 +77,10 @@ export const LoginPage = () => {
Zaboravili ste lozinku?
</a>
</div>
<div className={c.buttonContainer}>
<div
className={`${c.buttonContainer} ${
isKeyboardOpen ? c.keyboardOpen : ''
}`}>
<Button variant='orange' onClick={handleLogin}>
Prijavi se
</Button>
Expand Down

0 comments on commit b4fdc03

Please sign in to comment.