@selfhelp/shared 1.8.0 → 1.11.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/CHANGELOG.md +67 -0
- package/dist/{common-B96dASgu.d.mts → common-CJSHQL1E.d.mts} +13 -1
- package/dist/{common-B96dASgu.d.ts → common-CJSHQL1E.d.ts} +13 -1
- package/dist/index.d.mts +136 -44
- package/dist/index.d.ts +136 -44
- package/dist/index.js +395 -8
- package/dist/index.mjs +371 -8
- package/dist/plugin-sdk/index.d.mts +2 -2
- package/dist/plugin-sdk/index.d.ts +2 -2
- package/dist/plugin-sdk/index.js +10 -1
- package/dist/plugin-sdk/index.mjs +10 -1
- package/dist/registry/index.d.mts +42 -4
- package/dist/registry/index.d.ts +42 -4
- package/dist/registry/index.js +56 -3
- package/dist/registry/index.mjs +48 -2
- package/dist/{styles.registry-ClluRYkN.d.ts → styles.registry-BV1iF7k-.d.ts} +678 -461
- package/dist/{styles.registry-nl8uzvMN.d.mts → styles.registry-DkoT-FmN.d.mts} +678 -461
- package/dist/theme/index.d.mts +225 -4
- package/dist/theme/index.d.ts +225 -4
- package/dist/theme/index.js +343 -9
- package/dist/theme/index.mjs +325 -8
- package/dist/theme/tailwind.d.mts +2 -2
- package/dist/theme/tailwind.d.ts +2 -2
- package/package.json +1 -1
- package/src/registry/__tests__/platform.test.ts +187 -0
- package/src/registry/index.ts +73 -0
- package/src/registry/styles.registry.ts +60 -0
- package/src/theme/__tests__/mantineVariant.test.ts +87 -0
- package/src/theme/__tests__/semantic.test.ts +231 -0
- package/src/theme/index.ts +2 -0
- package/src/theme/mantineVariant.ts +145 -0
- package/src/theme/semantic.ts +472 -0
- package/src/theme/tailwind.ts +2 -2
- package/src/theme/tokens.ts +2 -2
- package/src/types/__tests__/mobile-adapters.test.ts +80 -0
- package/src/types/index.ts +1 -0
- package/src/types/mantine/common.ts +13 -0
- package/src/types/mobile-adapters.ts +144 -0
- package/src/types/styles/auth.ts +11 -11
- package/src/types/styles/base.ts +4 -4
- package/src/types/styles/composite.ts +59 -45
- package/src/types/styles/error.ts +24 -13
- package/src/types/styles/forms.ts +184 -184
- package/src/types/styles/interactive.ts +81 -76
- package/src/types/styles/layout.ts +103 -85
- package/src/types/styles/media.ts +20 -20
- package/src/types/styles/typography.ts +39 -39
- package/src/types/styles/unknown.ts +20 -2
- package/src/utils/spacing.ts +1 -1
|
@@ -0,0 +1,472 @@
|
|
|
1
|
+
/*
|
|
2
|
+
SPDX-FileCopyrightText: 2026 Humdek, University of Bern
|
|
3
|
+
SPDX-License-Identifier: MPL-2.0
|
|
4
|
+
*/
|
|
5
|
+
/**
|
|
6
|
+
* Shared semantic style mapper — the single source of truth that turns
|
|
7
|
+
* renderer-agnostic CMS values (`shared_*` fields) into concrete per-platform
|
|
8
|
+
* props. Renderers consume this module instead of duplicating mapping logic.
|
|
9
|
+
*
|
|
10
|
+
* - Web -> Mantine (`toMantineSemanticProps`)
|
|
11
|
+
* - Mobile -> HeroUI Native (`toHeroUiSemanticProps`)
|
|
12
|
+
* - Mobile -> React Native (`toReactNativeSemanticStyle`)
|
|
13
|
+
*
|
|
14
|
+
* Design rules (mobile rendering plan, section 8.2):
|
|
15
|
+
* - NO value clamping. The shared scales are the true common denominator
|
|
16
|
+
* (`shared_size` = sm|md|lg, `shared_radius` = none|sm|md|lg|full), so every
|
|
17
|
+
* value maps 1:1 to each platform. The backend enforces the same domain
|
|
18
|
+
* (migration `Version20260618195450`).
|
|
19
|
+
* - NO implicit cross-platform fallback. These functions take already-resolved
|
|
20
|
+
* SHARED props; the precedence `web_*`/`mobile_*` override -> `shared_*` ->
|
|
21
|
+
* component default is applied by each platform renderer, never here.
|
|
22
|
+
* - Exhaustive enum handling (every switch covers the full domain).
|
|
23
|
+
*
|
|
24
|
+
* Platform-specific props stay separate from these shared values:
|
|
25
|
+
* - `web_*` Mantine-only fields
|
|
26
|
+
* - `mobile_*` HeroUI/native-only fields
|
|
27
|
+
* - shared the semantic fields mapped here
|
|
28
|
+
*/
|
|
29
|
+
|
|
30
|
+
import { RADIUS_PX, SPACING_PX, type TCanonicalSpacing } from './tokens';
|
|
31
|
+
import type { TSharedRadius, TSharedSize } from '../types/mantine/common';
|
|
32
|
+
|
|
33
|
+
// ===== shared semantic scales (what the CMS stores) =====
|
|
34
|
+
|
|
35
|
+
/** Cross-platform size: the true common denominator (HeroUI Native has no xs/xl). */
|
|
36
|
+
export type TSemanticSize = TSharedSize; // 'sm' | 'md' | 'lg'
|
|
37
|
+
export type TSemanticSpacing = 'none' | 'xs' | 'sm' | 'md' | 'lg' | 'xl';
|
|
38
|
+
/** Cross-platform radius. `full` = pill; `none` = square. */
|
|
39
|
+
export type TSemanticRadius = TSharedRadius; // 'none' | 'sm' | 'md' | 'lg' | 'full'
|
|
40
|
+
export type TSemanticIntent =
|
|
41
|
+
| 'primary'
|
|
42
|
+
| 'secondary'
|
|
43
|
+
| 'success'
|
|
44
|
+
| 'warning'
|
|
45
|
+
| 'danger'
|
|
46
|
+
| 'neutral';
|
|
47
|
+
export type TSemanticState = 'disabled' | 'loading' | 'invalid' | 'required';
|
|
48
|
+
|
|
49
|
+
/** Pixel value used for a fully-rounded ("pill") radius on both platforms. */
|
|
50
|
+
export const FULL_RADIUS_PX = 9999;
|
|
51
|
+
|
|
52
|
+
const SEMANTIC_SIZES: readonly TSemanticSize[] = ['sm', 'md', 'lg'];
|
|
53
|
+
const SEMANTIC_RADII: readonly TSemanticRadius[] = ['none', 'sm', 'md', 'lg', 'full'];
|
|
54
|
+
const SEMANTIC_INTENTS: readonly TSemanticIntent[] = [
|
|
55
|
+
'primary',
|
|
56
|
+
'secondary',
|
|
57
|
+
'success',
|
|
58
|
+
'warning',
|
|
59
|
+
'danger',
|
|
60
|
+
'neutral',
|
|
61
|
+
];
|
|
62
|
+
|
|
63
|
+
/**
|
|
64
|
+
* Renderer-agnostic style values configured in the CMS. Every field is
|
|
65
|
+
* optional; renderers fall back to component defaults when a value is
|
|
66
|
+
* absent.
|
|
67
|
+
*/
|
|
68
|
+
export interface ISharedStyleProps {
|
|
69
|
+
size?: TSemanticSize;
|
|
70
|
+
spacing?: TSemanticSpacing;
|
|
71
|
+
radius?: TSemanticRadius;
|
|
72
|
+
intent?: TSemanticIntent;
|
|
73
|
+
/** Active boolean states. Stored as a small set so multiple can apply. */
|
|
74
|
+
states?: readonly TSemanticState[];
|
|
75
|
+
/** Layout: stretch the control to the full width of its container. */
|
|
76
|
+
fullWidth?: boolean;
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
// ===== Mantine (web) target shapes =====
|
|
80
|
+
|
|
81
|
+
export type TMantineMappedColor =
|
|
82
|
+
| 'blue'
|
|
83
|
+
| 'gray'
|
|
84
|
+
| 'green'
|
|
85
|
+
| 'yellow'
|
|
86
|
+
| 'red';
|
|
87
|
+
export type TMantineMappedVariant = 'filled' | 'light' | 'outline' | 'default';
|
|
88
|
+
|
|
89
|
+
export interface IMantineResolvedStyle {
|
|
90
|
+
/** Mantine accepts sm|md|lg 1:1 (a subset of its xs..xl scale). */
|
|
91
|
+
size?: TSemanticSize;
|
|
92
|
+
color?: TMantineMappedColor;
|
|
93
|
+
variant?: TMantineMappedVariant;
|
|
94
|
+
/** Mantine `radius` accepts a token or a number; pill => FULL_RADIUS_PX. */
|
|
95
|
+
radius?: TSemanticSize | number;
|
|
96
|
+
/** Margin/padding spacing token (xs..xl); `none` => undefined. */
|
|
97
|
+
spacing?: Exclude<TSemanticSpacing, 'none'>;
|
|
98
|
+
disabled?: boolean;
|
|
99
|
+
loading?: boolean;
|
|
100
|
+
error?: boolean;
|
|
101
|
+
required?: boolean;
|
|
102
|
+
fullWidth?: boolean;
|
|
103
|
+
}
|
|
104
|
+
|
|
105
|
+
// ===== HeroUI Native (mobile) target shapes =====
|
|
106
|
+
|
|
107
|
+
/** HeroUI Native sizes — sm | md | lg (identical to the shared scale). */
|
|
108
|
+
export type THeroUiSize = TSharedSize;
|
|
109
|
+
/** HeroUI Native Button variants. */
|
|
110
|
+
export type THeroUiButtonVariant =
|
|
111
|
+
| 'primary'
|
|
112
|
+
| 'secondary'
|
|
113
|
+
| 'tertiary'
|
|
114
|
+
| 'outline'
|
|
115
|
+
| 'ghost'
|
|
116
|
+
| 'danger'
|
|
117
|
+
| 'danger-soft';
|
|
118
|
+
/** HeroUI Native semantic colors (Chip / Tag / status surfaces). */
|
|
119
|
+
export type THeroUiColor = 'accent' | 'default' | 'success' | 'warning' | 'danger';
|
|
120
|
+
|
|
121
|
+
export interface IHeroUiResolvedStyle {
|
|
122
|
+
size?: THeroUiSize;
|
|
123
|
+
/** Intent mapped onto the Button variant vocabulary. */
|
|
124
|
+
buttonVariant?: THeroUiButtonVariant;
|
|
125
|
+
/** Intent mapped onto the semantic color vocabulary (Chip/Tag/etc). */
|
|
126
|
+
color?: THeroUiColor;
|
|
127
|
+
/** Resolved radius in px (pill => FULL_RADIUS_PX, none => 0). */
|
|
128
|
+
radiusPx?: number;
|
|
129
|
+
/** Resolved spacing in px (none => 0/undefined). */
|
|
130
|
+
spacingPx?: number;
|
|
131
|
+
isDisabled?: boolean;
|
|
132
|
+
isLoading?: boolean;
|
|
133
|
+
isInvalid?: boolean;
|
|
134
|
+
isRequired?: boolean;
|
|
135
|
+
fullWidth?: boolean;
|
|
136
|
+
}
|
|
137
|
+
|
|
138
|
+
/** Plain React Native `View`/`Text` style subset derived from shared props. */
|
|
139
|
+
export interface IReactNativeResolvedStyle {
|
|
140
|
+
borderRadius?: number;
|
|
141
|
+
padding?: number;
|
|
142
|
+
alignSelf?: 'stretch';
|
|
143
|
+
width?: '100%';
|
|
144
|
+
}
|
|
145
|
+
|
|
146
|
+
/** Optional px-scale overrides a native theme can inject (no implicit fallback). */
|
|
147
|
+
export interface IReactNativeThemeScales {
|
|
148
|
+
radiusPx?: Partial<Record<TSharedSize, number>>;
|
|
149
|
+
spacingPx?: Partial<Record<TCanonicalSpacing, number>>;
|
|
150
|
+
}
|
|
151
|
+
|
|
152
|
+
// ===== size =====
|
|
153
|
+
|
|
154
|
+
/** Mantine accepts sm|md|lg directly, so size passes through unchanged. */
|
|
155
|
+
export function mapSizeToMantine(size: TSemanticSize | undefined): TSemanticSize | undefined {
|
|
156
|
+
return size;
|
|
157
|
+
}
|
|
158
|
+
|
|
159
|
+
/** HeroUI Native uses the same sm|md|lg scale — a 1:1 pass-through (no clamp). */
|
|
160
|
+
export function mapSizeToHeroUi(size: TSemanticSize | undefined): THeroUiSize | undefined {
|
|
161
|
+
if (!size) return undefined;
|
|
162
|
+
switch (size) {
|
|
163
|
+
case 'sm':
|
|
164
|
+
return 'sm';
|
|
165
|
+
case 'md':
|
|
166
|
+
return 'md';
|
|
167
|
+
case 'lg':
|
|
168
|
+
return 'lg';
|
|
169
|
+
default:
|
|
170
|
+
return undefined;
|
|
171
|
+
}
|
|
172
|
+
}
|
|
173
|
+
|
|
174
|
+
// ===== radius =====
|
|
175
|
+
|
|
176
|
+
/** Resolve a semantic radius to px for mobile (none => 0, full => pill). */
|
|
177
|
+
export function mapRadiusToPx(radius: TSemanticRadius | undefined): number | undefined {
|
|
178
|
+
if (!radius) return undefined;
|
|
179
|
+
switch (radius) {
|
|
180
|
+
case 'none':
|
|
181
|
+
return 0;
|
|
182
|
+
case 'full':
|
|
183
|
+
return FULL_RADIUS_PX;
|
|
184
|
+
case 'sm':
|
|
185
|
+
return RADIUS_PX.sm;
|
|
186
|
+
case 'md':
|
|
187
|
+
return RADIUS_PX.md;
|
|
188
|
+
case 'lg':
|
|
189
|
+
return RADIUS_PX.lg;
|
|
190
|
+
default:
|
|
191
|
+
return undefined;
|
|
192
|
+
}
|
|
193
|
+
}
|
|
194
|
+
|
|
195
|
+
/** Resolve a semantic radius for Mantine (token, or px number for pill/none). */
|
|
196
|
+
export function mapRadiusToMantine(
|
|
197
|
+
radius: TSemanticRadius | undefined,
|
|
198
|
+
): TSemanticSize | number | undefined {
|
|
199
|
+
if (!radius) return undefined;
|
|
200
|
+
switch (radius) {
|
|
201
|
+
case 'none':
|
|
202
|
+
return 0;
|
|
203
|
+
case 'full':
|
|
204
|
+
return FULL_RADIUS_PX;
|
|
205
|
+
case 'sm':
|
|
206
|
+
return 'sm';
|
|
207
|
+
case 'md':
|
|
208
|
+
return 'md';
|
|
209
|
+
case 'lg':
|
|
210
|
+
return 'lg';
|
|
211
|
+
default:
|
|
212
|
+
return undefined;
|
|
213
|
+
}
|
|
214
|
+
}
|
|
215
|
+
|
|
216
|
+
// ===== spacing =====
|
|
217
|
+
|
|
218
|
+
/** Resolve a semantic spacing token to px (none => 0). */
|
|
219
|
+
export function mapSpacingToPx(spacing: TSemanticSpacing | undefined): number | undefined {
|
|
220
|
+
if (!spacing) return undefined;
|
|
221
|
+
if (spacing === 'none') return 0;
|
|
222
|
+
return SPACING_PX[spacing as TCanonicalSpacing];
|
|
223
|
+
}
|
|
224
|
+
|
|
225
|
+
// ===== intent =====
|
|
226
|
+
|
|
227
|
+
/**
|
|
228
|
+
* Map a semantic intent to a Mantine `{ color, variant }` pair.
|
|
229
|
+
* primary -> blue / filled
|
|
230
|
+
* secondary -> gray / light
|
|
231
|
+
* success -> green / filled
|
|
232
|
+
* warning -> yellow / filled
|
|
233
|
+
* danger -> red / filled
|
|
234
|
+
* neutral -> gray / default
|
|
235
|
+
*/
|
|
236
|
+
export function mapIntentToMantine(
|
|
237
|
+
intent: TSemanticIntent | undefined,
|
|
238
|
+
): { color: TMantineMappedColor; variant: TMantineMappedVariant } | undefined {
|
|
239
|
+
if (!intent) return undefined;
|
|
240
|
+
switch (intent) {
|
|
241
|
+
case 'primary':
|
|
242
|
+
return { color: 'blue', variant: 'filled' };
|
|
243
|
+
case 'secondary':
|
|
244
|
+
return { color: 'gray', variant: 'light' };
|
|
245
|
+
case 'success':
|
|
246
|
+
return { color: 'green', variant: 'filled' };
|
|
247
|
+
case 'warning':
|
|
248
|
+
return { color: 'yellow', variant: 'filled' };
|
|
249
|
+
case 'danger':
|
|
250
|
+
return { color: 'red', variant: 'filled' };
|
|
251
|
+
case 'neutral':
|
|
252
|
+
return { color: 'gray', variant: 'default' };
|
|
253
|
+
default:
|
|
254
|
+
return undefined;
|
|
255
|
+
}
|
|
256
|
+
}
|
|
257
|
+
|
|
258
|
+
/**
|
|
259
|
+
* Map a semantic intent to a HeroUI Native **Button** variant.
|
|
260
|
+
*
|
|
261
|
+
* HeroUI buttons have no dedicated success/warning variant. Rather than
|
|
262
|
+
* silently collapse them, the dedicated status surfaces use
|
|
263
|
+
* {@link mapIntentToHeroUiColor}; the Button variant maps success/warning to
|
|
264
|
+
* `primary` because that is the closest *prominent* action affordance, and the
|
|
265
|
+
* status color is carried separately. This is an explicit semantic choice, not
|
|
266
|
+
* a clamp that hides an unsupported size/scale value.
|
|
267
|
+
* primary -> primary
|
|
268
|
+
* secondary -> secondary
|
|
269
|
+
* success -> primary (status color carried by mapIntentToHeroUiColor)
|
|
270
|
+
* warning -> primary (status color carried by mapIntentToHeroUiColor)
|
|
271
|
+
* danger -> danger
|
|
272
|
+
* neutral -> secondary
|
|
273
|
+
*/
|
|
274
|
+
export function mapIntentToHeroUiButtonVariant(
|
|
275
|
+
intent: TSemanticIntent | undefined,
|
|
276
|
+
): THeroUiButtonVariant | undefined {
|
|
277
|
+
if (!intent) return undefined;
|
|
278
|
+
switch (intent) {
|
|
279
|
+
case 'primary':
|
|
280
|
+
return 'primary';
|
|
281
|
+
case 'secondary':
|
|
282
|
+
return 'secondary';
|
|
283
|
+
case 'success':
|
|
284
|
+
return 'primary';
|
|
285
|
+
case 'warning':
|
|
286
|
+
return 'primary';
|
|
287
|
+
case 'danger':
|
|
288
|
+
return 'danger';
|
|
289
|
+
case 'neutral':
|
|
290
|
+
return 'secondary';
|
|
291
|
+
default:
|
|
292
|
+
return undefined;
|
|
293
|
+
}
|
|
294
|
+
}
|
|
295
|
+
|
|
296
|
+
/**
|
|
297
|
+
* Map a semantic intent to a HeroUI Native semantic color (Chip/Tag/
|
|
298
|
+
* status surfaces, which DO support success/warning).
|
|
299
|
+
* primary -> accent
|
|
300
|
+
* secondary -> default
|
|
301
|
+
* success -> success
|
|
302
|
+
* warning -> warning
|
|
303
|
+
* danger -> danger
|
|
304
|
+
* neutral -> default
|
|
305
|
+
*/
|
|
306
|
+
export function mapIntentToHeroUiColor(
|
|
307
|
+
intent: TSemanticIntent | undefined,
|
|
308
|
+
): THeroUiColor | undefined {
|
|
309
|
+
if (!intent) return undefined;
|
|
310
|
+
switch (intent) {
|
|
311
|
+
case 'primary':
|
|
312
|
+
return 'accent';
|
|
313
|
+
case 'secondary':
|
|
314
|
+
return 'default';
|
|
315
|
+
case 'success':
|
|
316
|
+
return 'success';
|
|
317
|
+
case 'warning':
|
|
318
|
+
return 'warning';
|
|
319
|
+
case 'danger':
|
|
320
|
+
return 'danger';
|
|
321
|
+
case 'neutral':
|
|
322
|
+
return 'default';
|
|
323
|
+
default:
|
|
324
|
+
return undefined;
|
|
325
|
+
}
|
|
326
|
+
}
|
|
327
|
+
|
|
328
|
+
// ===== state helpers =====
|
|
329
|
+
|
|
330
|
+
function hasState(states: readonly TSemanticState[] | undefined, state: TSemanticState): boolean {
|
|
331
|
+
return !!states && states.includes(state);
|
|
332
|
+
}
|
|
333
|
+
|
|
334
|
+
// ===== field reader =====
|
|
335
|
+
|
|
336
|
+
function readString(value: unknown): string | undefined {
|
|
337
|
+
if (typeof value === 'string' && value !== '') return value;
|
|
338
|
+
return undefined;
|
|
339
|
+
}
|
|
340
|
+
|
|
341
|
+
function readBool(value: unknown): boolean | undefined {
|
|
342
|
+
if (typeof value === 'boolean') return value;
|
|
343
|
+
if (value === '1' || value === 1) return true;
|
|
344
|
+
if (value === '0' || value === 0) return false;
|
|
345
|
+
return undefined;
|
|
346
|
+
}
|
|
347
|
+
|
|
348
|
+
/**
|
|
349
|
+
* Read the `shared_*` CMS fields of a section into {@link ISharedStyleProps}.
|
|
350
|
+
*
|
|
351
|
+
* Only valid values for the narrowed shared scales are accepted; anything
|
|
352
|
+
* outside the domain (e.g. a stale `xl`) is ignored rather than clamped, so the
|
|
353
|
+
* renderer falls through to its component default. Platform `web_*`/`mobile_*`
|
|
354
|
+
* overrides are NOT read here — that precedence lives in the platform renderer.
|
|
355
|
+
*/
|
|
356
|
+
export function resolveSharedStyleProps(fields: Record<string, unknown>): ISharedStyleProps {
|
|
357
|
+
const resolved: ISharedStyleProps = {};
|
|
358
|
+
|
|
359
|
+
const size = readString(fields.shared_size);
|
|
360
|
+
if (size !== undefined && (SEMANTIC_SIZES as readonly string[]).includes(size)) {
|
|
361
|
+
resolved.size = size as TSemanticSize;
|
|
362
|
+
}
|
|
363
|
+
|
|
364
|
+
const radius = readString(fields.shared_radius);
|
|
365
|
+
if (radius !== undefined && (SEMANTIC_RADII as readonly string[]).includes(radius)) {
|
|
366
|
+
resolved.radius = radius as TSemanticRadius;
|
|
367
|
+
}
|
|
368
|
+
|
|
369
|
+
const intent = readString(fields.shared_intent);
|
|
370
|
+
if (intent !== undefined && (SEMANTIC_INTENTS as readonly string[]).includes(intent)) {
|
|
371
|
+
resolved.intent = intent as TSemanticIntent;
|
|
372
|
+
}
|
|
373
|
+
|
|
374
|
+
const fullWidth = readBool(fields.shared_full_width);
|
|
375
|
+
if (fullWidth !== undefined) {
|
|
376
|
+
resolved.fullWidth = fullWidth;
|
|
377
|
+
}
|
|
378
|
+
|
|
379
|
+
return resolved;
|
|
380
|
+
}
|
|
381
|
+
|
|
382
|
+
// ===== top-level resolvers =====
|
|
383
|
+
|
|
384
|
+
/** Resolve shared props into Mantine (web) props. */
|
|
385
|
+
export function toMantineSemanticProps(props: ISharedStyleProps): IMantineResolvedStyle {
|
|
386
|
+
const intent = mapIntentToMantine(props.intent);
|
|
387
|
+
const spacing = props.spacing && props.spacing !== 'none' ? props.spacing : undefined;
|
|
388
|
+
return {
|
|
389
|
+
size: mapSizeToMantine(props.size),
|
|
390
|
+
color: intent?.color,
|
|
391
|
+
variant: intent?.variant,
|
|
392
|
+
radius: mapRadiusToMantine(props.radius),
|
|
393
|
+
spacing,
|
|
394
|
+
disabled: hasState(props.states, 'disabled') || undefined,
|
|
395
|
+
loading: hasState(props.states, 'loading') || undefined,
|
|
396
|
+
error: hasState(props.states, 'invalid') || undefined,
|
|
397
|
+
required: hasState(props.states, 'required') || undefined,
|
|
398
|
+
fullWidth: props.fullWidth || undefined,
|
|
399
|
+
};
|
|
400
|
+
}
|
|
401
|
+
|
|
402
|
+
/** Resolve shared props into HeroUI Native (mobile) props. */
|
|
403
|
+
export function toHeroUiSemanticProps(props: ISharedStyleProps): IHeroUiResolvedStyle {
|
|
404
|
+
return {
|
|
405
|
+
size: mapSizeToHeroUi(props.size),
|
|
406
|
+
buttonVariant: mapIntentToHeroUiButtonVariant(props.intent),
|
|
407
|
+
color: mapIntentToHeroUiColor(props.intent),
|
|
408
|
+
radiusPx: mapRadiusToPx(props.radius),
|
|
409
|
+
spacingPx: mapSpacingToPx(props.spacing),
|
|
410
|
+
isDisabled: hasState(props.states, 'disabled') || undefined,
|
|
411
|
+
isLoading: hasState(props.states, 'loading') || undefined,
|
|
412
|
+
isInvalid: hasState(props.states, 'invalid') || undefined,
|
|
413
|
+
isRequired: hasState(props.states, 'required') || undefined,
|
|
414
|
+
fullWidth: props.fullWidth || undefined,
|
|
415
|
+
};
|
|
416
|
+
}
|
|
417
|
+
|
|
418
|
+
/**
|
|
419
|
+
* Resolve shared props into a plain React Native style object (for styles with
|
|
420
|
+
* no HeroUI Native equivalent). `theme` may override the token px scales; when a
|
|
421
|
+
* token is not overridden the shared default is used (no implicit web fallback).
|
|
422
|
+
*/
|
|
423
|
+
export function toReactNativeSemanticStyle(
|
|
424
|
+
props: ISharedStyleProps,
|
|
425
|
+
theme?: IReactNativeThemeScales,
|
|
426
|
+
): IReactNativeResolvedStyle {
|
|
427
|
+
const style: IReactNativeResolvedStyle = {};
|
|
428
|
+
|
|
429
|
+
if (props.radius !== undefined) {
|
|
430
|
+
const borderRadius = resolveRadiusPx(props.radius, theme);
|
|
431
|
+
if (borderRadius !== undefined) style.borderRadius = borderRadius;
|
|
432
|
+
}
|
|
433
|
+
|
|
434
|
+
if (props.spacing !== undefined) {
|
|
435
|
+
const padding = resolveSpacingPx(props.spacing, theme);
|
|
436
|
+
if (padding !== undefined) style.padding = padding;
|
|
437
|
+
}
|
|
438
|
+
|
|
439
|
+
if (props.fullWidth) {
|
|
440
|
+
style.alignSelf = 'stretch';
|
|
441
|
+
style.width = '100%';
|
|
442
|
+
}
|
|
443
|
+
|
|
444
|
+
return style;
|
|
445
|
+
}
|
|
446
|
+
|
|
447
|
+
function resolveRadiusPx(radius: TSemanticRadius, theme?: IReactNativeThemeScales): number | undefined {
|
|
448
|
+
if (radius === 'none') return 0;
|
|
449
|
+
if (radius === 'full') return FULL_RADIUS_PX;
|
|
450
|
+
return theme?.radiusPx?.[radius] ?? mapRadiusToPx(radius);
|
|
451
|
+
}
|
|
452
|
+
|
|
453
|
+
function resolveSpacingPx(spacing: TSemanticSpacing, theme?: IReactNativeThemeScales): number | undefined {
|
|
454
|
+
if (spacing === 'none') return 0;
|
|
455
|
+
return theme?.spacingPx?.[spacing] ?? mapSpacingToPx(spacing);
|
|
456
|
+
}
|
|
457
|
+
|
|
458
|
+
/** Resolve shared props for both UI-library platforms at once. */
|
|
459
|
+
export function resolveSharedStyle(props: ISharedStyleProps): {
|
|
460
|
+
web: IMantineResolvedStyle;
|
|
461
|
+
mobile: IHeroUiResolvedStyle;
|
|
462
|
+
} {
|
|
463
|
+
return {
|
|
464
|
+
web: toMantineSemanticProps(props),
|
|
465
|
+
mobile: toHeroUiSemanticProps(props),
|
|
466
|
+
};
|
|
467
|
+
}
|
|
468
|
+
|
|
469
|
+
/** @deprecated Use {@link toMantineSemanticProps}. */
|
|
470
|
+
export const resolveSharedStyleForWeb = toMantineSemanticProps;
|
|
471
|
+
/** @deprecated Use {@link toHeroUiSemanticProps}. */
|
|
472
|
+
export const resolveSharedStyleForMobile = toHeroUiSemanticProps;
|
package/src/theme/tailwind.ts
CHANGED
|
@@ -5,8 +5,8 @@ SPDX-License-Identifier: MPL-2.0
|
|
|
5
5
|
/**
|
|
6
6
|
* Shared Tailwind preset extended by both the web frontend (Tailwind v4)
|
|
7
7
|
* and the mobile app (Uniwind v4 inside HeroUI Native). Anchors the same
|
|
8
|
-
* scale values that drive `
|
|
9
|
-
* `
|
|
8
|
+
* scale values that drive `shared_size`, `shared_radius`,
|
|
9
|
+
* `shared_spacing`, and the color palette so a class like `p-md` or
|
|
10
10
|
* `text-blue-6` resolves identically on web and mobile.
|
|
11
11
|
*/
|
|
12
12
|
|
package/src/theme/tokens.ts
CHANGED
|
@@ -4,7 +4,7 @@ SPDX-License-Identifier: MPL-2.0
|
|
|
4
4
|
*/
|
|
5
5
|
/**
|
|
6
6
|
* Mantine semantic token tables. Both the web Mantine theme and the
|
|
7
|
-
* mobile HeroUI Native theme extend these so a `
|
|
7
|
+
* mobile HeroUI Native theme extend these so a `shared_size: 'md'` on a
|
|
8
8
|
* section renders identical sizing regardless of platform.
|
|
9
9
|
*
|
|
10
10
|
* Pixel values come straight from Mantine v9 defaults:
|
|
@@ -91,7 +91,7 @@ export const CONTAINER_SIZE_PX: Record<TMantineSize, number> = {
|
|
|
91
91
|
/**
|
|
92
92
|
* Mantine palette base colors (color-6 from each scale). Each renderer
|
|
93
93
|
* maps these to its own theme tokens. For HeroUI Native we feed these
|
|
94
|
-
* into the theme builder so `
|
|
94
|
+
* into the theme builder so `web_color: 'blue'` resolves to the
|
|
95
95
|
* same Mantine-blue look on mobile.
|
|
96
96
|
*/
|
|
97
97
|
export const COLOR_PALETTE: Record<TCanonicalColor, string> = {
|
|
@@ -0,0 +1,80 @@
|
|
|
1
|
+
/*
|
|
2
|
+
SPDX-FileCopyrightText: 2026 Humdek, University of Bern
|
|
3
|
+
SPDX-License-Identifier: MPL-2.0
|
|
4
|
+
*/
|
|
5
|
+
/**
|
|
6
|
+
* Mobile UI adapter contract test (mobile rendering plan, section 14 "Shared":
|
|
7
|
+
* "Mobile adapter contract type tests").
|
|
8
|
+
*
|
|
9
|
+
* The contract is the SINGLE public source both mobile tiers implement, so this
|
|
10
|
+
* locks the milestone-one capability set. A capability added or removed without
|
|
11
|
+
* a deliberate contract change (and matching OSS + Pro implementations) fails
|
|
12
|
+
* here. The shapes themselves are enforced at compile time by the consumers
|
|
13
|
+
* (`ossAdapters`/`proAdapters: IMobileUiAdapters` + the app `tsconfig.pro.json`).
|
|
14
|
+
*/
|
|
15
|
+
import { describe, expect, it } from 'vitest';
|
|
16
|
+
import type {
|
|
17
|
+
ComponentType,
|
|
18
|
+
} from 'react';
|
|
19
|
+
import type {
|
|
20
|
+
IMobileButtonProps,
|
|
21
|
+
IMobileCardProps,
|
|
22
|
+
IMobileCheckboxProps,
|
|
23
|
+
IMobileContainerProps,
|
|
24
|
+
IMobileInputProps,
|
|
25
|
+
IMobileModalProps,
|
|
26
|
+
IMobileSelectProps,
|
|
27
|
+
IMobileSwitchProps,
|
|
28
|
+
IMobileTextProps,
|
|
29
|
+
IMobileTextareaProps,
|
|
30
|
+
IMobileUiAdapters,
|
|
31
|
+
} from '../mobile-adapters';
|
|
32
|
+
|
|
33
|
+
/** The milestone-one capability set, as named keys. */
|
|
34
|
+
const CONTRACT_CAPABILITIES = [
|
|
35
|
+
'MobileButton',
|
|
36
|
+
'MobileText',
|
|
37
|
+
'MobileContainer',
|
|
38
|
+
'MobileCard',
|
|
39
|
+
'MobileInput',
|
|
40
|
+
'MobileTextarea',
|
|
41
|
+
'MobileSwitch',
|
|
42
|
+
'MobileCheckbox',
|
|
43
|
+
'MobileSelect',
|
|
44
|
+
'MobileModal',
|
|
45
|
+
] as const satisfies ReadonlyArray<keyof IMobileUiAdapters>;
|
|
46
|
+
|
|
47
|
+
/**
|
|
48
|
+
* Compile-time exhaustiveness: every `IMobileUiAdapters` key must appear in the
|
|
49
|
+
* list above and vice versa. If a capability is added to the interface without
|
|
50
|
+
* being listed here (or removed), this assignment fails to type-check.
|
|
51
|
+
*/
|
|
52
|
+
type ContractKey = keyof IMobileUiAdapters;
|
|
53
|
+
type ListedKey = (typeof CONTRACT_CAPABILITIES)[number];
|
|
54
|
+
const _exhaustiveForward: ListedKey = '' as ContractKey;
|
|
55
|
+
const _exhaustiveBackward: ContractKey = '' as ListedKey;
|
|
56
|
+
void _exhaustiveForward;
|
|
57
|
+
void _exhaustiveBackward;
|
|
58
|
+
|
|
59
|
+
describe('mobile UI adapter contract', () => {
|
|
60
|
+
it('exposes exactly the milestone-one capability set', () => {
|
|
61
|
+
// A trivial component every capability key can hold; proves a complete
|
|
62
|
+
// implementation set is structurally assignable to the contract.
|
|
63
|
+
const stub: ComponentType<never> = () => null;
|
|
64
|
+
const adapters: IMobileUiAdapters = {
|
|
65
|
+
MobileButton: stub as ComponentType<IMobileButtonProps>,
|
|
66
|
+
MobileText: stub as ComponentType<IMobileTextProps>,
|
|
67
|
+
MobileContainer: stub as ComponentType<IMobileContainerProps>,
|
|
68
|
+
MobileCard: stub as ComponentType<IMobileCardProps>,
|
|
69
|
+
MobileInput: stub as ComponentType<IMobileInputProps>,
|
|
70
|
+
MobileTextarea: stub as ComponentType<IMobileTextareaProps>,
|
|
71
|
+
MobileSwitch: stub as ComponentType<IMobileSwitchProps>,
|
|
72
|
+
MobileCheckbox: stub as ComponentType<IMobileCheckboxProps>,
|
|
73
|
+
MobileSelect: stub as ComponentType<IMobileSelectProps>,
|
|
74
|
+
MobileModal: stub as ComponentType<IMobileModalProps>,
|
|
75
|
+
};
|
|
76
|
+
|
|
77
|
+
expect(Object.keys(adapters).sort()).toEqual([...CONTRACT_CAPABILITIES].sort());
|
|
78
|
+
expect(CONTRACT_CAPABILITIES).toHaveLength(10);
|
|
79
|
+
});
|
|
80
|
+
});
|
package/src/types/index.ts
CHANGED
|
@@ -13,6 +13,19 @@ SPDX-License-Identifier: MPL-2.0
|
|
|
13
13
|
export type TMantineSize = 'xs' | 'sm' | 'md' | 'lg' | 'xl';
|
|
14
14
|
export type TMantineRadius = 'xs' | 'sm' | 'md' | 'lg' | 'xl' | string;
|
|
15
15
|
export type TMantineSpacing = 'xs' | 'sm' | 'md' | 'lg' | 'xl' | 'none' | string;
|
|
16
|
+
|
|
17
|
+
/**
|
|
18
|
+
* Cross-platform (`shared_*`) semantic scales — the TRUE common denominator
|
|
19
|
+
* between Mantine (web) and HeroUI Native / React Native (mobile). HeroUI Native
|
|
20
|
+
* only exposes `sm | md | lg`, so portable `shared_size` / `shared_radius` fields
|
|
21
|
+
* use these narrowed scales instead of the full Mantine `xs..xl` range. The
|
|
22
|
+
* backend enforces the same domain (migration `Version20260618195450`), and the
|
|
23
|
+
* semantic mapper (`theme/semantic.ts`) maps these 1:1 without clamping.
|
|
24
|
+
*
|
|
25
|
+
* Web-only `web_size` / `web_radius` keep the full `TMantineSize` / `TMantineRadius`.
|
|
26
|
+
*/
|
|
27
|
+
export type TSharedSize = 'sm' | 'md' | 'lg';
|
|
28
|
+
export type TSharedRadius = 'none' | 'sm' | 'md' | 'lg' | 'full';
|
|
16
29
|
export type TMantineVariant =
|
|
17
30
|
| 'default'
|
|
18
31
|
| 'filled'
|