This is a simple reusable clock React component to display time in an analogue clock format. See examples below.
This project uses yarn
instead of npm
. To make sure you have the correct version of npm packages please use yarn install
instead of npm install
.
This component has been setup with React CDK and help us to focus on the component development.
- Your component is at
src/index.js
. Open it with an editor. - Start React Storybook with:
npm run storybook
(stories atsrc/stories/index
). - Watch for tests with:
npm run test-watch
(tests atsrc/tests
).
Source files on this project lives under src
directory and you can write your component using ES2015+. (with babel-preset-stage-2).
src/index.js
is the main entrypoint in this component and you should expose a React component class from that module(file).
React Storybook is configured with this component and you can use it to develop your component without a need of an app.
You can try different states of your components very easily. You can write React Storybook stories inside the src/stories/index.js
.
You can run npm run storybook
to start the React Storybook console.
To learn more about React Storybook, visit here.
This component is configured to use Mocha and Enzyme. It's also setup for JSDOM so, you can get the full use of enzyme.
You can write your test cases in src/tests
directory. There are few commands to run your tests, here are they:
npm run testonly
- Run all the testsnpm run test-watch
- Run all the tests and watch for changesnpm test
- Same asnpm run testonly
, but also do linting before testing.
This component uses ESLint for linting and uses a slightly modified version of AirBnb style guide (for JS and React).
Modified rules can be found at .eslintrc
file.
Use these commands to do the linting:
npm run lint
- Run eslintnpm run lintfix
- Run eslint with --fix flag
<Clock />
The clock has a default diameter of 90
, and a default theme using light colours
. If no time prop is passed in, it will automatically show the time on the user's machine, and it will automatically tick each second to render the current time.
You can change the size of the clock - small, big - it really doesn't matter.
<Clock diameter={ 20000 } />
That's probably a bit too big.
<Clock time="09:45" />
This will create a new clock instance, which displays 12:30. If the time prop is passed to the clock, it will freeze the display and won't tick.
Seconds can also be passed in with the time, but it's really not necessary:
<Clock time="09:45:00" />
There is a theme for everyone built into the component (well as long as you like the light
colour theme, or the dark
one.
<Clock theme="light" />
If your inner stylist can't be satisfied with the light
or the dark
themes, feel free to go wild and use your own colours (I can't be held responsible for visual inconvenience caused by 3rd party madness).
<Clock theme={{
bezel: 'red',
face: 'blue',
hour: 'black',
minute: 'yellow',
second: 'rgba(0, 0, 0, 0)' }}
/>
The above example creates a clock instance for people with no taste. Please kindly note how we hide the second hand here. Tricks!
https://robertpataki.github.io/react-clock
- Use RAF instead of setInterval
- See if there is a way to write a unit test to check the timer being stopped on
componentWillUnmount