timepicker-ui
4.7.0GitHub
timepicker-ui
Getting Started / Quick Start

Quick Start

Get up and running with Timepicker-UI in minutes

Basic Usage

#

Create a simple time picker with default settings:

HTML

index.html
<input id="timepicker" type="text" placeholder="Select time" />

JavaScript

index.ts
import { TimepickerUI } from 'timepicker-ui';
const input = document.querySelector('#timepicker');
const picker = new TimepickerUI(input);
picker.create();

TimepickerUI is a named export, and also the default export, so import TimepickerUI from 'timepicker-ui' works too.

With Options

#

Customize the picker with options (v4.0.0 grouped structure):

index.ts
const picker = new TimepickerUI(input, {
ui: {
theme: 'dark',
animation: true,
backdrop: true
},
clock: {
type: '24h'
},
callbacks: {
onConfirm: (data) => {
console.log('Selected time:', data);
}
}
});
picker.create();

Framework Integration

#

React

Important for React

Use useRef to store the picker instance and avoid re-initialization on every render. If you pass options as an object, memoize them with useMemo to prevent unnecessary re-renders. Render the input inside <div className="tp-ui">, so unmounting the component does not throw. Server Rendering explains why.
index.tsx
import { useEffect, useRef, useMemo } from 'react';
import { TimepickerUI } from 'timepicker-ui';
export default function TimePicker() {
const inputRef = useRef<HTMLInputElement>(null);
const pickerRef = useRef<TimepickerUI | null>(null);
// Memoize options to prevent re-initialization
const options = useMemo(() => ({
callbacks: {
onConfirm: (data) => console.log('Time:', data)
}
}), []);
useEffect(() => {
if (!inputRef.current) return;
// Create instance only once
pickerRef.current = new TimepickerUI(inputRef.current, options);
pickerRef.current.create();
// Cleanup on unmount
return () => {
pickerRef.current?.destroy();
};
}, []); // Empty deps - create only once
return (
<div className="tp-ui">
<input ref={inputRef} type="text" />
</div>
);
}

Vue

index.vue
<template>
<input ref="timepickerInput" type="text" />
</template>
<script setup>
import { ref, onMounted, onUnmounted } from 'vue';
import { TimepickerUI } from 'timepicker-ui';
const timepickerInput = ref(null);
let picker = null; // Store instance outside reactive state
onMounted(() => {
picker = new TimepickerUI(timepickerInput.value);
picker.create();
});
onUnmounted(() => {
// Always cleanup to prevent memory leaks
picker?.destroy();
picker = null;
});
</script>

Angular

index.ts
import { Component, ElementRef, ViewChild, AfterViewInit, OnDestroy } from '@angular/core';
import { TimepickerUI } from 'timepicker-ui';
@Component({
selector: 'app-timepicker',
template: '<input #timepicker type="text" />'
})
export class TimepickerComponent implements AfterViewInit, OnDestroy {
@ViewChild('timepicker') input: ElementRef;
private picker: TimepickerUI;
ngAfterViewInit() {
this.picker = new TimepickerUI(this.input.nativeElement);
this.picker.create();
}
ngOnDestroy() {
this.picker?.destroy();
}
}

Svelte

index.svelte
<script>
import { onMount } from 'svelte';
import { TimepickerUI } from 'timepicker-ui';
import 'timepicker-ui/index.css';
let input;
onMount(() => {
const picker = new TimepickerUI(input);
picker.create();
return () => picker.destroy();
});
</script>
<input bind:this={input} type="text" />

For SvelteKit, see Server Rendering.

Pro tip

Always call destroy() when unmounting to prevent memory leaks. In React, use useMemo for options objects to avoid re-initialization on every render.
Esc