feat: Implement CSS-only scrolling date picker with infinite scroll
This commit is contained in:
1 parent
9175335d05
commit
b7f956c551
2 files changed
+72
-74
No files matched your search
@@ -1,15 +1,4 @@
|
||||
{% comment %}
|
||||
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 %}
|
||||
|
||||
<!-- Scrolling Date Picker Template with Snapping and Infinite Scroll -->
|
||||
{% assign current_year = "now" | date: "%Y" | plus: 0 %}
|
||||
{% assign future_year_limit = current_year | plus: 10 %}
|
||||
{% 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>
|
||||
<div class="relative">
|
||||
<input
|
||||
type="text"
|
||||
id="calendar-input-{{ include.id | default: 'default' }}"
|
||||
name="{{ include.id | default: 'default' }}_date_display"
|
||||
readonly
|
||||
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"
|
||||
aria-label="{{ include.label | default: 'Select Date' }}"
|
||||
type="text"
|
||||
id="calendar-input-{{ include.id | default: 'default' }}"
|
||||
name="{{ include.id | default: 'default' }}_date_display"
|
||||
readonly
|
||||
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"
|
||||
aria-label="{{ include.label | default: 'Select Date' }}"
|
||||
>
|
||||
<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">
|
||||
@@ -44,57 +33,54 @@ The input field will not be updated by this picker.
|
||||
</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
|
||||
opacity-0 pointer-events-none peer-focus:opacity-100 peer-focus:pointer-events-auto
|
||||
group-hover:opacity-100 group-hover:pointer-events-auto
|
||||
transition-opacity duration-150 ease-in-out">
|
||||
<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-8 bg-indigo-50 -translate-y-1/2 z-0 rounded-sm"></div>
|
||||
|
||||
|
||||
<!-- 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 flex-col items-center justify-start pt-16 pb-16"> <!-- Padding top/bottom to center items -->
|
||||
{% for year_val in years_array %}
|
||||
{% if year_val != "" %}
|
||||
<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>
|
||||
{% endif %}
|
||||
<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-48 pb-48 scroll-list">
|
||||
{% for repeat in (1..3) %}
|
||||
{% for year_val in years_array %}
|
||||
{% if year_val != "" %}
|
||||
<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 %}
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<!-- 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 flex-col items-center justify-start pt-16 pb-16">
|
||||
{% for month_val in months_array %}
|
||||
{% if month_val != "" %}
|
||||
<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>
|
||||
{% endif %}
|
||||
<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-48 pb-48 scroll-list">
|
||||
{% for repeat in (1..3) %}
|
||||
{% for month_val in months_array %}
|
||||
{% if month_val != "" %}
|
||||
<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 %}
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<!-- 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 flex-col items-center justify-start pt-16 pb-16">
|
||||
{% for day_val in days_array %}
|
||||
{% if day_val != "" %}
|
||||
<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>
|
||||
{% endif %}
|
||||
<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-48 pb-48 scroll-list">
|
||||
{% for repeat in (1..3) %}
|
||||
{% for day_val in days_array %}
|
||||
{% if day_val != "" %}
|
||||
<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 %}
|
||||
</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">
|
||||
<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>
|
||||
@@ -104,30 +90,41 @@ The input field will not be updated by this picker.
|
||||
</div>
|
||||
|
||||
<style>
|
||||
/* Minimal scrollbar styling for webkit browsers */
|
||||
.scrollbar-thin::-webkit-scrollbar {
|
||||
width: 6px;
|
||||
height: 6px;
|
||||
.snap-container {
|
||||
scroll-snap-type: y mandatory;
|
||||
}
|
||||
.scrollbar-thin::-webkit-scrollbar-track {
|
||||
background: #f1f1f1; /* gray-100 approx */
|
||||
border-radius: 10px;
|
||||
.snap-item {
|
||||
scroll-snap-align: center;
|
||||
}
|
||||
.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>
|
||||
|
||||
<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>
|
||||
Reference in new issue
Block a user