Skip to content

Commit

Permalink
chore(packages/react): add autostart property
Browse files Browse the repository at this point in the history
  • Loading branch information
crashmax-dev committed Nov 4, 2022
1 parent 945fbcb commit efcc9b5
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 12 deletions.
7 changes: 1 addition & 6 deletions examples/with-react/src/App.tsx
@@ -1,4 +1,4 @@
import { useEffect, useRef } from 'react'
import { useRef } from 'react'
import { Fireworks } from '@fireworks-js/react'
import type { FireworksHandlers } from '@fireworks-js/react'

Expand All @@ -14,11 +14,6 @@ export function App() {
}
}

useEffect(() => {
// prevent stop
ref.current?.stop()
}, [])

return (
<>
<div
Expand Down
18 changes: 12 additions & 6 deletions packages/react/src/index.tsx
Expand Up @@ -5,10 +5,11 @@ import React, { useEffect, useImperativeHandle, useRef } from 'react'
interface FireworksProps extends React.HTMLAttributes<HTMLDivElement> {
children?: React.ReactNode
options?: FireworksOptions
autostart?: boolean
}

const Fireworks = React.forwardRef<FireworksHandlers, FireworksProps>(
({ children, options, ...rest }, ref) => {
({ children, options, autostart = true, ...rest }, ref) => {
const container = useRef<HTMLDivElement>(null)
const fireworks = useRef<FireworksJs | null>(null)

Expand All @@ -20,10 +21,10 @@ const Fireworks = React.forwardRef<FireworksHandlers, FireworksProps>(
fireworks.current!.start()
},
stop() {
fireworks.current!.stop(true)
fireworks.current!.stop()
},
async waitStop() {
await fireworks.current!.waitStop(true)
await fireworks.current!.waitStop()
},
pause() {
fireworks.current!.pause()
Expand All @@ -43,11 +44,16 @@ const Fireworks = React.forwardRef<FireworksHandlers, FireworksProps>(
}))

useEffect(() => {
fireworks.current = new FireworksJs(container.current!, options)
fireworks.current.start()
if (!fireworks.current) {
fireworks.current = new FireworksJs(container.current!, options)
}

if (autostart) {
fireworks.current.start()
}

return () => {
fireworks.current!.stop(true)
fireworks.current!.stop()
}
}, [])

Expand Down

0 comments on commit efcc9b5

Please sign in to comment.