Skip to content

Commit efcc9b5

Browse files
committed
chore(packages/react): add autostart property
1 parent 945fbcb commit efcc9b5

File tree

2 files changed

+13
-12
lines changed

2 files changed

+13
-12
lines changed

examples/with-react/src/App.tsx

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

@@ -14,11 +14,6 @@ export function App() {
1414
}
1515
}
1616

17-
useEffect(() => {
18-
// prevent stop
19-
ref.current?.stop()
20-
}, [])
21-
2217
return (
2318
<>
2419
<div

packages/react/src/index.tsx

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,11 @@ import React, { useEffect, useImperativeHandle, useRef } from 'react'
55
interface FireworksProps extends React.HTMLAttributes<HTMLDivElement> {
66
children?: React.ReactNode
77
options?: FireworksOptions
8+
autostart?: boolean
89
}
910

1011
const Fireworks = React.forwardRef<FireworksHandlers, FireworksProps>(
11-
({ children, options, ...rest }, ref) => {
12+
({ children, options, autostart = true, ...rest }, ref) => {
1213
const container = useRef<HTMLDivElement>(null)
1314
const fireworks = useRef<FireworksJs | null>(null)
1415

@@ -20,10 +21,10 @@ const Fireworks = React.forwardRef<FireworksHandlers, FireworksProps>(
2021
fireworks.current!.start()
2122
},
2223
stop() {
23-
fireworks.current!.stop(true)
24+
fireworks.current!.stop()
2425
},
2526
async waitStop() {
26-
await fireworks.current!.waitStop(true)
27+
await fireworks.current!.waitStop()
2728
},
2829
pause() {
2930
fireworks.current!.pause()
@@ -43,11 +44,16 @@ const Fireworks = React.forwardRef<FireworksHandlers, FireworksProps>(
4344
}))
4445

4546
useEffect(() => {
46-
fireworks.current = new FireworksJs(container.current!, options)
47-
fireworks.current.start()
47+
if (!fireworks.current) {
48+
fireworks.current = new FireworksJs(container.current!, options)
49+
}
50+
51+
if (autostart) {
52+
fireworks.current.start()
53+
}
4854

4955
return () => {
50-
fireworks.current!.stop(true)
56+
fireworks.current!.stop()
5157
}
5258
}, [])
5359

0 commit comments

Comments
 (0)