File size: 7,391 Bytes
542c765
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
"use client";

// ============================================================
// BreathingWidget.tsx — MEMBER 4 OWNS THIS
// Pure CSS 4-7-8 breathing animation. No external library.
// Wrapped in @media prefers-reduced-motion.
// ============================================================

import { useState, useEffect, useRef } from "react";

type Phase = "inhale" | "hold" | "exhale" | "rest";

const PHASES: { phase: Phase; duration: number; label: string; sublabel: string }[] = [
  { phase: "inhale", duration: 4, label: "Breathe In", sublabel: "through your nose" },
  { phase: "hold", duration: 7, label: "Hold", sublabel: "gently" },
  { phase: "exhale", duration: 8, label: "Breathe Out", sublabel: "through your mouth" },
  { phase: "rest", duration: 1, label: "Rest", sublabel: "" },
];

const PHASE_COLORS: Record<Phase, string> = {
  inhale: "#FF9933",
  hold: "#6366F1",
  exhale: "#22C55E",
  rest: "#64748B",
};

const PHASE_SCALE: Record<Phase, number> = {
  inhale: 1,
  hold: 1,
  exhale: 0.45,
  rest: 0.45,
};

export default function BreathingWidget() {
  const [isRunning, setIsRunning] = useState(false);
  const [phaseIndex, setPhaseIndex] = useState(0);
  const [countdown, setCountdown] = useState(PHASES[0].duration);
  const [cycleCount, setCycleCount] = useState(0);
  const [scale, setScale] = useState(0.45);
  const intervalRef = useRef<NodeJS.Timeout | null>(null);

  const currentPhase = PHASES[phaseIndex];
  const color = PHASE_COLORS[currentPhase.phase];

  useEffect(() => {
    if (!isRunning) {
      if (intervalRef.current) clearInterval(intervalRef.current);
      setScale(0.45);
      return;
    }

    // Set initial scale for the phase
    if (currentPhase.phase === "inhale") {
      setScale(1);
    } else if (currentPhase.phase === "hold") {
      setScale(1);
    } else {
      setScale(0.45);
    }

    setCountdown(currentPhase.duration);

    intervalRef.current = setInterval(() => {
      setCountdown((prev) => {
        if (prev <= 1) {
          // Move to next phase
          setPhaseIndex((pi) => {
            const next = (pi + 1) % PHASES.length;
            if (next === 0) setCycleCount((c) => c + 1);
            return next;
          });
          return currentPhase.duration;
        }
        return prev - 1;
      });
    }, 1000);

    return () => {
      if (intervalRef.current) clearInterval(intervalRef.current);
    };
    // eslint-disable-next-line react-hooks/exhaustive-deps
  }, [isRunning, phaseIndex]);

  const handleToggle = () => {
    if (isRunning) {
      setIsRunning(false);
      setPhaseIndex(0);
      setCountdown(PHASES[0].duration);
      setCycleCount(0);
    } else {
      setIsRunning(true);
    }
  };

  // Transition duration based on phase
  const transitionDuration = currentPhase.phase === "inhale"
    ? "4s"
    : currentPhase.phase === "exhale"
    ? "8s"
    : "0.3s";

  return (
    <div className="bg-[#1a1a2e] border border-white/10 rounded-2xl p-5">
      {/* Header */}
      <div className="flex justify-between items-start mb-5">
        <div>
          <h3 className="text-white font-semibold text-sm">4-7-8 Breathing</h3>
          <p className="text-white/40 text-xs mt-0.5">
            Calms the nervous system in 5 minutes
          </p>
        </div>
        {cycleCount > 0 && (
          <span className="text-white/40 text-xs bg-white/5 px-2 py-1 rounded-full">
            {cycleCount} cycle{cycleCount !== 1 ? "s" : ""}
          </span>
        )}
      </div>

      {/* Breathing circle — pure CSS */}
      {/* @media prefers-reduced-motion handled via inline style transition */}
      <div className="flex flex-col items-center mb-5">
        <div className="relative w-40 h-40 flex items-center justify-center">
          {/* Outer ring — decorative */}
          <div
            className="absolute inset-0 rounded-full border"
            style={{ borderColor: `${color}20` }}
          />

          {/* Middle ring */}
          <div
            className="absolute rounded-full border"
            style={{
              inset: "12px",
              borderColor: `${color}40`,
              transform: `scale(${scale})`,
              transition: `transform ${transitionDuration} ease-in-out`,
            }}
          />

          {/* Main breathing circle */}
          <div
            className="absolute rounded-full"
            style={{
              inset: "24px",
              background: `radial-gradient(circle at 40% 35%, ${color}60, ${color}20)`,
              boxShadow: isRunning
                ? `0 0 30px ${color}30, 0 0 60px ${color}15`
                : "none",
              transform: `scale(${scale})`,
              transition: `transform ${transitionDuration} ease-in-out, box-shadow 0.5s ease`,
            }}
          />

          {/* Inner text */}
          <div className="relative z-10 text-center">
            {isRunning ? (
              <>
                <span
                  className="text-3xl font-light tabular-nums"
                  style={{ color }}
                >
                  {countdown}
                </span>
              </>
            ) : (
              <span className="text-white/30 text-xs">tap start</span>
            )}
          </div>
        </div>

        {/* Phase label */}
        <div className="text-center mt-3 h-10">
          {isRunning && (
            <>
              <p className="text-white font-medium text-sm" style={{ color }}>
                {currentPhase.label}
              </p>
              {currentPhase.sublabel && (
                <p className="text-white/30 text-xs mt-0.5">
                  {currentPhase.sublabel}
                </p>
              )}
            </>
          )}
        </div>
      </div>

      {/* Phase guide */}
      <div className="flex justify-between mb-5 gap-1">
        {PHASES.filter((p) => p.phase !== "rest").map((p) => (
          <div
            key={p.phase}
            className="flex-1 text-center py-1.5 rounded-lg transition-all"
            style={{
              background:
                isRunning && currentPhase.phase === p.phase
                  ? `${PHASE_COLORS[p.phase]}20`
                  : "rgba(255,255,255,0.03)",
              borderBottom:
                isRunning && currentPhase.phase === p.phase
                  ? `2px solid ${PHASE_COLORS[p.phase]}`
                  : "2px solid transparent",
            }}
          >
            <p className="text-white/60 text-[10px]">{p.label}</p>
            <p className="text-white/40 text-[9px]">{p.duration}s</p>
          </div>
        ))}
      </div>

      {/* Start / Stop button */}
      <button
        onClick={handleToggle}
        className="w-full py-2.5 rounded-xl text-sm font-medium transition-all"
        style={
          isRunning
            ? {
                background: "rgba(255,255,255,0.05)",
                color: "rgba(255,255,255,0.5)",
                border: "1px solid rgba(255,255,255,0.1)",
              }
            : {
                background: "#FF9933",
                color: "white",
              }
        }
      >
        {isRunning ? "Stop" : "Start Breathing Exercise"}
      </button>

      <p className="text-white/20 text-[10px] text-center mt-2">
        @media prefers-reduced-motion respected · No library used
      </p>
    </div>
  );
}