@tallyui/components 3.13.0 → 3.15.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/index.d.ts +357 -27
- package/dist/index.js +2386 -1370
- package/package.json +9 -9
- package/src/cart/cart-columns.tsx +57 -0
- package/src/cart/cart-foot.tsx +82 -0
- package/src/cart/cart-line.tsx +37 -17
- package/src/cart/index.ts +2 -0
- package/src/checkout/cash-tendered.tsx +5 -7
- package/src/index.ts +26 -3
- package/src/input/index.ts +1 -0
- package/src/input/keypad.tsx +38 -0
- package/src/lib/error-text.ts +5 -0
- package/src/order/refund-form.tsx +2 -1
- package/src/product/category-tile.tsx +43 -0
- package/src/product/in-cart-badge.tsx +26 -0
- package/src/product/index.ts +2 -0
- package/src/register/closure-sheet.tsx +2 -1
- package/src/register/handover-slip.tsx +46 -0
- package/src/register/index.ts +2 -0
- package/src/register/movement-sheet.tsx +2 -1
- package/src/register/open-register-card.tsx +2 -1
- package/src/register/register-bar.tsx +57 -11
- package/src/register/register-column.tsx +2 -1
- package/src/register/register-count.tsx +30 -11
- package/src/register/register-panel.tsx +45 -4
- package/src/register/switch-confirm.tsx +102 -0
- package/src/sale/cart.tsx +63 -31
- package/src/sale/catalogue.tsx +34 -5
- package/src/sale/customer-display-screen.tsx +92 -0
- package/src/sale/index.ts +5 -2
- package/src/sale/line-popover.tsx +145 -0
- package/src/sale/parked-sales.tsx +2 -1
- package/src/sale/price-form.tsx +37 -4
- package/src/sale/product-filter-bar.tsx +58 -0
- package/src/sale/tender-pane.tsx +171 -0
- package/src/sale/tender.tsx +20 -35
- package/src/shell/app-shell.tsx +71 -0
- package/src/shell/index.ts +4 -0
- package/src/shell/nav-drawer.tsx +130 -0
- package/src/shell/nav-item.tsx +70 -0
- package/src/shell/nav-rail.tsx +58 -0
- package/src/shell/shell-context.ts +18 -0
- package/src/ui/icon/menu-icon.tsx +10 -0
- package/src/sale/split-tender.tsx +0 -81
|
@@ -0,0 +1,171 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The tender pane, laid out as WCPOS v2's (monorepo main, packages/core/src/screens/main/pos/checkout/tender/):
|
|
3
|
+
* BalanceHeadline and LedgerLegs from ledger-pane.tsx, then the method tiles, entry, quick-cash chips,
|
|
4
|
+
* keypad and commit from tender-pane.tsx's TenderKeypad. Copy is v2's English catalogue. Every figure
|
|
5
|
+
* comes from pos (useSale's captured-only totals, tenderRounding, quickTenderedAmounts, planLegs).
|
|
6
|
+
*/
|
|
7
|
+
import { useEffect, useState, type JSX } from 'react';
|
|
8
|
+
import { Pressable, Text, TextInput, View } from 'react-native';
|
|
9
|
+
import { defaultPaymentMethods, formatMoney, minorUnitDigits, type PaymentMethodDescriptor } from '@tallyui/core';
|
|
10
|
+
import {
|
|
11
|
+
activePlan, canVoidLeg, changeMinor, countsTowardPaid, isInMotion, legStatus, planLegs, quickTenderedAmounts, tenderRounding,
|
|
12
|
+
type PaymentLegStatus, type TenderAction, type TenderKey, type TenderState, type useSale,
|
|
13
|
+
} from '@tallyui/pos';
|
|
14
|
+
import { cn } from '@tallyui/theme';
|
|
15
|
+
import { QUICK_CASH_STEPS } from '../checkout/cash-tendered';
|
|
16
|
+
import { DIGIT_ROWS, Keypad } from '../input/keypad';
|
|
17
|
+
|
|
18
|
+
type Sale = ReturnType<typeof useSale>;
|
|
19
|
+
type Leg = Sale['order']['payments'][number];
|
|
20
|
+
|
|
21
|
+
export interface TenderPaneProps {
|
|
22
|
+
sale: Sale;
|
|
23
|
+
/** The tender reducer's state (`tenderReducer` in @tallyui/pos); the host owns the reducer. */
|
|
24
|
+
state: TenderState;
|
|
25
|
+
dispatch: (action: TenderAction) => void;
|
|
26
|
+
/** Defaults to `defaultPaymentMethods()`; only `cash` and `external`, the order.create methods, are offered. */
|
|
27
|
+
methods?: PaymentMethodDescriptor[];
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
/** order.create's bound on `payments[].reference`, in UTF-16 units. */
|
|
31
|
+
const REFERENCE_MAX = 255;
|
|
32
|
+
/** v2 labels.ts statusLabel. */
|
|
33
|
+
const STATUS_LABEL: Record<PaymentLegStatus, string> = {
|
|
34
|
+
pending: 'Pending', authorized: 'Authorized', captured: 'Captured', failed: 'Failed', voided: 'Voided',
|
|
35
|
+
};
|
|
36
|
+
/** v2 labels.ts statusVariant: coloured by what the cashier has to do about the leg. */
|
|
37
|
+
const STATUS_TONE: Record<PaymentLegStatus, string> = {
|
|
38
|
+
pending: 'text-muted-foreground', authorized: 'text-success', captured: 'text-success', failed: 'text-destructive', voided: 'text-muted-foreground',
|
|
39
|
+
};
|
|
40
|
+
/** v2 tender-pane.tsx keypadRows: 123 / 456 / 789, then C, 0, backspace. No decimal key: digits shift in from the right. */
|
|
41
|
+
const KEYPAD_ROWS = [...DIGIT_ROWS,
|
|
42
|
+
[{ name: 'clear', label: 'Clear', text: 'C' }, { name: '0', label: '0' }, { name: 'backspace', label: 'Delete last digit', text: '⌫' }]];
|
|
43
|
+
const button = 'min-h-11 justify-center rounded-md border border-border bg-card px-4 py-2';
|
|
44
|
+
|
|
45
|
+
/** The captured legs since the plan began, as the reducer's `tender-recorded` and `planLegs` read them. */
|
|
46
|
+
const rowsSince = (payments: readonly Leg[], from: number) =>
|
|
47
|
+
payments.filter(countsTowardPaid).slice(from).map((leg) => ({ title: leg.method, amountMinor: leg.amountMinor }));
|
|
48
|
+
|
|
49
|
+
export function TenderPane({ sale, state, dispatch, methods }: TenderPaneProps): JSX.Element | null {
|
|
50
|
+
const { order, stage } = sale;
|
|
51
|
+
const [reference, setReference] = useState('');
|
|
52
|
+
const [refusal, setRefusal] = useState<string | null>(null);
|
|
53
|
+
const balance = order.balanceDueMinor;
|
|
54
|
+
// The ledger changed (a leg taken or voided) and the entry is untouched: pre-type the next leg or plan share.
|
|
55
|
+
useEffect(() => {
|
|
56
|
+
if (!state.entryDirty) dispatch({ type: 'tender-recorded', rowsSinceFrom: rowsSince(order.payments, state.plan?.from ?? 0), balanceMinor: balance });
|
|
57
|
+
}, [order.payments, balance, state.entryDirty, state.plan, dispatch]);
|
|
58
|
+
if (stage.kind !== 'tender') return null;
|
|
59
|
+
|
|
60
|
+
const offered = (methods ?? defaultPaymentMethods()).filter(({ id }) => id === 'cash' || id === 'external');
|
|
61
|
+
const descriptorOf = (id: string) => offered.find((method) => method.id === id);
|
|
62
|
+
const money = (minor: number) => formatMoney({ amount: minor, currency: order.currency }) ?? '';
|
|
63
|
+
const method = state.methodId ? descriptorOf(state.methodId) ?? null : null;
|
|
64
|
+
const givesChange = method?.capabilities.change === true;
|
|
65
|
+
const figures = tenderRounding(state, balance, method);
|
|
66
|
+
const entryChange = changeMinor(figures.entryMinor, figures.appliedMinor, givesChange);
|
|
67
|
+
const left = balance + figures.roundingMinor - figures.appliedMinor;
|
|
68
|
+
const noChange = method !== null && !givesChange && figures.entryMinor > balance;
|
|
69
|
+
const rows = rowsSince(order.payments, state.plan?.from ?? 0).map(({ title, amountMinor }) => ({ title, minor: amountMinor }));
|
|
70
|
+
const plan = activePlan(state.plan, rows.length, balance);
|
|
71
|
+
const planned = plan ? planLegs(plan, rows, balance) : null;
|
|
72
|
+
const dueMinor = planned && planned.thisPaymentMinor < balance ? planned.thisPaymentMinor : figures.roundedDueMinor;
|
|
73
|
+
const chips = givesChange ? quickTenderedAmounts(dueMinor, QUICK_CASH_STEPS.map((step) => step * 10 ** minorUnitDigits(order.currency))) : [];
|
|
74
|
+
const settled = order.payments.some(countsTowardPaid);
|
|
75
|
+
const firstPending = order.payments.find((leg) => legStatus(leg) === 'pending');
|
|
76
|
+
const [onlyLeg] = order.payments;
|
|
77
|
+
// startTender('external') records the card leg for the total: its reference is edited in place, as before.
|
|
78
|
+
const cardReference = order.payments.length === 1 && onlyLeg.method === 'external' && legStatus(onlyLeg) === 'captured' && balance === 0;
|
|
79
|
+
const hint = noChange ? `Only ${money(balance)} is due — ${method?.title} can't give change`
|
|
80
|
+
: entryChange > 0 ? `Change ${money(entryChange)}`
|
|
81
|
+
: figures.appliedMinor < balance && figures.entryMinor > 0 ? `Part payment · ${money(left)} left after this` : '';
|
|
82
|
+
const canComplete = balance === 0 && !order.payments.some(isInMotion);
|
|
83
|
+
const cannotTake = figures.appliedMinor <= 0 || noChange;
|
|
84
|
+
|
|
85
|
+
function take() {
|
|
86
|
+
if (!method) return;
|
|
87
|
+
const id = method.id === 'cash'
|
|
88
|
+
? sale.addTender({ method: 'cash', amountMinor: figures.entryMinor })
|
|
89
|
+
: sale.addTender({ method: 'external', amountMinor: figures.appliedMinor, ...(reference ? { reference } : {}) });
|
|
90
|
+
if (id === null) { setRefusal("That payment wasn't taken: nothing is due, or the amount isn't valid."); return; }
|
|
91
|
+
setRefusal(null);
|
|
92
|
+
setReference('');
|
|
93
|
+
dispatch({ type: 'tender-started' });
|
|
94
|
+
}
|
|
95
|
+
|
|
96
|
+
return <View dataSet={{ print: 'hide' }} testID="tender-pane" className="gap-4 p-4">
|
|
97
|
+
<View testID="tender-headline" className="gap-1">
|
|
98
|
+
<Text className="text-sm text-muted-foreground">{order.paidMinor > 0 ? 'Remaining' : 'Total'}</Text>
|
|
99
|
+
<Text testID="tender-balance" className="text-4xl font-bold text-foreground">{money(balance)}</Text>
|
|
100
|
+
{order.paidMinor > 0 ? <Text testID="tender-paid" className="text-sm text-muted-foreground">{`${money(order.paidMinor)} of ${money(order.totalMinor)} paid`}</Text> : null}
|
|
101
|
+
{order.changeDueMinor > 0 ? <Text testID="tender-change" className="text-lg font-semibold text-success">{`Change ${money(order.changeDueMinor)}`}</Text> : null}
|
|
102
|
+
</View>
|
|
103
|
+
|
|
104
|
+
<View testID="tender-ledger" className="gap-1">
|
|
105
|
+
{order.payments.length === 0 ? <Text className="text-muted-foreground">No payments yet</Text> : order.payments.map((leg) => {
|
|
106
|
+
const status = legStatus(leg);
|
|
107
|
+
const voided = status === 'voided';
|
|
108
|
+
const descriptor = descriptorOf(leg.method);
|
|
109
|
+
return <View key={leg.id} testID={`tender-leg-${leg.id}`} className="gap-1 border-b border-border py-2">
|
|
110
|
+
<View className="flex-row items-center gap-2">
|
|
111
|
+
<Text className={cn('flex-1 font-semibold text-foreground', voided && 'line-through')}>{descriptor?.title ?? leg.method}</Text>
|
|
112
|
+
<Text className={cn('text-foreground', voided && 'line-through')}>{money(leg.amountMinor)}</Text>
|
|
113
|
+
<Text testID={`tender-leg-${leg.id}-status`} className={cn('text-sm font-semibold', STATUS_TONE[status])}>{voided ? 'Cancelled' : STATUS_LABEL[status]}</Text>
|
|
114
|
+
</View>
|
|
115
|
+
{leg.reference ? <Text className="text-sm text-muted-foreground">{leg.reference}</Text> : null}
|
|
116
|
+
{leg === firstPending ? <Text className="text-sm text-muted-foreground">Next</Text> : null}
|
|
117
|
+
{descriptor && canVoidLeg(leg, descriptor) ? <Pressable testID={`tender-leg-${leg.id}-void`} accessibilityRole="button" accessibilityLabel="Cancel payment"
|
|
118
|
+
onPress={() => sale.voidTender(leg.id)} className="self-start min-h-11 justify-center px-2">
|
|
119
|
+
<Text className="text-destructive">Cancel payment</Text>
|
|
120
|
+
</Pressable> : null}
|
|
121
|
+
</View>;
|
|
122
|
+
})}
|
|
123
|
+
{balance === 0 && settled ? <Text className="text-sm text-success">Paid in full</Text> : null}
|
|
124
|
+
</View>
|
|
125
|
+
|
|
126
|
+
{cardReference ? <TextInput accessibilityLabel="Terminal reference" placeholder="Terminal reference" maxLength={REFERENCE_MAX}
|
|
127
|
+
value={onlyLeg.reference ?? ''} onChangeText={(text) => sale.setTender({ method: 'external', amountMinor: onlyLeg.amountMinor, reference: text })}
|
|
128
|
+
className="rounded-md border border-border px-3 py-2 text-foreground" /> : null}
|
|
129
|
+
|
|
130
|
+
{balance > 0 ? <View testID="tender-entry" className="gap-3">
|
|
131
|
+
<View className="flex-row flex-wrap gap-2">
|
|
132
|
+
{offered.map((tile) => <Pressable key={tile.id} testID={`tender-method-${tile.id}`} accessibilityRole="button" accessibilityState={{ selected: tile.id === state.methodId }}
|
|
133
|
+
onPress={() => dispatch({ type: 'pick-method', methodId: tile.id, prefillMinor: balance, readerId: null })}
|
|
134
|
+
className={cn(button, tile.id === state.methodId && 'border-primary bg-primary')}>
|
|
135
|
+
<Text className={cn('text-center font-semibold', tile.id === state.methodId ? 'text-primary-foreground' : 'text-foreground')}>{tile.title}</Text>
|
|
136
|
+
</Pressable>)}
|
|
137
|
+
</View>
|
|
138
|
+
{planned ? <Text testID="tender-plan" className="text-sm text-muted-foreground">{`${planned.label.n} of ${planned.label.ways}`}</Text> : null}
|
|
139
|
+
<Text testID="tender-entry-amount" className={cn('text-3xl font-bold text-foreground', !state.entryDirty && 'opacity-70')}>{money(figures.entryMinor)}</Text>
|
|
140
|
+
{figures.roundingMinor !== 0 ? <Text className="text-sm text-muted-foreground">{`Rounded from ${money(balance)}`}</Text> : null}
|
|
141
|
+
<Text testID="tender-entry-hint" className={cn('min-h-6 text-sm', noChange ? 'text-warning' : entryChange > 0 ? 'text-success' : 'text-muted-foreground')}>{hint}</Text>
|
|
142
|
+
{method?.id === 'external' ? <TextInput accessibilityLabel="Terminal reference" placeholder="Terminal reference" maxLength={REFERENCE_MAX}
|
|
143
|
+
value={reference} onChangeText={setReference} className="rounded-md border border-border px-3 py-2 text-foreground" /> : null}
|
|
144
|
+
{chips.length ? <View testID="tender-quick" className="flex-row flex-wrap gap-2">
|
|
145
|
+
{chips.map((minor, index) => <Pressable key={minor} testID={index === 0 ? 'tender-quick-exact' : `tender-quick-${minor}`} accessibilityRole="button"
|
|
146
|
+
onPress={() => dispatch({ type: 'set-entry', minor })} className={cn(button, state.entryDirty && figures.entryMinor === minor && 'border-primary')}>
|
|
147
|
+
<Text className="font-semibold text-foreground">{index === 0 ? `Exact ${money(minor)}` : money(minor)}</Text>
|
|
148
|
+
</Pressable>)}
|
|
149
|
+
</View> : null}
|
|
150
|
+
<Keypad testID="tender-keypad" rows={KEYPAD_ROWS} onPress={(name) => dispatch({ type: 'key', key: name as TenderKey })} />
|
|
151
|
+
{refusal ? <Text accessibilityRole="alert" className="text-destructive">{refusal}</Text> : null}
|
|
152
|
+
{method ? <Pressable testID="tender-take" accessibilityRole="button" disabled={cannotTake}
|
|
153
|
+
onPress={take} className={cn('rounded-md bg-primary px-4 py-3', cannotTake && 'opacity-50')}>
|
|
154
|
+
<Text className="text-center font-semibold text-primary-foreground">{`Take ${money(figures.appliedMinor)} in ${method.title}`}</Text>
|
|
155
|
+
</Pressable> : null}
|
|
156
|
+
</View> : null}
|
|
157
|
+
|
|
158
|
+
<Pressable testID="tender-complete" accessibilityRole="button" disabled={!canComplete} onPress={sale.complete}
|
|
159
|
+
className={cn('rounded-md bg-primary px-4 py-3', !canComplete && 'opacity-50')}>
|
|
160
|
+
<Text className="text-center font-semibold text-primary-foreground">Complete sale</Text>
|
|
161
|
+
</Pressable>
|
|
162
|
+
{sale.error ? <Text accessibilityRole="alert" className="text-destructive">{sale.error}</Text> : null}
|
|
163
|
+
{sale.canContinue ? <>
|
|
164
|
+
<Text className="text-foreground">This sale is stored and will be sent. Continue to the next sale.</Text>
|
|
165
|
+
<Pressable accessibilityRole="button" onPress={() => sale.continueSale()} className="rounded-md bg-primary px-4 py-3">
|
|
166
|
+
<Text className="text-center font-semibold text-primary-foreground">Continue</Text>
|
|
167
|
+
</Pressable>
|
|
168
|
+
</> : null}
|
|
169
|
+
<Pressable accessibilityRole="button" onPress={sale.cancelTender} className={button}><Text className="text-center text-foreground">Back</Text></Pressable>
|
|
170
|
+
</View>;
|
|
171
|
+
}
|
package/src/sale/tender.tsx
CHANGED
|
@@ -1,38 +1,23 @@
|
|
|
1
|
-
import {
|
|
2
|
-
import {
|
|
3
|
-
import type
|
|
4
|
-
import {
|
|
1
|
+
import { useReducer, type JSX } from 'react';
|
|
2
|
+
import type { PaymentMethodDescriptor } from '@tallyui/core';
|
|
3
|
+
import { initTenderState, tenderReducer, type useSale } from '@tallyui/pos';
|
|
4
|
+
import { TenderPane } from './tender-pane';
|
|
5
5
|
|
|
6
|
-
|
|
7
|
-
const REFERENCE_MAX = 255;
|
|
6
|
+
type Sale = ReturnType<typeof useSale>;
|
|
8
7
|
|
|
9
|
-
|
|
10
|
-
const {
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
<TextInput accessibilityLabel="Terminal reference" placeholder="Terminal reference" maxLength={REFERENCE_MAX}
|
|
25
|
-
value={order.payments[0]?.reference ?? ''}
|
|
26
|
-
onChangeText={(reference) => sale.setTender({ method: 'external', amountMinor: order.totalMinor, reference })} className="rounded-md border border-border px-3 py-2 text-foreground" />
|
|
27
|
-
<Pressable accessibilityRole="button" onPress={sale.complete} className="rounded-md bg-primary px-4 py-3"><Text className="text-center font-semibold text-primary-foreground">Payment approved on terminal</Text></Pressable>
|
|
28
|
-
</>}
|
|
29
|
-
{sale.error ? <Text accessibilityRole="alert" className="text-destructive">{sale.error}</Text> : null}
|
|
30
|
-
{sale.canContinue ? <>
|
|
31
|
-
<Text className="text-foreground">This sale is stored and will be sent. Continue to the next sale.</Text>
|
|
32
|
-
<Pressable accessibilityRole="button" onPress={() => sale.continueSale()} className="rounded-md bg-primary px-4 py-3">
|
|
33
|
-
<Text className="text-center font-semibold text-primary-foreground">Continue</Text>
|
|
34
|
-
</Pressable>
|
|
35
|
-
</> : null}
|
|
36
|
-
<Pressable accessibilityRole="button" onPress={sale.cancelTender} className="rounded-md border border-border bg-card px-4 py-3"><Text className="text-center text-foreground">Back</Text></Pressable>
|
|
37
|
-
</View>;
|
|
8
|
+
function HostedPane({ sale, method, methods }: { sale: Sale; method: string; methods?: PaymentMethodDescriptor[] }) {
|
|
9
|
+
const [state, dispatch] = useReducer(tenderReducer, { methodId: method, balanceMinor: sale.order.balanceDueMinor }, initTenderState);
|
|
10
|
+
return <TenderPane sale={sale} state={state} dispatch={dispatch} methods={methods} />;
|
|
11
|
+
}
|
|
12
|
+
|
|
13
|
+
type TenderProps = { sale: Sale; methods?: PaymentMethodDescriptor[] };
|
|
14
|
+
|
|
15
|
+
/** The tender pane with its own reducer, opened on the method `startTender` chose. Renders nothing outside the tender stage. */
|
|
16
|
+
export function Tender({ sale, methods }: TenderProps): JSX.Element | null {
|
|
17
|
+
return sale.stage.kind === 'tender' ? <HostedPane sale={sale} method={sale.stage.method} methods={methods} /> : null;
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
/** @deprecated Use Tender: the pane now handles split payments. */
|
|
21
|
+
export function SplitTender(props: TenderProps): JSX.Element {
|
|
22
|
+
return <Tender {...props} />;
|
|
38
23
|
}
|
|
@@ -0,0 +1,71 @@
|
|
|
1
|
+
// Port of WCPOS v2 `apps/main/app/(app)/(drawer)/_layout.tsx` (the permanent rail on wide, the front
|
|
2
|
+
// drawer on phones) and the layout boundary of `packages/components/src/lib/device.ts`, MIT, WCPOS.
|
|
3
|
+
import * as React from 'react';
|
|
4
|
+
import { View, useWindowDimensions } from 'react-native';
|
|
5
|
+
import { cn } from '@tallyui/theme';
|
|
6
|
+
import type { ShellNavItem } from './nav-item';
|
|
7
|
+
import { NavDrawer } from './nav-drawer';
|
|
8
|
+
import { NavRail } from './nav-rail';
|
|
9
|
+
import { ShellContext, type ShellState } from './shell-context';
|
|
10
|
+
|
|
11
|
+
/**
|
|
12
|
+
* v2's one layout boundary (owner, 2026-10-02): a window at least 768 wide AND 480 tall gets the
|
|
13
|
+
* rail; anything smaller, a phone on its side included (844 x 390), gets the drawer. There is no
|
|
14
|
+
* layout in between.
|
|
15
|
+
*/
|
|
16
|
+
const WIDE_MIN_WIDTH = 768;
|
|
17
|
+
const WIDE_MIN_HEIGHT = 480;
|
|
18
|
+
|
|
19
|
+
export interface AppShellProps {
|
|
20
|
+
navItems: ShellNavItem[];
|
|
21
|
+
activeId?: string;
|
|
22
|
+
/** Routing is the app's: it maps the pressed item's id to its own route. */
|
|
23
|
+
onNavigate: (id: string) => void;
|
|
24
|
+
/** Drawn above the page (a `RegisterBar`); its menu button opens the drawer on phones. */
|
|
25
|
+
registerBar?: React.ReactNode;
|
|
26
|
+
children?: React.ReactNode;
|
|
27
|
+
className?: string;
|
|
28
|
+
testID?: string;
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
/** The POS frame: the nav rail beside the page on wide windows, a slide-over drawer on phones. */
|
|
32
|
+
export function AppShell({
|
|
33
|
+
navItems,
|
|
34
|
+
activeId,
|
|
35
|
+
onNavigate,
|
|
36
|
+
registerBar,
|
|
37
|
+
children,
|
|
38
|
+
className,
|
|
39
|
+
testID = 'app-shell',
|
|
40
|
+
}: AppShellProps) {
|
|
41
|
+
const { width, height } = useWindowDimensions();
|
|
42
|
+
const phone = width < WIDE_MIN_WIDTH || height < WIDE_MIN_HEIGHT;
|
|
43
|
+
const [menuOpen, setMenuOpen] = React.useState(false);
|
|
44
|
+
// Widening past the boundary swaps the drawer for the rail; it must not reopen on the next shrink.
|
|
45
|
+
if (!phone && menuOpen) setMenuOpen(false);
|
|
46
|
+
const openMenu = React.useCallback(() => setMenuOpen(true), []);
|
|
47
|
+
const shell = React.useMemo<ShellState>(() => ({ phone, openMenu: phone ? openMenu : undefined }), [phone, openMenu]);
|
|
48
|
+
const page = (
|
|
49
|
+
<View className="min-w-0 flex-1">
|
|
50
|
+
{registerBar}
|
|
51
|
+
<View className="flex-1">{children}</View>
|
|
52
|
+
</View>
|
|
53
|
+
);
|
|
54
|
+
return (
|
|
55
|
+
<ShellContext.Provider value={shell}>
|
|
56
|
+
<View testID={testID} className={cn('flex-1 bg-background', !phone && 'flex-row', className)}>
|
|
57
|
+
{!phone && <NavRail items={navItems} activeId={activeId} onNavigate={onNavigate} />}
|
|
58
|
+
{page}
|
|
59
|
+
{phone && (
|
|
60
|
+
<NavDrawer
|
|
61
|
+
open={menuOpen}
|
|
62
|
+
onOpenChange={setMenuOpen}
|
|
63
|
+
items={navItems}
|
|
64
|
+
activeId={activeId}
|
|
65
|
+
onNavigate={onNavigate}
|
|
66
|
+
/>
|
|
67
|
+
)}
|
|
68
|
+
</View>
|
|
69
|
+
</ShellContext.Provider>
|
|
70
|
+
);
|
|
71
|
+
}
|
|
@@ -0,0 +1,130 @@
|
|
|
1
|
+
// Port of WCPOS v2 `packages/core/src/screens/main/components/drawer-content/index.tsx` (the `front`
|
|
2
|
+
// presentation) and the drawer options of `apps/main/app/(app)/(drawer)/_layout.tsx`, MIT, WCPOS.
|
|
3
|
+
import * as React from 'react';
|
|
4
|
+
import { Animated, Platform, Pressable, View } from 'react-native';
|
|
5
|
+
import { NavButton, splitGroups, type ShellNavItem } from './nav-item';
|
|
6
|
+
|
|
7
|
+
/** v2's phone drawer width, a literal there too (`drawerStyle.width: phone ? 200 : railWidth`). */
|
|
8
|
+
const DRAWER_WIDTH = 200;
|
|
9
|
+
const SLIDE_MS = 200;
|
|
10
|
+
const FOCUSABLE = 'button, [href], input, [tabindex]:not([tabindex="-1"])';
|
|
11
|
+
|
|
12
|
+
export interface NavDrawerProps {
|
|
13
|
+
open: boolean;
|
|
14
|
+
onOpenChange: (open: boolean) => void;
|
|
15
|
+
items: ShellNavItem[];
|
|
16
|
+
activeId?: string;
|
|
17
|
+
onNavigate: (id: string) => void;
|
|
18
|
+
/** Drawn above the items (a store name or logo). */
|
|
19
|
+
header?: React.ReactNode;
|
|
20
|
+
testID?: string;
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
/** Keeps Tab inside `panel`, moves focus in on open and hands it back on close (web only). */
|
|
24
|
+
function useFocusTrap(panel: React.RefObject<View | null>, active: boolean, onEscape: () => void) {
|
|
25
|
+
const escape = React.useRef(onEscape);
|
|
26
|
+
escape.current = onEscape;
|
|
27
|
+
React.useEffect(() => {
|
|
28
|
+
if (!active || Platform.OS !== 'web' || typeof document === 'undefined') return;
|
|
29
|
+
const root = panel.current as unknown as HTMLElement | null;
|
|
30
|
+
const previous = document.activeElement as HTMLElement | null;
|
|
31
|
+
const focusables = () => Array.from(root?.querySelectorAll<HTMLElement>(FOCUSABLE) ?? []);
|
|
32
|
+
focusables()[0]?.focus();
|
|
33
|
+
const onKeyDown = (event: KeyboardEvent) => {
|
|
34
|
+
if (event.key === 'Escape') {
|
|
35
|
+
escape.current();
|
|
36
|
+
return;
|
|
37
|
+
}
|
|
38
|
+
if (event.key !== 'Tab') return;
|
|
39
|
+
const list = focusables();
|
|
40
|
+
if (list.length === 0) return;
|
|
41
|
+
const first = list[0];
|
|
42
|
+
const last = list[list.length - 1];
|
|
43
|
+
const inside = root?.contains(document.activeElement) ?? false;
|
|
44
|
+
if (event.shiftKey && (document.activeElement === first || !inside)) {
|
|
45
|
+
event.preventDefault();
|
|
46
|
+
last.focus();
|
|
47
|
+
} else if (!event.shiftKey && (document.activeElement === last || !inside)) {
|
|
48
|
+
event.preventDefault();
|
|
49
|
+
first.focus();
|
|
50
|
+
}
|
|
51
|
+
};
|
|
52
|
+
document.addEventListener('keydown', onKeyDown);
|
|
53
|
+
return () => {
|
|
54
|
+
document.removeEventListener('keydown', onKeyDown);
|
|
55
|
+
previous?.focus?.();
|
|
56
|
+
};
|
|
57
|
+
}, [active, panel]);
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
/**
|
|
61
|
+
* The phone's navigation: a 200-wide panel that slides over the screen from the left, v2's
|
|
62
|
+
* `drawerType: 'front'`, with a backdrop that closes it. Pressing the current page only closes it.
|
|
63
|
+
*/
|
|
64
|
+
export function NavDrawer({ open, onOpenChange, items, activeId, onNavigate, header, testID = 'nav-drawer' }: NavDrawerProps) {
|
|
65
|
+
const [mounted, setMounted] = React.useState(open);
|
|
66
|
+
const offset = React.useRef(new Animated.Value(open ? 0 : -DRAWER_WIDTH)).current;
|
|
67
|
+
const panel = React.useRef<View>(null);
|
|
68
|
+
if (open && !mounted) setMounted(true);
|
|
69
|
+
|
|
70
|
+
React.useEffect(() => {
|
|
71
|
+
if (!mounted) return;
|
|
72
|
+
const animation = Animated.timing(offset, {
|
|
73
|
+
toValue: open ? 0 : -DRAWER_WIDTH,
|
|
74
|
+
duration: SLIDE_MS,
|
|
75
|
+
useNativeDriver: Platform.OS !== 'web',
|
|
76
|
+
});
|
|
77
|
+
animation.start(({ finished }) => {
|
|
78
|
+
if (finished && !open) setMounted(false);
|
|
79
|
+
});
|
|
80
|
+
return () => animation.stop();
|
|
81
|
+
}, [open, mounted, offset]);
|
|
82
|
+
|
|
83
|
+
const close = React.useCallback(() => onOpenChange(false), [onOpenChange]);
|
|
84
|
+
useFocusTrap(panel, open && mounted, close);
|
|
85
|
+
|
|
86
|
+
if (!mounted) return null;
|
|
87
|
+
const [top, bottom] = splitGroups(items);
|
|
88
|
+
const renderItem = (item: ShellNavItem) => (
|
|
89
|
+
<NavButton
|
|
90
|
+
key={item.id}
|
|
91
|
+
item={item}
|
|
92
|
+
labelled
|
|
93
|
+
active={item.id === activeId}
|
|
94
|
+
onPress={() => {
|
|
95
|
+
if (item.id !== activeId) onNavigate(item.id);
|
|
96
|
+
close();
|
|
97
|
+
}}
|
|
98
|
+
/>
|
|
99
|
+
);
|
|
100
|
+
return (
|
|
101
|
+
<View testID={testID} className="absolute inset-0 z-50" pointerEvents={open ? 'auto' : 'none'}>
|
|
102
|
+
<Animated.View
|
|
103
|
+
className="absolute inset-0"
|
|
104
|
+
style={{ opacity: offset.interpolate({ inputRange: [-DRAWER_WIDTH, 0], outputRange: [0, 1] }) }}
|
|
105
|
+
>
|
|
106
|
+
<Pressable
|
|
107
|
+
testID={`${testID}-backdrop`}
|
|
108
|
+
accessibilityLabel="Close menu"
|
|
109
|
+
className="flex-1 bg-black/50"
|
|
110
|
+
onPress={close}
|
|
111
|
+
/>
|
|
112
|
+
</Animated.View>
|
|
113
|
+
<Animated.View
|
|
114
|
+
ref={panel}
|
|
115
|
+
role="dialog"
|
|
116
|
+
aria-modal
|
|
117
|
+
aria-label="Menu"
|
|
118
|
+
testID={`${testID}-panel`}
|
|
119
|
+
className="absolute bottom-0 left-0 top-0 border-r border-border bg-card shadow-lg"
|
|
120
|
+
style={{ width: DRAWER_WIDTH, transform: [{ translateX: offset }] }}
|
|
121
|
+
>
|
|
122
|
+
{header}
|
|
123
|
+
<View role="navigation" aria-label="Main" className="flex-1 p-2">
|
|
124
|
+
<View>{top.map(renderItem)}</View>
|
|
125
|
+
{bottom.length > 0 && <View className="mt-auto">{bottom.map(renderItem)}</View>}
|
|
126
|
+
</View>
|
|
127
|
+
</Animated.View>
|
|
128
|
+
</View>
|
|
129
|
+
);
|
|
130
|
+
}
|
|
@@ -0,0 +1,70 @@
|
|
|
1
|
+
// Port of WCPOS v2 `packages/components/src/nav-item/index.tsx` (the `drawer` size), MIT, WCPOS.
|
|
2
|
+
import * as React from 'react';
|
|
3
|
+
import { Pressable, View, type PressableProps } from 'react-native';
|
|
4
|
+
import { cn } from '@tallyui/theme';
|
|
5
|
+
import { Text } from '../ui/text';
|
|
6
|
+
|
|
7
|
+
export interface ShellNavItem {
|
|
8
|
+
id: string;
|
|
9
|
+
label: string;
|
|
10
|
+
/** An icon element drawn in currentColor (`createSvgIcon`), 24 px. */
|
|
11
|
+
icon: React.ReactNode;
|
|
12
|
+
/** A count or short text on the icon's shoulder; nothing when undefined, 0 or ''. */
|
|
13
|
+
badge?: number | string;
|
|
14
|
+
/** `bottom` items sit in the group pinned to the end of the rail or drawer (v2's `marginTop: 'auto'`). */
|
|
15
|
+
group?: 'top' | 'bottom';
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
/** The items in their two groups, in the order given: v2's top group and its bottom group. */
|
|
19
|
+
export function splitGroups(items: ShellNavItem[]): [ShellNavItem[], ShellNavItem[]] {
|
|
20
|
+
return [items.filter((item) => item.group !== 'bottom'), items.filter((item) => item.group === 'bottom')];
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
type NavButtonProps = Omit<PressableProps, 'children'> & {
|
|
24
|
+
item: ShellNavItem;
|
|
25
|
+
active: boolean;
|
|
26
|
+
/** The drawer's labelled row; the rail draws the icon-only square. */
|
|
27
|
+
labelled?: boolean;
|
|
28
|
+
};
|
|
29
|
+
|
|
30
|
+
/** One nav entry: `h-12 rounded-lg` as v2's drawer size, lifted onto the primary tint when active. */
|
|
31
|
+
export const NavButton = React.forwardRef<View, NavButtonProps>(function NavButton(
|
|
32
|
+
{ item, active, labelled = false, className, ...props },
|
|
33
|
+
ref,
|
|
34
|
+
) {
|
|
35
|
+
const tint = active ? 'text-primary' : 'text-muted-foreground';
|
|
36
|
+
return (
|
|
37
|
+
<Pressable
|
|
38
|
+
ref={ref}
|
|
39
|
+
role="button"
|
|
40
|
+
aria-current={active ? 'page' : undefined}
|
|
41
|
+
accessibilityLabel={item.label}
|
|
42
|
+
testID={`nav-item-${item.id}`}
|
|
43
|
+
className={cn(
|
|
44
|
+
'h-12 flex-row items-center rounded-lg web:transition-colors',
|
|
45
|
+
labelled ? 'gap-3 px-3' : 'w-12 justify-center',
|
|
46
|
+
active ? 'bg-primary/10' : 'web:hover:bg-accent active:bg-accent',
|
|
47
|
+
'web:focus-visible:outline-none web:focus-visible:ring-2 web:focus-visible:ring-ring',
|
|
48
|
+
typeof className === 'string' ? className : undefined,
|
|
49
|
+
)}
|
|
50
|
+
{...props}
|
|
51
|
+
>
|
|
52
|
+
<View className={cn('h-6 w-6 items-center justify-center', tint)}>
|
|
53
|
+
{item.icon}
|
|
54
|
+
{item.badge !== undefined && item.badge !== 0 && item.badge !== '' && (
|
|
55
|
+
<View
|
|
56
|
+
testID={`nav-item-${item.id}-badge`}
|
|
57
|
+
className="absolute -right-2 -top-1 h-4 min-w-4 items-center justify-center rounded-full bg-destructive px-1"
|
|
58
|
+
>
|
|
59
|
+
<Text className="text-2xs font-semibold text-destructive-foreground">{item.badge}</Text>
|
|
60
|
+
</View>
|
|
61
|
+
)}
|
|
62
|
+
</View>
|
|
63
|
+
{labelled && (
|
|
64
|
+
<Text className={cn('flex-1 text-base', active ? 'font-semibold text-primary' : 'text-foreground')} numberOfLines={1}>
|
|
65
|
+
{item.label}
|
|
66
|
+
</Text>
|
|
67
|
+
)}
|
|
68
|
+
</Pressable>
|
|
69
|
+
);
|
|
70
|
+
});
|
|
@@ -0,0 +1,58 @@
|
|
|
1
|
+
// Port of WCPOS v2 `packages/core/src/screens/main/components/drawer-content/index.tsx` (the `rail`
|
|
2
|
+
// presentation) and `drawer-content/rail/index.tsx`, MIT, WCPOS.
|
|
3
|
+
import * as React from 'react';
|
|
4
|
+
import { View } from 'react-native';
|
|
5
|
+
import { cn } from '@tallyui/theme';
|
|
6
|
+
import { Text } from '../ui/text';
|
|
7
|
+
import { NavButton, splitGroups, type ShellNavItem } from './nav-item';
|
|
8
|
+
|
|
9
|
+
export interface NavRailProps {
|
|
10
|
+
items: ShellNavItem[];
|
|
11
|
+
activeId?: string;
|
|
12
|
+
onNavigate: (id: string) => void;
|
|
13
|
+
className?: string;
|
|
14
|
+
testID?: string;
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
/**
|
|
18
|
+
* The wide layout's permanent icon column. Its width is 16 spacing units, the `h-12 w-12` item
|
|
19
|
+
* plus 2 units each side (64 at Regular), so it follows the scale step as v2's does. Icon-only:
|
|
20
|
+
* the label shows to the right on hover or keyboard focus, v2's web tooltip.
|
|
21
|
+
*/
|
|
22
|
+
export function NavRail({ items, activeId, onNavigate, className, testID = 'nav-rail' }: NavRailProps) {
|
|
23
|
+
const [hinted, setHinted] = React.useState<string | null>(null);
|
|
24
|
+
const renderItem = (item: ShellNavItem) => (
|
|
25
|
+
<View key={item.id} className="relative z-10">
|
|
26
|
+
<NavButton
|
|
27
|
+
item={item}
|
|
28
|
+
active={item.id === activeId}
|
|
29
|
+
onPress={() => onNavigate(item.id)}
|
|
30
|
+
onHoverIn={() => setHinted(item.id)}
|
|
31
|
+
onHoverOut={() => setHinted((id) => (id === item.id ? null : id))}
|
|
32
|
+
onFocus={() => setHinted(item.id)}
|
|
33
|
+
onBlur={() => setHinted((id) => (id === item.id ? null : id))}
|
|
34
|
+
/>
|
|
35
|
+
{hinted === item.id && (
|
|
36
|
+
<View
|
|
37
|
+
testID={`nav-rail-label-${item.id}`}
|
|
38
|
+
pointerEvents="none"
|
|
39
|
+
className="absolute left-full top-2 z-50 ml-3 rounded-md border border-border bg-popover px-3 py-1.5 shadow-md"
|
|
40
|
+
>
|
|
41
|
+
<Text className="text-sm text-popover-foreground" numberOfLines={1}>{item.label}</Text>
|
|
42
|
+
</View>
|
|
43
|
+
)}
|
|
44
|
+
</View>
|
|
45
|
+
);
|
|
46
|
+
const [top, bottom] = splitGroups(items);
|
|
47
|
+
return (
|
|
48
|
+
<View
|
|
49
|
+
role="navigation"
|
|
50
|
+
aria-label="Main"
|
|
51
|
+
testID={testID}
|
|
52
|
+
className={cn('z-10 w-16 items-center border-r border-border bg-card py-2', className)}
|
|
53
|
+
>
|
|
54
|
+
<View className="gap-2">{top.map(renderItem)}</View>
|
|
55
|
+
{bottom.length > 0 && <View className="mt-auto gap-2">{bottom.map(renderItem)}</View>}
|
|
56
|
+
</View>
|
|
57
|
+
);
|
|
58
|
+
}
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
import * as React from 'react';
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* What a bar inside the shell reads: v2's `useBarMenu` (`packages/core/src/screens/main/components/
|
|
5
|
+
* bar-controls/index.tsx`, MIT, WCPOS) gives the menu only below the wide boundary, where there is
|
|
6
|
+
* a drawer to open; on wide the rail is always on screen.
|
|
7
|
+
*/
|
|
8
|
+
export interface ShellState {
|
|
9
|
+
phone: boolean;
|
|
10
|
+
openMenu?: () => void;
|
|
11
|
+
}
|
|
12
|
+
|
|
13
|
+
export const ShellContext = React.createContext<ShellState | null>(null);
|
|
14
|
+
|
|
15
|
+
/** The enclosing `AppShell`'s state, or `null` outside one. */
|
|
16
|
+
export function useShell(): ShellState | null {
|
|
17
|
+
return React.useContext(ShellContext);
|
|
18
|
+
}
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
import { Line } from 'react-native-svg';
|
|
2
|
+
import { createSvgIcon } from './svg-icon';
|
|
3
|
+
|
|
4
|
+
export const MenuIcon = createSvgIcon('MenuIcon', () => (
|
|
5
|
+
<>
|
|
6
|
+
<Line x1={4} y1={6} x2={20} y2={6} />
|
|
7
|
+
<Line x1={4} y1={12} x2={20} y2={12} />
|
|
8
|
+
<Line x1={4} y1={18} x2={20} y2={18} />
|
|
9
|
+
</>
|
|
10
|
+
));
|