@tallyui/components 3.11.0 → 3.13.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.
@@ -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
+ }
@@ -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,25 @@
1
+ import * as React from 'react';
2
+ import { Platform, type TextProps } from 'react-native';
3
+ import { fontFamilyStyle } from '@tallyui/theme';
4
+ import { useFontType } from '../../fonts';
5
+
6
+ const wrappers = new Map<React.ElementType, React.ComponentType<TextProps>>();
7
+
8
+ /** Wraps a text component (RN Text or Slot) so it draws the active type set's face. Cached: the same Base always returns the same wrapper. */
9
+ export function withFont<C extends React.ElementType>(Base: C): C {
10
+ let Wrapper = wrappers.get(Base);
11
+ if (!Wrapper) {
12
+ Wrapper = function FontText({ className, style, ...props }: TextProps) {
13
+ const { type } = useFontType();
14
+ const family = /(^|\s)font-(mono|sans|serif)(\s|$)/.test(className ?? '')
15
+ ? undefined
16
+ : /(^|\s)tabular-nums(\s|$)/.test(className ?? '') ? type.num ?? type.body : type.body;
17
+ return React.createElement(Base, {
18
+ ...props, className,
19
+ style: family ? [fontFamilyStyle(family, Platform.OS === 'web'), style] : style,
20
+ });
21
+ };
22
+ wrappers.set(Base, Wrapper);
23
+ }
24
+ return Wrapper as unknown as C;
25
+ }
@@ -1,8 +1,9 @@
1
1
  import * as React from 'react';
2
2
  import { Text as RNText } from 'react-native';
3
3
  import { Slot, TextClassContext } from '@tallyui/primitives';
4
- import { cn } from '@tallyui/theme';
4
+ import { cn, MAX_FONT_SCALE } from '@tallyui/theme';
5
5
  import { cva, type VariantProps } from 'class-variance-authority';
6
+ import { withFont } from './font-text';
6
7
 
7
8
  const textVariants = cva('text-foreground web:select-text text-base', {
8
9
  variants: {
@@ -21,12 +22,12 @@ type TextProps = React.ComponentPropsWithoutRef<typeof RNText> &
21
22
  asChild?: boolean;
22
23
  };
23
24
 
24
- function Text({ className, variant, asChild = false, children, ...props }: TextProps) {
25
+ function Text({ className, variant, asChild = false, children, maxFontSizeMultiplier = MAX_FONT_SCALE, ...props }: TextProps) {
25
26
  const textClass = React.useContext(TextClassContext);
26
- const Component = asChild ? Slot : RNText;
27
+ const Component = withFont(asChild ? Slot : RNText);
27
28
 
28
29
  return (
29
- <Component className={cn(textVariants({ variant }), textClass, className)} {...props}>
30
+ <Component className={cn(textVariants({ variant }), textClass, className)} {...props} maxFontSizeMultiplier={maxFontSizeMultiplier}>
30
31
  {children}
31
32
  </Component>
32
33
  );