refactor: subscript clock

This commit is contained in:
Carlos Valente
2025-01-18 21:53:51 +01:00
committed by Carlos Valente
parent 8fac23c078
commit 90700c85e8
2 changed files with 21 additions and 2 deletions
@@ -2,3 +2,7 @@ sup.period {
top: -1em; top: -1em;
font-size: 0.4em; font-size: 0.4em;
} }
.subscript {
font-size: 0.75em;
}
@@ -9,17 +9,32 @@ interface SuperscriptTimeProps {
} }
/** /**
* @description receives a string like 12:00 AM and adds the period part to the superscript * When the timer includes seconds, we want to split it from the rest
*/
function getTimerParts(time: string) {
if (time.length !== 8) {
return [time, ''];
}
return [time.slice(0, 5), time.slice(5)];
}
/**
* Receives a time string and formats it with a subscript or superscript
* @example 12:00 AM -> AM becomes a superscript
* @example 12:00:10 -> the seconds become a subscript
*/ */
export default function SuperscriptTime(props: SuperscriptTimeProps) { export default function SuperscriptTime(props: SuperscriptTimeProps) {
const { time, className, style } = props; const { time, className, style } = props;
// we assume anything after space is a period tag // we assume anything after space is a period tag
const [timeString, period] = time.split(' '); const [timeString, period] = time.split(' ');
const [mainTime, subscript] = getTimerParts(timeString);
return ( return (
<div className={className} style={style}> <div className={className} style={style}>
{timeString} {mainTime}
{subscript && <span className='subscript'>{subscript}</span>}
{period && <sup className='period'>{period}</sup>} {period && <sup className='period'>{period}</sup>}
</div> </div>
); );