Skip to content

update resize detector #33

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,8 @@
"@types/react-dom": "^15.5.1",
"classnames": "^2.2.5",
"date-fns": "^1.28.5",
"dx-util": "~0.11.5",
"dx-util": "~0.11.7",
"element-resize-detector": "^1.1.12",
"moment": "^2.15.0",
"prop-types": "^15.5.10",
"react": "^15.6.1",
Expand Down
8 changes: 0 additions & 8 deletions src/components/ResizeDetector/ResizeDetector.d.ts

This file was deleted.

40 changes: 0 additions & 40 deletions src/components/ResizeDetector/ResizeDetector.jsx

This file was deleted.

3 changes: 2 additions & 1 deletion src/components/ResizeDetector/ResizeDetector.page.jsx
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
import React from 'react';
import {storiesOf, action} from '@kadira/storybook';
import ResizeDetector from './ResizeDetector';
import {ResizeDetector} from './ResizeDetector';
import Demo from '../../demo/Demo';

storiesOf('ResizeDetector', module).add('default', () => (
<Demo>
<div>Resize current window to log events in Action Logger below</div>
<ResizeDetector onResize={action('resized')}/>
</Demo>
));
53 changes: 53 additions & 0 deletions src/components/ResizeDetector/ResizeDetector.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
import * as React from 'react';
import { PURE } from 'dx-util/lib/react/pure';
import { themr } from 'react-css-themr';
import { ComponentClass } from 'react';
import { ObjectClean } from 'typelevel-ts/lib';
import { PartialKeys } from 'dx-util/lib/object/object';

import * as detectorFactory from 'element-resize-detector';
import { raf } from 'dx-util/lib/function/raf';

const NativeResizeDetector = detectorFactory({
strategy: 'scroll'
});

export const RESIZE_DETECTOR = Symbol('ResizeDetector');

export type TFullResizeDetectorProps = {
theme: {
container?: string
},
onResize: (element: Element) => any
};

@PURE
class RawResizeDetector extends React.Component<TFullResizeDetectorProps> {
private element: HTMLDivElement | null;

componentDidMount() {
if (this.element) {
NativeResizeDetector.listenTo(this.element, this.onResize);
}
}

componentWillUnmount() {
if (this.element) {
NativeResizeDetector.uninstall(this.element);
}
}

render() {
const { theme } = this.props;
return (
<div className={theme.container} ref={el => this.element = el}></div>
);
}

onResize = raf((element: Element) => {
this.props.onResize(element);
});
}

export type TResizeDetectorProps = ObjectClean<PartialKeys<TFullResizeDetectorProps, 'theme'>>;
export const ResizeDetector: ComponentClass<TResizeDetectorProps> = themr(RESIZE_DETECTOR)(RawResizeDetector);
2 changes: 1 addition & 1 deletion src/components/Scrollable/Scrollable.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import {
} from './Scrollable.const';

import {SCROLLBAR_TYPE} from '../Scrollbar/Scrollbar';
import ResizeDetector from '../ResizeDetector/ResizeDetector.jsx';
import {ResizeDetector} from '../ResizeDetector/ResizeDetector';
import HorizontalScrollbar from '../Scrollbar/HorizontalScrollbar.jsx';
import VerticalScrollbar from '../Scrollbar/VerticalScrollbar.jsx';

Expand Down
30 changes: 30 additions & 0 deletions src/typings/element-resize-detector/index.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
declare module 'element-resize-detector' {
type TDetectoryFactoryOptions = {
strategy?: 'scroll'
};
type THandler = (element: Element) => any;
//tslint:disable max-line-length
type TDetector = {
/**
* Listens to the element for resize events and calls the listener function with the element as argument on resize events.
*/
listenTo: (element: Element, handler: THandler) => void,

/**
* Removes the listener from the elemens.
*/
removeListener: (element: Element, handler: THandler) => void,
/**
* 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.
*/
removeAllListeners: (element: Element) => void,
/**
*
*/
uninstall: (element: Element) => void
};
//tslint:enable max-line-length

const detectorFactory: (options: TDetectoryFactoryOptions) => TDetector;
export = detectorFactory;
}