Skip to content
This repository has been archived by the owner on Feb 8, 2020. It is now read-only.

Commit

Permalink
fix: properly set pointerEvents on the views
Browse files Browse the repository at this point in the history
  • Loading branch information
satya164 committed Aug 18, 2019
1 parent ebc4865 commit 0589275
Show file tree
Hide file tree
Showing 5 changed files with 74 additions and 5 deletions.
2 changes: 1 addition & 1 deletion packages/stack/src/views/Header/HeaderContainer.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ export default function HeaderContainer({
<View
key={scene.route.key}
onLayout={onLayout}
pointerEvents={isFocused ? 'box-none' : 'none'}
pointerEvents="box-none"
accessibilityElementsHidden={!isFocused}
importantForAccessibility={
isFocused ? 'auto' : 'no-hide-descendants'
Expand Down
9 changes: 7 additions & 2 deletions packages/stack/src/views/Stack/Card.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,10 @@ import {
import { TransitionSpec, CardStyleInterpolator, Layout } from '../../types';
import memoize from '../../utils/memoize';
import StackGestureContext from '../../utils/StackGestureContext';
import PointerEventsView from './PointerEventsView';

type Props = ViewProps & {
active: boolean;
closing?: boolean;
transparent?: boolean;
next?: Animated.Node<number>;
Expand Down Expand Up @@ -413,6 +415,7 @@ export default class Card extends React.Component<Props> {

render() {
const {
active,
transparent,
layout,
current,
Expand Down Expand Up @@ -466,14 +469,16 @@ export default class Card extends React.Component<Props> {
pointerEvents="none"
/>
) : null}
<View
<PointerEventsView
active={active}
progress={this.props.current}
style={[
StyleSheet.absoluteFill,
transparent ? styles.transparent : styles.opaque,
]}
>
{children}
</View>
</PointerEventsView>
</Animated.View>
</PanGestureHandler>
</Animated.View>
Expand Down
63 changes: 63 additions & 0 deletions packages/stack/src/views/Stack/PointerEventsView.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
import * as React from 'react';
import { View, ViewProps } from 'react-native';
import Animated from 'react-native-reanimated';

type Binary = 0 | 1;

type Props = ViewProps & {
active: boolean;
progress: Animated.Node<number>;
children: React.ReactNode;
};

const MIN_PROGRESS = 0.99;

const TRUE = 1;
const FALSE = 0;
const NOOP = 0;

const { block, greaterThan, cond, set, call, onChange } = Animated;

/**
* Component that automatically computes the `pointerEvents` property
* whenever position changes.
*/
export default class PointerEventsView extends React.Component<Props> {
private pointerEventsEnabled = new Animated.Value<Binary>(
this.props.active ? TRUE : FALSE
);

private exec = block([
cond(
greaterThan(this.props.progress, MIN_PROGRESS),
cond(
this.pointerEventsEnabled,
NOOP,
set(this.pointerEventsEnabled, TRUE)
),
cond(this.pointerEventsEnabled, set(this.pointerEventsEnabled, FALSE))
),
onChange(
this.pointerEventsEnabled,
call([this.pointerEventsEnabled], ([value]) => {
const pointerEvents = this.props.active && value ? 'box-none' : 'none';

this.root && this.root.setNativeProps({ pointerEvents });
})
),
]);

private root: View | null = null;

render() {
// eslint-disable-next-line @typescript-eslint/no-unused-vars
const { active, progress, ...rest } = this.props;

return (
<React.Fragment>
<Animated.Code exec={this.exec} />
<View ref={c => (this.root = c)} {...rest} />
</React.Fragment>
);
}
}
3 changes: 2 additions & 1 deletion packages/stack/src/views/Stack/Stack.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -192,7 +192,7 @@ export default class Stack extends React.Component<Props, State> {
onLayout={this.handleLayout}
pointerEvents={layout.height && layout.width ? 'box-none' : 'none'}
>
{routes.map((route, index) => {
{routes.map((route, index, self) => {
const focused = focusedRoute.key === route.key;
const current = progress[route.key];
const descriptor = descriptors[route.key];
Expand All @@ -201,6 +201,7 @@ export default class Stack extends React.Component<Props, State> {
return (
<Card
key={route.key}
active={index === self.length - 1}
transparent={transparentCard}
direction={direction}
layout={layout}
Expand Down
2 changes: 1 addition & 1 deletion packages/stack/src/views/Stack/StackView.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ class StackView extends React.Component<Props, State> {
const { navigation } = props;
const { transitions } = navigation.state;

let { routes } = navigation.state;
let routes = navigation.state.routes.slice(0, navigation.state.index + 1);

if (transitions.pushing.length) {
// If there are multiple routes being pushed/popped, we'll encounter glitches
Expand Down

0 comments on commit 0589275

Please sign in to comment.