Skip to content

Commit 57d0a06

Browse files
committed
fix(readme): code to collapsibles
1 parent 3acf54a commit 57d0a06

File tree

1 file changed

+82
-4
lines changed

1 file changed

+82
-4
lines changed

README.md

Lines changed: 82 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,9 @@
7474

7575
[Try it on codesandbox](https://codesandbox.io/s/console-date-on-animation-frame-z7xof4)
7676

77+
<details open>
78+
<summary><b>Source code</b></summary>
79+
7780
```typescript
7881
import React, { useEffect } from "react";
7982
import { useAnimationFrame } from "use-listen-on-animation-frame";
@@ -97,10 +100,15 @@ const ConsoleDateComponent: React.FC = () => {
97100
};
98101
```
99102

103+
</details>
104+
100105
#### Animation frame counter
101106

102107
[Try it on codesandbox](https://codesandbox.io/s/animation-frame-counter-00pfwo)
103108

109+
<details>
110+
<summary><b>Source code</b></summary>
111+
104112
```typescript
105113
import React, { useCallback, useState } from "react";
106114
import { useAnimationFrame } from "use-listen-on-animation-frame";
@@ -119,12 +127,17 @@ const AnimationFrameCounter: React.FC = () => {
119127
};
120128
```
121129

130+
</details>
131+
122132
#### Video & current timer
123133

124134
[Try it on codesandbox](https://codesandbox.io/s/player-current-time-3yy31o)
125135

126136
[Btw, compare it to relying on `timeupdate` event](https://codesandbox.io/s/hooks-vs-timeupdate-g6zqgl)
127137

138+
<details>
139+
<summary><b>Source code</b></summary>
140+
128141
```typescript
129142
import React, { useCallback, useEffect, useState, useRef } from "react";
130143
import { useListenOnAnimationFrame } from "use-listen-on-animation-frame";
@@ -163,12 +176,17 @@ const VideoWithCurrentTime: React.FC = () => {
163176
};
164177
```
165178

179+
</details>
180+
166181
### Track your function return on every animation frame
167182

168183
If you need to track your function return on every animation frame and do something with it - go for it!
169184

170185
[Try it on codesandbox](https://codesandbox.io/s/evening-hours-indicator-pwp3wv)
171186

187+
<details>
188+
<summary><b>Source code</b></summary>
189+
172190
```typescript
173191
import "./styles.css";
174192

@@ -221,6 +239,8 @@ const EveningHoursIndicator: React.FC = () => {
221239
};
222240
```
223241

242+
</details>
243+
224244
## :orange_book: API
225245

226246
Library provides 2 hooks which are `useAnimationFrame`, `useListenOnAnimationFrame`.
@@ -237,7 +257,10 @@ First argument `fn` - a function to be invoked on every animation frame.
237257

238258
**It's better for this `fn` to be memoized either with `useCallback` or to be defined outside of your component if that still fits your needs**
239259

240-
`fn` might accept a single argument of previous invocation return like this:
260+
`fn` might accept a single argument of previous invocation return like in the example:
261+
262+
<details open>
263+
<summary><b>Example</b></summary>
241264

242265
```typescript
243266
import { useAnimationFrame } from "use-listen-on-animation-frame";
@@ -255,12 +278,17 @@ const fn = useCallback((previousFnReturn) => {
255278
useAnimationFrame(fn);
256279
```
257280

281+
</details>
282+
258283
##### `options`
259284

260285
###### `options.autoStart`
261286

262287
Boolean indicator whether to start invoking function immediately when the hook is used or not. **Defaults** to `true`.
263288

289+
<details>
290+
<summary><b>Example</b></summary>
291+
264292
```typescript
265293
const [_stop, start] = useAnimationFrame(fn, {
266294
autoStart: false,
@@ -273,6 +301,8 @@ useEffect(() => {
273301
}, [start]);
274302
```
275303

304+
</details>
305+
276306
#### Returns
277307

278308
Returns `[stop, start]` functions.
@@ -281,8 +311,12 @@ Returns `[stop, start]` functions.
281311

282312
Function to stop the function from being invoked on every animation frame, and if that was the last function in application still running on animation frame - will effectively `cancelRequestAnimationFrame`.
283313

284-
**You can be assured in it's static reference - no changes between renders, same function**
314+
**You can be assured in it's static reference - no changes between renders, same function**.
315+
316+
<details>
317+
<summary><b>Example</b></summary>
285318

319+
<!-- prettier-ignore-start -->
286320
```typescript
287321
const [stop] = useAnimationFrame(fn);
288322

@@ -291,14 +325,21 @@ useEffect(() => {
291325
stop();
292326
}
293327
}, [stop]);
328+
294329
```
330+
<!-- prettier-ignore-end -->
331+
332+
</details>
295333

296334
##### `[ , start]`
297335

298336
Function to start the function invocations on every animation frame, and if it's the first function in application to be run on animation frame - starts a single animation frame loop.
299337

300338
**You can be assured in it's static reference - no changes between renders, same function**
301339

340+
<details>
341+
<summary><b>Example</b></summary>
342+
302343
```typescript
303344
const [stop, start] = useAnimationFrame(fn);
304345

@@ -315,9 +356,11 @@ useEffect(() => {
315356
}, [start]);
316357
```
317358

359+
</details>
360+
318361
#### Note
319362

320-
This is a handy alias for.
363+
`useAnimationFrame` is a handy alias for.
321364

322365
```typescript
323366
const [_add, _remove, stop, start] = useListenOnAnimationFrame(fn, {
@@ -351,6 +394,9 @@ Function that accepts 2 arguments `thisFrameReturn` and `previousFrameReturn` an
351394

352395
**It's better for this function to be memoized with `useCallback` or defined outside of your component if it still fits your needs**
353396

397+
<details>
398+
<summary><b>Example</b></summary>
399+
354400
```typescript
355401
const shouldInvokeListeners = useCallback(
356402
(thisFrameReturn, previousFrameReturn) => {
@@ -365,6 +411,8 @@ const [addFrameListener] = useListenOnAnimationFrame(fn, {
365411
});
366412
```
367413

414+
</details>
415+
368416
#### Returns
369417

370418
Returns `[addListener, removeListener, stop, start]`;
@@ -378,6 +426,9 @@ Function that accepts your `listener` function.
378426

379427
**You can be assured in it's static reference - no changes between renders, same function**
380428

429+
<details>
430+
<summary><b>Example</b></summary>
431+
381432
```typescript
382433
const fn = useCallback(() => {
383434
return new Date().getTime();
@@ -399,6 +450,8 @@ useEffect(() => {
399450
}, [addListener]);
400451
```
401452

453+
</details>
454+
402455
##### `[ , removeListener]`
403456

404457
Function that accepts `listenerId: string` unique uuid from [`[addListener]`](#addlistener) and removes a listener.
@@ -407,6 +460,9 @@ Function that accepts `listenerId: string` unique uuid from [`[addListener]`](#a
407460

408461
**NOTE!** There is no need to removeListener as a cleanup for a component. Whole listener tree will be destroyed when component will be unmounted. Use it only when you explicitly need to remove your side effect for some matter, or if you add your listener conditionally, and want it to be conditionally removed.
409462

463+
<details>
464+
<summary><b>Example</b></summary>
465+
410466
```typescript
411467
const [addListener, removeListener] = useListenOnAnimationFrame(fn);
412468

@@ -429,6 +485,8 @@ useEffect(() => {
429485
}, [removeListener, listenerId, somethingBadHappened]);
430486
```
431487

488+
</details>
489+
432490
##### `[ , , stop]`
433491

434492
[See `useAnimationFrame` `stop`](#stop)
@@ -441,7 +499,10 @@ useEffect(() => {
441499

442500
You might have noticed that you could simply put your function inside `useAnimationFrame`, do side effects inside it, `start` and `stop` it when you need.
443501

444-
It is true, however **you should consider using `useListenOnAnimationFrame` with listeners when you want multiple side effects (or callbacks)** for your function on animation frames.
502+
It is true, however **you should consider using `useListenOnAnimationFrame` with listeners when you want multiple side effects (or callbacks)** for your function on animation frames because of performance implications.
503+
504+
<details>
505+
<summary><b>Explanation</b></summary>
445506

446507
Comparing
447508

@@ -509,6 +570,8 @@ useEffect(() => {
509570

510571
Which will effectively call `video.currentTime` once on each animationFrame and 3 listeners to it.
511572

573+
</details>
574+
512575
## :gear: Advanced usage
513576

514577
### Access previous animation frame function return
@@ -517,6 +580,9 @@ If you for some reason need previous animation frame return of your function - i
517580

518581
[Try it on codesandbox](https://codesandbox.io/s/ms-elapsed-from-1970-pwgpft)
519582

583+
<details>
584+
<summary><b>Source code</b></summary>
585+
520586
```typescript
521587
import React, { useEffect, useState } from "react";
522588
import { useListenOnAnimationFrame } from "use-listen-on-animation-frame";
@@ -557,6 +623,8 @@ const MilisecondsElapsedFrom1970: React.FC = () => {
557623
};
558624
```
559625

626+
</details>
627+
560628
### Start and stop tracking your function
561629

562630
You can stop and start tracking again whenever you want.
@@ -565,6 +633,9 @@ You can stop and start tracking again whenever you want.
565633

566634
<em>[Btw, compare the above performance with `setInterval`. You couldn't achieve same smoothness when event loop is busy](https://codesandbox.io/s/interval-vs-animation-frame-065es8)</em>
567635

636+
<details>
637+
<summary><b>Source code</b></summary>
638+
568639
```typescript
569640
import React, { useEffect, useState } from "react";
570641
import { useListenOnAnimationFrame } from "use-listen-on-animation-frame";
@@ -635,12 +706,17 @@ export const ExtremelySmoothTimer: React.FC = () => {
635706
};
636707
```
637708

709+
</details>
710+
638711
### Optimize/Unoptimize your listeners
639712

640713
By default, if you don't provide [`shouldInvokeListeners` option](#optionsshouldinvokelisteners) - listeners will be invoked only if tracked function return changes. It means that a supplied function will still be invoked on every animation frame, but listeners will not.
641714

642715
[Try it on codesandbox](https://codesandbox.io/s/player-timer-heavy-load-yqz79q)
643716

717+
<details>
718+
<summary><b>Source code</b></summary>
719+
644720
```typescript
645721
import React, { useCallback, useEffect, useState, useRef } from "react";
646722
import { useListenOnAnimationFrame } from "use-listen-on-animation-frame";
@@ -723,6 +799,8 @@ const VideoWithCurrentTime: React.FC = () => {
723799
};
724800
```
725801

802+
</details>
803+
726804
## :exclamation: Q&A
727805

728806
- ### Why would you even consider using animation frames?

0 commit comments

Comments
 (0)