Skip to content

Commit 675b103

Browse files
authored
Improvement/ie11 demo (#27)
* rewrite demo to use hooks * add own webpack setup to build demo * import from build * only transpile src files * only transpile demo files in webpack * copy useWatcher hook to demo * useWatcher in demo * update gitignore * change how demo is published * simplify setup * fix code error for FPS * build before demo is created * remove unnecessary condition
1 parent 50fe2d0 commit 675b103

20 files changed

+676
-91
lines changed

.circleci/config.yml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,9 @@ jobs:
8989
name: Set git name & email
9090
command: |
9191
git config --global user.name "Workflow Bot" && git config --global user.email "[email protected]"
92+
- run:
93+
name: build
94+
command: yarn build
9295
- run:
9396
name: Build demo
9497
command: yarn build-demo

.gitignore

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,4 +3,5 @@
33
/es
44
/lib
55
/node_modules
6-
npm-debug.log*
6+
npm-debug.log*
7+
/build

babel.config.js

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
const { NODE_ENV } = process.env
2+
3+
module.exports = {
4+
presets: [
5+
[
6+
'@babel/env',
7+
{
8+
modules: NODE_ENV === 'test' ? 'auto' : false,
9+
useBuiltIns: 'entry',
10+
corejs: 3,
11+
},
12+
],
13+
'@babel/react',
14+
'@babel/preset-flow',
15+
],
16+
comments: true,
17+
plugins: [
18+
'@babel/transform-runtime',
19+
'@babel/plugin-transform-flow-strip-types',
20+
'@babel/plugin-proposal-object-rest-spread',
21+
'@babel/plugin-proposal-export-namespace-from',
22+
'@babel/plugin-proposal-optional-chaining',
23+
24+
'lodash',
25+
'syntax-dynamic-import',
26+
27+
...(NODE_ENV === 'test' ? ['dynamic-import-node'] : []),
28+
],
29+
}

demo/src/AutoAlignment.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import React from 'react'
22

3-
import Stick from '../../src'
3+
import Stick from '../../es'
44

55
function AutoAlignment() {
66
return (

demo/src/PositionAlignOverview.js

Lines changed: 18 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
import { compact } from 'lodash'
2-
import React, { Component, useState } from 'react'
2+
import React, { useCallback, useRef, useState } from 'react'
33

4-
import Stick from '../../src'
4+
import Stick from '../../es'
5+
import { useWatcher } from './hooks'
56

67
const formPairs = (listA: Array<string>, listB: Array<string>) =>
78
compact(
@@ -42,44 +43,28 @@ const Node = () => (
4243
/>
4344
)
4445

45-
class FramesPerSecond extends Component {
46-
state = {
47-
fps: 0,
48-
}
46+
function FramesPerSecond({ updateOnAnimationFrame }) {
47+
const [fps, setFps] = useState(0)
48+
const lastUpdated = useRef(Date.now())
49+
const framesSinceLastUpdate = useRef(0)
4950

50-
lastUpdated = Date.now()
51-
framesSinceLastUpdate = 0
51+
const measure = useCallback(() => {
52+
framesSinceLastUpdate.current += 1
5253

53-
startTracking() {
54-
const requestCallback = this.props.updateOnAnimationFrame
55-
? requestAnimationFrame
56-
: requestIdleCallback
57-
this.lastCallbackAsAnimationFrame = this.props.updateOnAnimationFrame
58-
59-
this.animationId = requestCallback(() => this.startTracking())
60-
this.measure()
61-
}
62-
63-
measure() {
64-
this.framesSinceLastUpdate += 1
65-
let duration = Date.now() - this.lastUpdated
54+
const duration = Date.now() - lastUpdated.current
6655
if (duration >= 1000) {
67-
this.setState({
68-
fps: this.framesSinceLastUpdate,
69-
})
70-
this.framesSinceLastUpdate = 0
71-
this.lastUpdated = Date.now()
56+
setFps(framesSinceLastUpdate.current)
57+
58+
framesSinceLastUpdate.current = 0
59+
lastUpdated.current = Date.now()
7260
}
73-
}
61+
}, [])
7462

75-
componentDidMount() {
76-
this.startTracking()
77-
}
63+
useWatcher(measure, { updateOnAnimationFrame })
7864

79-
render() {
80-
return <div>FPS: {this.state.fps}</div>
81-
}
65+
return <div>FPS: {fps}</div>
8266
}
67+
8368
function PositionAlignOverview() {
8469
const [updateOnAnimationFrame, setUpdateOnAnimationFrame] = useState(false)
8570
const [inline, setInline] = useState(false)

demo/src/hooks/index.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
// @flow
2+
export { default as useWatcher } from './useWatcher'

demo/src/hooks/useWatcher.js

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
// @flow
2+
import { useEffect } from 'react'
3+
4+
type WatcherFuncT = () => void
5+
type OptionsT = {|
6+
updateOnAnimationFrame: boolean,
7+
|}
8+
9+
function useWatcher(
10+
watcher: WatcherFuncT,
11+
{ updateOnAnimationFrame }: OptionsT
12+
): void {
13+
useEffect(() => {
14+
let animationFrameId
15+
let idleCallbackId
16+
17+
// do not track in node
18+
if (typeof window.requestAnimationFrame !== 'undefined') {
19+
const callback = () => {
20+
watcher()
21+
22+
if (updateOnAnimationFrame) {
23+
animationFrameId = requestAnimationFrame(callback)
24+
} else {
25+
idleCallbackId = requestIdleCallback(callback)
26+
}
27+
}
28+
29+
callback()
30+
}
31+
32+
return () => {
33+
if (animationFrameId) {
34+
cancelAnimationFrame(animationFrameId)
35+
}
36+
37+
if (idleCallbackId) {
38+
cancelIdleCallback(idleCallbackId)
39+
}
40+
}
41+
}, [updateOnAnimationFrame, watcher])
42+
}
43+
44+
export default useWatcher

demo/src/index.html

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
<!DOCTYPE html>
2+
<html lang="en">
3+
<head>
4+
<meta charset="UTF-8" />
5+
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
6+
<title>react-stick</title>
7+
</head>
8+
<body>
9+
<div id="demo"></div>
10+
</body>
11+
</html>

demo/src/index.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
1+
import 'core-js/stable'
2+
import 'regenerator-runtime/runtime'
3+
14
import React from 'react'
25
import { render } from 'react-dom'
36
import { StylesAsDataAttributes } from 'substyle-glamor'

demo/src/regressions/ButtonOverlay.js

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
import React from 'react'
22

3-
import Stick from '../../../src'
4-
3+
import Stick from '../../../es'
54
import Regression from './Regression'
65

76
export default function ButtonOverlay() {

0 commit comments

Comments
 (0)