Skip to content

Commit ed448cc

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

File tree

2 files changed

+25
-16
lines changed

2 files changed

+25
-16
lines changed

examples/with-preact/src/App.tsx

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,17 +5,25 @@ import type { FireworksHandlers } from '@fireworks-js/preact'
55
export function App() {
66
const ref = useRef<FireworksHandlers>(null)
77

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

1217
return (
1318
<>
14-
<div style={{ position: 'absolute', zIndex: 1 }}>
15-
<button onClick={() => toggle()}>Toggle</button>
19+
<div
20+
style={{ display: 'flex', gap: '4px', position: 'absolute', zIndex: 1 }}
21+
>
22+
<button onClick={() => toggleFireworks()}>Toggle</button>
1623
<button onClick={() => ref.current!.clear()}>Clear</button>
1724
</div>
1825
<Fireworks
26+
autostart={false}
1927
ref={ref}
2028
options={{ opacity: 0.5 }}
2129
style={{

packages/preact/src/index.tsx

Lines changed: 13 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,17 @@
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 type { ComponentChildren } from 'preact'
44
import React from 'preact/compat'
55
import { useEffect, useImperativeHandle, useRef } from 'preact/hooks'
66

77
interface FireworksProps extends React.HTMLAttributes<HTMLDivElement> {
88
children?: ComponentChildren
99
options?: FireworksOptions
10-
}
11-
12-
interface FireworksHandlers
13-
extends Pick<
14-
FireworksJs,
15-
'isRunning' | 'start' | 'pause' | 'clear' | 'updateOptions' | 'updateSize'
16-
> {
17-
stop(): void
10+
autostart?: boolean
1811
}
1912

2013
const Fireworks = React.forwardRef<FireworksHandlers, FireworksProps>(
21-
({ children, options, ...rest }, ref) => {
14+
({ children, options, autostart = true, ...rest }, ref) => {
2215
const container = useRef<HTMLDivElement>(null)
2316
const fireworks = useRef<FireworksJs | null>(null)
2417

@@ -32,6 +25,9 @@ const Fireworks = React.forwardRef<FireworksHandlers, FireworksProps>(
3225
stop() {
3326
fireworks.current!.stop()
3427
},
28+
async waitStop() {
29+
await fireworks.current!.waitStop()
30+
},
3531
pause() {
3632
fireworks.current!.pause()
3733
},
@@ -43,15 +39,20 @@ const Fireworks = React.forwardRef<FireworksHandlers, FireworksProps>(
4339
},
4440
updateSize(size) {
4541
fireworks.current!.updateSize(size)
42+
},
43+
updateBoundaries(boundaries) {
44+
fireworks.current!.updateBoundaries(boundaries)
4645
}
4746
}))
4847

4948
useEffect(() => {
5049
fireworks.current = new FireworksJs(container.current!, options)
51-
fireworks.current.start()
50+
if (autostart) {
51+
fireworks.current.start()
52+
}
5253

5354
return () => {
54-
fireworks.current!.stop(true)
55+
fireworks.current!.stop()
5556
}
5657
}, [])
5758

0 commit comments

Comments
 (0)