|
| 1 | +/** |
| 2 | +* Types.ts |
| 3 | +* |
| 4 | +* Copyright (c) Microsoft Corporation. All rights reserved. |
| 5 | +* Licensed under the MIT license. |
| 6 | +* Type definitions for reactxp-naviigation extension. |
| 7 | +*/ |
| 8 | + |
| 9 | +// Use only for type data |
| 10 | +import React = require('react'); |
| 11 | +import RX = require('reactxp'); |
| 12 | + |
| 13 | +export type ReactNode = React.ReactNode; |
| 14 | + |
| 15 | +// |
| 16 | +// Navigator |
| 17 | +// ---------------------------------------------------------------------- |
| 18 | +export enum NavigatorSceneConfigType { |
| 19 | + FloatFromRight, |
| 20 | + FloatFromLeft, |
| 21 | + FloatFromBottom, |
| 22 | + Fade, |
| 23 | + FadeWithSlide |
| 24 | +} |
| 25 | + |
| 26 | +export interface NavigatorRoute { |
| 27 | + routeId: number; |
| 28 | + // Route's animation configuration |
| 29 | + sceneConfigType: NavigatorSceneConfigType; |
| 30 | + |
| 31 | + // NOTE: The following props are for the experimental navigator. |
| 32 | + // They aren't considered when working with the standard navigator. |
| 33 | + // Optional gesture response distance override |
| 34 | + // 0 is equivalent to disabling gestures |
| 35 | + gestureResponseDistance?: number; |
| 36 | + // Optional custom scene config |
| 37 | + customSceneConfig?: CustomNavigatorSceneConfig; |
| 38 | +} |
| 39 | + |
| 40 | +// NOTE: Experimental navigator only |
| 41 | +export type NavigationTransitionSpec = { |
| 42 | + duration?: number; |
| 43 | + |
| 44 | + // NOTE: Elastic and bounce easing will not work as expected due to how the navigator interpolates styles |
| 45 | + easing?: RX.Types.Animated.EasingFunction; |
| 46 | +}; |
| 47 | + |
| 48 | +// NOTE: Experimental navigator only |
| 49 | +export type NavigationTransitionStyleConfig = { |
| 50 | + // By default input range is defined as [index - 1, index, index + 1]; |
| 51 | + // Input and output ranges must contain the same number of elements |
| 52 | + inputRange?: number[]; |
| 53 | + opacityOutput: number | number[]; |
| 54 | + scaleOutput: number | number[]; |
| 55 | + translateXOutput: number | number[]; |
| 56 | + translateYOutput: number | number[]; |
| 57 | +}; |
| 58 | + |
| 59 | +// NOTE: Experimental navigator only |
| 60 | +export type CustomNavigatorSceneConfig = { |
| 61 | + // Optional transition styles |
| 62 | + transitionStyle?: (sceneIndex: number, sceneDimensions: RX.Types.Dimensions) => NavigationTransitionStyleConfig; |
| 63 | + // Optional overrides for duration, easing, and timing |
| 64 | + transitionSpec?: NavigationTransitionSpec; |
| 65 | + // Optional cardStyle override |
| 66 | + cardStyle?: RX.Types.ViewStyleRuleSet; |
| 67 | + // Optionally hide drop shadow |
| 68 | + hideShadow?: boolean; |
| 69 | + // Optionally flip the visual order of the last two scenes |
| 70 | + presentBelowPrevious?: boolean; |
| 71 | +}; |
| 72 | + |
| 73 | +export interface NavigatorProps extends RX.CommonProps { |
| 74 | + renderScene: (route: NavigatorRoute) => JSX.Element; |
| 75 | + navigateBackCompleted?: () => void; |
| 76 | + // NOTE: Arguments are only passed to transitionStarted by the experimental navigator |
| 77 | + transitionStarted?: (progress?: RX.Types.AnimatedValue, |
| 78 | + toRouteId?: string, fromRouteId?: string, |
| 79 | + toIndex?: number, fromIndex?: number) => void; |
| 80 | + transitionCompleted?: () => void; |
| 81 | + cardStyle?: RX.Types.ViewStyleRuleSet; |
| 82 | + children?: ReactNode; |
| 83 | + // Selector of the navigator delegate. Currently make difference only in react-native. |
| 84 | + delegateSelector?: NavigatorDelegateSelector; |
| 85 | +} |
| 86 | + |
| 87 | +export enum CommandType { |
| 88 | + Push, |
| 89 | + Pop, |
| 90 | + Replace |
| 91 | +} |
| 92 | + |
| 93 | +export interface CommandParam { |
| 94 | + route?: NavigatorRoute; |
| 95 | + value?: number; |
| 96 | +} |
| 97 | + |
| 98 | +export interface NavigationCommand { |
| 99 | + type: CommandType; |
| 100 | + param: CommandParam; |
| 101 | +} |
| 102 | + |
| 103 | +// Empty state |
| 104 | +export interface NavigatorState { |
| 105 | +} |
| 106 | + |
| 107 | +export abstract class Navigator<S> extends React.Component<NavigatorProps, S> { |
| 108 | + abstract push(route: NavigatorRoute): void; |
| 109 | + abstract pop(): void; |
| 110 | + abstract replace(route: NavigatorRoute): void; |
| 111 | + abstract replacePrevious(route: NavigatorRoute): void; |
| 112 | + abstract replaceAtIndex(route: NavigatorRoute, index: number): void; |
| 113 | + abstract immediatelyResetRouteStack(nextRouteStack: NavigatorRoute[]): void; |
| 114 | + abstract popToRoute(route: NavigatorRoute): void; |
| 115 | + abstract popToTop(): void; |
| 116 | + abstract getCurrentRoutes(): NavigatorRoute[]; |
| 117 | +} |
| 118 | + |
| 119 | +export interface NavigatorDelegateSelector { |
| 120 | + getNavigatorDelegate(navigator: Navigator<NavigatorState>): NavigatorDelegate; |
| 121 | +} |
| 122 | + |
| 123 | +export abstract class NavigatorDelegate { |
| 124 | + protected _owner: Navigator<NavigatorState>; |
| 125 | + |
| 126 | + constructor(navigator: Navigator<NavigatorState>) { |
| 127 | + this._owner = navigator; |
| 128 | + } |
| 129 | + |
| 130 | + onBackPress = (): boolean => { |
| 131 | + const routes = this.getRoutes(); |
| 132 | + if (routes.length > 1) { |
| 133 | + this.handleBackPress(); |
| 134 | + |
| 135 | + if (this._owner.props.navigateBackCompleted) { |
| 136 | + this._owner.props.navigateBackCompleted(); |
| 137 | + } |
| 138 | + |
| 139 | + // Indicate that we handled the event. |
| 140 | + return true; |
| 141 | + } |
| 142 | + |
| 143 | + return false; |
| 144 | + } |
| 145 | + |
| 146 | + abstract getRoutes(): NavigatorRoute[]; |
| 147 | + abstract immediatelyResetRouteStack(nextRouteStack: NavigatorRoute[]): void; |
| 148 | + abstract render(): JSX.Element; |
| 149 | + abstract processCommand(commandQueue: NavigationCommand[]): void; |
| 150 | + abstract handleBackPress(): void; |
| 151 | +} |
0 commit comments