Web Component

Input Time

Easily create nice looking time input.

Table of contents

Usage

The Input Time component is a web component built from scratch to allow users to easily select time in a 12-hour format, while outputting the selected time in a 24-hour format. To get started, simply use the <uk-input-time> markup in your HTML code.

Capturing values

There are several ways to capture values from the <uk-input-time> component. The simplest approach is to add a name attribute to the component. When you do this, a hidden input field with the specified name will be automatically generated, allowing you to easily capture the selected value on your server.

Prepopulating values

To prepopulate the Input Time component with an existing value, simply pass the value attribute with a 24-hour time format. To prepopulate with current time, just use the now attribute and it will automatically set the current time as the default value.

Size modifiers

There are several size modifiers available. Just add one of the following classes to the cls attribute to make the input smaller or larger.

ClassDescription
.uk-form-xsmallApplies extra small size.
.uk-form-smallApplies small size.
.uk-form-mediumApplies medium size.
.uk-form-largeApplies large size.

Disable input

To prevent user input, add the disabled attribute to the <uk-input-time> element. This will disable all input fields, making it impossible for users to enter or modify time.

Error state

To indicate an error state in the form, simply add the .uk-form-danger class to the cls attribute. This will apply a “danger” state to the component, providing visual feedback to the user.

  • This field is required.

PR

Preventing layout shift

When loading Web Components, there may be a brief delay before the content is fully rendered. This can result in a flash of unstyled content or unprocessed templates. To mitigate this issue, consider setting a predefined height on the parent element to prevent layout shift and ensure a smooth user experience.

Internationalization

The Input Time component supports internationalization through multiple methods with the following priority order (highest to lowest):

  • Component-level i18n (via i18n attribute or script tag)
  • Global component-specific namespace (via <script id="uk-i18n">)
  • Default values

Using the i18n attribute

Using a configuration script

PR

Using global i18n

Place this in your document <head> or before any input-time components:

Available i18n options

KeyDefaultDescription
amAMText for AM period (12-hour clock only)
pmPMText for PM period (12-hour clock only)
hour-labelHourAria-label for hour input field
minute-labelMinuteAria-label for minute input field
meridiem-labelAM/PMAria-label for AM/PM toggle button
time-labelTimeAria-label for the time input group
hour-placeholderHHPlaceholder text for hour input
minute-placeholderMMPlaceholder text for minute input
invalid-timeInvalid time formatError message for invalid time format

Custom classes

The Input Time component supports custom CSS classes through the cls attribute. This allows you to style specific elements within the time input without modifying the component’s internal structure.

Using simple string format

Apply a class to the default element (container):

Using JSON object format

Target specific elements within the input time:

Using configuration script

Available cls targets

TargetDescription
containerThe main wrapper container for the entire component
inputBase class applied to both hour and minute inputs
hour-inputThe hour input field (overrides input for hour field)
minute-inputThe minute input field (overrides input for minute field)
separatorThe colon separator between hour and minute
meridiem-buttonThe AM/PM toggle button (12-hour clock only)
buttonBase button class (fallback for meridiem-button)

Custom inline styles

The Input Time component supports custom inline styles through the stl attribute. This allows you to apply specific CSS styles to individual elements within the time input.

Using simple string format

Apply styles to the default element (container):

Using JSON object format

Target specific elements with custom styles:

Using configuration script

Available stl targets

The stl attribute supports the same targets as the cls attribute. See the Available cls targets table above for a complete list of targetable elements.

Attributes

The following attributes are available for this component:

NameTypeDefaultDescription
autofocusBooleanfalseAutomatically focuses on the hour input field when the component is rendered.
nowBooleanfalseAutomatically sets the current time as the default value.
clockString12hClock format: 12h (with AM/PM) or 24h (without AM/PM).
minStringMinimum allowed time in HH:MM format.
maxStringMaximum allowed time in HH:MM format.
nameStringName attribute for the hidden input field, enabling form submission.
disabledBooleanfalseDisables all input fields, making the entire component read-only.
requiredBooleanfalseMarks the time input as required for form validation.
valueStringPre-populated time value in 24-hour format (HH:MM).
clsStringCustom CSS classes. Can be a simple string or JSON object for targeting specific elements.
stlStringCustom inline styles. Can be a simple string or JSON object for targeting specific elements.
i18nStringInternationalization strings as JSON object or via configuration script.
force-prevent-rerenderBooleanfalsePrevents component rerendering (useful for HTMX or SPA scenarios).

Events

The Input Time component triggers the following event:

NameDescription
uk-input-time:inputFired when the time value changes. Event detail contains {value: string} in 24-hour format.