npmshadcn · Web componentshadcn · React + TailwindManualnpminstall@purpurabog/uinpxshadcn@4add@kanok-ui/time-pickernpxshadcn@4add@kanok-ui/tw-time-picker# Web component# 1. Install the dependenciesnpminstalllit@^3.3.0# 2. Copy each file's "content" from# https://cdn.jsdelivr.net/npm/@purpurabog/ui@2.3.0/registry/time-picker.json# to its target:# components/ui/primitives/time-picker/time-picker-select.ts# components/ui/primitives/time-picker/time-picker-shared.ts# components/ui/primitives/time-picker/time-picker-skeleton.ts# components/ui/primitives/time-picker/time-picker.ts# 3. Do the same for the items it needs (registry/<name>.json):# core-date, shared-element, shared-field, shared-form-control, shared-localize, shared-skeleton, shared-slots, tokens# React + Tailwind# 1. Install the dependenciesnpminstall@purpurabog/ui@^2.3.0# 2. Copy each file's "content" from# https://cdn.jsdelivr.net/npm/@purpurabog/ui@2.3.0/registry/tw-time-picker.json# to its target:# components/ui/react-tw/time-picker.tsx# 3. Do the same for the items it needs (registry/<name>.json):# tw-field, tw-i18n, tw-skeleton, tw-theme, tw-utils
npm gives you every component from the package. shadcn copies the source into your app instead: a copy pinned to this version that you can change. The shadcn commands need the @kanok-ui registry in your components.json once, see Installation.
By default the box takes 12-hour time, h:mm or hh:mm (9:30, 09:30, 12:45), and at most 5 characters. Carbon's own pattern rejects a leading zero; this one accepts it, so typing what the hh:mm placeholder shows works.
For 24-hour time, set your own pattern and leave out the AM / PM select:
maxlength (maxLength in React) changes the length limit; placeholder the hint.
Start with a value
To edit a time that is already saved, set value on the picker and on each select. A select without value starts on its <option selected>, else its first option, as a native select does.
size: sm 32px, md 40px (the default, as in Carbon), lg 48px. The selects take the picker's size. The prop has the same name in React and React + Tailwind.
HTMLReactReact + Tailwind<ui-time-pickersize="sm"label="Small (sm)"value="9:30">
<ui-time-picker-selectlabel="AM or PM">
<optionvalue="AM">AM</option>
<optionvalue="PM">PM</option>
</ui-time-picker-select>
</ui-time-picker>
<ui-time-pickersize="lg"label="Large (lg)"value="9:30">
<ui-time-picker-selectlabel="AM or PM">
<optionvalue="AM">AM</option>
<optionvalue="PM">PM</option>
</ui-time-picker-select>
</ui-time-picker><UiTimePickersize="sm"label="Small (sm)"value="9:30">
<UiTimePickerSelectlabel="AM or PM">
<optionvalue="AM">AM</option>
<optionvalue="PM">PM</option>
</UiTimePickerSelect>
</UiTimePicker>
<UiTimePickersize="lg"label="Large (lg)"value="9:30">
<UiTimePickerSelectlabel="AM or PM">
<optionvalue="AM">AM</option>
<optionvalue="PM">PM</option>
</UiTimePickerSelect>
</UiTimePicker><TimePickersize="sm"label="Small (sm)"defaultValue="9:30">
<TimePickerSelectlabel="AM or PM">
<optionvalue="AM">AM</option>
<optionvalue="PM">PM</option>
</TimePickerSelect>
</TimePicker>
<TimePickersize="lg"label="Large (lg)"defaultValue="9:30">
<TimePickerSelectlabel="AM or PM">
<optionvalue="AM">AM</option>
<optionvalue="PM">PM</option>
</TimePickerSelect>
</TimePicker>
Fluid
fluid, or a fluid form around it (<ui-fluid-form>, FluidForm in React + Tailwind), gives Carbon's fluid time picker: the box and each select as equal 64px columns with their labels inside. The prop has the same name in React and React + Tailwind.
In the Kanok design, fields are filled by default: a soft grey fill, no edge. Put data-field-variant="outlined" on an element around the field (a <form>, or <html> for the whole app) for a 1px edge on no fill; focus is still the soft blue halo. The attribute does nothing in the Carbon design, so the example below sits inside data-design="kanok". See Kanok design for the details.
The web component checks the text against pattern after the user leaves the box or tries to submit, and shows the error under it. invalid-text replaces the browser's own message ("Please match the requested format"). Set invalid for an error from your own validation, or warn with warn-text for a warning that doesn't block submit:
HTMLReactReact + Tailwind<ui-time-pickerlabel="Pick-up"value="13:30"invalidinvalid-text="The shop closes at 12:00."></ui-time-picker>
<ui-time-pickerlabel="Site visit"value="7:00"warnwarn-text="Before office hours. Someone must open the gate.">
<ui-time-picker-selectlabel="AM or PM">
<optionvalue="AM">AM</option>
<optionvalue="PM">PM</option>
</ui-time-picker-select>
</ui-time-picker><UiTimePickerlabel="Pick-up"value="13:30"invalidinvalidText="The shop closes at 12:00." />
<UiTimePickerlabel="Site visit"value="7:00"warnwarnText="Before office hours. Someone must open the gate.">
<UiTimePickerSelectlabel="AM or PM">
<optionvalue="AM">AM</option>
<optionvalue="PM">PM</option>
</UiTimePickerSelect>
</UiTimePicker><TimePickerlabel="Pick-up"defaultValue="13:30"invalidinvalidText="The shop closes at 12:00." />
<TimePickerlabel="Site visit"defaultValue="7:00"warnwarnText="Before office hours. Someone must open the gate.">
<TimePickerSelectlabel="AM or PM">
<optionvalue="AM">AM</option>
<optionvalue="PM">PM</option>
</TimePickerSelect>
</TimePicker>
React + Tailwind follows Carbon React: pattern still takes part in the form's validation, but no message shows by itself. Set invalid and invalidText when your check fails. More in Forms and validation.
Read-only and disabled
readonly keeps every value and submits it: the box can't be edited, and a select still takes focus but doesn't open or change. disabled disables the box and every select inside.
HTMLReactReact + Tailwind<ui-time-pickerlabel="Start time"value="9:30"readonly>
<ui-time-picker-selectlabel="AM or PM">
<optionvalue="AM">AM</option>
<optionvalue="PM">PM</option>
</ui-time-picker-select>
</ui-time-picker>
<ui-time-pickerlabel="Start time"value="9:30"disabled>
<ui-time-picker-selectlabel="AM or PM">
<optionvalue="AM">AM</option>
<optionvalue="PM">PM</option>
</ui-time-picker-select>
</ui-time-picker><UiTimePickerlabel="Start time"value="9:30"readonly>
<UiTimePickerSelectlabel="AM or PM">
<optionvalue="AM">AM</option>
<optionvalue="PM">PM</option>
</UiTimePickerSelect>
</UiTimePicker>
<UiTimePickerlabel="Start time"value="9:30"disabled>
<UiTimePickerSelectlabel="AM or PM">
<optionvalue="AM">AM</option>
<optionvalue="PM">PM</option>
</UiTimePickerSelect>
</UiTimePicker><TimePickerlabel="Start time"defaultValue="9:30"readOnly>
<TimePickerSelectlabel="AM or PM">
<optionvalue="AM">AM</option>
<optionvalue="PM">PM</option>
</TimePickerSelect>
</TimePicker>
<TimePickerlabel="Start time"defaultValue="9:30"disabled>
<TimePickerSelectlabel="AM or PM">
<optionvalue="AM">AM</option>
<optionvalue="PM">PM</option>
</TimePickerSelect>
</TimePicker>
The web component and its React wrapper spell it readonly; React + Tailwind uses readOnly.
What the form gets
Each part submits on its own: the time box under its name and each select under its own name. The time is the text as typed (9:30 or 09:30), and the select values are your <option> values. Nothing is converted: the time zone select is a plain choice, with no time zone arithmetic behind it. required blocks the submit while the box is empty.
The web component fires input while typing and change on commit; each select fires change. In React the wrapper props are onInput / onChange; the Tailwind TimePicker passes onChange to its <input>, as React does.
Your own rules, such as a closing time after the opening time, go through invalid and invalid-text: check on change and set them. Here both boxes take 24-hour time.
HTMLReactReact + Tailwind<divstyle="display: flex; flex-wrap: wrap; gap: 16px; align-items: flex-start">
<ui-time-pickerid="time-picker-opens"label="Branch opens"value="08:30"pattern="([01]?[0-9]|2[0-3]):[0-5][0-9]"></ui-time-picker>
<ui-time-pickerid="time-picker-closes"label="Branch closes"value="17:30"pattern="([01]?[0-9]|2[0-3]):[0-5][0-9]"></ui-time-picker>
</div>
<scripttype="module">
const opens = document.getElementById('time-picker-opens');
const closes = document.getElementById('time-picker-closes');
constminutes= (text) => {
const [h, m] = text.split(':').map(Number);
return h *60+ m;
};
constcheck= () => {
const wrong =minutes(closes.value) <=minutes(opens.value);
closes.invalid = wrong;
closes.invalidText = wrong ?'Must be after the opening time.':'';
};
opens.addEventListener('change', check);
closes.addEventListener('change', check);
</script>const H24 ='([01]?[0-9]|2[0-3]):[0-5][0-9]';
constminutes= (text:string) => {
const [h, m] = text.split(':').map(Number);
return h *60+ m;
};
const wrong =minutes(closes) <=minutes(opens);
<UiTimePickerlabel="Branch opens"value={opens} pattern={H24}
onChange={(e) =>setOpens((e.target asTimePickerElement).value)} />
<UiTimePickerlabel="Branch closes"value={closes} pattern={H24}
onChange={(e) =>setCloses((e.target asTimePickerElement).value)}
invalid={wrong} invalidText={wrong ?'Must be after the opening time.':''} />// H24 and minutes as in the React tabconst wrong =minutes(closes) <=minutes(opens);
<TimePickerlabel="Branch opens"value={opens} pattern={H24} onChange={(e) =>setOpens(e.target.value)} />
<TimePickerlabel="Branch closes"value={closes} pattern={H24} onChange={(e) =>setCloses(e.target.value)}
invalid={wrong} invalidText={wrong ?'Must be after the opening time.':''} />
Skeleton
While the page loads its data, show <ui-time-picker-skeleton> in the picker's place. Carbon has only the fluid time picker skeleton, so this is that one: the box, the AM / PM select and the time zone select. is-only-two (isOnlyTwo in React + Tailwind) draws two equal parts instead. It has its own import, @purpurabog/ui/time-picker-skeleton, and is hidden from screen readers.
Generated from the package. In React, pass a property as a prop of the same name; events are props too (the React column).
<ui-time-picker> · React UiTimePicker
Attributes
Attribute
Property
Type
Default
Description
max-length
Deprecated: use maxlength
label
label
string
''
hide-label
hideLabel
boolean
false
value
value
string
''
name
name
string
''
placeholder
placeholder
string
'hh:mm'
pattern
pattern
String.raw`(1[012]|0?[1-9]):[0-5][0-9](\s)?`
12-hour time with an optional trailing space. Carbon's default rejects a leading zero, so "09:30" typed as the hh:mm placeholder shows would fail; this one accepts "9:30" and "09:30".
maxlength
maxLength
number
5
Like <input maxlength> (Carbon maxLength); the max-length attribute of 0.8 and earlier is still read.
size
size
TimePickerSize
'md'
disabled
disabled
boolean
false
readonly
readonly
boolean
false
fluid
fluid
boolean
false
Carbon FluidTimePicker: equal 64px columns with labels inside, an error / warning inside the box. <ui-fluid-form> sets it for every field inside.
required
required
boolean
false
invalid
invalid
boolean
false
invalid-text
invalidText
string
''
warn
warn
boolean
false
warn-text
warnText
string
''
Properties
Property
Type
Description
willValidate
(read-only) Whether the field takes part in constraint validation (not while disabled), as natively.
labels
(read-only) The <label>s that point at this field.
Methods
Method
Description
setCustomValidity(message)
Like <input>.setCustomValidity(): a non-empty message makes the field invalid (and is shown as its error once touched) until cleared with ''.
Events
Event
React
Description
change
onChange
Slots
Slot
Description
(default)
<ui-time-picker-select> elements
label
Rich label
invalid-text
Rich error text
warn-text
Rich warning text
CSS parts
Part
Description
label
The label
base
Row with the input and the selects
input
The text input
error-text
Error text
invalid-text
Same line as error-text (named after the invalid-text attribute / slot)
warning-text
Warning text
warn-text
Same line as warning-text (named after the warn-text attribute / slot)
CSS custom properties
Custom property
Default
Description
--ui-field-border
Border on every side of the box (default none: Carbon's underline only; Kanok: a 1px outline)
--ui-field-disabled-border-color
Border colour when disabled (default: the underline transparent)
--ui-field-readonly-border-color
Border colour when read-only (default: a hairline underline)
--ui-field-radius
Corner radius of the box (default 0; Kanok: 6px)
--ui-field-background
Fill of the box (default --ui-surface-1; Kanok: transparent)
--ui-label-font
Font of the label (default --ui-font-caption; Kanok: 14px semibold)
--ui-label-color
Colour of the label (default --ui-ink-muted; Kanok: --ui-ink)
--ui-field-readonly-background
The box's fill when read-only (default transparent; Kanok: a faint ink tint, so read-only reads as set and never weaker than disabled)