@klyftig/mascot 0.0.0-stage → 0.1.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/README.md CHANGED
@@ -1,3 +1,25 @@
1
- # Temporary Holding Version
1
+ # @klyftig/mascot
2
2
 
3
- This version is a temporary placeholder for this package. An operational version to replace this has been submitted for review and is awaiting a staged release.
3
+ Shared mascot layer for Klyftig React Native / Expo apps: generated pose art (supplied by each app) plus light native-driver motion. JS-only, no native module, no art shipped.
4
+
5
+ ```tsx
6
+ import { defineCharacter, Mascot, MascotBubble, useMascotLine } from '@klyftig/mascot';
7
+
8
+ type Mood = 'cheering' | 'happy' | 'sad';
9
+ export const biscuit = defineCharacter<Mood, 'cheer' | 'happy' | 'sad'>({
10
+ name: 'Biscuit',
11
+ poses: { cheer: require('../assets/mascot/biscuit-cheering.png'), happy: ..., sad: ... },
12
+ moodToPose: { cheering: 'cheer', happy: 'happy', sad: 'sad' }, // every mood, compile-time checked
13
+ moodMotion: { sad: 'droop' },
14
+ moodOverlay: { sad: 'rain' },
15
+ });
16
+
17
+ <Mascot character={biscuit} mood={mood} size={120} bounceKey={logCount} paused={!isFocused} />
18
+ ```
19
+
20
+ - **Mood decisions stay in the app.** The package only renders a mood; each character has its own mood union.
21
+ - **Motion:** breathing loop (`breathe` | `slow` | `droop` | `still`), one bounce per `bounceKey` change, drawn overlays (`rain`, `zzz`) so the art stays clean. All off under Reduce Motion, while the app is backgrounded, or when `paused`.
22
+ - **Copy:** `useMascotLine(pool, mood, seed)` picks from an app-supplied pool; the package ships no text.
23
+ - **Art:** 512px transparent PNG, never shown above ~170dp on a 3x screen. Wrap sources with the app's `sizedImageSource` before passing them in (undersized `Image` sources cause iOS offscreen-render stutter).
24
+ - `react`, `react-native`, `expo` are peerDependencies only (never dependencies).
25
+ - Publish: bump `version` first (`npm publish` does not), then `npm publish --access public`.
package/package.json CHANGED
@@ -1,6 +1,33 @@
1
1
  {
2
2
  "name": "@klyftig/mascot",
3
- "version": "0.0.0-stage",
4
- "stub": true,
5
- "description": "Temporary package placeholder for staged publishing"
6
- }
3
+ "version": "0.1.0",
4
+ "description": "Klyftig shared mascot layer for React Native / Expo: generated pose art + light native-driver motion",
5
+ "main": "src/index.ts",
6
+ "types": "src/index.ts",
7
+ "files": ["src", "!src/**/*.test.ts", "!src/**/*.test.tsx"],
8
+ "scripts": { "test": "jest", "typecheck": "tsc --noEmit" },
9
+ "keywords": ["react-native", "expo", "mascot"],
10
+ "license": "MIT",
11
+ "peerDependencies": {
12
+ "expo": "*",
13
+ "react": ">=19.0.0",
14
+ "react-native": ">=0.83.0"
15
+ },
16
+ "publishConfig": { "access": "public" },
17
+ "devDependencies": {
18
+ "@testing-library/react-native": "^13.3.3",
19
+ "@types/jest": "^29.5.14",
20
+ "@types/react": "~19.2.0",
21
+ "expo": "~55.0.11",
22
+ "jest": "^29.7.0",
23
+ "jest-expo": "~55.0.14",
24
+ "react": "19.2.0",
25
+ "react-native": "0.83.4",
26
+ "react-test-renderer": "19.2.0",
27
+ "typescript": "^6.0.3"
28
+ },
29
+ "jest": {
30
+ "preset": "jest-expo",
31
+ "testMatch": ["**/*.test.ts", "**/*.test.tsx"]
32
+ }
33
+ }
package/src/Mascot.tsx ADDED
@@ -0,0 +1,86 @@
1
+ import React, { useEffect, useRef } from 'react';
2
+ import { Animated, Easing, Image, StyleSheet, View, type StyleProp, type ViewStyle } from 'react-native';
3
+ import { MascotOverlay } from './MascotOverlay';
4
+ import type { Character, MotionKind } from './types';
5
+ import { useMotionEnabled } from './useMotionEnabled';
6
+
7
+ interface Props<M extends string> {
8
+ character: Character<M, string>;
9
+ mood: M;
10
+ size?: number;
11
+ style?: StyleProp<ViewStyle>;
12
+ /** Change this value (e.g. a counter) to play one happy bounce, like on a food log. */
13
+ bounceKey?: number | string;
14
+ /** Pass `!isFocused` from the app's navigation to stop the loop off screen. */
15
+ paused?: boolean;
16
+ }
17
+
18
+ const BREATH: Record<MotionKind, { to: number; ms: number } | null> = {
19
+ breathe: { to: 1.03, ms: 1500 },
20
+ slow: { to: 1.025, ms: 2600 },
21
+ droop: { to: 1.012, ms: 2600 },
22
+ still: null,
23
+ };
24
+
25
+ /**
26
+ * One generated pose per mood plus a light native-driver loop. The pose is chosen
27
+ * only by `character.moodToPose`; motion and overlays are per mood too.
28
+ */
29
+ export function Mascot<M extends string>({ character, mood, size = 96, style, bounceKey, paused }: Props<M>) {
30
+ const enabled = useMotionEnabled(paused);
31
+ const motion: MotionKind = character.moodMotion?.[mood] ?? 'breathe';
32
+ const overlay = character.moodOverlay?.[mood];
33
+ const pose = character.moodToPose[mood];
34
+
35
+ const breath = useRef(new Animated.Value(1)).current;
36
+ const hop = useRef(new Animated.Value(0)).current;
37
+
38
+ useEffect(() => {
39
+ const spec = BREATH[motion];
40
+ if (!enabled || !spec) {
41
+ breath.setValue(1);
42
+ return;
43
+ }
44
+ const loop = Animated.loop(
45
+ Animated.sequence([
46
+ Animated.timing(breath, { toValue: spec.to, duration: spec.ms, easing: Easing.inOut(Easing.sin), useNativeDriver: true }),
47
+ Animated.timing(breath, { toValue: 1, duration: spec.ms, easing: Easing.inOut(Easing.sin), useNativeDriver: true }),
48
+ ]),
49
+ );
50
+ loop.start();
51
+ return () => loop.stop();
52
+ }, [enabled, motion, breath]);
53
+
54
+ const first = useRef(true);
55
+ useEffect(() => {
56
+ if (first.current) {
57
+ first.current = false;
58
+ return;
59
+ }
60
+ if (!enabled) return;
61
+ hop.setValue(0);
62
+ Animated.sequence([
63
+ Animated.timing(hop, { toValue: -size * 0.12, duration: 160, easing: Easing.out(Easing.quad), useNativeDriver: true }),
64
+ Animated.spring(hop, { toValue: 0, friction: 4, tension: 120, useNativeDriver: true }),
65
+ ]).start();
66
+ // size intentionally not a trigger: only a new bounceKey plays a bounce
67
+ // eslint-disable-next-line react-hooks/exhaustive-deps
68
+ }, [bounceKey]);
69
+
70
+ const sag = motion === 'droop' ? size * 0.02 : 0;
71
+
72
+ return (
73
+ <View style={[{ width: size, height: size }, style]} accessible={false} testID={`mascot-${character.name.toLowerCase()}-${mood}`}>
74
+ <Animated.View style={{ transform: [{ translateY: Animated.add(hop, sag) }, { scaleY: breath }] }}>
75
+ <Image
76
+ source={character.poses[pose]}
77
+ style={{ width: size, height: size }}
78
+ resizeMode="contain"
79
+ accessible={false}
80
+ testID={`mascot-pose-${pose}`}
81
+ />
82
+ </Animated.View>
83
+ {overlay ? <MascotOverlay kind={overlay} size={size} animate={enabled} /> : null}
84
+ </View>
85
+ );
86
+ }
@@ -0,0 +1,57 @@
1
+ import React from 'react';
2
+ import { StyleSheet, Text, View, type StyleProp, type TextStyle, type ViewStyle } from 'react-native';
3
+
4
+ interface Props {
5
+ text: string;
6
+ /** 'bottom' points down at a mascot below; 'left' points at one beside the bubble. Default 'bottom'. */
7
+ tail?: 'bottom' | 'left';
8
+ backgroundColor?: string;
9
+ textColor?: string;
10
+ style?: StyleProp<ViewStyle>;
11
+ textStyle?: StyleProp<TextStyle>;
12
+ }
13
+
14
+ /** Speech bubble with a small tail on the bottom-left, pointing down at the mascot. */
15
+ export function MascotBubble({ text, tail = 'bottom', backgroundColor = '#FFFFFF', textColor = '#1B1B1B', style, textStyle }: Props) {
16
+ return (
17
+ <View style={style} accessibilityRole="text">
18
+ <View style={[styles.bubble, { backgroundColor }]}>
19
+ <Text style={[styles.text, { color: textColor }, textStyle]}>{text}</Text>
20
+ </View>
21
+ {tail === 'bottom' ? (
22
+ <View style={[styles.tail, { borderTopColor: backgroundColor }]} />
23
+ ) : (
24
+ <View style={[styles.tailLeft, { borderRightColor: backgroundColor }]} testID="mascot-bubble-tail-left" />
25
+ )}
26
+ </View>
27
+ );
28
+ }
29
+
30
+ const styles = StyleSheet.create({
31
+ bubble: { borderRadius: 16, paddingHorizontal: 14, paddingVertical: 10 },
32
+ text: { fontSize: 14, lineHeight: 19 },
33
+ tailLeft: {
34
+ position: 'absolute',
35
+ left: -7,
36
+ top: '50%',
37
+ marginTop: -7,
38
+ width: 0,
39
+ height: 0,
40
+ borderTopWidth: 7,
41
+ borderBottomWidth: 7,
42
+ borderRightWidth: 8,
43
+ borderTopColor: 'transparent',
44
+ borderBottomColor: 'transparent',
45
+ },
46
+ tail: {
47
+ alignSelf: 'flex-start',
48
+ marginLeft: 22,
49
+ width: 0,
50
+ height: 0,
51
+ borderLeftWidth: 7,
52
+ borderRightWidth: 7,
53
+ borderTopWidth: 8,
54
+ borderLeftColor: 'transparent',
55
+ borderRightColor: 'transparent',
56
+ },
57
+ });
@@ -0,0 +1,66 @@
1
+ import React, { useEffect, useRef } from 'react';
2
+ import { Animated, Easing, StyleSheet, Text, View } from 'react-native';
3
+ import type { OverlayKind } from './types';
4
+
5
+ interface Props {
6
+ kind: OverlayKind;
7
+ size: number;
8
+ animate: boolean;
9
+ }
10
+
11
+ /** Little rain cloud over the head, or "z z" drifting up. Plain views, no art. */
12
+ export function MascotOverlay({ kind, size, animate }: Props) {
13
+ const t = useRef(new Animated.Value(0)).current;
14
+
15
+ useEffect(() => {
16
+ if (!animate) {
17
+ t.setValue(0);
18
+ return;
19
+ }
20
+ const loop = Animated.loop(
21
+ Animated.timing(t, { toValue: 1, duration: kind === 'zzz' ? 2400 : 1400, easing: Easing.linear, useNativeDriver: true }),
22
+ );
23
+ loop.start();
24
+ return () => loop.stop();
25
+ }, [animate, kind, t]);
26
+
27
+ if (kind === 'zzz') {
28
+ const rise = t.interpolate({ inputRange: [0, 1], outputRange: [0, -size * 0.12] });
29
+ const fade = t.interpolate({ inputRange: [0, 0.2, 0.8, 1], outputRange: [0, 1, 1, 0] });
30
+ return (
31
+ <Animated.View
32
+ pointerEvents="none"
33
+ testID="mascot-overlay-zzz"
34
+ style={[styles.zzz, { right: size * 0.12, top: size * 0.08, opacity: animate ? fade : 1, transform: [{ translateY: animate ? rise : 0 }] }]}
35
+ >
36
+ <Text style={[styles.z, { fontSize: size * 0.16 }]}>z</Text>
37
+ <Text style={[styles.z, { fontSize: size * 0.22 }]}>Z</Text>
38
+ </Animated.View>
39
+ );
40
+ }
41
+
42
+ const drop = t.interpolate({ inputRange: [0, 1], outputRange: [0, size * 0.1] });
43
+ const dropFade = t.interpolate({ inputRange: [0, 0.7, 1], outputRange: [1, 1, 0] });
44
+ const c = size * 0.12;
45
+ return (
46
+ <View pointerEvents="none" testID="mascot-overlay-rain" style={[styles.rain, { top: 0, width: size * 0.4, height: size * 0.3 }]}>
47
+ <View style={[styles.puff, { width: c * 1.6, height: c * 1.6, left: 0, top: c * 0.5 }]} />
48
+ <View style={[styles.puff, { width: c * 2, height: c * 2, left: c, top: 0 }]} />
49
+ <View style={[styles.puff, { width: c * 1.6, height: c * 1.6, left: c * 2.4, top: c * 0.5 }]} />
50
+ {[0.5, 1.4, 2.3].map((x, i) => (
51
+ <Animated.View
52
+ key={i}
53
+ style={[styles.drop, { left: c * x + c * 0.3, top: c * 2.1, opacity: animate ? dropFade : 1, transform: [{ translateY: animate ? drop : 0 }] }]}
54
+ />
55
+ ))}
56
+ </View>
57
+ );
58
+ }
59
+
60
+ const styles = StyleSheet.create({
61
+ zzz: { position: 'absolute', flexDirection: 'row', alignItems: 'flex-end', gap: 2 },
62
+ z: { color: '#E58FA3', fontWeight: '800' },
63
+ rain: { position: 'absolute', alignSelf: 'center' },
64
+ puff: { position: 'absolute', borderRadius: 999, backgroundColor: '#8D97A5' },
65
+ drop: { position: 'absolute', width: 3, height: 7, borderRadius: 2, backgroundColor: '#7FB2E5' },
66
+ });
@@ -0,0 +1,36 @@
1
+ import React from 'react';
2
+ import { StyleSheet, Text, View, type StyleProp, type TextStyle } from 'react-native';
3
+ import { Mascot } from './Mascot';
4
+ import type { Character } from './types';
5
+
6
+ interface Props<M extends string> {
7
+ character: Character<M, string>;
8
+ mood: M;
9
+ title: string;
10
+ description?: string;
11
+ /** Anything below the line: a spinner, a button. */
12
+ action?: React.ReactNode;
13
+ size?: number;
14
+ titleStyle?: StyleProp<TextStyle>;
15
+ descriptionStyle?: StyleProp<TextStyle>;
16
+ }
17
+
18
+ /** Mascot plus a short title and line: the shared look of a calm empty / waiting state. */
19
+ export function MascotState<M extends string>({
20
+ character, mood, title, description, action, size = 120, titleStyle, descriptionStyle,
21
+ }: Props<M>) {
22
+ return (
23
+ <View style={styles.container}>
24
+ <Mascot character={character} mood={mood} size={size} />
25
+ <Text style={[styles.title, titleStyle]} accessibilityRole="header">{title}</Text>
26
+ {description ? <Text style={[styles.description, descriptionStyle]}>{description}</Text> : null}
27
+ {action}
28
+ </View>
29
+ );
30
+ }
31
+
32
+ const styles = StyleSheet.create({
33
+ container: { alignItems: 'center', gap: 10, paddingVertical: 32, paddingHorizontal: 24 },
34
+ title: { fontSize: 20, fontWeight: '700', textAlign: 'center' },
35
+ description: { fontSize: 14, textAlign: 'center' },
36
+ });
package/src/index.ts ADDED
@@ -0,0 +1,9 @@
1
+ export { defineCharacter } from './types';
2
+ export type { Character, MotionKind, OverlayKind } from './types';
3
+ export { Mascot } from './Mascot';
4
+ export { MascotBubble } from './MascotBubble';
5
+ export { MascotState } from './MascotState';
6
+ export { MascotOverlay } from './MascotOverlay';
7
+ export { pickLine, useMascotLine } from './useMascotLine';
8
+ export type { LinePool } from './useMascotLine';
9
+ export { useMotionEnabled } from './useMotionEnabled';
package/src/types.ts ADDED
@@ -0,0 +1,25 @@
1
+ import type { ImageSourcePropType } from 'react-native';
2
+
3
+ /** How the figure moves while a mood is showing. */
4
+ export type MotionKind = 'breathe' | 'slow' | 'droop' | 'still';
5
+
6
+ /** Small drawn extra over the pose (the art itself stays clean). */
7
+ export type OverlayKind = 'rain' | 'zzz';
8
+
9
+ export interface Character<M extends string, P extends string = string> {
10
+ name: string;
11
+ poses: Record<P, ImageSourcePropType>;
12
+ /** Every mood of the character's own vocabulary must name a pose. */
13
+ moodToPose: Record<M, P>;
14
+ /** Default 'breathe'. */
15
+ moodMotion?: Partial<Record<M, MotionKind>>;
16
+ moodOverlay?: Partial<Record<M, OverlayKind>>;
17
+ }
18
+
19
+ /**
20
+ * Identity helper that makes the compiler enforce the contract: every mood maps
21
+ * to a pose that exists in `poses`.
22
+ */
23
+ export function defineCharacter<M extends string, P extends string>(c: Character<M, P>): Character<M, P> {
24
+ return c;
25
+ }
@@ -0,0 +1,16 @@
1
+ import { useMemo } from 'react';
2
+
3
+ /** A copy pool keyed by mood. Apps own the text (and its translations); the package ships none. */
4
+ export type LinePool<M extends string> = Partial<Record<M, readonly string[]>>;
5
+
6
+ /** Stable pick: the same seed always gives the same line, so a re-render never flickers. */
7
+ export function pickLine<M extends string>(pool: LinePool<M>, mood: M, seed: number): string | undefined {
8
+ const lines = pool[mood];
9
+ if (!lines || lines.length === 0) return undefined;
10
+ return lines[Math.abs(Math.trunc(seed)) % lines.length];
11
+ }
12
+
13
+ /** Hook form; bump `seed` (e.g. on tap) to rotate to the next line. */
14
+ export function useMascotLine<M extends string>(pool: LinePool<M>, mood: M, seed = 0): string | undefined {
15
+ return useMemo(() => pickLine(pool, mood, seed), [pool, mood, seed]);
16
+ }
@@ -0,0 +1,22 @@
1
+ import { useEffect, useState } from 'react';
2
+ import { AccessibilityInfo, AppState } from 'react-native';
3
+
4
+ /** False under Reduce Motion or while the app is not in the foreground. */
5
+ export function useMotionEnabled(paused = false): boolean {
6
+ const [reduce, setReduce] = useState(false);
7
+ const [active, setActive] = useState(AppState.currentState !== 'background' && AppState.currentState !== 'inactive');
8
+
9
+ useEffect(() => {
10
+ let alive = true;
11
+ AccessibilityInfo.isReduceMotionEnabled().then((v) => alive && setReduce(!!v)).catch(() => {});
12
+ const a = AccessibilityInfo.addEventListener('reduceMotionChanged', setReduce);
13
+ const b = AppState.addEventListener('change', (s) => setActive(s === 'active'));
14
+ return () => {
15
+ alive = false;
16
+ a.remove();
17
+ b.remove();
18
+ };
19
+ }, []);
20
+
21
+ return !paused && !reduce && active;
22
+ }