@pinnacle0/react-stack-router
Advanced tools
Comparing version 0.1.3-beta.41 to 0.1.3-beta.42
@@ -5,3 +5,3 @@ import { Route } from "../route"; | ||
import type { History, To } from "history"; | ||
import type { LocationState, PushOption } from "../type"; | ||
import type { LocationState, PushOption, PopOption } from "../type"; | ||
export type Subscriber = (screens: Screen[]) => void; | ||
@@ -30,2 +30,3 @@ export type StackRoutePattern = { | ||
private pushOption; | ||
private popOption; | ||
private resolve; | ||
@@ -38,3 +39,4 @@ constructor(options: StackRouterOptions); | ||
push(to: To, option?: PushOption): Promise<void>; | ||
pop(): Promise<void>; | ||
pop(option?: PopOption): Promise<void>; | ||
popAll(): Promise<void>; | ||
replace(to: To, state?: Record<string, any>): void; | ||
@@ -41,0 +43,0 @@ replaceSearchParams<T extends Record<string, string> = Record<string, string>>(newParam: T | ((current: T) => T)): void; |
@@ -16,2 +16,3 @@ import { Action } from "history"; | ||
pushOption = new Snapshot(); | ||
popOption = new Snapshot(); | ||
resolve = new Snapshot(); | ||
@@ -48,3 +49,3 @@ constructor(options) { | ||
} | ||
return Promise.all(stack.map((to, index) => (index === 0 ? this.replace(to) : this.push(to, { transition: "exiting" })))); | ||
return Promise.all(stack.map((to, index) => (index === 0 ? this.replace(to) : this.push(to, { transition: { type: "exiting" } })))); | ||
} | ||
@@ -66,12 +67,14 @@ updateRoute(route) { | ||
return; | ||
this.pushOption.value = option ?? null; | ||
const wait = new Promise(resolve => (this.resolve.value = resolve)); | ||
this.pushOption.value = option ?? null; | ||
this.stackHistory.push(to, { $key: this.createKey(), ...(option?.state ?? {}) }); | ||
return wait; | ||
} | ||
async pop() { | ||
async pop(option) { | ||
const wait = new Promise(resolve => (this.resolve.value = resolve)); | ||
this.popOption.value = option ?? null; | ||
this.stackHistory.pop(); | ||
return wait; | ||
} | ||
async popAll() { } | ||
replace(to, state) { | ||
@@ -116,3 +119,3 @@ if (!this.matchRoute(to)) | ||
} | ||
createScreen(location, transitionType) { | ||
createScreen(location, transitionOption) { | ||
const matched = this.matchRoute(location.pathname); | ||
@@ -127,4 +130,4 @@ if (!matched) | ||
transition: { | ||
type: transitionType, | ||
duration: this.transitionDuration, | ||
type: transitionOption.type, | ||
duration: transitionOption.duration, | ||
}, | ||
@@ -136,3 +139,3 @@ }); | ||
const resolve = this.resolve.value; | ||
const screen = this.createScreen(location, option?.transition ?? transition); | ||
const screen = this.createScreen(location, { type: option?.transition?.type ?? transition, duration: option?.transition?.duration ?? this.transitionDuration }); | ||
if (!screen) { | ||
@@ -147,7 +150,12 @@ resolve?.(); | ||
popScreen(transition) { | ||
const option = this.popOption.value; | ||
const resolve = this.resolve.value; | ||
const top = this.getTopScreen(); | ||
top?.transition.update(transition); | ||
top ? top.lifecycle.attachOnce("didExit", () => resolve?.()) : resolve?.(); | ||
if (!top) { | ||
resolve?.(); | ||
return; | ||
} | ||
top.transition.update(option?.transition?.type ?? transition); | ||
this.screens.pop(); | ||
top.lifecycle.attachOnce("didExit", () => resolve?.()); | ||
this.notify(); | ||
@@ -159,3 +167,3 @@ } | ||
this.screens.pop(); | ||
const screen = this.createScreen(location, "exiting"); | ||
const screen = this.createScreen(location, { type: "exiting", duration: this.transitionDuration }); | ||
if (!screen) { | ||
@@ -162,0 +170,0 @@ return; |
@@ -12,8 +12,10 @@ import type React from "react"; | ||
export interface TransitionOption { | ||
transition?: TransitionType; | ||
type?: TransitionType; | ||
duration?: number; | ||
} | ||
export interface PushOption extends TransitionOption { | ||
export interface PushOption { | ||
state?: LocationState; | ||
transition?: TransitionOption; | ||
} | ||
export type PopOption = PushOption; | ||
export interface ReplaceOption { | ||
@@ -20,0 +22,0 @@ remove: TransitionOption; |
{ | ||
"name": "@pinnacle0/react-stack-router", | ||
"version": "0.1.3-beta.41", | ||
"version": "0.1.3-beta.42", | ||
"author": "Pinnacle", | ||
@@ -5,0 +5,0 @@ "license": "MIT", |
@@ -9,3 +9,3 @@ import {Action} from "history"; | ||
import type {History, Update, To, Key} from "history"; | ||
import type {LocationState, Location, PushOption} from "../type"; | ||
import type {LocationState, Location, PushOption, TransitionOption, PopOption} from "../type"; | ||
import type {Match} from "../route"; | ||
@@ -47,2 +47,3 @@ import type {StackHistory} from "./stackHistory"; | ||
private pushOption = new Snapshot<PushOption>(); | ||
private popOption = new Snapshot<PopOption>(); | ||
private resolve = new Snapshot<() => void>(); | ||
@@ -85,3 +86,3 @@ | ||
return Promise.all(stack.map((to, index) => (index === 0 ? this.replace(to) : this.push(to, {transition: "exiting"})))); | ||
return Promise.all(stack.map((to, index) => (index === 0 ? this.replace(to) : this.push(to, {transition: {type: "exiting"}})))); | ||
} | ||
@@ -107,4 +108,4 @@ | ||
this.pushOption.value = option ?? null; | ||
const wait = new Promise<void>(resolve => (this.resolve.value = resolve)); | ||
this.pushOption.value = option ?? null; | ||
this.stackHistory.push(to, {$key: this.createKey(), ...(option?.state ?? {})}); | ||
@@ -115,4 +116,5 @@ | ||
async pop(): Promise<void> { | ||
async pop(option?: PopOption): Promise<void> { | ||
const wait = new Promise<void>(resolve => (this.resolve.value = resolve)); | ||
this.popOption.value = option ?? null; | ||
this.stackHistory.pop(); | ||
@@ -122,2 +124,4 @@ return wait; | ||
async popAll(): Promise<void> {} | ||
replace(to: To, state?: Record<string, any>): void { | ||
@@ -166,2 +170,3 @@ if (!this.matchRoute(to)) return; | ||
const matched = this.route.lookup(pathname ?? window.location.pathname); | ||
invariant(matched, `None of the route match current pathname:${pathname}. Please make sure you have defined fallback route using "**"`); | ||
@@ -171,3 +176,3 @@ return matched; | ||
private createScreen(location: Location, transitionType: TransitionType): Screen | null { | ||
private createScreen(location: Location, transitionOption: Required<TransitionOption>): Screen | null { | ||
const matched = this.matchRoute(location.pathname); | ||
@@ -182,4 +187,4 @@ if (!matched) return null; | ||
transition: { | ||
type: transitionType, | ||
duration: this.transitionDuration, | ||
type: transitionOption.type, | ||
duration: transitionOption.duration, | ||
}, | ||
@@ -193,3 +198,3 @@ }); | ||
const screen = this.createScreen(location, option?.transition ?? transition); | ||
const screen = this.createScreen(location, {type: option?.transition?.type ?? transition, duration: option?.transition?.duration ?? this.transitionDuration}); | ||
if (!screen) { | ||
@@ -206,8 +211,14 @@ resolve?.(); | ||
private popScreen(transition: TransitionType) { | ||
const option = this.popOption.value; | ||
const resolve = this.resolve.value; | ||
const top = this.getTopScreen(); | ||
top?.transition.update(transition); | ||
top ? top.lifecycle.attachOnce("didExit", () => resolve?.()) : resolve?.(); | ||
if (!top) { | ||
resolve?.(); | ||
return; | ||
} | ||
top.transition.update(option?.transition?.type ?? transition); | ||
this.screens.pop(); | ||
top.lifecycle.attachOnce("didExit", () => resolve?.()); | ||
this.notify(); | ||
@@ -222,3 +233,3 @@ } | ||
const screen = this.createScreen(location, "exiting"); | ||
const screen = this.createScreen(location, {type: "exiting", duration: this.transitionDuration}); | ||
if (!screen) { | ||
@@ -225,0 +236,0 @@ return; |
@@ -15,10 +15,13 @@ import type React from "react"; | ||
export interface TransitionOption { | ||
transition?: TransitionType; | ||
type?: TransitionType; | ||
duration?: number; | ||
} | ||
export interface PushOption extends TransitionOption { | ||
export interface PushOption { | ||
state?: LocationState; | ||
transition?: TransitionOption; | ||
} | ||
export type PopOption = PushOption; | ||
export interface ReplaceOption { | ||
@@ -25,0 +28,0 @@ remove: TransitionOption; |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
161147
2513