|
1 | 1 | import type { CSSProperties } from 'react';
|
2 | 2 |
|
3 |
| -import { useMemo, useRef, useState } from 'react'; |
| 3 | +import { register } from 'node:module'; |
4 | 4 |
|
| 5 | +import { useField } from '../useField/useField'; |
| 6 | +import { useToggle } from '../useToggle/useToggle'; |
5 | 7 | import { useScroll } from './useScroll';
|
6 | 8 |
|
7 | 9 | const styles: Record<string, CSSProperties> = {
|
8 | 10 | container: {
|
9 | 11 | width: '300px',
|
10 | 12 | height: '300px',
|
11 |
| - margin: 'auto', |
12 | 13 | overflow: 'scroll',
|
13 | 14 | backgroundColor: 'rgba(128, 128, 128, 0.05)',
|
14 | 15 | borderRadius: '8px'
|
@@ -64,65 +65,82 @@ const styles: Record<string, CSSProperties> = {
|
64 | 65 | };
|
65 | 66 |
|
66 | 67 | const Demo = () => {
|
67 |
| - const elementRef = useRef<HTMLDivElement>(null); |
| 68 | + const xInput = useField({ initialValue: 0 }); |
| 69 | + const yInput = useField({ initialValue: 0 }); |
| 70 | + const [behavior, setBehavior] = useToggle<ScrollBehavior>(['auto', 'smooth']); |
68 | 71 |
|
69 |
| - const [scrollX, setScrollX] = useState(0); |
70 |
| - const [scrollY, setScrollY] = useState(0); |
71 |
| - const [behavior, setBehavior] = useState<ScrollBehavior>('auto'); |
| 72 | + const scrollX = xInput.watch(); |
| 73 | + const scrollY = yInput.watch(); |
72 | 74 |
|
73 |
| - const { x, y, isScrolling, arrivedState, directions } = useScroll(elementRef, { |
| 75 | + const scroll = useScroll<HTMLDivElement>({ |
74 | 76 | x: scrollX,
|
75 | 77 | y: scrollY,
|
76 |
| - behavior |
| 78 | + behavior, |
| 79 | + onScroll: (event) => { |
| 80 | + console.log('onScroll', event); |
| 81 | + } |
77 | 82 | });
|
78 | 83 |
|
79 |
| - const { left, right, top, bottom } = useMemo(() => arrivedState, [arrivedState]); |
80 |
| - |
81 |
| - const { |
82 |
| - left: toLeft, |
83 |
| - right: toRight, |
84 |
| - top: toTop, |
85 |
| - bottom: toBottom |
86 |
| - } = useMemo(() => directions, [directions]); |
87 |
| - |
88 | 84 | return (
|
89 |
| - <div style={{ display: 'flex' }}> |
90 |
| - <div ref={elementRef} style={styles.container}> |
91 |
| - <div style={styles.inner}> |
92 |
| - <div style={styles.topLeft}>TopLeft</div> |
93 |
| - <div style={styles.bottomLeft}>BottomLeft</div> |
94 |
| - <div style={styles.topRight}>TopRight</div> |
95 |
| - <div style={styles.bottomRight}>BottomRight</div> |
96 |
| - <div style={styles.center}>Scroll Me</div> |
| 85 | + <div> |
| 86 | + <div style={{ display: 'flex', gap: 10, flexDirection: 'column', marginBottom: 10 }}> |
| 87 | + <div style={{ display: 'flex', gap: 10 }}> |
| 88 | + <div style={{ display: 'flex', gap: 10, alignItems: 'center' }}> |
| 89 | + <span>x:</span> |
| 90 | + <input type='number'{...xInput.register()} /> |
| 91 | + </div> |
| 92 | + <div style={{ display: 'flex', gap: 10, alignItems: 'center' }}> |
| 93 | + <span>y:</span> |
| 94 | + <input type='number' {...yInput.register()} /> |
| 95 | + </div> |
97 | 96 | </div>
|
98 |
| - </div> |
99 |
| - <div style={styles.containerInfo}> |
100 |
| - <div> |
101 |
| - X Position: |
102 |
| - <input value={x} onChange={(event) => setScrollX(+event.target.value)} /> |
| 97 | + <div style={{ display: 'flex', gap: 10, flexDirection: 'column' }}> |
| 98 | + <div style={{ display: 'flex', gap: 10, alignItems: 'center' }}> |
| 99 | + Smooth scrolling:{' '} |
| 100 | + <input |
| 101 | + type='checkbox' |
| 102 | + value={behavior} |
| 103 | + onChange={(event) => setBehavior(event.target.checked ? 'smooth' : 'auto')} |
| 104 | + /> |
| 105 | + </div> |
| 106 | + <div>scrolling: <code>{String(scroll.scrolling)}</code></div> |
103 | 107 | </div>
|
104 |
| - <div> |
105 |
| - Y Position: |
106 |
| - <input value={y} onChange={(event) => setScrollY(+event.target.value)} /> |
| 108 | + </div> |
| 109 | + |
| 110 | + |
| 111 | + <div style={{ display: 'flex', gap: 10, justifyContent: 'space-between' }}> |
| 112 | + <div ref={scroll.ref} style={styles.container}> |
| 113 | + <div style={styles.inner}> |
| 114 | + <div style={styles.topLeft}>TopLeft</div> |
| 115 | + <div style={styles.bottomLeft}>BottomLeft</div> |
| 116 | + <div style={styles.topRight}>TopRight</div> |
| 117 | + <div style={styles.bottomRight}>BottomRight</div> |
| 118 | + <div style={styles.center}>Scroll Me</div> |
| 119 | + </div> |
107 | 120 | </div>
|
108 |
| - <div style={{ display: 'flex', gap: 10, alignItems: 'center' }}> |
109 |
| - Smooth scrolling:{' '} |
110 |
| - <input |
111 |
| - type='checkbox' |
112 |
| - value={behavior} |
113 |
| - onChange={(event) => setBehavior(event.target.checked ? 'smooth' : 'auto')} |
114 |
| - /> |
| 121 | + |
| 122 | + <div style={{ display: 'flex', gap: 10, alignItems: 'center', padding: 15 }}> |
| 123 | + <div style={{ display: 'flex', gap: 10, flexDirection: 'column', minWidth: 150 }}> |
| 124 | + <h4>Arrived</h4> |
| 125 | + <div style={{ display: 'flex', gap: 15, flexDirection: 'column' }}> |
| 126 | + <div>top: <code>{String(scroll.arrived.top)}</code></div> |
| 127 | + <div>right: <code>{String(scroll.arrived.right)}</code></div> |
| 128 | + <div>bottom: <code>{String(scroll.arrived.bottom)}</code></div> |
| 129 | + <div>left: <code>{String(scroll.arrived.left)}</code></div> |
| 130 | + </div> |
| 131 | + </div> |
| 132 | + |
| 133 | + <div style={{ display: 'flex', gap: 10, flexDirection: 'column' }}> |
| 134 | + <h4>Directions</h4> |
| 135 | + <div style={{ display: 'flex', gap: 15, flexDirection: 'column', minWidth: 150 }}> |
| 136 | + <div>top: <code>{String(scroll.directions.top)}</code></div> |
| 137 | + <div>right: <code>{String(scroll.directions.right)}</code></div> |
| 138 | + <div>bottom: <code>{String(scroll.directions.bottom)}</code></div> |
| 139 | + <div>left: <code>{String(scroll.directions.left)}</code></div> |
| 140 | + </div> |
| 141 | + </div> |
115 | 142 | </div>
|
116 |
| - <div>isScrolling: {JSON.stringify(isScrolling)}</div> |
117 |
| - <div>Top Arrived: {JSON.stringify(top)}</div> |
118 |
| - <div>Right Arrived: {JSON.stringify(right)}</div> |
119 |
| - <div>Bottom Arrived: {JSON.stringify(bottom)}</div> |
120 |
| - <div>Left Arrived: {JSON.stringify(left)}</div> |
121 |
| - <div>Scrolling Up: {JSON.stringify(toTop)}</div> |
122 |
| - <div>Scrolling Right: {JSON.stringify(toRight)}</div> |
123 |
| - <div>Scrolling Down: {JSON.stringify(toBottom)}</div> |
124 |
| - <div>Scrolling Left: {JSON.stringify(toLeft)}</div> |
125 |
| - </div> |
| 143 | + </div > |
126 | 144 | </div>
|
127 | 145 | );
|
128 | 146 | };
|
|
0 commit comments