-
Notifications
You must be signed in to change notification settings - Fork 514
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
343 additions
and
9 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,104 @@ | ||
"use client"; | ||
|
||
import { useEffect, useRef, useState } from "react"; | ||
|
||
const CONFIG = { | ||
min: 0, | ||
max: 20000, | ||
}; | ||
|
||
const Character = ({ className, key, value }) => { | ||
return ( | ||
<span data-value={value} className={`character ${className || ""}`}> | ||
<span className="character__track" style={{ "--v": value }}> | ||
<span>9</span> | ||
{[0, 1, 2, 3, 4, 5, 6, 7, 8, 9].map((val, index) => { | ||
return <span key={`${key}--${index}`}>{val}</span>; | ||
})} | ||
<span>0</span> | ||
</span> | ||
</span> | ||
); | ||
}; | ||
|
||
const InnerCounter = ({ currency, pad, value, locale }) => { | ||
const padCount = pad | ||
? CONFIG.max.toFixed(2).toString().length - value.toString().length | ||
: 0; | ||
|
||
const paddedValue = value | ||
.toString() | ||
.padStart(value.toString().length + padCount, "1"); | ||
|
||
let i = 0; | ||
const renderValue = new Intl.NumberFormat(locale, { | ||
style: "currency", | ||
currency, | ||
}) | ||
.format(paddedValue) | ||
.split("") | ||
.map((character, index) => { | ||
if (!Number.isNaN(parseInt(character, 10)) && i < padCount) { | ||
i++; | ||
return "0"; | ||
} | ||
return character; | ||
}) | ||
.join(""); | ||
|
||
return ( | ||
<div className="counter"> | ||
<fieldset> | ||
<h2> | ||
<span className="sr-only">{renderValue}</span> | ||
<span aria-hidden="true" className="characters"> | ||
{renderValue.split("").map((character, index) => { | ||
if (Number.isNaN(parseInt(character, 10))) | ||
return ( | ||
<span | ||
key={index.toString()} | ||
className="character character--symbol" | ||
> | ||
{character} | ||
</span> | ||
); | ||
return ( | ||
<Character | ||
key={index.toString()} | ||
value={character} | ||
className={ | ||
index > renderValue.split("").length - 3 ? "fraction" : "" | ||
} | ||
/> | ||
); | ||
})} | ||
</span> | ||
</h2> | ||
</fieldset> | ||
</div> | ||
); | ||
}; | ||
|
||
export function Counter({ | ||
value: initalValue, | ||
currency, | ||
locale, | ||
lastPeriodAmount = 0, | ||
}) { | ||
const hasRunned = useRef(false); | ||
const [value, setValue] = useState(initalValue - lastPeriodAmount); | ||
|
||
useEffect(() => { | ||
if (!hasRunned.current) { | ||
setValue((prev) => prev + lastPeriodAmount); | ||
hasRunned.current = true; | ||
} | ||
}, [lastPeriodAmount, hasRunned]); | ||
|
||
return ( | ||
<> | ||
<InnerCounter pad value={value} currency={currency} locale={locale} /> | ||
<InnerCounter pad value={value} currency={currency} locale={locale} /> | ||
</> | ||
); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters