Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,10 @@ const MyDiceApp = () => {

const reactDice = useRef<ReactDiceRef>(null)

const onRoll = () => {
console.log("Rolling...")
}

const rollDone = (totalValue: number, values: number[]) => {
console.log('individual die values array:', values)
console.log('total dice value:', totalValue)
Expand All @@ -37,6 +41,7 @@ const MyDiceApp = () => {
numDice={2}
ref={reactDice}
rollDone={rollDone}
onRoll={onRoll}
/>
)

Expand All @@ -59,6 +64,7 @@ const MyDiceApp = () => {
| **`outline`** | `{Bool}` | `false` | Show a 1px outline for each face of the die |
| **`outlineColor`** | `{String}` | `#000000` | hex color code for outline color if outline is `true` |
| **`rollDone`** | `{String/Function}` | `null` | callback returns total & individual values from dice roll |
| **`onRoll`** | `{Function}` | `null` | callback triggered when die rolls |
| **`rollTime`** | `{Number}` | `2` | time in seconds for the roll animation |

## Provided functions
Expand Down
1 change: 1 addition & 0 deletions lib/DiceContainer.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ export type DieContainerRef = {

export interface DiceContainerProps extends Omit<DieProps, 'onRollDone'> {
numDice: number
onRoll?: () => void
totalCb: (newTotalValue: number, newDiceValues: number[]) => void
}

Expand Down
7 changes: 6 additions & 1 deletion lib/Die.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ export interface DieProps {
dotColor?: string
faceColor?: string
margin?: number
onRoll?: () => void
onRollDone: (value: number) => void
outline?: boolean
outlineColor?: string
Expand All @@ -32,6 +33,7 @@ const Die = forwardRef<DieRef, DieProps>(
dotColor = '#1dff00',
faceColor = '#ff00ac',
margin = 15,
onRoll,
onRollDone,
outline = false,
outlineColor = '#000000',
Expand All @@ -41,7 +43,7 @@ const Die = forwardRef<DieRef, DieProps>(
ref
): JSX.Element => {
const dieRef = useRef<HTMLDivElement>(null)

useImperativeHandle(ref, () => ({
getValue: () => {
return dieValue
Expand All @@ -62,6 +64,9 @@ const Die = forwardRef<DieRef, DieProps>(
dieRef.current && (dieRef.current.className = `die`)
void dieRef.current?.offsetWidth
let roll = disableRandom ? dieValue : value || getRandomInt()
if (onRoll) {
onRoll()
}
dieRef.current?.classList.add(`roll${roll}`)
setTimeout(() => {
setHasRolled(true)
Expand Down
4 changes: 2 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "react-dice-complete",
"version": "2.2.0",
"version": "2.3.0",
"description": "Create dice for any game and roll for totals. Complete UI and Logic",
"main": "dist/react-dice-complete.js",
"files": [
Expand Down
6 changes: 6 additions & 0 deletions src/TestApp.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,10 @@ const TestApp = () => {
setDiceTotal(value)
}

const handleOnRoll = () => {
console.log('Rolling...');
}

const rollAll = () => {
reactDice.current?.rollAll()
setRolling(true)
Expand Down Expand Up @@ -54,6 +58,7 @@ const TestApp = () => {
outline={outline}
outlineColor={outlineColor}
ref={reactDice}
onRoll={handleOnRoll}
rollDone={rollDone}
rollTime={rollTime}
sides={sides}
Expand All @@ -71,6 +76,7 @@ const TestApp = () => {
numDice,
outline,
outlineColor,
handleOnRoll,
rollDone,
rollTime,
sides,
Expand Down