1- import { JsxProps } from 'dom-renderer' ;
21import { computed , observable } from 'mobx' ;
32import {
3+ AnimateCSS ,
4+ AnimationType ,
45 ClassComponent ,
5- FunctionComponent ,
6+ FC ,
7+ WebCell ,
8+ WebCellProps ,
69 attribute ,
710 component ,
8- observer ,
9- reaction
11+ observer
1012} from 'web-cell' ;
1113
1214import history , { History } from './History' ;
13- import { PageProps , nextTick , watchStop } from './utility' ;
15+ import { IncludeText , PageProps } from './utility' ;
1416
15- export interface CellRouteProps extends JsxProps < HTMLElement > {
17+ export interface CellRouteProps extends WebCellProps {
1618 path : string ;
17- component : FunctionComponent < PageProps > | ClassComponent ;
18- startClass ?: string ;
19- endClass ?: string ;
19+ component : FC < PageProps > | ClassComponent ;
20+ inAnimation ?: IncludeText < AnimationType , 'In' > ;
21+ outAnimation ?: IncludeText < AnimationType , 'Out' > ;
2022}
2123
24+ export interface CellRoute extends WebCell { }
25+
2226@component ( { tagName : 'cell-route' } )
2327@observer
24- export class CellRoute extends HTMLElement {
28+ export class CellRoute extends HTMLElement implements WebCell {
2529 declare props : CellRouteProps ;
2630
2731 @attribute
@@ -32,17 +36,11 @@ export class CellRoute extends HTMLElement {
3236
3337 @attribute
3438 @observable
35- accessor startClass = '' ;
39+ accessor inAnimation : CellRouteProps [ 'inAnimation' ] = 'fadeIn ' ;
3640
3741 @attribute
3842 @observable
39- accessor endClass = '' ;
40-
41- @observable
42- accessor moveClass = '' ;
43-
44- @observable
45- accessor moved = ! this . endClass ;
43+ accessor outAnimation : CellRouteProps [ 'outAnimation' ] = 'fadeOut' ;
4644
4745 @computed
4846 get matched ( ) {
@@ -54,44 +52,45 @@ export class CellRoute extends HTMLElement {
5452 return History . match ( this . path , history . oldPath ) ;
5553 }
5654
57- @reaction ( ( { matched } ) => matched )
58- protected async toggleMotion ( enter ?: any ) {
59- if ( ! this . startClass || ! this . endClass ) return ;
60-
61- this . moved = false ;
62- if ( enter ) {
63- this . moveClass = this . startClass ;
64- await nextTick ( ) ;
65- } else {
66- const end = watchStop ( this , `.${ this . endClass } ` ) ;
67- this . moveClass = this . endClass ;
68- await end ;
69- this . moved = true ;
70- }
71- this . moveClass = undefined ;
55+ connectedCallback ( ) {
56+ if ( getComputedStyle ( this . parentElement ) . position === 'static' )
57+ this . parentElement . style . position = 'relative' ;
7258 }
7359
7460 render ( ) {
75- const { matched, oldMatched, component : Page , moveClass, moved } = this ,
61+ const { inAnimation, outAnimation, matched, oldMatched } = this ,
62+ Tag = this . component ,
7663 { path, oldPath } = history ;
7764
7865 return matched ? (
79- < Page
80- className = { moveClass }
81- { ...matched }
82- { ...History . dataOf ( path ) }
83- { ...{ path, history } }
66+ < AnimateCSS
67+ type = { inAnimation }
68+ component = { props => (
69+ < Tag
70+ { ...props }
71+ style = { { position : 'absolute' , top : '0' , left : '0' } }
72+ { ...matched }
73+ { ...History . dataOf ( path ) }
74+ { ...{ path, history } }
75+ />
76+ ) }
77+ />
78+ ) : oldMatched ? (
79+ < AnimateCSS
80+ type = { outAnimation }
81+ component = { props => (
82+ < Tag
83+ { ...props }
84+ style = { { position : 'absolute' , top : '0' , left : '0' } }
85+ { ...oldMatched }
86+ { ...History . dataOf ( oldPath ) }
87+ path = { oldPath }
88+ history = { history }
89+ />
90+ ) }
8491 />
8592 ) : (
86- oldMatched && ! moved && (
87- < Page
88- className = { moveClass }
89- { ...oldMatched }
90- { ...History . dataOf ( oldPath ) }
91- path = { oldPath }
92- history = { history }
93- />
94- )
93+ < > </ >
9594 ) ;
9695 }
9796}
0 commit comments