Skip to content

Commit c3f33ff

Browse files
committed
update ResizeDetector to element-resize-detector's scroll strategy
1 parent 6cef91b commit c3f33ff

File tree

7 files changed

+88
-69
lines changed

7 files changed

+88
-69
lines changed

package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -99,7 +99,8 @@
9999
"@types/react-dom": "^15.5.1",
100100
"classnames": "^2.2.5",
101101
"date-fns": "^1.28.5",
102-
"dx-util": "~0.11.5",
102+
"dx-util": "~0.11.7",
103+
"element-resize-detector": "^1.1.12",
103104
"moment": "^2.15.0",
104105
"prop-types": "^15.5.10",
105106
"react": "^15.6.1",

src/components/ResizeDetector/ResizeDetector.d.ts

Lines changed: 0 additions & 8 deletions
This file was deleted.

src/components/ResizeDetector/ResizeDetector.jsx

Lines changed: 0 additions & 58 deletions
This file was deleted.
Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,11 @@
11
import React from 'react';
22
import {storiesOf, action} from '@kadira/storybook';
3-
import ResizeDetector from './ResizeDetector';
3+
import {ResizeDetector} from './ResizeDetector';
44
import Demo from '../../demo/Demo';
55

66
storiesOf('ResizeDetector', module).add('default', () => (
77
<Demo>
8+
<div>Resize current window to log events in Action Logger below</div>
89
<ResizeDetector onResize={action('resized')}/>
910
</Demo>
1011
));
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
import * as React from 'react';
2+
import { PURE } from 'dx-util/lib/react/pure';
3+
import { themr } from 'react-css-themr';
4+
import { ComponentClass } from 'react';
5+
import { ObjectClean } from 'typelevel-ts/lib';
6+
import { PartialKeys } from 'dx-util/lib/object/object';
7+
8+
import * as detectorFactory from 'element-resize-detector';
9+
import { raf } from 'dx-util/lib/function/raf';
10+
11+
const NativeResizeDetector = detectorFactory({
12+
strategy: 'scroll'
13+
});
14+
15+
export const RESIZE_DETECTOR = Symbol('ResizeDetector');
16+
17+
export type TFullResizeDetectorProps = {
18+
theme: {
19+
container?: string
20+
},
21+
onResize: (element: Element) => any
22+
};
23+
24+
@PURE
25+
class RawResizeDetector extends React.Component<TFullResizeDetectorProps> {
26+
private element: HTMLDivElement | null;
27+
28+
componentDidMount() {
29+
if (this.element) {
30+
NativeResizeDetector.listenTo(this.element, this.onResize);
31+
}
32+
}
33+
34+
componentWillUnmount() {
35+
if (this.element) {
36+
NativeResizeDetector.uninstall(this.element);
37+
}
38+
}
39+
40+
render() {
41+
const { theme } = this.props;
42+
return (
43+
<div className={theme.container} ref={el => this.element = el}></div>
44+
);
45+
}
46+
47+
onResize = raf((element: Element) => {
48+
this.props.onResize(element);
49+
});
50+
}
51+
52+
export type TResizeDetectorProps = ObjectClean<PartialKeys<TFullResizeDetectorProps, 'theme'>>;
53+
export const ResizeDetector: ComponentClass<TResizeDetectorProps> = themr(RESIZE_DETECTOR)(RawResizeDetector);

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';
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+
}

0 commit comments

Comments
 (0)