File tree 6 files changed +113
-41
lines changed
typings/element-resize-detector
6 files changed +113
-41
lines changed Original file line number Diff line number Diff line change 29
29
"autoprefixer" : " ^6.3.6" ,
30
30
"babel-core" : " ^6.10.4" ,
31
31
"babel-eslint" : " ^6.0.5" ,
32
+ "element-resize-detector" : " ^1.1.12" ,
32
33
"babel-jest" : " ^13.2.2" ,
33
34
"babel-loader" : " ^6.2.4" ,
34
35
"babel-plugin-check-es2015-constants" : " ^6.8.0" ,
Load Diff This file was deleted.
Original file line number Diff line number Diff line change
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 > ;
Original file line number Diff line number Diff line change @@ -10,7 +10,7 @@ import {
10
10
} from './Scrollable.const' ;
11
11
12
12
import { SCROLLBAR_TYPE } from '../Scrollbar/Scrollbar' ;
13
- import ResizeDetector from '../ResizeDetector/ResizeDetector.jsx ' ;
13
+ import ResizeDetector from '../ResizeDetector/ResizeDetector.tsx ' ;
14
14
import HorizontalScrollbar from '../Scrollbar/HorizontalScrollbar.jsx' ;
15
15
import VerticalScrollbar from '../Scrollbar/VerticalScrollbar.jsx' ;
16
16
Original file line number Diff line number Diff line change
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
+ }
Original file line number Diff line number Diff line change
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
+ } ;
You can’t perform that action at this time.
0 commit comments