added Overlay component

This commit is contained in:
Wenszel
2023-12-11 11:52:06 +01:00
parent e9cb2e4de9
commit aa6c03ebcb
8 changed files with 708 additions and 40 deletions
@@ -7,6 +7,7 @@
border: 1px solid white;
border-radius: 8px;
margin: 20px;
z-index: 2;
}
.name-input-container > button {
margin-top: 5px;
@@ -17,18 +18,6 @@
.name-input-container > input {
margin-top: 10px;
}
.name-overlay {
position: absolute;
display: flex;
align-items: center;
justify-content: center;
top: 0;
left: 0;
width: 100%;
height: 100%;
background-color: rgba(0, 0, 0, 0.5);
z-index: 1;
}
input,
button {
padding: 0;
@@ -22,19 +22,17 @@ const NameInput = ({ isRoomPrivate, roomId }) => {
}, [socket]);
return (
<div className='name-overlay'>
<div className='name-input-container' style={{ height: isRoomPrivate ? '100px' : '50px' }}>
<input placeholder='Nickname' type='text' onChange={nickname.onChange} />
{isRoomPrivate ? (
<input
placeholder='Room password'
type='text'
onChange={password.onChange}
style={{ backgroundColor: isPasswordWrong ? 'red' : null }}
/>
) : null}
<button onClick={handleButtonClick}>JOIN</button>
</div>
<div className='name-input-container' style={{ height: isRoomPrivate ? '100px' : '50px' }}>
<input placeholder='Nickname' type='text' onChange={nickname.onChange} />
{isRoomPrivate ? (
<input
placeholder='Room password'
type='text'
onChange={password.onChange}
style={{ backgroundColor: isPasswordWrong ? 'red' : null }}
/>
) : null}
<button onClick={handleButtonClick}>JOIN</button>
</div>
);
};
@@ -6,6 +6,7 @@ import ReactLoading from 'react-loading';
import './ServerList.css';
import NameInput from '../NameInput/NameInput';
import Overlay from '../../Overlay/Overlay';
const ServerList = () => {
const socket = useContext(SocketContext);
@@ -70,7 +71,11 @@ const ServerList = () => {
</div>
)}
</div>
{joining ? <NameInput roomId={clickedRoom._id} isRoomPrivate={clickedRoom.private} /> : null}
{joining ? (
<Overlay handleOverlayClose={() => setJoining(false)}>
<NameInput roomId={clickedRoom._id} isRoomPrivate={clickedRoom.private} />
</Overlay>
) : null}
</div>
);
};
+9
View File
@@ -0,0 +1,9 @@
import styles from './Overlay.module.css';
import useKeyPress from '../../hooks/useKeyPress';
const Overlay = ({ children, handleOverlayClose }) => {
useKeyPress('Escape', handleOverlayClose);
return <div className={styles.container}>{children}</div>;
};
export default Overlay;
+13
View File
@@ -0,0 +1,13 @@
.container {
position: absolute;
display: flex;
align-items: center;
justify-content: center;
top: 0;
left: 0;
width: 100%;
height: 100%;
background-color: rgba(0, 0, 0, 0.5);
z-index: 1;
cursor: pointer;
}
+34
View File
@@ -0,0 +1,34 @@
import '@testing-library/jest-dom';
import React from 'react';
import { render } from '@testing-library/react';
import Overlay from './Overlay';
import userEvent from '@testing-library/user-event';
describe('Overlay component', () => {
beforeEach(() => {
jest.clearAllMocks();
});
it('renders Overlay component', () => {
const { container } = render(<Overlay handleOverlayClose={() => {}} />);
expect(container).toBeInTheDocument();
});
it('renders children inside Overlay', () => {
const { getByTestId } = render(
<Overlay handleOverlayClose={() => {}}>
<div data-testid='test-child' />
</Overlay>
);
expect(getByTestId('test-child')).toBeInTheDocument();
});
it('calls handleOverlayClose on Escape key press', async () => {
const handleOverlayCloseMock = jest.fn();
render(<Overlay handleOverlayClose={handleOverlayCloseMock} />);
await userEvent.type(document.body, '{Escape}');
expect(handleOverlayCloseMock).toHaveBeenCalled();
});
});