-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathuseLiveRunner.ts
32 lines (27 loc) · 917 Bytes
/
useLiveRunner.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
import { useState, useEffect } from 'react';
import { useRunner, UseRunnerProps, UseRunnerReturn } from './react-runner-swc';
export type UseLiveRunnerProps = Omit<UseRunnerProps, 'code'> & {
/** initial code for the live runner */
initialCode?: string;
/** transform the code before transpiling */
transformCode?: (code: string) => string;
};
export type UseLiveRunnerReturn = UseRunnerReturn & {
code: string;
onChange: (value: string) => void;
};
export const useLiveRunner = ({
initialCode = '',
transformCode,
...rest
}: UseLiveRunnerProps): UseLiveRunnerReturn => {
const [code, onChange] = useState(initialCode);
const { element, error } = useRunner({
code: transformCode ? transformCode(code) : code,
...rest,
});
useEffect(() => {
onChange(initialCode);
}, [initialCode]);
return { element, error, code, onChange };
};