Skip to content

Commit

Permalink
Provide Loading message sooner instead of blank page
Browse files Browse the repository at this point in the history
  • Loading branch information
RReverser committed Dec 6, 2024
1 parent ca9e5b1 commit ec3f446
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 5 deletions.
3 changes: 3 additions & 0 deletions examples/preact/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -67,4 +67,7 @@
href='data:image/svg+xml,<svg xmlns="http://www.w3.org/2000/svg" viewBox="10 0 110 100"><text y=".9em" font-size="90">📷</text></svg>'
/>
</head>
<body>
<div class="center">⌛ Loading...</div>
</body>
</html>
12 changes: 7 additions & 5 deletions examples/preact/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
*/

import { h, render, Component } from 'preact';
import { h, hydrate, Component } from 'preact';
import { CaptureButton } from './capture-button.js';
import { Camera, rethrowIfCritical } from 'web-gphoto2';
import { Preview } from './preview.js';
Expand All @@ -34,6 +34,9 @@ class App extends Component {
/** @type {Camera | undefined} */
camera;

// Make sure that first render hydrates the existing HTML smoothly.
state = { type: 'Status', message: '⌛ Loading...' };

componentDidMount() {
addEventListener('error', ({ message }) =>
this.setState({
Expand All @@ -52,7 +55,6 @@ class App extends Component {
);
// Try to connect to camera at startup.
// If none is found among saved connections, it will fallback to a picker.
this.setState({ type: 'Status', message: '⌛ Loading...' });
this.tryToConnectToCamera();
}

Expand Down Expand Up @@ -130,7 +132,7 @@ class App extends Component {
switch (state.type) {
case 'CameraPicker':
return h(
'div',
'div',
{
class: 'center'
},
Expand All @@ -155,7 +157,7 @@ class App extends Component {
'repo'
),
'!'
)
)
);
case 'Status':
return h('div', { class: 'center' }, state.message);
Expand Down Expand Up @@ -201,4 +203,4 @@ class App extends Component {
}
}

render(h(App, null), document.body);
hydrate(h(App, null), document.body);

0 comments on commit ec3f446

Please sign in to comment.