clock/src/components/RollClock.tsx
ysm a4979186a5 统一三种时钟的12/24小时制配置并使用本地存储
- 将12/24小时制配置统一到App组件管理
- 使用localStorage保存用户选择,键名为'clock-is24Hour'
- 默认值为24小时制(true)
- 三种时钟样式共用同一配置,切换时保持一致
- FlipClock和RollClock接收统一props而非各自管理状态

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
2026-03-27 14:13:59 +08:00

129 lines
3.8 KiB
TypeScript

import { useState } from 'react';
import { RollDigit } from './RollDigit';
import './RollClock.css';
interface DateInfo {
dateString: string;
weekday: string;
}
interface RollClockProps {
time: string; // "HH:MM:SS" format
size: number;
dateInfo: DateInfo;
period?: string;
is24Hour: boolean;
onToggleFormat: () => void;
}
export function RollClock({ time, size, dateInfo, period, is24Hour, onToggleFormat }: RollClockProps) {
// Roll direction: 'down' = roll down, 'up' = roll up
const [direction, setDirection] = useState<'down' | 'up'>('down');
// Parse time - it's already in 12h format when period is provided
const [hoursStr, minutes, seconds] = time.split(':');
const displayHours = hoursStr;
const buttonSize = size * 0.25;
const periodSize = size * 0.20;
const dateSize = size * 0.20;
// Toggle direction
const toggleDirection = () => {
setDirection((prev) => (prev === 'down' ? 'up' : 'down'));
};
return (
<div className="roll-clock-outer">
<div className="roll-clock-side-panel">
<button
className="roll-format-toggle"
onClick={onToggleFormat}
>
{is24Hour ? '24H' : '12H'}
</button>
</div>
<div className="roll-clock-wrapper">
<div className="roll-clock-main">
<div className="roll-clock-header">
<div className="roll-clock-header-left">
{period && (
<span
className="roll-clock-period"
style={{ fontSize: `${periodSize}px` }}
>
{period}
</span>
)}
</div>
<span
className="roll-clock-date"
style={{ fontSize: `${dateSize}px` }}
>
{dateInfo.dateString} {dateInfo.weekday}
</span>
</div>
<div className="roll-clock-container">
{/* Hours */}
<div className="roll-clock-digit-group">
<RollDigit value={parseInt(displayHours[0], 10)} size={size} direction={direction} />
<RollDigit value={parseInt(displayHours[1], 10)} size={size} direction={direction} />
</div>
<span
className="roll-clock-separator"
style={{
fontSize: `${size * 0.5}px`,
marginTop: `${-size * 0.08}px`
}}
>
:
</span>
{/* Minutes */}
<div className="roll-clock-digit-group">
<RollDigit value={parseInt(minutes[0], 10)} size={size} direction={direction} />
<RollDigit value={parseInt(minutes[1], 10)} size={size} direction={direction} />
</div>
<span
className="roll-clock-separator"
style={{
fontSize: `${size * 0.5}px`,
marginTop: `${-size * 0.08}px`
}}
>
:
</span>
{/* Seconds */}
<div className="roll-clock-digit-group">
<RollDigit value={parseInt(seconds[0], 10)} size={size} direction={direction} />
<RollDigit value={parseInt(seconds[1], 10)} size={size} direction={direction} />
</div>
</div>
</div>
{/* Direction toggle button */}
<button
className="roll-clock-button"
style={{
width: `${buttonSize}px`,
height: `${buttonSize}px`,
borderRadius: `${buttonSize / 2}px`,
marginLeft: '8px',
}}
onClick={toggleDirection}
>
<span
className="roll-clock-button-text"
style={{ fontSize: `${buttonSize * 0.6}px` }}
>
{direction === 'down' ? '↓' : '↑'}
</span>
</button>
</div>
</div>
);
}