apple2js/js/components/PauseControl.tsx
Will Scullin 9a940935af
Refactor some controls (#114)
* Refactor some controls

* Feedback
2022-05-15 14:57:21 -07:00

52 lines
1.2 KiB
TypeScript

import { h, Fragment } from 'preact';
import { useCallback, useState } from 'preact/hooks';
import { Apple2 as Apple2Impl } from 'js/apple2';
import { ControlButton } from './ControlButton';
/**
* PauseControl component properties.
*/
export interface PauseControlProps {
apple2: Apple2Impl | undefined;
}
/**
* Provides a control to pause and unpause the CPU.
*
* @param apple2 The Apple2 object
* @returns PauseControl component
*/
export const PauseControl = ({ apple2 }: PauseControlProps) => {
const [running, setRunning] = useState(true);
const doPause = useCallback(() => {
apple2?.stop();
setRunning(false);
}, [apple2]);
const doRun = useCallback(() => {
apple2?.run();
setRunning(true);
}, [apple2]);
return (
<>
{running ? (
<ControlButton
onClick={doPause}
disabled={!apple2}
title="Pause"
icon="pause"
/>
) : (
<ControlButton
onClick={doRun}
disabled={!apple2}
title="Run"
icon="play"
/>
)}
</>
);
};