UNPKG

6.56 kBMarkdownView Raw
1react-custom-scrollbars
2=========================
3
4[![npm](https://img.shields.io/badge/npm-react--custom--scrollbars-brightgreen.svg?style=flat-square)]()
5[![npm version](https://img.shields.io/npm/v/react-custom-scrollbars.svg?style=flat-square)](https://www.npmjs.com/package/react-custom-scrollbars)
6[![npm downloads](https://img.shields.io/npm/dm/react-custom-scrollbars.svg?style=flat-square)](https://www.npmjs.com/package/react-custom-scrollbars)
7
8* lightweight scrollbars made of 100% react goodness
9* frictionless native browser scrolling
10* native scrollbars for mobile devices
11* fully customizable
12* `requestAnimationFrame` for 60fps
13* no extra stylesheets
14* IE9+ support
15* **[check out the demo](http://malte-wessel.github.io/react-custom-scrollbars/)**
16
17## Table of Contents
18
19- [Installation](#installation)
20- [Usage](#usage)
21- [Customization](#customization)
22- [API](#api)
23- [Examples](#examples)
24- [Tests](#tests)
25- [License](#license)
26
27## Installation
28```bash
29npm install react-custom-scrollbars --save
30```
31
32## Usage
33```javascript
34import { Scrollbars } from 'react-custom-scrollbars';
35
36class App extends Component {
37 render() {
38 return (
39 <Scrollbars style={{ width: 500, height: 300 }}>
40 <p>Some great content...</p>
41 </Scrollbars>
42 );
43 }
44}
45```
46
47Don't forget to set the `viewport` meta tag, if you want to support mobile devices
48
49```html
50<meta
51 name="viewport"
52 content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=0"/>
53```
54
55## Customization
56```javascript
57import { Scrollbars } from 'react-custom-scrollbars';
58
59class CustomScrollbars extends Component {
60 render() {
61 return (
62 <Scrollbars
63 className="container"
64 renderScrollbarHorizontal={props => <div {...props} className="scrollbar-horizontal" />}
65 renderScrollbarVertical={props => <div {...props} className="scrollbar-vertical"/>}
66 renderThumbHorizontal={props => <div {...props} className="thumb-horizontal"/>}
67 renderThumbVertical={props => <div {...props} className="thumb-vertical"/>}
68 renderView={props => <div {...props} className="view"/>}>
69 {this.props.children}
70 </Scrollbars>
71 );
72 }
73}
74
75class App extends Component {
76 render() {
77 return (
78 <CustomScrollbars style={{ width: 500, height: 300 }}>
79 <p>Some great content...</p>
80 </CustomScrollbars>
81 );
82 }
83}
84```
85
86**NOTE**: If you use `renderScrollbarHorizontal`, **make sure that you define a height value** with css or inline styles. If you use `renderScrollbarVertical`, **make sure that you define a width value with** css or inline styles.
87
88## API
89
90### `<Scrollbars>`
91
92#### Props
93
94* `renderScrollbarHorizontal`: (Function) Horizontal scrollbar element
95* `renderScrollbarVertical`: (Function) Vertical scrollbar element
96* `renderThumbHorizontal`: (Function) Horizontal thumb element
97* `renderThumbVertical`: (Function) Vertical thumb element
98* `renderView`: (Function) The element your content will be rendered in
99* `onScroll`: (Function) Event handler. Will be called with the native scroll event and some handy values about the current position.
100 * **Signature**: `onScroll(event, values)`
101 * `event`: (Event) Native onScroll event
102 * `values`: (Object) Values about the current position
103 * `values.top`: (Number) scrollTop progess, from 0 to 1
104 * `values.left`: (Number) scrollLeft progess, from 0 to 1
105 * `values.clientWidth`: (Number) width of the view
106 * `values.clientHeight`: (Number) height of the view
107 * `values.scrollWidth`: (Number) native scrollWidth
108 * `values.scrollHeight`: (Number) native scrollHeight
109 * `values.scrollLeft`: (Number) native scrollLeft
110 * `values.scrollTop`: (Number) native scrollTop
111
112**Don't forget to pass the received props to your custom element. Example:**
113
114**NOTE**: If you use `renderScrollbarHorizontal`, **make sure that you define a height value** with css or inline styles. If you use `renderScrollbarVertical`, **make sure that you define a width value with** css or inline styles.
115
116```javascript
117import { Scrollbars } from 'react-custom-scrollbars';
118
119class CustomScrollbars extends Component {
120 render() {
121 return (
122 <Scrollbars
123 // Set a custom className
124 renderScrollbarHorizontal={props => <div {...props} className="scrollbar-vertical"/>}
125 // Customize inline styles
126 renderScrollbarVertical={({ style, ...props}) => {
127 return <div style={{...style, padding: 20}} {...props}/>;
128 }}>
129 {this.props.children}
130 </Scrollbars>
131 );
132 }
133}
134```
135
136#### Methods
137
138* `scrollTop(top)`: scroll to the top value
139* `scrollLeft(left)`: scroll to the left value
140* `scrollToTop()`: scroll to top
141* `scrollToBottom()`: scroll to bottom
142* `scrollToLeft()`: scroll to left
143* `scrollToRight()`: scroll to right
144* `getScrollLeft`: get scrollLeft value
145* `getScrollTop`: get scrollTop value
146* `getScrollWidth`: get scrollWidth value
147* `getScrollHeight`: get scrollHeight value
148* `getWidth`: get view client width
149* `getHeight`: get view client height
150* `getValues`: get an object with values about the current position.
151 * `left`, `top`, `scrollLeft`, `scrollTop`, `scrollWidth`, `scrollHeight`, `clientWidth`, `clientHeight`
152
153```javascript
154import { Scrollbars } from 'react-custom-scrollbars';
155
156class App extends Component {
157 handleClick() {
158 this.refs.scrollbars.scrollToTop()
159 },
160 render() {
161 return (
162 <div>
163 <Scrollbars
164 ref="scrollbars"
165 style={{ width: 500, height: 300 }}>
166 {/* your content */}
167 </Scrollbars>
168 <button onClick={this.handleClick.bind(this)}>
169 Scroll to top
170 </button>
171 </div>
172 );
173 }
174}
175```
176
177### Receive values about the current position
178
179```javascript
180class CustomScrollbars extends Component {
181 handleScroll(event, values) {
182 console.log(values);
183 /*
184 {
185 left: 0,
186 top: 0.21513353115727002
187 clientWidth: 952
188 clientHeight: 300
189 scrollWidth: 952
190 scrollHeight: 1648
191 scrollLeft: 0
192 scrollTop: 290
193 }
194 */
195 }
196 render() {
197 return (
198 <Scrollbars onScroll={this.handleScroll}>
199 {this.props.children}
200 </Scrollbars>
201 );
202 }
203}
204```
205
206## Examples
207
208Run the simple example:
209```bash
210# Make sure that you've installed the dependencies
211npm install
212# Move to example directory
213cd react-custom-scrollbars/examples/simple
214npm install
215npm start
216```
217
218## Tests
219```bash
220# Make sure that you've installed the dependencies
221npm install
222# Run tests
223npm test
224```
225## License
226
227MIT