feat: Implement CSS-only scrolling date picker with infinite scroll

This commit is contained in:
Kerby Martino authored and Kerby Martino (aider) committed 2025-05-11 05:41:04 +08:00
1 parent 9175335d05
commit b7f956c551
2 files changed
+72 -74

No files matched your search

+67 -70
View File
@@ -1,15 +1,4 @@
{% comment %} <!-- Scrolling Date Picker Template with Snapping and Infinite Scroll -->
Pure CSS Scrolling Date Picker Visual Mock-up
Usage:
{% include calendar_input.liquid
id="unique_calendar_id" (required for multiple instances)
label="Select Date" (optional, defaults to "Select Date")
placeholder="YYYY-MM-DD" (optional, defaults to "YYYY-MM-DD")
%}
This component is a visual mock-up and does not provide actual date selection functionality.
The input field will not be updated by this picker.
{% endcomment %}
{% assign current_year = "now" | date: "%Y" | plus: 0 %} {% assign current_year = "now" | date: "%Y" | plus: 0 %}
{% assign future_year_limit = current_year | plus: 10 %} {% assign future_year_limit = current_year | plus: 10 %}
{% assign years = "" %} {% assign years = "" %}
@@ -29,13 +18,13 @@ The input field will not be updated by this picker.
<label for="calendar-input-{{ include.id | default: 'default' }}" class="block text-xs font-medium text-gray-700 mb-1">{{ include.label | default: "Select Date" }}</label> <label for="calendar-input-{{ include.id | default: 'default' }}" class="block text-xs font-medium text-gray-700 mb-1">{{ include.label | default: "Select Date" }}</label>
<div class="relative"> <div class="relative">
<input <input
type="text" type="text"
id="calendar-input-{{ include.id | default: 'default' }}" id="calendar-input-{{ include.id | default: 'default' }}"
name="{{ include.id | default: 'default' }}_date_display" name="{{ include.id | default: 'default' }}_date_display"
readonly readonly
placeholder="{{ include.placeholder | default: 'YYYY-MM-DD' }}" placeholder="{{ include.placeholder | default: 'YYYY-MM-DD' }}"
class="w-full px-3 py-2 pr-10 border border-gray-300 rounded-sm focus:outline-none focus:border-indigo-700 focus:ring-1 focus:ring-indigo-700 text-sm cursor-pointer bg-white peer" class="w-full px-3 py-2 pr-10 border border-gray-300 rounded-sm focus:outline-none focus:border-indigo-700 focus:ring-1 focus:ring-indigo-700 text-sm cursor-pointer bg-white peer"
aria-label="{{ include.label | default: 'Select Date' }}" aria-label="{{ include.label | default: 'Select Date' }}"
> >
<div class="absolute inset-y-0 right-0 pr-3 flex items-center pointer-events-none"> <div class="absolute inset-y-0 right-0 pr-3 flex items-center pointer-events-none">
<svg class="h-5 w-5 text-gray-400" xmlns="http://www.w3.org/2000/svg" viewBox="0 0 20 20" fill="currentColor"> <svg class="h-5 w-5 text-gray-400" xmlns="http://www.w3.org/2000/svg" viewBox="0 0 20 20" fill="currentColor">
@@ -44,57 +33,54 @@ The input field will not be updated by this picker.
</div> </div>
</div> </div>
<!-- CSS-only "Dropdown" - Appears on input focus or hover of the parent div -->
<!-- For a pure CSS dropdown, we often rely on :focus-within or :hover on a parent.
Here, using :focus on the input (peer) to show the picker.
Alternatively, wrap input and picker in a div and use group-hover.
For simplicity, this example shows it on input focus.
-->
<div class="absolute z-20 mt-1 w-full sm:w-80 bg-white shadow-lg rounded-base border border-gray-300 p-3 <div class="absolute z-20 mt-1 w-full sm:w-80 bg-white shadow-lg rounded-base border border-gray-300 p-3
opacity-0 pointer-events-none peer-focus:opacity-100 peer-focus:pointer-events-auto opacity-0 pointer-events-none peer-focus:opacity-100 peer-focus:pointer-events-auto
group-hover:opacity-100 group-hover:pointer-events-auto group-hover:opacity-100 group-hover:pointer-events-auto
transition-opacity duration-150 ease-in-out"> transition-opacity duration-150 ease-in-out">
<div class="flex justify-between items-stretch h-48 relative"> <div class="flex justify-between items-stretch h-48 relative">
<!-- Selection Indicator Line -->
<div class="absolute top-1/2 left-0 right-0 h-px bg-indigo-300 -translate-y-1/2 z-0"></div> <div class="absolute top-1/2 left-0 right-0 h-px bg-indigo-300 -translate-y-1/2 z-0"></div>
<div class="absolute top-1/2 left-0 right-0 h-8 bg-indigo-50 -translate-y-1/2 z-0 rounded-sm"></div> <div class="absolute top-1/2 left-0 right-0 h-8 bg-indigo-50 -translate-y-1/2 z-0 rounded-sm"></div>
<!-- Year Column --> <!-- Year Column -->
<div class="flex-1 overflow-y-scroll scrollbar-thin scrollbar-thumb-gray-300 scrollbar-track-gray-100 relative z-10"> <div class="flex-1 overflow-y-scroll relative z-10 snap-container" data-scroll-snap>
<div class="flex flex-col items-center justify-start pt-16 pb-16"> <!-- Padding top/bottom to center items --> <div class="flex flex-col items-center justify-start pt-48 pb-48 scroll-list">
{% for year_val in years_array %} {% for repeat in (1..3) %}
{% if year_val != "" %} {% for year_val in years_array %}
<div class="h-8 flex items-center justify-center text-sm text-gray-700 hover:text-indigo-700 w-full cursor-default">{{ year_val }}</div> {% if year_val != "" %}
{% endif %} <div class="h-8 flex items-center justify-center text-sm text-gray-700 hover:text-indigo-700 w-full cursor-default snap-item">{{ year_val }}</div>
{% endif %}
{% endfor %}
{% endfor %} {% endfor %}
</div> </div>
</div> </div>
<!-- Month Column --> <!-- Month Column -->
<div class="flex-1 overflow-y-scroll scrollbar-thin scrollbar-thumb-gray-300 scrollbar-track-gray-100 relative z-10"> <div class="flex-1 overflow-y-scroll relative z-10 snap-container" data-scroll-snap>
<div class="flex flex-col items-center justify-start pt-16 pb-16"> <div class="flex flex-col items-center justify-start pt-48 pb-48 scroll-list">
{% for month_val in months_array %} {% for repeat in (1..3) %}
{% if month_val != "" %} {% for month_val in months_array %}
<div class="h-8 flex items-center justify-center text-sm text-gray-700 hover:text-indigo-700 w-full cursor-default">{{ month_val }}</div> {% if month_val != "" %}
{% endif %} <div class="h-8 flex items-center justify-center text-sm text-gray-700 hover:text-indigo-700 w-full cursor-default snap-item">{{ month_val }}</div>
{% endif %}
{% endfor %}
{% endfor %} {% endfor %}
</div> </div>
</div> </div>
<!-- Day Column --> <!-- Day Column -->
<div class="flex-1 overflow-y-scroll scrollbar-thin scrollbar-thumb-gray-300 scrollbar-track-gray-100 relative z-10"> <div class="flex-1 overflow-y-scroll relative z-10 snap-container" data-scroll-snap>
<div class="flex flex-col items-center justify-start pt-16 pb-16"> <div class="flex flex-col items-center justify-start pt-48 pb-48 scroll-list">
{% for day_val in days_array %} {% for repeat in (1..3) %}
{% if day_val != "" %} {% for day_val in days_array %}
<div class="h-8 flex items-center justify-center text-sm text-gray-700 hover:text-indigo-700 w-full cursor-default">{{ day_val }}</div> {% if day_val != "" %}
{% endif %} <div class="h-8 flex items-center justify-center text-sm text-gray-700 hover:text-indigo-700 w-full cursor-default snap-item">{{ day_val }}</div>
{% endif %}
{% endfor %}
{% endfor %} {% endfor %}
</div> </div>
</div> </div>
</div> </div>
<!-- Buttons Below -->
<div class="mt-3 pt-2.5 border-t border-gray-200 flex justify-end items-center space-x-2"> <div class="mt-3 pt-2.5 border-t border-gray-200 flex justify-end items-center space-x-2">
<button type="button" class="text-xs text-gray-600 hover:text-gray-800 font-medium px-3 py-1.5 rounded-sm hover:bg-gray-100 focus:outline-none focus:ring-1 focus:ring-indigo-500">Cancel</button> <button type="button" class="text-xs text-gray-600 hover:text-gray-800 font-medium px-3 py-1.5 rounded-sm hover:bg-gray-100 focus:outline-none focus:ring-1 focus:ring-indigo-500">Cancel</button>
<button type="button" class="text-xs text-white bg-indigo-700 hover:bg-indigo-800 font-medium px-3 py-1.5 rounded-sm focus:outline-none focus:ring-1 focus:ring-offset-1 focus:ring-indigo-500">Set Date</button> <button type="button" class="text-xs text-white bg-indigo-700 hover:bg-indigo-800 font-medium px-3 py-1.5 rounded-sm focus:outline-none focus:ring-1 focus:ring-offset-1 focus:ring-indigo-500">Set Date</button>
@@ -104,30 +90,41 @@ The input field will not be updated by this picker.
</div> </div>
<style> <style>
/* Minimal scrollbar styling for webkit browsers */ .snap-container {
.scrollbar-thin::-webkit-scrollbar { scroll-snap-type: y mandatory;
width: 6px;
height: 6px;
} }
.scrollbar-thin::-webkit-scrollbar-track { .snap-item {
background: #f1f1f1; /* gray-100 approx */ scroll-snap-align: center;
border-radius: 10px;
} }
.scrollbar-thin::-webkit-scrollbar-thumb {
background: #d1d5db; /* gray-300 approx */
border-radius: 10px;
}
.scrollbar-thin::-webkit-scrollbar-thumb:hover {
background: #9ca3af; /* gray-400 approx */
}
/* For Firefox */
.scrollbar-thin {
scrollbar-width: thin;
scrollbar-color: #d1d5db #f1f1f1;
}
/* Ensure scrolling snaps to items if desired (more complex, browser support varies) */
/* Basic approach: each item has a fixed height, scroll container has a height multiple of item height */
/* The pt-16/pb-16 on the inner divs of scroll columns is a hack to "center" the first/last few items against the selection line */
</style> </style>
<script>
document.querySelectorAll('[data-scroll-snap]').forEach(container => {
const list = container.querySelector('.scroll-list');
const items = list.children;
const itemHeight = 32;
const totalItems = items.length;
const third = totalItems / 3;
container.scrollTop = third * itemHeight;
let scrollTimeout;
container.addEventListener('scroll', () => {
clearTimeout(scrollTimeout);
scrollTimeout = setTimeout(() => {
const scrollPos = container.scrollTop;
const snappedIndex = Math.round(scrollPos / itemHeight);
container.scrollTo({
top: snappedIndex * itemHeight,
behavior: 'smooth'
});
if (snappedIndex < third || snappedIndex >= 2 * third) {
const centerOffset = (snappedIndex % third) + third;
container.scrollTop = centerOffset * itemHeight;
}
}, 100);
});
});
</script>
+5 -4
View File
@@ -131,10 +131,11 @@
<div> <div>
<h3 class="text-sm font-medium text-gray-700 mb-2">Availability Date Range</h3> <h3 class="text-sm font-medium text-gray-700 mb-2">Availability Date Range</h3>
<div class="grid grid-cols-1 md:grid-cols-2 gap-3"> <div class="grid grid-cols-1 md:grid-cols-2 gap-3">
<div> {% include calendar_input.liquid
<label for="availability-start-date-modal" class="block text-xs font-medium text-gray-700 mb-1">Start Date</label> id="unique_calendar_id"
<input type="date" id="availability-start-date-modal" name="availability-start-date" class="w-full px-3 py-2 border border-gray-300 rounded-sm focus:outline-none focus:border-indigo-600 text-sm"> label="Select Date"
</div> placeholder="YYYY-MM-DD"
%}
<div> <div>
<label for="availability-end-date-modal" class="block text-xs font-medium text-gray-700 mb-1">End Date</label> <label for="availability-end-date-modal" class="block text-xs font-medium text-gray-700 mb-1">End Date</label>
<input type="date" id="availability-end-date-modal" name="availability-end-date" class="w-full px-3 py-2 border border-gray-300 rounded-sm focus:outline-none focus:border-indigo-600 text-sm"> <input type="date" id="availability-end-date-modal" name="availability-end-date" class="w-full px-3 py-2 border border-gray-300 rounded-sm focus:outline-none focus:border-indigo-600 text-sm">