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

Initial React migration #10

Merged
merged 15 commits into from
Jan 26, 2024
Merged
Show file tree
Hide file tree
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
3 changes: 3 additions & 0 deletions .eslintrc.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"extends": "react-app"
}
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
This is TypeScript, using [Preact](https://preactjs.com/) bundled with [Vite](https://vitejs.dev/).
This is TypeScript, using [React](https://react.dev/) bundled with [Vite](https://vitejs.dev/).

Install with `npm install`

Expand Down
7 changes: 6 additions & 1 deletion index.html
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,12 @@
<meta charset="UTF-8" />
<link rel="icon" type="image/svg+xml" href="/vite.svg" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>Vite + Preact + TS</title>
<link rel="preconnect" href="https://fonts.gstatic.com" />
<link
href="https://fonts.googleapis.com/css2?family=Open+Sans:ital,wght@0,300;0,400;0,600;0,700;0,800;1,400;1,600;1,700&display=swap"
rel="stylesheet"
/>
<title>Vite + React + TS</title>
</head>
<body>
<div id="app"></div>
Expand Down
7,804 changes: 6,860 additions & 944 deletions package-lock.json

Large diffs are not rendered by default.

18 changes: 16 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,27 @@
"preview": "vite preview"
},
"dependencies": {
"preact": "^10.19.3",
"@czi-sds/components": "^19.2.0",
"@emotion/css": "^11.11.2",
"@emotion/react": "^11.11.3",
"@emotion/styled": "^11.11.0",
"@mui/base": "^5.0.0-beta.31",
"@mui/icons-material": "^5.15.4",
"@mui/lab": "^5.0.0-alpha.160",
"@mui/material": "^5.15.4",
"czifui": "^14.5.0",
"react": "^18.2.0",
"react-dom": "^18.2.0",
"three": "^0.159.0",
"zarr": "^0.6.2"
},
"devDependencies": {
"@preact/preset-vite": "^2.7.0",
"@types/react": "^18.2.47",
"@types/react-dom": "^18.2.18",
"@types/three": "^0.159.0",
"@vitejs/plugin-react": "^4.2.1",
"eslint": "^8.56.0",
"eslint-config-react-app": "^7.0.1",
"typescript": "^5.2.2",
"vite": "^5.0.8"
}
Expand Down
5 changes: 4 additions & 1 deletion src/app.css
Original file line number Diff line number Diff line change
Expand Up @@ -6,5 +6,8 @@
max-width: 1280px;
margin: 0 auto;
padding: var(--app-padding);
text-align: center;
display: flex;
flex-direction: column;
justify-content: center;
align-items: center;
}
59 changes: 22 additions & 37 deletions src/app.tsx
Original file line number Diff line number Diff line change
@@ -1,26 +1,17 @@
import { Component } from 'preact';
import { useState, useEffect } from 'react';
import './app.css'
import Scene from './scene.tsx'

const aspectRatio = 4 / 3;

interface AppState {
renderWidth: number;
}

class App extends Component {
state: AppState;
export default function App() {
const [renderWidth, setRenderWidth] = useState(800);

constructor() {
super();
this.state = {
renderWidth: this.calculateRenderWidth(),
};
}

calculateRenderWidth() {
function handleWindowResize() {
const windowWidth = window.innerWidth;
const appPadding = parseFloat(getComputedStyle(document.documentElement).getPropertyValue('--app-padding'));
const appPadding = parseFloat(
getComputedStyle(document.documentElement).getPropertyValue('--app-padding')
);
let w: number;
if (windowWidth < 800) {
w = windowWidth;
Expand All @@ -31,30 +22,24 @@ class App extends Component {
} else {
w = 1200;
}
const renderWidth = w - appPadding * 2;
return renderWidth < 0 ? windowWidth : renderWidth;
let renderWidth = w - appPadding * 2;
renderWidth = renderWidth < 0 ? windowWidth : renderWidth;
setRenderWidth(renderWidth);
}

componentDidMount() {
const handleWindowResize = this.handleWindowResize.bind(this);
useEffect(() => {
handleWindowResize();
window.addEventListener('resize', handleWindowResize);
}
return () => {
window.removeEventListener('resize', handleWindowResize);
}
}, [renderWidth]);

handleWindowResize() {
this.setState({
renderWidth: this.calculateRenderWidth(),
});
}

render() {
const renderWidth = this.state.renderWidth || 800;
const renderHeight = renderWidth / aspectRatio;
return (
<>
<Scene renderWidth={renderWidth} renderHeight={renderHeight} />
</>
)
}
}
return (
<>
<Scene renderWidth={renderWidth} renderHeight={renderWidth / aspectRatio} />
</>
)

export default App;
}
68 changes: 12 additions & 56 deletions src/index.css
Original file line number Diff line number Diff line change
Expand Up @@ -13,77 +13,33 @@
-moz-osx-font-smoothing: grayscale;
}

a {
font-weight: 500;
color: #646cff;
text-decoration: inherit;
}
a:hover {
color: #535bf2;
}

body {
margin: 0;
display: flex;
place-items: center;
min-width: 320px;
min-height: 100vh;
}

h1 {
font-size: 3.2em;
line-height: 1.1;
}

button {
border-radius: 8px;
border: 1px solid transparent;
padding: 0.6em 1.2em;
font-size: 1em;
font-weight: 500;
font-family: inherit;
background-color: #1a1a1a;
cursor: pointer;
transition: border-color 0.25s;
}
button:hover {
border-color: #646cff;
}
button:focus,
button:focus-visible {
outline: 4px auto -webkit-focus-ring-color;
}

@media (prefers-color-scheme: light) {
:root {
color: #213547;
background-color: #ffffff;
}
a:hover {
color: #747bff;
}
button {
background-color: #f9f9f9;
}
}

.inputcontainer {
width: 100%; /* Width of the outside container */
}

.textinput {
font-size: 12pt;
width: 100%; /* Full-width */
margin: 1em 0;
display: flex;
gap: 1em;
flex-direction: column;
}

.slider {
width: 80%;
vertical-align: middle;
.buttoncontainer {
width: 100%; /* Full-width */
margin: 1em 0;
display: flex;
flex-direction: row;
gap: 1em;
justify-content: flex-start;
}

/* From https://github.com/mrdoob/three.js/blob/master/examples/misc_boxselection.html */
.selectBox {
border: 1px solid #ffffff;
background-color: #ffffff55;
position: fixed;
}
}
21 changes: 19 additions & 2 deletions src/main.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,22 @@
import { render } from 'preact'
import React from "react";
import { createRoot } from "react-dom/client";
import { ThemeProvider as EmotionThemeProvider } from "@emotion/react";
import { StyledEngineProvider, ThemeProvider } from "@mui/material/styles";
import { defaultTheme } from "@czi-sds/components";

import App from './app.tsx'
import './index.css'

render(<App />, document.getElementById('app')!)
const domNode = document.getElementById('app')!
const root = createRoot(domNode);
root.render(
<React.StrictMode>
<StyledEngineProvider injectFirst>
<ThemeProvider theme={defaultTheme}>
<EmotionThemeProvider theme={defaultTheme}>
<App />
</EmotionThemeProvider>
</ThemeProvider>
</StyledEngineProvider>
</React.StrictMode>
);
Loading