@galangel/react-scroll-magic 1.0.1 → 1.0.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +165 -22
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -8,9 +8,14 @@ The Scroll Magic Component is a React component that provides smooth scrolling f
|
|
|
8
8
|
|
|
9
9
|
## Features
|
|
10
10
|
|
|
11
|
-
-
|
|
12
|
-
- scroll to
|
|
13
|
-
-
|
|
11
|
+
- **Sticky Headers**: Headers can stick to top, bottom, or both directions
|
|
12
|
+
- **Click-to-Scroll**: Click any header to smoothly scroll to its content
|
|
13
|
+
- **Collapsible Sections**: Headers can expand/collapse their nested content
|
|
14
|
+
- **Infinite Scrolling**: Load more content when reaching the bottom
|
|
15
|
+
- **Nested Structure**: Support for deeply nested hierarchical content
|
|
16
|
+
- **Multiple Header Behaviors**: Choose between sticky, push, or normal header behavior
|
|
17
|
+
- **TypeScript Support**: Fully typed with comprehensive interfaces
|
|
18
|
+
- **Customizable Rendering**: Complete control over how items and loading states are rendered
|
|
14
19
|
|
|
15
20
|
## Installation
|
|
16
21
|
|
|
@@ -32,41 +37,179 @@ Here is a basic example of how to use the Scroll Component in your React applica
|
|
|
32
37
|
|
|
33
38
|
```jsx
|
|
34
39
|
import React from 'react';
|
|
35
|
-
import {
|
|
40
|
+
import { Scroll } from '@galangel/react-scroll-magic';
|
|
36
41
|
|
|
37
42
|
const App = () => {
|
|
43
|
+
const items = [
|
|
44
|
+
{
|
|
45
|
+
// Header item with nested content
|
|
46
|
+
render: ({ collapse }) => (
|
|
47
|
+
<div style={{ padding: '10px', backgroundColor: '#f0f0f0' }}>
|
|
48
|
+
Header 1
|
|
49
|
+
{collapse && (
|
|
50
|
+
<button onClick={collapse.isOpen ? collapse.close : collapse.open}>{collapse.isOpen ? '▼' : '▶'}</button>
|
|
51
|
+
)}
|
|
52
|
+
</div>
|
|
53
|
+
),
|
|
54
|
+
nestedItems: [
|
|
55
|
+
{
|
|
56
|
+
// Regular item
|
|
57
|
+
render: () => <div style={{ padding: '10px' }}>Item 1.1</div>,
|
|
58
|
+
},
|
|
59
|
+
{
|
|
60
|
+
render: () => <div style={{ padding: '10px' }}>Item 1.2</div>,
|
|
61
|
+
},
|
|
62
|
+
],
|
|
63
|
+
},
|
|
64
|
+
{
|
|
65
|
+
// Simple item without nesting
|
|
66
|
+
render: () => <div style={{ padding: '10px' }}>Simple Item</div>,
|
|
67
|
+
},
|
|
68
|
+
];
|
|
69
|
+
|
|
38
70
|
return (
|
|
39
|
-
<
|
|
40
|
-
items={
|
|
41
|
-
|
|
42
|
-
/* array of items */
|
|
43
|
-
]
|
|
44
|
-
}
|
|
45
|
-
{...props}
|
|
46
|
-
/>
|
|
71
|
+
<div style={{ height: '400px', width: '100%' }}>
|
|
72
|
+
<Scroll items={items} stickTo="all" headerBehavior="stick" scrollBehavior="smooth" />
|
|
73
|
+
</div>
|
|
47
74
|
);
|
|
48
75
|
};
|
|
49
76
|
|
|
50
77
|
export default App;
|
|
51
78
|
```
|
|
52
79
|
|
|
80
|
+
### Infinite Scrolling Example
|
|
81
|
+
|
|
82
|
+
```jsx
|
|
83
|
+
import React, { useState } from 'react';
|
|
84
|
+
import { Scroll } from '@galangel/react-scroll-magic';
|
|
85
|
+
|
|
86
|
+
const InfiniteScrollExample = () => {
|
|
87
|
+
const [items, setItems] = useState([
|
|
88
|
+
{ render: () => <div>Initial Item 1</div> },
|
|
89
|
+
{ render: () => <div>Initial Item 2</div> },
|
|
90
|
+
]);
|
|
91
|
+
|
|
92
|
+
const loadMoreItems = async () => {
|
|
93
|
+
// Simulate API call
|
|
94
|
+
await new Promise((resolve) => setTimeout(resolve, 1000));
|
|
95
|
+
|
|
96
|
+
const newItems = Array.from({ length: 10 }, (_, i) => ({
|
|
97
|
+
render: () => <div>New Item {items.length + i + 1}</div>,
|
|
98
|
+
}));
|
|
99
|
+
|
|
100
|
+
setItems((prev) => [...prev, ...newItems]);
|
|
101
|
+
};
|
|
102
|
+
|
|
103
|
+
const loading = {
|
|
104
|
+
onBottomReached: loadMoreItems,
|
|
105
|
+
render: (isLoading) => (
|
|
106
|
+
<div style={{ textAlign: 'center', padding: '20px' }}>{isLoading ? 'Loading...' : 'Load more'}</div>
|
|
107
|
+
),
|
|
108
|
+
};
|
|
109
|
+
|
|
110
|
+
return (
|
|
111
|
+
<div style={{ height: '400px', width: '100%' }}>
|
|
112
|
+
<Scroll items={items} loading={loading} headerBehavior="none" />
|
|
113
|
+
</div>
|
|
114
|
+
);
|
|
115
|
+
};
|
|
116
|
+
```
|
|
117
|
+
|
|
53
118
|
## Props
|
|
54
119
|
|
|
55
120
|
The Scroll Component accepts the following props:
|
|
56
121
|
|
|
57
|
-
| Prop
|
|
58
|
-
|
|
|
59
|
-
| `
|
|
60
|
-
| `
|
|
61
|
-
| `
|
|
62
|
-
| `
|
|
63
|
-
| `loading`
|
|
64
|
-
|
|
65
|
-
|
|
122
|
+
| Prop | Type | Default | Description |
|
|
123
|
+
| ---------------- | --------------------------------- | ---------- | ---------------------------------------------------------------------------------------------------------- |
|
|
124
|
+
| `items` | `Items` | Required | Array of items to render. Each item has a `render` function and optional `nestedItems` |
|
|
125
|
+
| `stickTo` | `'top' \| 'bottom' \| 'all'` | `'all'` | Where headers should stick when scrolling |
|
|
126
|
+
| `scrollBehavior` | `'auto' \| 'instant' \| 'smooth'` | `'smooth'` | CSS scroll-behavior when clicking headers to scroll to content |
|
|
127
|
+
| `headerBehavior` | `'stick' \| 'push' \| 'none'` | `'none'` | How headers behave: `stick` = sticky positioning, `push` = headers push down content, `none` = normal flow |
|
|
128
|
+
| `loading` | `Loading` | Optional | Configuration for infinite scrolling and loading states |
|
|
129
|
+
|
|
130
|
+
### Items Type Definition
|
|
131
|
+
|
|
132
|
+
```typescript
|
|
133
|
+
type Items = Item[];
|
|
134
|
+
|
|
135
|
+
interface Item {
|
|
136
|
+
id?: string; // Optional unique identifier
|
|
137
|
+
render: (renderProps: RenderProps) => JSX.Element; // Function to render the item
|
|
138
|
+
nestedItems?: Item[]; // Optional nested items (makes this a header)
|
|
139
|
+
}
|
|
140
|
+
|
|
141
|
+
interface RenderProps {
|
|
142
|
+
collapse?: {
|
|
143
|
+
open: () => void; // Function to expand nested items
|
|
144
|
+
close: () => void; // Function to collapse nested items
|
|
145
|
+
isOpen: boolean; // Current collapse state
|
|
146
|
+
};
|
|
147
|
+
}
|
|
148
|
+
```
|
|
149
|
+
|
|
150
|
+
### Loading Type Definition
|
|
151
|
+
|
|
152
|
+
```typescript
|
|
153
|
+
interface Loading {
|
|
154
|
+
onBottomReached?: () => Promise<void>; // Callback when user scrolls to bottom
|
|
155
|
+
render?: (isLoading: boolean) => JSX.Element; // Custom loading indicator renderer
|
|
156
|
+
}
|
|
157
|
+
```
|
|
158
|
+
|
|
159
|
+
## Item Structure
|
|
160
|
+
|
|
161
|
+
- **Simple Items**: Items without `nestedItems` are rendered as regular list items
|
|
162
|
+
- **Header Items**: Items with `nestedItems` become clickable headers that can:
|
|
163
|
+
- Scroll to their content when clicked
|
|
164
|
+
- Be collapsed/expanded using the `collapse` props in the render function
|
|
165
|
+
- Stick to top/bottom based on `stickTo` and `headerBehavior` settings
|
|
166
|
+
- **Nested Structure**: Items can be nested to any depth for complex hierarchical layouts
|
|
167
|
+
|
|
168
|
+
## Styling
|
|
169
|
+
|
|
170
|
+
The component uses CSS classes that you can target for styling:
|
|
171
|
+
|
|
172
|
+
```css
|
|
173
|
+
/* Main scroll container */
|
|
174
|
+
.scroll-list {
|
|
175
|
+
/* Your styles here */
|
|
176
|
+
}
|
|
177
|
+
|
|
178
|
+
/* Header items */
|
|
179
|
+
.scroll-header {
|
|
180
|
+
/* Your styles here */
|
|
181
|
+
}
|
|
182
|
+
|
|
183
|
+
/* Sticky header behavior */
|
|
184
|
+
.scroll-header.stick {
|
|
185
|
+
position: sticky;
|
|
186
|
+
/* Additional sticky styles */
|
|
187
|
+
}
|
|
188
|
+
|
|
189
|
+
/* Push header behavior */
|
|
190
|
+
.scroll-header.push {
|
|
191
|
+
/* Push behavior styles */
|
|
192
|
+
}
|
|
193
|
+
|
|
194
|
+
/* Regular items */
|
|
195
|
+
.scroll-item {
|
|
196
|
+
/* Your styles here */
|
|
197
|
+
}
|
|
198
|
+
```
|
|
199
|
+
|
|
200
|
+
**Important**: Make sure your scroll container has a defined height for the scrolling to work properly:
|
|
201
|
+
|
|
202
|
+
```jsx
|
|
203
|
+
<div style={{ height: '400px' }}>
|
|
204
|
+
{' '}
|
|
205
|
+
{/* Define height here */}
|
|
206
|
+
<Scroll items={items} />
|
|
207
|
+
</div>
|
|
208
|
+
```
|
|
66
209
|
|
|
67
210
|
## License
|
|
68
211
|
|
|
69
|
-
This project is licensed under the
|
|
212
|
+
This project is licensed under the Apache License 2.0. See the [LICENSE](LICENSE) file for details.
|
|
70
213
|
|
|
71
214
|
## Contributing
|
|
72
215
|
|