Skip to content

Commit

Permalink
fix: update dimension listeners everywhere
Browse files Browse the repository at this point in the history
  • Loading branch information
doomsower authored and doomsower committed Dec 30, 2022
1 parent 06106fa commit 89fee6a
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 9 deletions.
10 changes: 8 additions & 2 deletions src/Popover.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import {
StyleProp,
I18nManager,
StatusBar,
EmitterSubscription,
} from 'react-native';
import {
computeGeometry,
Expand Down Expand Up @@ -94,6 +95,7 @@ export interface PopoverProps {
useNativeDriver?: boolean;
supportedOrientations?: Orientation[];
calculateStatusBar?: boolean;
children?: React.ReactNode;
}

export interface PopoverState extends Geometry {
Expand Down Expand Up @@ -155,6 +157,7 @@ export class Popover extends React.PureComponent<PopoverProps, PopoverState> {
static displayName = 'Popover';

private defaultDisplayArea!: Rect;
private dimensionsSub?: EmitterSubscription;

constructor(props: PopoverProps) {
super(props);
Expand All @@ -171,11 +174,14 @@ export class Popover extends React.PureComponent<PopoverProps, PopoverState> {
}

componentDidMount() {
Dimensions.addEventListener('change', this.onOrientationChange);
this.dimensionsSub = Dimensions.addEventListener(
'change',
this.onOrientationChange,
);
}

componentWillUnmount() {
Dimensions.removeEventListener('change', this.onOrientationChange);
this.dimensionsSub?.remove();
}

private computeGeometry = (
Expand Down
10 changes: 8 additions & 2 deletions src/PopoverController.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import {
NativeModules,
I18nManager,
StatusBar,
EmitterSubscription,
} from 'react-native';
import { Rect } from './PopoverGeometry';

Expand Down Expand Up @@ -38,12 +39,17 @@ export class PopoverController extends React.PureComponent<Props, State> {
popoverAnchor: { x: 0, y: 0, width: 0, height: 0 },
};

private dimensionsSub?: EmitterSubscription;

componentDidMount() {
Dimensions.addEventListener('change', this.onOrientationChange);
this.dimensionsSub = Dimensions.addEventListener(
'change',
this.onOrientationChange,
);
}

componentWillUnmount() {
Dimensions.removeEventListener('change', this.onOrientationChange);
this.dimensionsSub?.remove();
}

private onOrientationChange = () => {
Expand Down
18 changes: 13 additions & 5 deletions src/PopoverTouchable.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,13 @@ import {
findNodeHandle,
MeasureOnSuccessCallback,
NativeModules,
EmitterSubscription,
} from 'react-native';
import { Rect } from './PopoverGeometry';

export interface Props {
onPopoverDisplayed?: () => any;
onPopoverDisplayed?: () => unknown;
children?: React.ReactNode;
}

export interface State {
Expand All @@ -22,22 +24,28 @@ export class PopoverTouchable extends React.PureComponent<Props, State> {
onPopoverDisplayed: PropTypes.func,
};

private dimensionsSub?: EmitterSubscription;

constructor(props: Props) {
super(props);
this.state = {
showPopover: false,
popoverAnchor: { x: 0, y: 0, width: 0, height: 0 },
};
// Not yet
// console.warn('PopoverTouchable is deprecated, please use PopoverController instead');
console.warn(
'PopoverTouchable is deprecated, please use PopoverController instead',
);
}

componentDidMount() {
Dimensions.addEventListener('change', this.onOrientationChange);
this.dimensionsSub = Dimensions.addEventListener(
'change',
this.onOrientationChange,
);
}

componentWillUnmount() {
Dimensions.removeEventListener('change', this.onOrientationChange);
this.dimensionsSub?.remove();
}

private onOrientationChange = () => {
Expand Down

0 comments on commit 89fee6a

Please sign in to comment.