Skip to content

Commit b8e51ec

Browse files
committed
Merge remote-tracking branch 'origin/support/0.7.x' into support/0.7.x
2 parents 4a22aaf + 69f8eab commit b8e51ec

File tree

6 files changed

+113
-41
lines changed

6 files changed

+113
-41
lines changed

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@
2929
"autoprefixer": "^6.3.6",
3030
"babel-core": "^6.10.4",
3131
"babel-eslint": "^6.0.5",
32+
"element-resize-detector": "^1.1.12",
3233
"babel-jest": "^13.2.2",
3334
"babel-loader": "^6.2.4",
3435
"babel-plugin-check-es2015-constants": "^6.8.0",

src/components/ResizeDetector/ResizeDetector.jsx

Lines changed: 0 additions & 40 deletions
This file was deleted.
Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
import * as React from 'react';
2+
import { PURE } from 'dx-util/src/react/pure';
3+
import { themr } from 'react-css-themr';
4+
import { ComponentClass } from 'react';
5+
6+
import * as detectorFactory from 'element-resize-detector';
7+
import { raf } from '../../util/func/raf';
8+
9+
const NativeResizeDetector = detectorFactory({
10+
strategy: 'scroll'
11+
});
12+
13+
export const RESIZE_DETECTOR = Symbol('ResizeDetector');
14+
15+
type TResizeDetectorOwnProps = {
16+
onResize: (element: Element) => any
17+
};
18+
19+
type TResizeDetectorInjectedProps = {
20+
theme: {
21+
container?: string
22+
},
23+
24+
};
25+
26+
type TResizeDetectorFullProps = TResizeDetectorOwnProps & TResizeDetectorInjectedProps;
27+
28+
@PURE
29+
class RawResizeDetector extends React.Component<TResizeDetectorFullProps, any> {
30+
private element: HTMLDivElement | null;
31+
32+
componentDidMount() {
33+
if (this.element) {
34+
NativeResizeDetector.listenTo(this.element, this.onResize);
35+
}
36+
}
37+
38+
componentWillUnmount() {
39+
if (this.element) {
40+
NativeResizeDetector.uninstall(this.element);
41+
}
42+
}
43+
44+
render() {
45+
const { theme } = this.props;
46+
return (
47+
<div className={theme.container} ref={el => this.element = el} />
48+
);
49+
}
50+
51+
onResize = raf((element: Element) => {
52+
this.props.onResize(element);
53+
});
54+
}
55+
56+
export type TResizeDetectorProps = TResizeDetectorOwnProps & Partial<TResizeDetectorInjectedProps>;
57+
export default themr(RESIZE_DETECTOR)(RawResizeDetector) as ComponentClass<TResizeDetectorProps>;

src/components/Scrollable/Scrollable.jsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ import {
1010
} from './Scrollable.const';
1111

1212
import {SCROLLBAR_TYPE} from '../Scrollbar/Scrollbar';
13-
import ResizeDetector from '../ResizeDetector/ResizeDetector.jsx';
13+
import ResizeDetector from '../ResizeDetector/ResizeDetector.tsx';
1414
import HorizontalScrollbar from '../Scrollbar/HorizontalScrollbar.jsx';
1515
import VerticalScrollbar from '../Scrollbar/VerticalScrollbar.jsx';
1616

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
declare module 'element-resize-detector' {
2+
type TDetectoryFactoryOptions = {
3+
strategy?: 'scroll'
4+
};
5+
type THandler = (element: Element) => any;
6+
//tslint:disable max-line-length
7+
type TDetector = {
8+
/**
9+
* Listens to the element for resize events and calls the listener function with the element as argument on resize events.
10+
*/
11+
listenTo: (element: Element, handler: THandler) => void,
12+
13+
/**
14+
* Removes the listener from the elemens.
15+
*/
16+
removeListener: (element: Element, handler: THandler) => void,
17+
/**
18+
* Removes all listeners from the element, but does not completely remove the detector. Use this function if you may add listeners later and don't want the detector to have to initialize again.
19+
*/
20+
removeAllListeners: (element: Element) => void,
21+
/**
22+
*
23+
*/
24+
uninstall: (element: Element) => void
25+
};
26+
//tslint:enable max-line-length
27+
28+
const detectorFactory: (options: TDetectoryFactoryOptions) => TDetector;
29+
export = detectorFactory;
30+
}

src/util/func/raf.ts

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
// @TODO remove after update dx-util 0.11.7 version or more. use /src/function/raf.ts instead dx-util
2+
export const raf = <F extends Function>(cb: F): F & { cancel: () => void } => {
3+
let id: number | undefined;
4+
5+
const invoke = (ctx: any, args: any[]) => () => {
6+
id = undefined;
7+
cb.apply(ctx, args);
8+
};
9+
10+
//use function to save original context
11+
function synced(this: any, ...args: any[]) {
12+
if (typeof id === 'undefined') {
13+
id = requestAnimationFrame(invoke(this, args));
14+
}
15+
}
16+
17+
synced['cancel'] = () => {
18+
if (id) {
19+
cancelAnimationFrame(id);
20+
}
21+
};
22+
23+
return synced as any;
24+
};

0 commit comments

Comments
 (0)