Skip to content

Commit 189b8d2

Browse files
committed
typings changes;
implemented scrollTo and centerAt methods;
1 parent 15c31f2 commit 189b8d2

File tree

4 files changed

+95
-15
lines changed

4 files changed

+95
-15
lines changed

dist/index.js

Lines changed: 38 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -322,6 +322,44 @@ function (_React$Component) {
322322
passive: true
323323
});
324324
}
325+
}, {
326+
key: "scrollTo",
327+
328+
/**
329+
* Set scroll at given coordinates
330+
*
331+
* @param {number} x
332+
* @param {number} y
333+
*
334+
* @return {Scrollbar}
335+
*/
336+
value: function scrollTo(y, x) {
337+
this.contentEl.scrollTop = y;
338+
this.contentEl.scrollLeft = x;
339+
return this;
340+
}
341+
/**
342+
* Set viewport's center at given coordinates
343+
*
344+
* @param {number} x
345+
* @param {number} y
346+
*
347+
* @return {Scrollbar}
348+
*/
349+
350+
}, {
351+
key: "centerAt",
352+
value: function centerAt(y, x) {
353+
this.contentEl.scrollTop = y - this.contentEl.clientHeight / 2;
354+
this.contentEl.scrollLeft = x - this.contentEl.clientWidth / 2;
355+
return this;
356+
}
357+
/**
358+
* Return the vertical scroll position
359+
*
360+
* @return {number}
361+
*/
362+
325363
}, {
326364
key: "scrollToTop",
327365

@@ -710,12 +748,6 @@ function (_React$Component) {
710748
}
711749
}, {
712750
key: "scrollTop",
713-
714-
/**
715-
* Return the vertical scroll position
716-
*
717-
* @return {number}
718-
*/
719751
get: function get() {
720752
if (this.contentEl) {
721753
return this.contentEl.scrollTop;

index.d.ts

Lines changed: 13 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1-
import {Component, CSSProperties, SFC} from "react";
1+
import {Component, CSSProperties, Props, SFC} from "react";
22

3-
export interface ScrollValues {
3+
interface ScrollValues {
44
clientHeight?: number;
55
clientWidth?: number;
66
scrollHeight?: number;
@@ -16,35 +16,35 @@ export interface ScrollValues {
1616
isRtl?: boolean;
1717
}
1818

19-
export interface ScrollbarRendererProps {
19+
interface ScrollbarRendererProps {
2020
className: string;
2121
style: CSSProperties;
2222

2323
elementRef: void;
2424
}
2525

26-
export interface ScrollbarTrackRendererProps {
26+
interface ScrollbarTrackRendererProps {
2727
className: string;
2828
style: CSSProperties;
2929

3030
elementRef: void;
3131
onClick: void;
3232
}
3333

34-
export interface ScrollbarThumbRendererProps {
34+
interface ScrollbarThumbRendererProps {
3535
className: string;
3636
style: CSSProperties;
3737

3838
elementRef: void;
3939
onMouseDown: void;
4040
}
4141

42-
export enum TrackClickBehaviorPossibilities {
42+
declare enum TrackClickBehaviorPossibilities {
4343
Jump = "jump",
4444
Step = "step",
4545
}
4646

47-
export interface ScrollbarProps {
47+
interface ScrollbarProps extends Props<Scrollbar> {
4848
native?: boolean;
4949

5050
minimalThumbsSize?: number;
@@ -100,7 +100,7 @@ export interface ScrollbarProps {
100100
onScrollStop?(scrollValues: ScrollValues, instance: Scrollbar): void;
101101
}
102102

103-
declare class Scrollbar extends Component<ScrollbarProps> {
103+
export default class Scrollbar extends Component<ScrollbarProps> {
104104
public scrollTop: number;
105105
public scrollLeft: number;
106106
public readonly scrollHeight: number;
@@ -116,6 +116,10 @@ declare class Scrollbar extends Component<ScrollbarProps> {
116116

117117
public scrollToRight(): Scrollbar;
118118

119+
public scrollTo(x: number, y: number): Scrollbar;
120+
121+
public centerAt(x: number, y: number): Scrollbar;
122+
119123
/**
120124
* Return the scroll values actual for the last update.
121125
*
@@ -146,4 +150,4 @@ declare class Scrollbar extends Component<ScrollbarProps> {
146150
public static computeScrollForOffset(trackSize: number, thumbSize: number, offset: number, scrollableSize: number, viewportSize: number): number
147151
}
148152

149-
export default Scrollbar;
153+
export as namespace Scrollbar;

src/index.js

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -273,6 +273,36 @@ export default class Scrollbar extends React.Component {
273273
}, this.props.scrollDetectionThreshold);
274274
};
275275

276+
/**
277+
* Set scroll at given coordinates
278+
*
279+
* @param {number} x
280+
* @param {number} y
281+
*
282+
* @return {Scrollbar}
283+
*/
284+
scrollTo(y, x) {
285+
this.contentEl.scrollTop = y;
286+
this.contentEl.scrollLeft = x;
287+
288+
return this;
289+
}
290+
291+
/**
292+
* Set viewport's center at given coordinates
293+
*
294+
* @param {number} x
295+
* @param {number} y
296+
*
297+
* @return {Scrollbar}
298+
*/
299+
centerAt(y, x) {
300+
this.contentEl.scrollTop = y - this.contentEl.clientHeight / 2;
301+
this.contentEl.scrollLeft = x - this.contentEl.clientWidth / 2;
302+
303+
return this;
304+
}
305+
276306
/**
277307
* Return the vertical scroll position
278308
*

tests/Scrollbar/getset.js

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -188,5 +188,19 @@ export default function performTests() {
188188
scrollbar.contentEl.scrollWidth - scrollbar.contentEl.clientWidth
189189
);
190190
});
191+
192+
it("scrollTo should set scrolls at given position", () => {
193+
scrollbar.scrollTo(50, 50);
194+
195+
expect(scrollbar.contentEl.scrollTop).toBe(50);
196+
expect(scrollbar.contentEl.scrollLeft).toBe(50);
197+
});
198+
199+
it("centerAt should set viewport center at given position at given position", () => {
200+
scrollbar.centerAt(100, 100);
201+
202+
expect(scrollbar.contentEl.scrollTop).toBe(50);
203+
expect(scrollbar.contentEl.scrollLeft).toBe(50);
204+
});
191205
});
192206
}

0 commit comments

Comments
 (0)