Skip to content

Commit

Permalink
fix: disable scroll when using adjustToContentHeight
Browse files Browse the repository at this point in the history
  • Loading branch information
jeremybarbet committed Feb 5, 2020
1 parent 9413350 commit 5550f3f
Show file tree
Hide file tree
Showing 6 changed files with 34 additions and 10 deletions.
2 changes: 1 addition & 1 deletion examples/expo/App.js
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ export default () => {
modal[6] = el;
}}
/>
<AlwaysOpen />
{/* <AlwaysOpen /> */}
</Layout>
);
};
2 changes: 1 addition & 1 deletion examples/expo/src/components/modals/FixedContent.js
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ export class FixedContent extends React.PureComponent {

render() {
return (
<Modalize ref={this.modal} scrollViewProps={{ scrollEnabled: false }} adjustToContentHeight>
<Modalize ref={this.modal} adjustToContentHeight>
{this.renderContent()}
</Modalize>
);
Expand Down
7 changes: 1 addition & 6 deletions examples/react-native-navigation/src/screens/FixedContent.js
Original file line number Diff line number Diff line change
Expand Up @@ -48,12 +48,7 @@ export class FixedContent extends React.PureComponent {

render() {
return (
<Modalize
ref={this.modal}
onClosed={this.onClosed}
scrollViewProps={{ scrollEnabled: false }}
adjustToContentHeight
>
<Modalize ref={this.modal} onClosed={this.onClosed} adjustToContentHeight>
{this.renderContent()}
</Modalize>
);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ export class FixedContent extends React.PureComponent {

render() {
return (
<Modalize ref={this.modal} scrollViewProps={{ scrollEnabled: false }} adjustToContentHeight>
<Modalize ref={this.modal} adjustToContentHeight>
{this.renderContent()}
</Modalize>
);
Expand Down
20 changes: 19 additions & 1 deletion src/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ export class Modalize<FlatListItem = any, SectionListItem = any> extends React.C
handlePosition: 'outside',
useNativeDriver: true,
adjustToContentHeight: false,
disableScrollIfPossible: true,
avoidKeyboardLikeIOS: Platform.select({
ios: true,
android: false,
Expand Down Expand Up @@ -136,6 +137,7 @@ export class Modalize<FlatListItem = any, SectionListItem = any> extends React.C
enableBounces: true,
keyboardToggle: false,
keyboardHeight: 0,
disableScroll: undefined,
};

this.beginScrollY.addListener(({ value }) => (this.beginScrollYValue = value));
Expand Down Expand Up @@ -370,6 +372,20 @@ export class Modalize<FlatListItem = any, SectionListItem = any> extends React.C
});
};

private onContentViewLayout = ({ nativeEvent }: LayoutChangeEvent): void => {
const { adjustToContentHeight, disableScrollIfPossible } = this.props;

if (!adjustToContentHeight) {
return;
}

const { height } = nativeEvent.layout;
const shorterHeight = height < this.initialComputedModalHeight;
const disableScroll = shorterHeight && disableScrollIfPossible;

this.setState({ disableScroll });
};

private onHandleComponent = ({ nativeEvent }: PanGestureHandlerStateChangeEvent): void => {
if (nativeEvent.oldState === State.BEGAN) {
this.beginScrollY.setValue(0);
Expand Down Expand Up @@ -565,7 +581,7 @@ export class Modalize<FlatListItem = any, SectionListItem = any> extends React.C

private renderContent = (): React.ReactNode => {
const { children, scrollViewProps, flatListProps, sectionListProps } = this.props;
const { enableBounces } = this.state;
const { enableBounces, disableScroll } = this.state;
const keyboardDismissMode = isIos ? 'interactive' : 'on-drag';

const opts = {
Expand All @@ -576,6 +592,8 @@ export class Modalize<FlatListItem = any, SectionListItem = any> extends React.C
{ useNativeDriver: false },
),
scrollEventThrottle: 16,
onLayout: this.onContentViewLayout,
scrollEnabled: !disableScroll,
keyboardDismissMode,
};

Expand Down
11 changes: 11 additions & 0 deletions src/options.ts
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,12 @@ export interface IProps<FlatListItem = any, SectionListItem = any> {
*/
adjustToContentHeight?: boolean;

/**
* Disable the scroll when the content is shorter than screen's height.
* @default true
*/
disableScrollIfPossible: boolean;

/**
* Define keyboard's Android behavior like iOS's one.
* @default Platform.select({ ios: true, android: false })
Expand Down Expand Up @@ -257,6 +263,11 @@ export interface IState {
*/
enableBounces: boolean;

/**
* Disable scroll if disableScrollIfPossible is true or if we are the initial position of the snapPoint or alwaysOpen modals
*/
disableScroll: boolean | undefined;

/**
* Store if the keyboard is displayed. Used to change the offset on the ContentView when the keyboard is open.
*/
Expand Down

0 comments on commit 5550f3f

Please sign in to comment.