FlipClock.js Documentation
repository·master·Indexed 25 days ago
https://github.com/objectivehtml/flipclockA themeable, type-safe library for creating clocks, timers, counters, and flipboards. Version 1.0.0 features a modular architecture consisting of Clock, Face, FaceValue, and Theme components. It includes specialized components like Alphanumeric for text-based flip animations, ElapsedTime for durations, and CounterCountdown for timers. The library supports custom theme creation using SolidJS and provides a built-in CSS-in-JS solution for styling.
What's inside FlipClock.js
- FlipClock.js is an open-source TypeScript library used for building clocks, counters, scoreboards, and flipboards. It provides a well-tested, strongly typed, and flexible API designed for ease of use.
Use the Sequencer to manage DigitizedValues changes
masterTheSequenceris used to increment or decrement changes toDigitizedValuesusing a specifiedCharset. It includes the ability to automatically stop after a defined set of changes based on a stop predicate.Use the Timer class for efficient ticking
masterTimeris a high-performance utility based onwindow.requestAnimationFramerather thansetInterval. It is designed to be fast, efficient, and non-blocking, allowing for ticks at any defined interval.Understand FlipClock core concepts
masterFlipClock.js is built on a modular architecture consisting of four primary components:
- Clock: The main
FlipClockinstance. It acts as the central controller, housing the internal timer and managing the overall interface and functions. - Face: Defines the behavior and functionality (e.g., 12-hour vs 24-hour format, stopwatch, or alphanumeric displays).
- FaceValue: Responsible for digitizing the data used by the
Face. Each face type implements its ownFaceValuelogic. - Theme: Handles the visual rendering. A
Thememanages the DOM structure, markup, animations, and CSS for the clock.
- Clock: The main
Customize FlipClock styling via CSS and Themes
masterYou can style your FlipClock using several methods:
- Themes: Since a
Themecontrols the DOM markup and animations, you can create a newThemeto completely change how the clock is rendered. - CSS-in-JS: FlipClock.js provides a built-in CSS-in-JS solution for creating new themes.
- CSS Overrides: You can extend existing CSS or use traditional CSS to override the default styles of a theme.
- Themes: Since a
Start, stop, and toggle the FlipClock
masterThe clock starts automatically by default. If
autoStartis set tofalsein your configuration, you must manage the clock state manually usingstart(),stop(), ortoggle(). These methods accept an optional callback function that executes when the state change occurs.import { flipClock, clock, theme, css } from 'flipclock'; const clock = flipClock({ // your options here... }); // Start the clock clock.start(() => { console.log('The clock started!') }); // Stop the clock clock.stop(() => { console.log('The clock stopped!') }); // Toggle starts the clock if stopped, and stops if started. clock.toggle(() => { console.log(`Status:`, clock.timer.isStopped) });Use the Counter component
masterThe
Countercomponent is used to increment or decrement a numerical face by one or more steps at a time. It is a Vue component designed for use within a<script setup>environment.<script setup lang="ts"> import Counter from '../components/Counter.vue'; </script> <template> <Counter /> </template>Listen to FlipClock lifecycle events
masterYou can subscribe to specific lifecycle events using the
.on(eventName, callback)method or.once(eventName, callback)for a single execution. The callback receives theFlipClockinstance as its argument. Common events includeafterMount,beforeInterval, andafterInterval.import { flipClock, counter, theme, css } from 'flipclock'; const clock = flipClock({ parent: document.querySelector('#clock')!, face: counter(0), theme: theme({ css: css() }) }); clock.on('afterMount', (instance: FlipClock) => { console.log('After the clock mounts.') }); clock.on('beforeInterval', (instance: FlipClock) => { console.log('Before the timer ticks.') }); clock.on('afterInterval', (instance: FlipClock) => { console.log('After the timer ticks.') }); clock.once('afterInterval', (instance: FlipClock) => { console.log('Called once after the timer ticks.') });Create a custom theme for FlipClock.js
masterThemes define how the clock faces are rendered and allow you to control the markup. FlipClock.js uses SolidJS for reactivity and rendering. A theme is an object that must implement theThemetype. The only required method isrender. Themes can also tap into the Event Hooks lifecycle.Extend an existing CSS declaration
masterTo build upon an existing CSS declaration, use the
.extend()method on the object returned bycss(). The callback function receivespropswhich you can use to apply conditional or dynamic overrides.import { css } from 'flipclock'; const declaration = css({ animationDuration: '100ms', fontSize: '3rem' }).extend((props) => ({ // your CSS overrides here. }));Understand the Face interface in FlipClock.js
masterIn FlipClock.js, aFacedefines the specific functionality of a clock (e.g., a stopwatch vs. a lunar clock). While each face has unique options and methods, they all implement a minimal unified API. To be considered a valid face, an implementation must provide thefaceValue()andinterval()methods.Install flipclock via CDN
masterTo useflipclockdirectly in the browser without a build step, include the UMD build via JSDelivr or Unpkg.