Skip to content

Commit ea47b25

Browse files
authored
Add the ability for fallback UIs to be rendered if the sketch is ever undefined (#248)
1 parent 9bfba86 commit ea47b25

File tree

5 files changed

+3072
-1210
lines changed

5 files changed

+3072
-1210
lines changed

README.md

Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -441,6 +441,80 @@ Of course, you can also use any other css-in-js library or by just using simple
441441
css to achieve almost anything you can imagine just by using the wrapper class
442442
as your root selector.
443443

444+
## Fallback UIs
445+
446+
Lets say you want to have a fallback UI in case the `sketch` ever falls out of
447+
sync or is undefined for some reason. If this is a use case for you then you
448+
call use the `fallback` prop to provide the necessary UI to show in the case
449+
that the `sketch` becomes undefined. An example could be as follows:
450+
451+
```jsx
452+
import * as React from "react";
453+
import { ReactP5Wrapper } from "@p5-wrapper/react";
454+
455+
function sketchOne(p5) {
456+
p5.setup = () => p5.createCanvas(600, 400, p5.WEBGL);
457+
458+
p5.draw = () => {
459+
p5.background(250);
460+
p5.normalMaterial();
461+
p5.push();
462+
p5.rotateZ(p5.frameCount * 0.01);
463+
p5.rotateX(p5.frameCount * 0.01);
464+
p5.rotateY(p5.frameCount * 0.01);
465+
p5.plane(100);
466+
p5.pop();
467+
};
468+
}
469+
470+
function sketchTwo(p5) {
471+
p5.setup = () => p5.createCanvas(600, 400, p5.WEBGL);
472+
473+
p5.draw = () => {
474+
p5.background(500);
475+
p5.normalMaterial();
476+
p5.push();
477+
p5.rotateZ(p5.frameCount * 0.01);
478+
p5.rotateX(p5.frameCount * 0.01);
479+
p5.rotateY(p5.frameCount * 0.01);
480+
p5.plane(100);
481+
p5.pop();
482+
};
483+
}
484+
485+
export function App() {
486+
const [sketch, setSketch] = React.useState(undefined);
487+
const chooseNothing = () => setSketch(undefined);
488+
const chooseSketchOne = () => setSketch(sketchOne);
489+
const chooseSketchTwo = () => setSketch(sketchTwo);
490+
491+
return (
492+
<>
493+
<ul>
494+
<li>
495+
<button onClick={chooseNothing}>Choose nothing</button>
496+
</li>
497+
<li>
498+
<button onClick={chooseSketchOne}>Choose sketch 1</button>
499+
</li>
500+
<li>
501+
<button onClick={chooseSketchTwo}>Choose sketch 2</button>
502+
</li>
503+
</ul>
504+
<ReactP5Wrapper
505+
fallback={<h1>No sketch selected yet.</h1>}
506+
sketch={sketch}
507+
/>
508+
</>
509+
);
510+
}
511+
```
512+
513+
In this case, by default the fallback UI containing
514+
`<h1>No sketch selected yet.</h1>` will be rendered, then if you select a
515+
sketch, it will be rendered until you choose to once again "show nothing" which
516+
falls back to the fallback UI.
517+
444518
## P5 plugins and constructors
445519

446520
As discussed in multiple issues such as

0 commit comments

Comments
 (0)