Skip to content

Commit

Permalink
Make modals work, if not elegantly
Browse files Browse the repository at this point in the history
  • Loading branch information
Hal-9k1 committed Jul 7, 2024
1 parent 49dd69c commit 8cce125
Show file tree
Hide file tree
Showing 11 changed files with 118 additions and 10 deletions.
7 changes: 5 additions & 2 deletions src/renderer/App.css
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,14 @@ body {
flex-direction: row;
}
.App-modal-container {
position: fixed;
top: 0;
left: 0;
width: 100%;
height: 100%;
position: fixed;
background-color: rgb(0 0 0 / 80%);
background-color: rgb(0 0 0 / 50%);
display: none;
z-index: 4;
}
.App-modal-container:has(.modal-active) {
display: block;
Expand Down
11 changes: 10 additions & 1 deletion src/renderer/ConnectionConfig.tsx
Original file line number Diff line number Diff line change
@@ -1,8 +1,17 @@
import { useDispatch } from 'react-redux';
import { modalActions, Modals } from './store/modalSlice';

/**
* Button component that opens the ConnectionConfigModal.
*/
export default function ConnectionConfig() {
const dispatch = useDispatch();
return (
<div className="ConnectionConfig"></div>
<div className="ConnectionConfig">
<button className="ConnectionConfig-button"

Check failure on line 11 in src/renderer/ConnectionConfig.tsx

View workflow job for this annotation

GitHub Actions / test (ubuntu-latest)

Missing an explicit type attribute for button

Check failure on line 11 in src/renderer/ConnectionConfig.tsx

View workflow job for this annotation

GitHub Actions / test (ubuntu-latest)

Insert `⏎·······`
onClick={() => dispatch(modalActions.setActive(Modals.ConnectionConfig))}>

Check failure on line 12 in src/renderer/ConnectionConfig.tsx

View workflow job for this annotation

GitHub Actions / test (ubuntu-latest)

Replace `··onClick={()·=>·dispatch(modalActions.setActive(Modals.ConnectionConfig))}` with `onClick={()·=>⏎··········dispatch(modalActions.setActive(Modals.ConnectionConfig))⏎········}⏎······`
Connection settings...
</button>
</div>
);
}
2 changes: 1 addition & 1 deletion src/renderer/ResizeBar.css
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
.ResizeBar {
background-color: black;
background-color: gray;
width: 5px;
cursor: col-resize;
}
Expand Down
2 changes: 1 addition & 1 deletion src/renderer/ResizeBar.tsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import React from 'react';
import { useSelector, useDispatch } from 'react-redux';
import { editorActions } from './store/editorSlice';
import { State } from './store/store';
import type { State } from './store/store';
import './ResizeBar.css'

Check failure on line 5 in src/renderer/ResizeBar.tsx

View workflow job for this annotation

GitHub Actions / test (ubuntu-latest)

Insert `;`

/**
Expand Down
2 changes: 1 addition & 1 deletion src/renderer/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,6 @@ const root = createRoot(container);
root.render(<App />);

window.electron.ipcRenderer.on('renderer-store-dispatch', (action) => {
console.log('renderer store dispatch');
//console.log('renderer store dispatch');
store.dispatch(action);
});
7 changes: 6 additions & 1 deletion src/renderer/modals/ConnectionInfoModal.tsx
Original file line number Diff line number Diff line change
@@ -1,8 +1,13 @@
import Modal from './Modal';
import { Modals } from '../store/modalSlice';

/**
* Modal component exposing info about the connection to the robot (IP address, port, etc.)
*/
export default function ConnectionInfoModal() {
return (
<div className="ConnectionInfoModal">Connection info modal</div>
<Modal modalType={Modals.ConnectionConfig} modalTitle="Connection info">
Test
</Modal>
);
}
7 changes: 6 additions & 1 deletion src/renderer/modals/GamepadInfoModal.tsx
Original file line number Diff line number Diff line change
@@ -1,8 +1,13 @@
import { Modals } from '../store/modalSlice';
import Modal from './Modal';

/**
* Modal component displaying info about a connected gamepad.
*/
export default function GamepadInfoModal() {
return (
<div className="GamepadInfoModal">Gamepad info modal</div>
<Modal modalType={Modals.GamepadInfo} modalTitle="Gamepad info">
Test
</Modal>
);
}
28 changes: 28 additions & 0 deletions src/renderer/modals/Modal.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
.Modal {
display: none;
position: fixed;
top: 50%;
left: 50%;
transform: translate(-50%, -50%);
background-color: white;
border-radius: 5px;
}
.modal-active {
display: block;
}
.Modal-title-bar {
white-space: nowrap;
padding: 20px;
background-color: blue;
border-radius: inherit;
}
.Modal-title {
padding-right: 20vw;
}
.Modal-close-button {
background-color: transparent;
border: none;
}
.Modal-content {
padding: 20px;
}
28 changes: 28 additions & 0 deletions src/renderer/modals/Modal.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
import { useSelector, useDispatch } from 'react-redux';
import { modalActions, Modals } from '../store/modalSlice';
import type { State } from '../store/store';
import './Modal.css';

/**
* Generic modal component.
*/
export default function Modal({ modalType, modalTitle, children }) {
const isActive = useSelector((state: State) => state.modal.activeModal === modalType);
const dispatch = useDispatch();
return (
<div className={'Modal' + (isActive ? ' modal-active' : '')}>
<div className="Modal-title-bar">
<span className="Modal-title">
{modalTitle}
</span>
<button className="Modal-close-button"
onClick={() => dispatch(modalActions.setActive(Modals.None))}>
X
</button>
</div>
<div className="Modal-content">
{children}
</div>
</div>
);
}
27 changes: 27 additions & 0 deletions src/renderer/store/modalSlice.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
import { createSlice } from '@reduxjs/toolkit';

export enum Modals {
None,
ConnectionConfig,
GamepadInfo
}
const initialState = {
activeModal: Modals.None
};
export const modalSlice = createSlice({
name: 'modal',
initialState: initialState,
reducers: {
/**
* Sets the currently active modal.
*/
setActive: (state, { payload }) => {
console.log('set active to ' + payload.toString());

Check warning on line 19 in src/renderer/store/modalSlice.tsx

View workflow job for this annotation

GitHub Actions / test (ubuntu-latest)

Unexpected console statement
state.activeModal = payload;
}
}
});

export const modalActions = modalSlice.actions;
export type ModalState = typeof initialState;
export default modalSlice.reducer;
7 changes: 5 additions & 2 deletions src/renderer/store/store.tsx
Original file line number Diff line number Diff line change
@@ -1,14 +1,17 @@
import { configureStore } from '@reduxjs/toolkit';
import robotInfoReducer, { RobotInfoState } from './robotInfoSlice';
import editorReducer, { EditorState } from './editorSlice';
import modalReducer, { ModalState } from './modalSlice';

export interface State {
robotInfo: RobotInfoState,
editor: EditorState
editor: EditorState,
modal: ModalState
}
export default configureStore({
reducer: {
robotInfo: robotInfoReducer,
editor: editorReducer
editor: editorReducer,
modal: modalReducer
}
});

0 comments on commit 8cce125

Please sign in to comment.