|
| 1 | +import { useEffect, useRef, useState } from 'react' |
| 2 | +import * as ReactDOM from 'react-dom/client' |
| 3 | +import VanillaTilt from 'vanilla-tilt' |
| 4 | + |
| 5 | +interface HTMLVanillaTiltElement extends HTMLDivElement { |
| 6 | + vanillaTilt: VanillaTilt |
| 7 | +} |
| 8 | + |
| 9 | +function Tilt({ |
| 10 | + children, |
| 11 | + max = 25, |
| 12 | + speed = 400, |
| 13 | + glare = true, |
| 14 | + maxGlare = 0.5, |
| 15 | +}: { |
| 16 | + children: React.ReactNode |
| 17 | + max?: number |
| 18 | + speed?: number |
| 19 | + glare?: boolean |
| 20 | + maxGlare?: number |
| 21 | +}) { |
| 22 | + const tiltRef = useRef<HTMLVanillaTiltElement>(null) |
| 23 | + |
| 24 | + // 🐨 move this into the useEffect directly |
| 25 | + const vanillaTiltOptions = { |
| 26 | + max, |
| 27 | + speed, |
| 28 | + glare, |
| 29 | + 'max-glare': maxGlare, |
| 30 | + } |
| 31 | + |
| 32 | + useEffect(() => { |
| 33 | + const { current: tiltNode } = tiltRef |
| 34 | + if (tiltNode === null) return |
| 35 | + VanillaTilt.init(tiltNode, vanillaTiltOptions) |
| 36 | + return () => tiltNode.vanillaTilt.destroy() |
| 37 | + // 🐨 instead of passing the options object here, pass each primitive option |
| 38 | + }, [vanillaTiltOptions]) |
| 39 | + |
| 40 | + return ( |
| 41 | + <div ref={tiltRef} className="tilt-root"> |
| 42 | + <div className="tilt-child">{children}</div> |
| 43 | + </div> |
| 44 | + ) |
| 45 | +} |
| 46 | + |
| 47 | +function App() { |
| 48 | + const [count, setCount] = useState(0) |
| 49 | + const [options, setOptions] = useState({ |
| 50 | + max: 25, |
| 51 | + speed: 400, |
| 52 | + glare: true, |
| 53 | + maxGlare: 0.5, |
| 54 | + }) |
| 55 | + return ( |
| 56 | + <div className="app"> |
| 57 | + <form |
| 58 | + onSubmit={e => e.preventDefault()} |
| 59 | + onChange={event => { |
| 60 | + const formData = new FormData(event.currentTarget) |
| 61 | + setOptions({ |
| 62 | + max: formData.get('max') as any, |
| 63 | + speed: formData.get('speed') as any, |
| 64 | + glare: formData.get('glare') === 'on', |
| 65 | + maxGlare: formData.get('maxGlare') as any, |
| 66 | + }) |
| 67 | + }} |
| 68 | + > |
| 69 | + <div> |
| 70 | + <label htmlFor="max">Max:</label> |
| 71 | + <input id="max" name="max" type="number" defaultValue={25} /> |
| 72 | + </div> |
| 73 | + <div> |
| 74 | + <label htmlFor="speed">Speed:</label> |
| 75 | + <input id="speed" name="speed" type="number" defaultValue={400} /> |
| 76 | + </div> |
| 77 | + <div> |
| 78 | + <label> |
| 79 | + <input id="glare" name="glare" type="checkbox" defaultChecked /> |
| 80 | + Glare |
| 81 | + </label> |
| 82 | + </div> |
| 83 | + <div> |
| 84 | + <label htmlFor="maxGlare">Max Glare:</label> |
| 85 | + <input |
| 86 | + id="maxGlare" |
| 87 | + name="maxGlare" |
| 88 | + type="number" |
| 89 | + defaultValue={0.5} |
| 90 | + /> |
| 91 | + </div> |
| 92 | + </form> |
| 93 | + <br /> |
| 94 | + <Tilt {...options}> |
| 95 | + <div className="totally-centered"> |
| 96 | + <button className="count-button" onClick={() => setCount(c => c + 1)}> |
| 97 | + {count} |
| 98 | + </button> |
| 99 | + </div> |
| 100 | + </Tilt> |
| 101 | + </div> |
| 102 | + ) |
| 103 | +} |
| 104 | + |
| 105 | +const rootEl = document.createElement('div') |
| 106 | +document.body.append(rootEl) |
| 107 | +ReactDOM.createRoot(rootEl).render(<App />) |
| 108 | + |
| 109 | +// 🤫 we'll fix this in the next step! |
| 110 | +// (ALMOST) NEVER DISABLE THIS LINT RULE IN REAL LIFE! |
| 111 | +/* |
| 112 | +eslint |
| 113 | + react-hooks/exhaustive-deps: "off", |
| 114 | +*/ |
0 commit comments