@kitsuyui/react-timer
v0.2.2
Published
A react component to display a timer
Readme
@kitsuyui/react-timer
Simple timer React component.
Design role
This package provides:
TimerContextProvideras the timer state/controller primitiveDefaultTimeras the replaceable default UI
Consumers can use the default UI, or consume TimerContext directly and render their own interface.
Demo
Storybook: https://react-playground.docs.kitsuyui.com/storybook/
Installation
npm
npm install @kitsuyui/react-timeryarn
yarn add @kitsuyui/react-timerpnpm
pnpm add @kitsuyui/react-timerUsage
import {
TimerContextProvider,
TimerContext,
DefaultTimer,
type TimerContextValue,
} from '@kitsuyui/react-timer'
const Timer = () => {
return (
<TimerContextProvider>
<TimerContext.Consumer>
{(timer: TimerContextValue) => <DefaultTimer {...timer} />}
</TimerContext.Consumer>
</TimerContextProvider>
)
}TimerContextProvider is the primitive state/controller layer.
DefaultTimer is the default UI layered on top of that context value.
You can replace the UI entirely by consuming TimerContext yourself.
export interface TimerValue {
remaining: number
running: boolean
}
export interface TimerActions {
start(): void
stop(): void
toggle(): void
reset(): void
incrementTimerValue(value: number): void
setTimerValue(value: number): void
}
export type TimerContextValue = TimerValue & TimerActionsLicense
MIT
