@tallyui/components 3.12.0 → 3.14.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 +466 -28
- package/dist/index.js +3194 -1574
- package/package.json +9 -9
- package/src/cart/cart-columns.tsx +57 -0
- package/src/cart/cart-line.tsx +32 -17
- package/src/cart/index.ts +1 -0
- package/src/checkout/cash-tendered.tsx +5 -7
- package/src/fonts/font-provider.tsx +65 -0
- package/src/fonts/index.ts +1 -0
- package/src/index.ts +40 -2
- package/src/lib/error-text.ts +5 -0
- package/src/order/index.ts +2 -0
- package/src/order/refund-form.tsx +205 -0
- package/src/order/refund-modal.tsx +32 -0
- 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 +13 -2
- 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/reports/cards.tsx +197 -0
- package/src/reports/filters-bar.tsx +95 -0
- package/src/reports/hero.tsx +111 -0
- package/src/reports/index.ts +4 -0
- package/src/reports/sales-room.tsx +28 -0
- package/src/sale/cart.tsx +37 -21
- 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/open-sale-tabs.tsx +67 -0
- package/src/sale/order-reference.ts +3 -0
- package/src/sale/orders-list.tsx +4 -2
- package/src/sale/parked-sales.tsx +2 -1
- package/src/sale/price-form.tsx +28 -4
- package/src/sale/product-filter-bar.tsx +58 -0
- package/src/sale/receipt.tsx +8 -2
- package/src/sale/tender-pane.tsx +179 -0
- package/src/sale/tender.tsx +20 -35
- package/src/scale/index.ts +3 -0
- package/src/scale/scale-provider.tsx +70 -0
- package/src/scale/use-pointer.ts +23 -0
- package/src/settings/display-settings.tsx +122 -0
- package/src/settings/index.ts +1 -0
- 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/ui/text/font-text.tsx +25 -0
- package/src/ui/text/index.tsx +5 -4
- package/src/sale/split-tender.tsx +0 -81
|
@@ -0,0 +1,179 @@
|
|
|
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
|
+
|
|
17
|
+
type Sale = ReturnType<typeof useSale>;
|
|
18
|
+
type Leg = Sale['order']['payments'][number];
|
|
19
|
+
|
|
20
|
+
export interface TenderPaneProps {
|
|
21
|
+
sale: Sale;
|
|
22
|
+
/** The tender reducer's state (`tenderReducer` in @tallyui/pos); the host owns the reducer. */
|
|
23
|
+
state: TenderState;
|
|
24
|
+
dispatch: (action: TenderAction) => void;
|
|
25
|
+
/** Defaults to `defaultPaymentMethods()`; only `cash` and `external`, the order.create methods, are offered. */
|
|
26
|
+
methods?: PaymentMethodDescriptor[];
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
/** order.create's bound on `payments[].reference`, in UTF-16 units. */
|
|
30
|
+
const REFERENCE_MAX = 255;
|
|
31
|
+
/** v2 labels.ts statusLabel. */
|
|
32
|
+
const STATUS_LABEL: Record<PaymentLegStatus, string> = {
|
|
33
|
+
pending: 'Pending', authorized: 'Authorized', captured: 'Captured', failed: 'Failed', voided: 'Voided',
|
|
34
|
+
};
|
|
35
|
+
/** v2 labels.ts statusVariant: coloured by what the cashier has to do about the leg. */
|
|
36
|
+
const STATUS_TONE: Record<PaymentLegStatus, string> = {
|
|
37
|
+
pending: 'text-muted-foreground', authorized: 'text-success', captured: 'text-success', failed: 'text-destructive', voided: 'text-muted-foreground',
|
|
38
|
+
};
|
|
39
|
+
/** v2 tender-pane.tsx keypadRows: 123 / 456 / 789, then C, 0, backspace. No decimal key: digits shift in from the right. */
|
|
40
|
+
const KEYPAD_ROWS: { key: TenderKey; label: string; name?: string }[][] = [
|
|
41
|
+
...['123', '456', '789'].map((row) => [...row].map((digit) => ({ key: digit as TenderKey, label: digit }))),
|
|
42
|
+
[{ key: 'clear', label: 'C', name: 'Clear' }, { key: '0', label: '0' }, { key: 'backspace', label: '⌫', name: 'Delete last digit' }],
|
|
43
|
+
];
|
|
44
|
+
const button = 'min-h-11 justify-center rounded-md border border-border bg-card px-4 py-2';
|
|
45
|
+
|
|
46
|
+
/** The captured legs since the plan began, as the reducer's `tender-recorded` and `planLegs` read them. */
|
|
47
|
+
const rowsSince = (payments: readonly Leg[], from: number) =>
|
|
48
|
+
payments.filter(countsTowardPaid).slice(from).map((leg) => ({ title: leg.method, amountMinor: leg.amountMinor }));
|
|
49
|
+
|
|
50
|
+
export function TenderPane({ sale, state, dispatch, methods }: TenderPaneProps): JSX.Element | null {
|
|
51
|
+
const { order, stage } = sale;
|
|
52
|
+
const [reference, setReference] = useState('');
|
|
53
|
+
const [refusal, setRefusal] = useState<string | null>(null);
|
|
54
|
+
const balance = order.balanceDueMinor;
|
|
55
|
+
// The ledger changed (a leg taken or voided) and the entry is untouched: pre-type the next leg or plan share.
|
|
56
|
+
useEffect(() => {
|
|
57
|
+
if (!state.entryDirty) dispatch({ type: 'tender-recorded', rowsSinceFrom: rowsSince(order.payments, state.plan?.from ?? 0), balanceMinor: balance });
|
|
58
|
+
}, [order.payments, balance, state.entryDirty, state.plan, dispatch]);
|
|
59
|
+
if (stage.kind !== 'tender') return null;
|
|
60
|
+
|
|
61
|
+
const offered = (methods ?? defaultPaymentMethods()).filter(({ id }) => id === 'cash' || id === 'external');
|
|
62
|
+
const descriptorOf = (id: string) => offered.find((method) => method.id === id);
|
|
63
|
+
const money = (minor: number) => formatMoney({ amount: minor, currency: order.currency }) ?? '';
|
|
64
|
+
const method = state.methodId ? descriptorOf(state.methodId) ?? null : null;
|
|
65
|
+
const givesChange = method?.capabilities.change === true;
|
|
66
|
+
const figures = tenderRounding(state, balance, method);
|
|
67
|
+
const entryChange = changeMinor(figures.entryMinor, figures.appliedMinor, givesChange);
|
|
68
|
+
const left = balance + figures.roundingMinor - figures.appliedMinor;
|
|
69
|
+
const noChange = method !== null && !givesChange && figures.entryMinor > balance;
|
|
70
|
+
const rows = rowsSince(order.payments, state.plan?.from ?? 0).map(({ title, amountMinor }) => ({ title, minor: amountMinor }));
|
|
71
|
+
const plan = activePlan(state.plan, rows.length, balance);
|
|
72
|
+
const planned = plan ? planLegs(plan, rows, balance) : null;
|
|
73
|
+
const dueMinor = planned && planned.thisPaymentMinor < balance ? planned.thisPaymentMinor : figures.roundedDueMinor;
|
|
74
|
+
const chips = givesChange ? quickTenderedAmounts(dueMinor, QUICK_CASH_STEPS.map((step) => step * 10 ** minorUnitDigits(order.currency))) : [];
|
|
75
|
+
const settled = order.payments.some(countsTowardPaid);
|
|
76
|
+
const firstPending = order.payments.find((leg) => legStatus(leg) === 'pending');
|
|
77
|
+
const [onlyLeg] = order.payments;
|
|
78
|
+
// startTender('external') records the card leg for the total: its reference is edited in place, as before.
|
|
79
|
+
const cardReference = order.payments.length === 1 && onlyLeg.method === 'external' && legStatus(onlyLeg) === 'captured' && balance === 0;
|
|
80
|
+
const hint = noChange ? `Only ${money(balance)} is due — ${method?.title} can't give change`
|
|
81
|
+
: entryChange > 0 ? `Change ${money(entryChange)}`
|
|
82
|
+
: figures.appliedMinor < balance && figures.entryMinor > 0 ? `Part payment · ${money(left)} left after this` : '';
|
|
83
|
+
const canComplete = balance === 0 && !order.payments.some(isInMotion);
|
|
84
|
+
const cannotTake = figures.appliedMinor <= 0 || noChange;
|
|
85
|
+
|
|
86
|
+
function take() {
|
|
87
|
+
if (!method) return;
|
|
88
|
+
const id = method.id === 'cash'
|
|
89
|
+
? sale.addTender({ method: 'cash', amountMinor: figures.entryMinor })
|
|
90
|
+
: sale.addTender({ method: 'external', amountMinor: figures.appliedMinor, ...(reference ? { reference } : {}) });
|
|
91
|
+
if (id === null) { setRefusal("That payment wasn't taken: nothing is due, or the amount isn't valid."); return; }
|
|
92
|
+
setRefusal(null);
|
|
93
|
+
setReference('');
|
|
94
|
+
dispatch({ type: 'tender-started' });
|
|
95
|
+
}
|
|
96
|
+
|
|
97
|
+
return <View dataSet={{ print: 'hide' }} testID="tender-pane" className="gap-4 p-4">
|
|
98
|
+
<View testID="tender-headline" className="gap-1">
|
|
99
|
+
<Text className="text-sm text-muted-foreground">{order.paidMinor > 0 ? 'Remaining' : 'Total'}</Text>
|
|
100
|
+
<Text testID="tender-balance" className="text-4xl font-bold text-foreground">{money(balance)}</Text>
|
|
101
|
+
{order.paidMinor > 0 ? <Text testID="tender-paid" className="text-sm text-muted-foreground">{`${money(order.paidMinor)} of ${money(order.totalMinor)} paid`}</Text> : null}
|
|
102
|
+
{order.changeDueMinor > 0 ? <Text testID="tender-change" className="text-lg font-semibold text-success">{`Change ${money(order.changeDueMinor)}`}</Text> : null}
|
|
103
|
+
</View>
|
|
104
|
+
|
|
105
|
+
<View testID="tender-ledger" className="gap-1">
|
|
106
|
+
{order.payments.length === 0 ? <Text className="text-muted-foreground">No payments yet</Text> : order.payments.map((leg) => {
|
|
107
|
+
const status = legStatus(leg);
|
|
108
|
+
const voided = status === 'voided';
|
|
109
|
+
const descriptor = descriptorOf(leg.method);
|
|
110
|
+
return <View key={leg.id} testID={`tender-leg-${leg.id}`} className="gap-1 border-b border-border py-2">
|
|
111
|
+
<View className="flex-row items-center gap-2">
|
|
112
|
+
<Text className={cn('flex-1 font-semibold text-foreground', voided && 'line-through')}>{descriptor?.title ?? leg.method}</Text>
|
|
113
|
+
<Text className={cn('text-foreground', voided && 'line-through')}>{money(leg.amountMinor)}</Text>
|
|
114
|
+
<Text testID={`tender-leg-${leg.id}-status`} className={cn('text-sm font-semibold', STATUS_TONE[status])}>{voided ? 'Cancelled' : STATUS_LABEL[status]}</Text>
|
|
115
|
+
</View>
|
|
116
|
+
{leg.reference ? <Text className="text-sm text-muted-foreground">{leg.reference}</Text> : null}
|
|
117
|
+
{leg === firstPending ? <Text className="text-sm text-muted-foreground">Next</Text> : null}
|
|
118
|
+
{descriptor && canVoidLeg(leg, descriptor) ? <Pressable testID={`tender-leg-${leg.id}-void`} accessibilityRole="button" accessibilityLabel="Cancel payment"
|
|
119
|
+
onPress={() => sale.voidTender(leg.id)} className="self-start min-h-11 justify-center px-2">
|
|
120
|
+
<Text className="text-destructive">Cancel payment</Text>
|
|
121
|
+
</Pressable> : null}
|
|
122
|
+
</View>;
|
|
123
|
+
})}
|
|
124
|
+
{balance === 0 && settled ? <Text className="text-sm text-success">Paid in full</Text> : null}
|
|
125
|
+
</View>
|
|
126
|
+
|
|
127
|
+
{cardReference ? <TextInput accessibilityLabel="Terminal reference" placeholder="Terminal reference" maxLength={REFERENCE_MAX}
|
|
128
|
+
value={onlyLeg.reference ?? ''} onChangeText={(text) => sale.setTender({ method: 'external', amountMinor: onlyLeg.amountMinor, reference: text })}
|
|
129
|
+
className="rounded-md border border-border px-3 py-2 text-foreground" /> : null}
|
|
130
|
+
|
|
131
|
+
{balance > 0 ? <View testID="tender-entry" className="gap-3">
|
|
132
|
+
<View className="flex-row flex-wrap gap-2">
|
|
133
|
+
{offered.map((tile) => <Pressable key={tile.id} testID={`tender-method-${tile.id}`} accessibilityRole="button" accessibilityState={{ selected: tile.id === state.methodId }}
|
|
134
|
+
onPress={() => dispatch({ type: 'pick-method', methodId: tile.id, prefillMinor: balance, readerId: null })}
|
|
135
|
+
className={cn(button, tile.id === state.methodId && 'border-primary bg-primary')}>
|
|
136
|
+
<Text className={cn('text-center font-semibold', tile.id === state.methodId ? 'text-primary-foreground' : 'text-foreground')}>{tile.title}</Text>
|
|
137
|
+
</Pressable>)}
|
|
138
|
+
</View>
|
|
139
|
+
{planned ? <Text testID="tender-plan" className="text-sm text-muted-foreground">{`${planned.label.n} of ${planned.label.ways}`}</Text> : null}
|
|
140
|
+
<Text testID="tender-entry-amount" className={cn('text-3xl font-bold text-foreground', !state.entryDirty && 'opacity-70')}>{money(figures.entryMinor)}</Text>
|
|
141
|
+
{figures.roundingMinor !== 0 ? <Text className="text-sm text-muted-foreground">{`Rounded from ${money(balance)}`}</Text> : null}
|
|
142
|
+
<Text testID="tender-entry-hint" className={cn('min-h-6 text-sm', noChange ? 'text-warning' : entryChange > 0 ? 'text-success' : 'text-muted-foreground')}>{hint}</Text>
|
|
143
|
+
{method?.id === 'external' ? <TextInput accessibilityLabel="Terminal reference" placeholder="Terminal reference" maxLength={REFERENCE_MAX}
|
|
144
|
+
value={reference} onChangeText={setReference} className="rounded-md border border-border px-3 py-2 text-foreground" /> : null}
|
|
145
|
+
{chips.length ? <View testID="tender-quick" className="flex-row flex-wrap gap-2">
|
|
146
|
+
{chips.map((minor, index) => <Pressable key={minor} testID={index === 0 ? 'tender-quick-exact' : `tender-quick-${minor}`} accessibilityRole="button"
|
|
147
|
+
onPress={() => dispatch({ type: 'set-entry', minor })} className={cn(button, state.entryDirty && figures.entryMinor === minor && 'border-primary')}>
|
|
148
|
+
<Text className="font-semibold text-foreground">{index === 0 ? `Exact ${money(minor)}` : money(minor)}</Text>
|
|
149
|
+
</Pressable>)}
|
|
150
|
+
</View> : null}
|
|
151
|
+
<View testID="tender-keypad" className="gap-2">
|
|
152
|
+
{KEYPAD_ROWS.map((row) => <View key={row[0].key} className="flex-row gap-2">
|
|
153
|
+
{row.map(({ key, label, name }) => <Pressable key={key} accessibilityRole="button" accessibilityLabel={name ?? label}
|
|
154
|
+
onPress={() => dispatch({ type: 'key', key })} className={cn(button, 'flex-1 items-center')}>
|
|
155
|
+
<Text className="text-xl font-semibold text-foreground">{label}</Text>
|
|
156
|
+
</Pressable>)}
|
|
157
|
+
</View>)}
|
|
158
|
+
</View>
|
|
159
|
+
{refusal ? <Text accessibilityRole="alert" className="text-destructive">{refusal}</Text> : null}
|
|
160
|
+
{method ? <Pressable testID="tender-take" accessibilityRole="button" disabled={cannotTake}
|
|
161
|
+
onPress={take} className={cn('rounded-md bg-primary px-4 py-3', cannotTake && 'opacity-50')}>
|
|
162
|
+
<Text className="text-center font-semibold text-primary-foreground">{`Take ${money(figures.appliedMinor)} in ${method.title}`}</Text>
|
|
163
|
+
</Pressable> : null}
|
|
164
|
+
</View> : null}
|
|
165
|
+
|
|
166
|
+
<Pressable testID="tender-complete" accessibilityRole="button" disabled={!canComplete} onPress={sale.complete}
|
|
167
|
+
className={cn('rounded-md bg-primary px-4 py-3', !canComplete && 'opacity-50')}>
|
|
168
|
+
<Text className="text-center font-semibold text-primary-foreground">Complete sale</Text>
|
|
169
|
+
</Pressable>
|
|
170
|
+
{sale.error ? <Text accessibilityRole="alert" className="text-destructive">{sale.error}</Text> : null}
|
|
171
|
+
{sale.canContinue ? <>
|
|
172
|
+
<Text className="text-foreground">This sale is stored and will be sent. Continue to the next sale.</Text>
|
|
173
|
+
<Pressable accessibilityRole="button" onPress={() => sale.continueSale()} className="rounded-md bg-primary px-4 py-3">
|
|
174
|
+
<Text className="text-center font-semibold text-primary-foreground">Continue</Text>
|
|
175
|
+
</Pressable>
|
|
176
|
+
</> : null}
|
|
177
|
+
<Pressable accessibilityRole="button" onPress={sale.cancelTender} className={button}><Text className="text-center text-foreground">Back</Text></Pressable>
|
|
178
|
+
</View>;
|
|
179
|
+
}
|
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,70 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Port of WCPOS v2 `packages/core/src/contexts/scale/index.tsx` and
|
|
3
|
+
* `packages/components/src/lib/device.ts`.
|
|
4
|
+
* The native scope is a prop so components do not depend on Uniwind's ScopedVariables.
|
|
5
|
+
*/
|
|
6
|
+
import * as React from 'react';
|
|
7
|
+
import { Platform, useWindowDimensions } from 'react-native';
|
|
8
|
+
import type { ScaleOverride } from '@tallyui/theme';
|
|
9
|
+
import {
|
|
10
|
+
SCALE_VARIABLE_NAMES, resolveStep, scaleVariables,
|
|
11
|
+
type Pointer, type ScaleStep, type ScaleVariables,
|
|
12
|
+
} from '@tallyui/theme';
|
|
13
|
+
import { usePointer } from './use-pointer';
|
|
14
|
+
|
|
15
|
+
export interface ScaleScopeProps {
|
|
16
|
+
variables: ScaleVariables;
|
|
17
|
+
children?: React.ReactNode;
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
export interface ScaleState {
|
|
21
|
+
step: ScaleStep;
|
|
22
|
+
pointer: Pointer;
|
|
23
|
+
variables: ScaleVariables;
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
export interface ScaleProviderProps {
|
|
27
|
+
/** The app's stored `ui.scale` setting (from `useAppSettings` in `@tallyui/pos`); missing or unknown means Auto. */
|
|
28
|
+
scale?: ScaleOverride | null;
|
|
29
|
+
/** Scopes the variables below it on native, e.g. Uniwind's ScopedVariables. Optional: the web root mirror covers the web. */
|
|
30
|
+
scope?: React.ComponentType<ScaleScopeProps>;
|
|
31
|
+
children?: React.ReactNode;
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
const ScaleContext = React.createContext<ScaleState>({
|
|
35
|
+
step: 'regular',
|
|
36
|
+
pointer: 'coarse',
|
|
37
|
+
variables: scaleVariables('regular', 'coarse'),
|
|
38
|
+
});
|
|
39
|
+
|
|
40
|
+
/** Mirror on the root because portalled content leaves the subtree. */
|
|
41
|
+
function useRootScaleMirror(variables: ScaleVariables) {
|
|
42
|
+
React.useLayoutEffect(() => {
|
|
43
|
+
if (Platform.OS !== 'web' || typeof document === 'undefined') return;
|
|
44
|
+
const style = document.documentElement.style;
|
|
45
|
+
for (const name of SCALE_VARIABLE_NAMES) style.setProperty(name, `${variables[name]}px`);
|
|
46
|
+
return () => {
|
|
47
|
+
for (const name of SCALE_VARIABLE_NAMES) style.removeProperty(name);
|
|
48
|
+
};
|
|
49
|
+
}, [variables]);
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
export function ScaleProvider({ scale, scope: Scope, children }: ScaleProviderProps): React.JSX.Element {
|
|
53
|
+
const { width, height } = useWindowDimensions();
|
|
54
|
+
const pointer = usePointer();
|
|
55
|
+
const extent = Platform.OS === 'web' ? width : Math.min(width, height);
|
|
56
|
+
const step = resolveStep(scale, extent);
|
|
57
|
+
const variables = React.useMemo(() => scaleVariables(step, pointer), [step, pointer]);
|
|
58
|
+
const state = React.useMemo(() => ({ step, pointer, variables }), [step, pointer, variables]);
|
|
59
|
+
useRootScaleMirror(variables);
|
|
60
|
+
|
|
61
|
+
return (
|
|
62
|
+
<ScaleContext.Provider value={state}>
|
|
63
|
+
{Scope ? <Scope variables={variables}>{children}</Scope> : children}
|
|
64
|
+
</ScaleContext.Provider>
|
|
65
|
+
);
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
export function useScale(): ScaleState {
|
|
69
|
+
return React.useContext(ScaleContext);
|
|
70
|
+
}
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
/** Port of WCPOS v2 `packages/components/src/lib/device.ts`, without the override context. */
|
|
2
|
+
import { useCallback, useMemo, useSyncExternalStore } from 'react';
|
|
3
|
+
import { Platform } from 'react-native';
|
|
4
|
+
import type { Pointer } from '@tallyui/theme';
|
|
5
|
+
|
|
6
|
+
export function usePointer(): Pointer {
|
|
7
|
+
const queries = useMemo(() => {
|
|
8
|
+
if (Platform.OS !== 'web' || typeof window === 'undefined' || typeof window.matchMedia !== 'function') {
|
|
9
|
+
return [];
|
|
10
|
+
}
|
|
11
|
+
return [window.matchMedia('(pointer: fine)'), window.matchMedia('(hover: hover)')];
|
|
12
|
+
}, []);
|
|
13
|
+
const subscribe = useCallback((notify: () => void) => {
|
|
14
|
+
for (const query of queries) query.addEventListener('change', notify);
|
|
15
|
+
return () => {
|
|
16
|
+
for (const query of queries) query.removeEventListener('change', notify);
|
|
17
|
+
};
|
|
18
|
+
}, [queries]);
|
|
19
|
+
const getSnapshot = useCallback((): Pointer => (
|
|
20
|
+
queries.length === 2 && queries.every((query) => query.matches) ? 'fine' : 'coarse'
|
|
21
|
+
), [queries]);
|
|
22
|
+
return useSyncExternalStore(subscribe, getSnapshot, () => 'coarse');
|
|
23
|
+
}
|
|
@@ -0,0 +1,122 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Settings › Display: theme, scale and fonts, device-local through `useAppSettings`.
|
|
3
|
+
* Port of WCPOS v2 `packages/core/src/screens/main/settings/theme.tsx` (`ThemeSettingsContent`,
|
|
4
|
+
* `ScaleSection`, `FontsSection`); the labels are v2's English strings from
|
|
5
|
+
* `core/contexts/translations/locales/en/core.json`. v2's icons section has no TallyUI counterpart.
|
|
6
|
+
*/
|
|
7
|
+
import { Platform, View } from 'react-native';
|
|
8
|
+
import type { JSX } from 'react';
|
|
9
|
+
import { UI_SCALES, UI_THEMES, type AppSettingKey, type AppSettings, type UiScale, type UiTheme, type useAppSettings } from '@tallyui/pos';
|
|
10
|
+
import { FONT_SETTINGS, TYPE_SETS, cn, fontFamilyStyle } from '@tallyui/theme';
|
|
11
|
+
import { RadioGroup, RadioGroupItem } from '../ui/radio-group';
|
|
12
|
+
import { Text } from '../ui/text';
|
|
13
|
+
import { ToggleGroup, ToggleGroupItem } from '../ui/toggle-group';
|
|
14
|
+
import { SettingsGroup } from './settings-group';
|
|
15
|
+
|
|
16
|
+
type FontSetting = (typeof FONT_SETTINGS)[number];
|
|
17
|
+
|
|
18
|
+
export interface DisplaySettingsProps {
|
|
19
|
+
/** The hook's settings: `<DisplaySettings {...useAppSettings(db.app_settings)} />`. */
|
|
20
|
+
settings: Pick<AppSettings, 'ui.theme' | 'ui.scale' | 'ui.font'>;
|
|
21
|
+
setSetting: ReturnType<typeof useAppSettings>['setSetting'];
|
|
22
|
+
className?: string;
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
const THEME_OPTIONS: Record<UiTheme, { label: string; description: string }> = {
|
|
26
|
+
system: { label: 'System', description: 'Follow your device settings' },
|
|
27
|
+
light: { label: 'Light', description: 'Clean and bright' },
|
|
28
|
+
dark: { label: 'Dark', description: 'Easy on the eyes' },
|
|
29
|
+
};
|
|
30
|
+
|
|
31
|
+
const SCALE_LABELS: Record<UiScale, string> = {
|
|
32
|
+
auto: 'Auto', compact: 'Compact', regular: 'Regular', spacious: 'Spacious',
|
|
33
|
+
};
|
|
34
|
+
|
|
35
|
+
const FONT_LABELS: Record<FontSetting, string> = {
|
|
36
|
+
auto: 'Match theme',
|
|
37
|
+
system: 'System',
|
|
38
|
+
instrument: 'DM Sans · Instrument Serif',
|
|
39
|
+
grotesk: 'Space Grotesk',
|
|
40
|
+
fraunces: 'DM Sans · Fraunces',
|
|
41
|
+
manrope: 'Manrope',
|
|
42
|
+
};
|
|
43
|
+
|
|
44
|
+
interface OptionRowProps {
|
|
45
|
+
id: string;
|
|
46
|
+
testID: string;
|
|
47
|
+
label: string;
|
|
48
|
+
description?: string;
|
|
49
|
+
onSelect: () => void;
|
|
50
|
+
right?: JSX.Element;
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
function OptionRow({ id, testID, label, description, onSelect, right }: OptionRowProps): JSX.Element {
|
|
54
|
+
return (
|
|
55
|
+
<View className="flex-row items-center gap-3 px-4 py-3">
|
|
56
|
+
<RadioGroupItem value={id} testID={testID} aria-label={label} />
|
|
57
|
+
<View className="flex-1 gap-0.5">
|
|
58
|
+
<Text className="text-sm font-medium text-foreground" onPress={onSelect}>{label}</Text>
|
|
59
|
+
{description && <Text className="text-xs text-muted-foreground">{description}</Text>}
|
|
60
|
+
</View>
|
|
61
|
+
{right}
|
|
62
|
+
</View>
|
|
63
|
+
);
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
export function DisplaySettings({ settings, setSetting, className }: DisplaySettingsProps): JSX.Element {
|
|
67
|
+
const save = <K extends AppSettingKey>(key: K, value: AppSettings[K]) => {
|
|
68
|
+
setSetting(key, value).catch((error: unknown) => console.error('Failed to save display setting', error));
|
|
69
|
+
};
|
|
70
|
+
const theme = settings['ui.theme'];
|
|
71
|
+
const scale = settings['ui.scale'];
|
|
72
|
+
// A retired or unknown set shows Auto, as `resolveTypeSet` falls back.
|
|
73
|
+
const font = FONT_SETTINGS.find((id) => id === settings['ui.font']) ?? 'auto';
|
|
74
|
+
const isWeb = Platform.OS === 'web';
|
|
75
|
+
|
|
76
|
+
return (
|
|
77
|
+
<View className={cn('gap-6', className)}>
|
|
78
|
+
<SettingsGroup title="Theme">
|
|
79
|
+
<RadioGroup value={theme} onValueChange={(value) => save('ui.theme', value as UiTheme)} aria-label="Theme" className="gap-0">
|
|
80
|
+
{UI_THEMES.map((id) => (
|
|
81
|
+
<OptionRow
|
|
82
|
+
key={id} id={id} testID={`settings-theme-${id}`} label={THEME_OPTIONS[id].label}
|
|
83
|
+
description={THEME_OPTIONS[id].description} onSelect={() => save('ui.theme', id)}
|
|
84
|
+
/>
|
|
85
|
+
))}
|
|
86
|
+
</RadioGroup>
|
|
87
|
+
</SettingsGroup>
|
|
88
|
+
|
|
89
|
+
<SettingsGroup title="Scale" description="Auto follows the window size.">
|
|
90
|
+
<ToggleGroup
|
|
91
|
+
type="single" variant="outline" value={scale} aria-label="Scale" className="flex-wrap justify-start p-3"
|
|
92
|
+
onValueChange={(value) => { if (value) save('ui.scale', value as UiScale); }}
|
|
93
|
+
>
|
|
94
|
+
{UI_SCALES.map((id) => (
|
|
95
|
+
<ToggleGroupItem key={id} value={id} testID={`settings-scale-${id}`} className={cn(scale === id && 'bg-accent')}>
|
|
96
|
+
<Text>{SCALE_LABELS[id]}</Text>
|
|
97
|
+
</ToggleGroupItem>
|
|
98
|
+
))}
|
|
99
|
+
</ToggleGroup>
|
|
100
|
+
</SettingsGroup>
|
|
101
|
+
|
|
102
|
+
<SettingsGroup
|
|
103
|
+
title="Fonts"
|
|
104
|
+
description="The typeface the app's text uses on this device. Match theme follows the theme you chose."
|
|
105
|
+
>
|
|
106
|
+
<RadioGroup value={font} onValueChange={(value) => save('ui.font', value)} aria-label="Fonts" className="gap-0">
|
|
107
|
+
{FONT_SETTINGS.map((id) => (
|
|
108
|
+
<OptionRow
|
|
109
|
+
key={id} id={id} testID={`settings-fonts-${id}`} label={FONT_LABELS[id]}
|
|
110
|
+
onSelect={() => save('ui.font', id)}
|
|
111
|
+
right={(
|
|
112
|
+
<Text className="text-lg text-foreground" style={id === 'auto' ? undefined : fontFamilyStyle(TYPE_SETS[id].body, isWeb)}>
|
|
113
|
+
Aa
|
|
114
|
+
</Text>
|
|
115
|
+
)}
|
|
116
|
+
/>
|
|
117
|
+
))}
|
|
118
|
+
</RadioGroup>
|
|
119
|
+
</SettingsGroup>
|
|
120
|
+
</View>
|
|
121
|
+
);
|
|
122
|
+
}
|
package/src/settings/index.ts
CHANGED
|
@@ -1,3 +1,4 @@
|
|
|
1
1
|
export { SettingsRow, type SettingsRowProps } from './settings-row';
|
|
2
2
|
export { SettingsGroup, type SettingsGroupProps } from './settings-group';
|
|
3
3
|
export { ConnectorStatus, type ConnectorStatusProps, type ConnectionStatus } from './connector-status';
|
|
4
|
+
export { DisplaySettings, type DisplaySettingsProps } from './display-settings';
|
|
@@ -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
|
+
}
|