Clock
The Clock component provides a simple and customizable way to display the current date and time on your web page. It automatically updates and can be configured to show various date and time formats.
Dependencies
Section titled “Dependencies”- Datetime module (imported from ”../../datetime/index.js”)
Basic Usage
Section titled “Basic Usage”<!-- Basic clock with default settings --><div data-role="clock"></div>
<!-- Clock showing only time --><div data-role="clock" data-show-date="false"></div>
<!-- Clock showing only date --><div data-role="clock" data-show-time="false"></div>
Display Modes
Section titled “Display Modes”<!-- Clock in row mode (default) --><div data-role="clock" data-show="row"></div>
<!-- Clock in column mode --><div data-role="clock" data-show="column"></div>
Custom Formats
Section titled “Custom Formats”<!-- Clock with custom date format --><div data-role="clock" data-date-format="YYYY-MM-DD"></div>
<!-- Clock with custom time format --><div data-role="clock" data-time-format="HH:mm:ss"></div>
<!-- Clock with custom date and time formats --><div data-role="clock" data-date-format="dddd, MMMM D, YYYY" data-time-format="h:mm:ss a"></div>
Plugin Parameters
Section titled “Plugin Parameters”Parameter | Type | Default | Description |
---|---|---|---|
clockDeferred | number | 0 | Delay before initialization (in milliseconds) |
show | string | ”row” | Display mode: “row” (horizontal) or “column” (vertical) |
showTime | boolean | true | Whether to display the time |
showDate | boolean | true | Whether to display the date |
dateFormat | string | ”DD.MM.YYYY” | Format for displaying the date |
timeFormat | string | ”HH:mm” | Format for displaying the time |
divider | string | ” “ | HTML content to use as divider between date and time |
twoLines | boolean | false | Whether to display date and time on separate lines |
Events
Section titled “Events”Event | Description |
---|---|
onTick | Triggered every 500ms when the clock updates |
onSecond | Triggered every second |
onClockCreate | Triggered when the clock component is created |
API Methods
Section titled “API Methods”changeAttribute(attr, val)
Section titled “changeAttribute(attr, val)”Changes a component attribute dynamically.
// Get the clock componentconst clock = Metro.getPlugin('#myClock', 'clock');
// Change the date formatclock.changeAttribute('data-date-format', 'YYYY-MM-DD');
// Change the time formatclock.changeAttribute('data-time-format', 'HH:mm:ss');
// Show/hide dateclock.changeAttribute('data-show-date', 'true');
// Show/hide timeclock.changeAttribute('data-show-time', 'false');
// Change the dividerclock.changeAttribute('data-divider', ' | ');
// Enable/disable two lines modeclock.changeAttribute('data-two-lines', 'true');
destroy()
Section titled “destroy()”Removes the clock component and cleans up resources.
// Get the clock componentconst clock = Metro.getPlugin('#myClock', 'clock');
// Destroy the componentclock.destroy();
Styling with CSS Variables
Section titled “Styling with CSS Variables”Variable | Default (Light) | Dark Mode | Description |
---|---|---|---|
--clock-background | transparent | transparent | Background color of the clock |
--clock-color | #191919 | #dbdfe7 | Text color of the clock |
--clock-font-size | 14px | 14px | Font size of the clock text |
Example of Custom Styling
Section titled “Example of Custom Styling”/* Custom styling for a specific clock */#myClock { --clock-background: #f0f8ff; --clock-color: #0066cc; --clock-font-size: 18px;}
Available CSS Classes
Section titled “Available CSS Classes”Base Classes
Section titled “Base Classes”.clock
- The main clock container.clock.show-column
- Clock with vertical layout
Element Classes
Section titled “Element Classes”.clock-time
- The time display element.clock-date
- The date display element
Best Practices
Section titled “Best Practices”- Use appropriate date and time formats for your target audience’s locale
- Consider using the column display mode for narrow containers
- For better accessibility, ensure sufficient contrast between the clock text and background
- Use the
onTick
event for custom functionality that needs to run with clock updates