Skip to content

Commit

Permalink
Use state to show static logo with ternary conditional
Browse files Browse the repository at this point in the history
  • Loading branch information
danielhjacobs committed Mar 5, 2024
1 parent 47a771c commit 2361578
Showing 1 changed file with 20 additions and 9 deletions.
29 changes: 20 additions & 9 deletions src/components/logo.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -27,18 +27,31 @@ interface LogoProps {
className?: string;
}

export default class InteractiveLogo extends React.Component<LogoProps> {
interface LogoState {
player: RufflePlayer | null;
}

export default class InteractiveLogo extends React.Component<LogoProps, LogoState> {
private readonly container: React.RefObject<HTMLDivElement>;
private player: RufflePlayer | null = null;

constructor(props: LogoProps) {
super(props);

this.container = React.createRef();
this.state = {
player: null,
};
}

private removeRufflePlayer() {
this.player?.remove();
this.player = null;
this.setState({player: null});
}

private load() {
if (this.player) {
if (this.state.player) {
// Already loaded.
return;
}
Expand All @@ -47,6 +60,7 @@ export default class InteractiveLogo extends React.Component<LogoProps> {

if (this.player) {
this.container.current!.appendChild(this.player);

this.player.load({
url: "/logo-anim.swf",
autoplay: "on",
Expand All @@ -55,14 +69,12 @@ export default class InteractiveLogo extends React.Component<LogoProps> {
contextMenu: "off",
splashScreen: false,
preferredRenderer: "canvas",
}).then(() => {
this?.container?.current?.querySelector("img")?.classList?.toggle(classes.hidden);
}).catch(() => {
this.player?.remove();
this.player = null;
this.removeRufflePlayer();
});
this.player.style.width = "100%";
this.player.style.height = "100%";
this.setState({player: this.player});
}
}

Expand All @@ -71,8 +83,7 @@ export default class InteractiveLogo extends React.Component<LogoProps> {
}

componentWillUnmount() {
this.player?.remove();
this.player = null;
this.removeRufflePlayer();
}

render() {
Expand All @@ -83,7 +94,7 @@ export default class InteractiveLogo extends React.Component<LogoProps> {
onReady={() => this.load()}
/>
<div ref={this.container} className={this.props.className}>
<Image src="/logo.svg" alt="Ruffle Logo" className={classes.staticLogo} width="340" height="110" />
<Image src="/logo.svg" alt="Ruffle Logo" className={this.state.player ? classes.hidden : classes.staticLogo} width="340" height="110" />
</div>
</>
);
Expand Down

0 comments on commit 2361578

Please sign in to comment.