Published on

6 Incredibly Easy Ways To Build A Updated Date Range Picker with Tailwind and Alpine JS With Tailwind CSS Better While Spending Less

Updated Date Range Picker with Tailwind and Alpine JS

What is Tailwind CSS?

Tailwind CSS is a utility-first CSS framework that focuses on creating personalized user interfaces quickly. It can gives you all the building blocks you are able to create personalized designs without having to fight to override irritating opinionated styles. Also, Tailwind CSS is a highly configurable, low-level CSS framework.

The description of Updated Date Range Picker with Tailwind and Alpine JS ui component

This date range picker is based on my previous component (www.tailwindcsscomponent.com/date-range-picker-with-tailwind-and-alpine-js) but this one allows both ends of the range to be selected and also allows the range to be flipped.

Why use Tailwind CSS to make a Updated Date Range Picker with Tailwind and Alpine JS ui component?

  • It can make the building process of Updated Date Range Picker with Tailwind and Alpine JS ui component faster and more easily.
  • Enables building complex responsive layouts and components freely.
  • Minimum lines of CSS code in Updated Date Range Picker with Tailwind and Alpine JS component file.

The preview of Updated Date Range Picker with Tailwind and Alpine JS ui component

Free download of the Updated Date Range Picker with Tailwind and Alpine JS's source code

The source code of Updated Date Range Picker with Tailwind and Alpine JS ui component

<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1">
<link rel="stylesheet" href="https://unpkg.com/[email protected]^2/dist/tailwind.min.css">
<script defer src="https://unpkg.com/[email protected]/dist/cdn.min.js"></script>

<style>
[x-cloak] {
  display: none;
}
</style>

</head>
<body class="h-screen overflow-hidden flex justify-center">
<div class="h-screen w-screen flex justify-center bg-blue-100 ">
<div class="antialiased sans-serif">
	<div x-data="app" x-cloak>
		<div class="container mx-auto px-4 py-2 md:py-10">
			<div>
				<span class="font-bold my-1 text-gray-700 block">Results (would normally be hidden)</span>
				<input type="text" name="date_from" x-model="dateFromYmd">
				<input type="text" name="date_to" x-model="dateToYmd">
				<label for="datepicker" class="font-bold mt-3 mb-1 text-gray-700 block">Select Date Range</label>
				<div class="relative" @keydown.escape="closeDatepicker()" @click.outside="closeDatepicker()">
					<div class="inline-flex items-center border rounded-md mt-3 bg-gray-200">
						<input type="text" @click="endToShow = 'from'; init(); showDatepicker = true" x-model="outputDateFromValue" :class="{'font-semibold': endToShow == 'from' }" class="focus:outline-none border-0 p-2 w-40 rounded-l-md border-r border-gray-300"/>
						<div class="inline-block px-2 h-full">to</div>
						<input type="text" @click="endToShow = 'to'; init(); showDatepicker = true" x-model="outputDateToValue" :class="{'font-semibold': endToShow == 'to' }" class="focus:outline-none border-0 p-2 w-40 rounded-r-md border-l border-gray-300"/>
					</div>
					<div 
						class="bg-white mt-2 rounded-lg shadow p-4 absolute" 
						style="width: 17rem" 
						x-show="showDatepicker"
						x-transition
					>
						<div class="flex flex-col items-center">

							<div class="w-full flex justify-between items-center mb-2">
								<div>
									<span x-text="MONTH_NAMES[month]" class="text-lg font-bold text-gray-800"></span>
									<span x-text="year" class="ml-1 text-lg text-gray-600 font-normal"></span>
								</div>
								<div>
									<button 
										type="button"
										class="transition ease-in-out duration-100 inline-flex cursor-pointer hover:bg-gray-200 p-1 rounded-full" 
										@click="if (month == 0) {year--; month=11;} else {month--;} getNoOfDays()">
										<svg class="h-6 w-6 text-gray-500 inline-flex"  fill="none" viewBox="0 0 24 24" stroke="currentColor">
											<path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M15 19l-7-7 7-7"/>
										</svg>  
									</button>
									<button 
										type="button"
										class="transition ease-in-out duration-100 inline-flex cursor-pointer hover:bg-gray-200 p-1 rounded-full" 
										@click="if (month == 11) {year++; month=0;} else {month++;}; getNoOfDays()">
										<svg class="h-6 w-6 text-gray-500 inline-flex"  fill="none" viewBox="0 0 24 24" stroke="currentColor">
											<path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M9 5l7 7-7 7"/>
										</svg>									  
									</button>
								</div>
							</div>

							<div class="w-full flex flex-wrap mb-3 -mx-1">
								<template x-for="(day, index) in DAYS" :key="index">	
									<div style="width: 14.26%" class="px-1">
										<div
											x-text="day" 
											class="text-gray-800 font-medium text-center text-xs"
										></div>
									</div>
								</template>
							</div>

							<div class="flex flex-wrap -mx-1">
								<template x-for="blankday in blankdays">
									<div 
										style="width: 14.28%"
										class="text-center border p-1 border-transparent text-sm"	
									></div>
								</template>	
								<template x-for="(date, dateIndex) in no_of_days" :key="dateIndex">	
									<div style="width: 14.28%">
										<div
											@click="getDateValue(date, false)"
											@mouseover="getDateValue(date, true)"
											x-text="date"
											class="p-1 cursor-pointer text-center text-sm leading-none leading-loose transition ease-in-out duration-100"
											:class="{'font-bold': isToday(date) == true, 'bg-blue-800 text-white rounded-l-full': isDateFrom(date) == true, 'bg-blue-800 text-white rounded-r-full': isDateTo(date) == true, 'bg-blue-200': isInRange(date) == true }"	
										></div>
									</div>
								</template>
							</div>
						</div>
					</div>

				</div>	 
			</div>

		</div>
	</div>

	<script>
		const MONTH_NAMES = ['January', 'February', 'March', 'April', 'May', 'June', 'July', 'August', 'September', 'October', 'November', 'December'];
		const DAYS = ['Sun', 'Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat'];

		document.addEventListener('alpine:init', () => {
			Alpine.data('app', () => ({
				showDatepicker: false,
				dateFromYmd: '',
				dateToYmd: '',
				outputDateFromValue: '',
				outputDateToValue: '',
				dateFromValue: '',
				dateToValue: '',
				currentDate: null,
				dateFrom: null,
				dateTo: null,
				endToShow: '',
				selecting: false,
				month: '',
				year: '',
				no_of_days: [],
				blankdays: [],
				
				convertFromYmd(dateYmd) {
					const year = Number(dateYmd.substr(0, 4));
					const month = Number(dateYmd.substr(5, 2)) - 1;
					const date = Number(dateYmd.substr(8, 2));
					
					return new Date(year, month, date);
				},
				
				convertToYmd(dateObject) {
					const year = dateObject.getFullYear();
					const month = dateObject.getMonth() + 1;
					const date = dateObject.getDate();
					
					return year + "-" + ('0' + month).slice(-2) + "-" +  ('0' + date).slice(-2);
				},

				init() {
				this.selecting = ( this.endToShow === 'to' && this.dateTo ) || ( this.endToShow === 'from' && this.dateFrom);
					if ( ! this.dateFrom ) {
						if ( this.dateFromYmd ) {
							this.dateFrom = this.convertFromYmd( this.dateFromYmd );
						}
					}
					if ( ! this.dateTo ) {
						if ( this.dateToYmd ) {
							this.dateTo = this.convertFromYmd( this.dateToYmd );
						}
					}
					if ( ! this.dateFrom ) {
						this.dateFrom = this.dateTo;
					}
					if ( ! this.dateTo ) {
						this.dateTo = this.dateFrom;
					}
					if ( this.endToShow === 'from' && this.dateFrom ) {
						this.currentDate = this.dateFrom;
					} else if ( this.endToShow === 'to' && this.dateTo ) {
						this.currentDate = this.dateTo;
					} else {
						this.currentDate = new Date();
					}
					currentMonth = this.currentDate.getMonth();
					currentYear = this.currentDate.getFullYear();
					if ( this.month !== currentMonth || this.year !== currentYear ) {
						this.month = currentMonth;
						this.year = currentYear;
						this.getNoOfDays();
					}
					this.setDateValues();
				},

				isToday(date) {
					const today = new Date();
					const d = new Date(this.year, this.month, date);

					return today.toDateString() === d.toDateString();
				},

				isDateFrom(date) {
					const d = new Date(this.year, this.month, date);
					
					if ( !this.dateFrom ) {
						return false;
					}

					return d.getTime() === this.dateFrom.getTime();
				},

				isDateTo(date) {
					const d = new Date(this.year, this.month, date);

					if ( !this.dateTo ) {
						return false;
					}

					return d.getTime() === this.dateTo.getTime();
				},

				isInRange(date) {
					const d = new Date(this.year, this.month, date);

					return d > this.dateFrom && d < this.dateTo;
				},
			  
				outputDateValues() {
					if (this.dateFrom) {
						this.outputDateFromValue = this.dateFrom.toDateString();
						this.dateFromYmd = this.convertToYmd(this.dateFrom);
					}
					if (this.dateTo) {
						this.outputDateToValue = this.dateTo.toDateString();
						this.dateToYmd = this.convertToYmd(this.dateTo);
					}
				},

				setDateValues() {
					if (this.dateFrom) {
						this.dateFromValue = this.dateFrom.toDateString();
					}
					if (this.dateTo) {
						this.dateToValue = this.dateTo.toDateString();
					}
				},

				getDateValue(date, temp) {
					// if we are in mouse over mode but have not started selecting a range, there is nothing more to do.
					if (temp && !this.selecting) {
						return;
					}
					let selectedDate = new Date(this.year, this.month, date);
					if ( this.endToShow === 'from' ) {
						this.dateFrom = selectedDate;
						if ( ! this.dateTo ) {
							this.dateTo = selectedDate;
						} else if ( selectedDate > this.dateTo ) {
							this.endToShow = 'to';
							this.dateFrom = this.dateTo;
							this.dateTo = selectedDate;
						}
					} else if ( this.endToShow === 'to' ) {
						this.dateTo = selectedDate;
						if ( ! this.dateFrom ) {
							this.dateFrom = selectedDate;
						} else if ( selectedDate < this.dateFrom ) {
							this.endToShow = 'from';
							this.dateTo = this.dateFrom;
							this.dateFrom = selectedDate;
						}
					}
					this.setDateValues();

					if (!temp) {
						if (this.selecting) {
							this.outputDateValues();
							this.closeDatepicker();
						}
						this.selecting = !this.selecting;
					}
				},

				getNoOfDays() {
					let daysInMonth = new Date(this.year, this.month + 1, 0).getDate();

					// find where to start calendar day of week
					let dayOfWeek = new Date(this.year, this.month).getDay();
					let blankdaysArray = [];
					for ( var i=1; i <= dayOfWeek; i++) {
						blankdaysArray.push(i);
					}

					let daysArray = [];
					for ( var i=1; i <= daysInMonth; i++) {
						daysArray.push(i);
					}

					this.blankdays = blankdaysArray;
					this.no_of_days = daysArray;
				},
				
				closeDatepicker() {
					this.endToShow = '';
					this.showDatepicker = false;
				}
			}))
		})
	</script>
</div>
</div>
</body>
</html>

How to make a Updated Date Range Picker with Tailwind and Alpine JS with Tailwind CSS?

Install tailwind css of verion 2.2.19

Use the script html tag to import the script of Tailwind CSS of the version 2.2.19

<script src="https://cdn.tailwindcss.com"></script>

All the unility class needed to make a Updated Date Range Picker with Tailwind and Alpine JS component

  • h-screen
  • overflow-hidden
  • flex
  • w-screen
  • bg-blue-100
  • mx-auto
  • px-4
  • py-2
  • md:py-10
  • my-1
  • text-gray-700
  • block
  • mt-3
  • mb-1
  • relative
  • inline-flex
  • bg-gray-200
  • border-0
  • p-2
  • w-40
  • border-r
  • border-gray-300
  • inline-block
  • px-2
  • h-full
  • border-l
  • bg-white
  • mt-2
  • p-4
  • absolute
  • flex-col
  • w-full
  • mb-2
  • text-lg
  • text-gray-800
  • ml-1
  • text-gray-600
  • hover:bg-gray-200
  • p-1
  • h-6
  • w-6
  • text-gray-500
  • flex-wrap
  • mb-3
  • -mx-1
  • px-1
  • text-center
  • text-xs
  • border-transparent
  • text-sm
  • bg-blue-800
  • text-white
  • bg-blue-200

53 steps to make a Updated Date Range Picker with Tailwind and Alpine JS component with Tailwind CSS

  1. Use h-screen to make an element span the entire height of the viewport.

  2. Use overflow-hidden to clip any content within an element that overflows the bounds of that element.

  3. Use flex to create a block-level flex container.

  4. Use w-screen to make an element span the entire width of the viewport.

  5. Control the background color of an element to blue-100 using the bg-blue-100 utilities.

  6. Control the horizontal margin of an element to auto using the mx-auto utilities.

  7. Control the horizontal padding of an element to 1rem using the px-4 utilities.

  8. Control the vertical padding of an element to 0.5rem using the py-2 utilities.

  9. Control the vertical padding of an element to 2.5rem at only medium screen sizes using the md:py-10 utilities.

  10. Control the vertical margin of an element to 0.25rem using the my-1 utilities.

  11. Control the text color of an element to gray-700 using the text-gray-700 utilities.

  12. Use inline utilities to put the element on its own line and fill its parent.

  13. Control the margin on top side of an element to 0.75rem using the mt-3 utilities.

  14. Control the margin on bottom side of an element to 0.25rem using the mb-1 utilities.

  15. Use relative to position an element according to the normal flow of the document.

  16. Use inline-flex to create an inline flex container that flows with text.

  17. Control the background color of an element to gray-200 using the bg-gray-200 utilities.

  18. Control the border color of an element to 0rem using the border-0 utilities.

  19. Control the padding on all sides of an element to 0.5rem using the p-2 utilities.

  20. Use w-40 to set an element to a fixed width(10rem).

  21. Control the border color of an element to r using the border-r utilities.

  22. Control the border color of an element to gray-300 using the border-gray-300 utilities.

  23. Use inline-block utilities to wrap the element to prevent the text inside from extending beyond its parent.

  24. Control the horizontal padding of an element to 0.5rem using the px-2 utilities.

  25. Use h-full to set an element’s height to 100% of its parent, as long as the parent has a defined height.

  26. Control the border color of an element to l using the border-l utilities.

  27. Control the background color of an element to white using the bg-white utilities.

  28. Control the margin on top side of an element to 0.5rem using the mt-2 utilities.

  29. Control the padding on all sides of an element to 1rem using the p-4 utilities.

  30. Use absolute to position an element outside of the normal flow of the document, causing neighboring elements to act as if the element doesn’t exist.

  31. Use flex to create a block-level flex container.

  32. Use w-full to set an element to a 100% based width.

  33. Control the margin on bottom side of an element to 0.5rem using the mb-2 utilities.

  34. Control the text color of an element to lg using the text-lg utilities.

  35. Control the text color of an element to gray-800 using the text-gray-800 utilities.

  36. Control the margin on left side of an element to 0.25rem using the ml-1 utilities.

  37. Control the text color of an element to gray-600 using the text-gray-600 utilities.

  38. Control the background color of an element to gray-200 using the hover:bg-gray-200 utilities on hover.

  39. Control the padding on all sides of an element to 0.25rem using the p-1 utilities.

  40. Use h-6 to set an element to a fixed height(1.5rem).

  41. Use w-6 to set an element to a fixed width(1.5rem).

  42. Control the text color of an element to gray-500 using the text-gray-500 utilities.

  43. Use flex to create a block-level flex container.

  44. Control the margin on bottom side of an element to 0.75rem using the mb-3 utilities.

  45. Control the horizontal margin of an element to -0.25rem using the -mx-1 utilities.

  46. Control the horizontal padding of an element to 0.25rem using the px-1 utilities.

  47. Control the text color of an element to center using the text-center utilities.

  48. Control the text color of an element to xs using the text-xs utilities.

  49. Control the border color of an element to transparent using the border-transparent utilities.

  50. Control the text color of an element to sm using the text-sm utilities.

  51. Control the background color of an element to blue-800 using the bg-blue-800 utilities.

  52. Control the text color of an element to white using the text-white utilities.

  53. Control the background color of an element to blue-200 using the bg-blue-200 utilities.

Conclusion

The above is a step-by-step tutorial on how to use Tailwind CSS to make a Updated Date Range Picker with Tailwind and Alpine JS components, learn and follow along to implement your own components.