Skip to content

Commit 41c6530

Browse files
committed
chore(packages/react): refactor react wrapper
1 parent f464d38 commit 41c6530

File tree

2 files changed

+25
-18
lines changed

2 files changed

+25
-18
lines changed

examples/with-react/src/App.tsx

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,29 @@
1-
import { useRef } from 'react'
1+
import { useEffect, useRef } from 'react'
22
import { Fireworks } from '@fireworks-js/react'
33
import type { FireworksHandlers } from '@fireworks-js/react'
44

55
export function App() {
66
const ref = useRef<FireworksHandlers>(null)
77

88
const toggle = () => {
9-
ref.current!.isRunning ? ref.current!.stop() : ref.current!.start()
9+
if (!ref.current) return
10+
if (ref.current.isRunning) {
11+
ref.current.stop()
12+
} else {
13+
ref.current.start()
14+
}
1015
}
1116

17+
useEffect(() => {
18+
// prevent stop
19+
ref.current?.stop()
20+
}, [])
21+
1222
return (
1323
<>
14-
<div style={{ position: 'absolute', zIndex: 1 }}>
24+
<div
25+
style={{ display: 'flex', gap: '4px', position: 'absolute', zIndex: 1 }}
26+
>
1527
<button onClick={() => toggle()}>Toggle</button>
1628
<button onClick={() => ref.current!.clear()}>Clear</button>
1729
</div>

packages/react/src/index.tsx

Lines changed: 10 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,14 @@
11
import { Fireworks as FireworksJs } from 'fireworks-js'
2-
import type { FireworksOptions } from 'fireworks-js'
2+
import type { FireworksHandlers, FireworksOptions } from 'fireworks-js'
33
import React, { useEffect, useImperativeHandle, useRef } from 'react'
44

55
interface FireworksProps extends React.HTMLAttributes<HTMLDivElement> {
66
children?: React.ReactNode
7-
autostart?: boolean
87
options?: FireworksOptions
98
}
109

11-
interface FireworksHandlers
12-
extends Pick<
13-
FireworksJs,
14-
'isRunning' | 'start' | 'pause' | 'clear' | 'updateOptions' | 'updateSize'
15-
> {
16-
stop(): void
17-
}
18-
1910
const Fireworks = React.forwardRef<FireworksHandlers, FireworksProps>(
20-
({ children, options, autostart = true, ...rest }, ref) => {
11+
({ children, options, ...rest }, ref) => {
2112
const container = useRef<HTMLDivElement>(null)
2213
const fireworks = useRef<FireworksJs | null>(null)
2314

@@ -29,7 +20,10 @@ const Fireworks = React.forwardRef<FireworksHandlers, FireworksProps>(
2920
fireworks.current!.start()
3021
},
3122
stop() {
32-
fireworks.current!.stop()
23+
fireworks.current!.stop(true)
24+
},
25+
async waitStop() {
26+
await fireworks.current!.waitStop(true)
3327
},
3428
pause() {
3529
fireworks.current!.pause()
@@ -42,14 +36,15 @@ const Fireworks = React.forwardRef<FireworksHandlers, FireworksProps>(
4236
},
4337
updateSize(size) {
4438
fireworks.current!.updateSize(size)
39+
},
40+
updateBoundaries(boundaries) {
41+
fireworks.current!.updateBoundaries(boundaries)
4542
}
4643
}))
4744

4845
useEffect(() => {
4946
fireworks.current = new FireworksJs(container.current!, options)
50-
if (autostart) {
51-
fireworks.current.start()
52-
}
47+
fireworks.current.start()
5348

5449
return () => {
5550
fireworks.current!.stop(true)

0 commit comments

Comments
 (0)