(tree = createTree())
| 17 | Partial<Omit<NativeScreenProps, "children" | "active">>; |
| 18 | |
| 19 | export function createStack(tree = createTree()) { |
| 20 | let counter = 0; |
| 21 | const store = createTransitionStore(); |
| 22 | |
| 23 | function push(element: React.ReactElement, options: ScreenOptions = {}) { |
| 24 | const id = options.id ?? `stack-${counter++}`; |
| 25 | let slotId = options.stackId ?? ""; |
| 26 | |
| 27 | if (!slotId) { |
| 28 | const activeNode = tree.getActiveNode(STACK_SLOT); |
| 29 | slotId = activeNode?.extraId; |
| 30 | } |
| 31 | |
| 32 | store.add(id, element, { ...options, id, slotId }); |
| 33 | store.transition(id, EVENTS.OPEN); |
| 34 | } |
| 35 | |
| 36 | function pop(amount = 1, options: BaseOptions = {}) { |
| 37 | let slotId = options.stackId ?? ""; |
| 38 | |
| 39 | if (!slotId) { |
| 40 | const activeNode = tree.getActiveNode(STACK_SLOT); |
| 41 | slotId = activeNode?.extraId; |
| 42 | } |
| 43 | |
| 44 | if (slotId) { |
| 45 | const slotEntries = store |
| 46 | .getEntries(store.getState()) |
| 47 | .filter((e) => e.props.slotId === slotId); |
| 48 | |
| 49 | const max = Math.min(slotEntries.length, amount); |
| 50 | |
| 51 | for (let i = 0; i < max; i++) { |
| 52 | const entry = slotEntries[slotEntries.length - 1 - i]; |
| 53 | store.transition(entry.id, EVENTS.CLOSE); |
| 54 | } |
| 55 | } |
| 56 | } |
| 57 | |
| 58 | const Provider = React.memo(function Provider({ |
| 59 | children, |
| 60 | }: { |
| 61 | children: React.ReactNode; |
| 62 | }) { |
| 63 | return ( |
| 64 | <TreeProvider tree={tree}> |
| 65 | <StackStoreContext.Provider value={store}> |
| 66 | {children} |
| 67 | </StackStoreContext.Provider> |
| 68 | </TreeProvider> |
| 69 | ); |
| 70 | }); |
| 71 | |
| 72 | return { |
| 73 | push, |
| 74 | pop, |
| 75 | Provider, |
| 76 | }; |
no test coverage detected