Repository navigation
fix(datetime): improve range selection behavior and styling - #31759
OS-susmitabhowmik wants to merge 16 commits into
Conversation
Once a full date range is selected (start + end), the next tap now always starts a new range instead of modifying the existing one. This aligns with Material and OutSystems UI Date Picker behavior. Resolves ROU-13222
- Add horizontal grid padding and --range-track-edge-overflow to the ionic theme so the range track bridges across week boundaries, matching the ios and md themes. - Lift range-start and range-end wrappers to z-index: 1 so their circles are not covered by the next row's range track when they overflow their grid cell.
When a date range is set programmatically with start/end dates outside the min/max constraints, clamp the range to the valid bounds. If the entire range falls outside min/max, clear the selection entirely. Resolves ROU-13214
|
The latest updates on your projects. Learn more about Vercel for GitHub.
|
When the second tap in a range selection is earlier than the first, swap start and end so the range renders correctly.
Consolidate duplicate "entirely before/after" e2e tests into one, update test page with start-before-min, end-after-max, and entirely-outside cases, and add ionChange console logger.
thetaPC
left a comment
There was a problem hiding this comment.
Notes on the description:
- The description doesn't say why the tap behavior is changing. Could you add the reason, such as design feedback asking that a third tap start a new range, matching Material's range picker? If the swap is dropped (refer to the inline comment), the "Range order not normalized" bullets can go too, since
nextalready treats an earlier second tap as a new start. - The out-of-bounds console warning already existed (
warnIfValueOutOfBounds), so it isn't new behavior.
| // Clamp range bounds to min/max so out-of-range dates are not highlighted. | ||
| // If the entire range falls outside min/max, clear the selection. | ||
| const clampedStart = clampDate(startParts, minParts, maxParts); | ||
| const clampedEnd = endParts ? clampDate(endParts, minParts, maxParts) : undefined; | ||
|
|
||
| const rangeFullyOutOfBounds = | ||
| clampedEnd && endParts && isSameDay(clampedStart, clampedEnd) && !isSameDay(startParts, endParts); | ||
|
|
||
| if (rangeFullyOutOfBounds) { | ||
| this.activeParts = []; | ||
| } else { | ||
| startParts = clampedStart; | ||
| endParts = clampedEnd; | ||
| this.activeParts = endParts ? { start: startParts, end: endParts } : { start: startParts }; | ||
| } |
There was a problem hiding this comment.
The same-day check misses ranges that touch min or max:
['2022-06-01', '2022-06-05']withmin="2022-06-05"clamps both ends to Jun 5, so the selection is cleared even though Jun 5 is in bounds['2022-01-01', '2022-01-01']or['2022-01-01']withmin="2022-06-05"skips the check because start equals end or there is no end, so Jun 5 is highlighted as a range the developer never set
Comparing the original dates against the bounds covers all three:
| // Clamp range bounds to min/max so out-of-range dates are not highlighted. | |
| // If the entire range falls outside min/max, clear the selection. | |
| const clampedStart = clampDate(startParts, minParts, maxParts); | |
| const clampedEnd = endParts ? clampDate(endParts, minParts, maxParts) : undefined; | |
| const rangeFullyOutOfBounds = | |
| clampedEnd && endParts && isSameDay(clampedStart, clampedEnd) && !isSameDay(startParts, endParts); | |
| if (rangeFullyOutOfBounds) { | |
| this.activeParts = []; | |
| } else { | |
| startParts = clampedStart; | |
| endParts = clampedEnd; | |
| this.activeParts = endParts ? { start: startParts, end: endParts } : { start: startParts }; | |
| } | |
| /** | |
| * Clamp the range to min/max so out-of-bounds dates are not highlighted. | |
| * Clear the selection when no day of the range falls within min/max. | |
| */ | |
| const lastParts = endParts ?? startParts; | |
| const rangeOutOfBounds = | |
| (maxParts && isAfter(startParts, maxParts)) || (minParts && isBefore(lastParts, minParts)); | |
| if (rangeOutOfBounds) { | |
| this.activeParts = []; | |
| } else { | |
| startParts = clampDate(startParts, minParts, maxParts); | |
| endParts = endParts ? clampDate(endParts, minParts, maxParts) : undefined; | |
| this.activeParts = endParts ? { start: startParts, end: endParts } : { start: startParts }; | |
| } |
A test for the first case, to go after range entirely outside min/max should clear the selection:
test('range ending on min should keep that day selected', async ({ page }) => {
await page.setContent(
`
<ion-datetime
locale="en-US"
presentation="date"
selection-mode="range"
min="2022-06-05"
max="2022-06-25"
></ion-datetime>
<script>
const datetime = document.querySelector('ion-datetime');
datetime.value = ['2022-06-01', '2022-06-05'];
</script>
`,
config
);
await page.locator('.datetime-ready').waitFor();
const day5 = page.locator('ion-datetime [data-month="6"][data-day="5"]');
await expect(day5).toHaveClass(/calendar-day-range-start/);
await expect(day5).toHaveClass(/calendar-day-range-end/);
});There was a problem hiding this comment.
Thanks for catching this! Fixed and added a test in 70f66d3
| // Calendar / Body | ||
| // ----------------------------------- | ||
|
|
||
| :host .calendar-body .calendar-month .calendar-month-grid { | ||
| @include globals.padding(0, globals.$ion-space-200); | ||
| } | ||
|
|
||
| // Range Selection | ||
| // ----------------------------------- | ||
|
|
||
| :host { | ||
| --range-background: #{globals.$ion-bg-primary-subtle-default}; | ||
| --range-track-height: #{globals.$ion-scale-1200}; | ||
| --range-track-edge-overflow: #{globals.$ion-space-200}; |
There was a problem hiding this comment.
The ionic theme design has no side padding on the grid. Each row is seven 49px cells across the full 343px width, and in the range example the track stops flush with the grid edge on Saturday and Sunday instead of running past it.
- Adding
padding: 0 8pxshrinks the cells in every ionic datetime, not only ranges, which is why the basic, display and show-adjacent-days screenshots changed - It also doesn't remove the line. At Pixel 9 density (2.625x), the ionic track has a faint 1px line at every column boundary on both
nextand this branch; the padding only moves where the boundaries fall. There are no lines at 2x or 3x, and none inmd, so the cause is likely specific to the ionic theme
Reverting both rules keeps the grid matching the design, and the line can be fixed on its own:
| // Calendar / Body | |
| // ----------------------------------- | |
| :host .calendar-body .calendar-month .calendar-month-grid { | |
| @include globals.padding(0, globals.$ion-space-200); | |
| } | |
| // Range Selection | |
| // ----------------------------------- | |
| :host { | |
| --range-background: #{globals.$ion-bg-primary-subtle-default}; | |
| --range-track-height: #{globals.$ion-scale-1200}; | |
| --range-track-edge-overflow: #{globals.$ion-space-200}; | |
| // Range Selection | |
| // ----------------------------------- | |
| :host { | |
| --range-background: #{globals.$ion-bg-primary-subtle-default}; | |
| --range-track-height: #{globals.$ion-scale-1200}; |
There was a problem hiding this comment.
I reverted the padding changes and tested this issue further. It was actually occurring on all three themes and seems to have been caused by the way we were drawing the range tracker previously one day at a time. I updated the way we are drawing the tracker to be drawn per week instead in 01dd633. Could you please retest to verify if the line issue is fixed?
| * Creating a new range: first click sets start, second sets end. | ||
| * Modifying an existing range: | ||
| * - Before range → new start | ||
| * - Within range → new end | ||
| * - After range → new end | ||
| * Once a full range is selected (both start and end), the next | ||
| * tap always starts a new range with the clicked date as the start. | ||
| * If the resulting span contains a disabled date, start a new range instead. | ||
| */ | ||
| private setActivePartsRange = (parts: DatetimeParts) => { | ||
| const current = this.rangeParts; | ||
|
|
||
| if (!current || isBefore(parts, current.start) || this.rangeContainsDisabledDate(current.start, parts)) { | ||
| if (!current || current.end || this.rangeContainsDisabledDate(current.start, parts)) { | ||
| this.activeParts = { start: parts }; | ||
| } else { | ||
| this.activeParts = { start: current.start, end: parts }; | ||
| const start = current.start; | ||
| // Normalize so start is always before end | ||
| if (isBefore(parts, start)) { | ||
| this.activeParts = { start: parts, end: start }; | ||
| } else { | ||
| this.activeParts = { start, end: parts }; | ||
| } | ||
| } | ||
| }; |
There was a problem hiding this comment.
Swapping an earlier second tap into the end goes against Material's range picker, which the md theme follows and ios mirrors: there, an earlier second tap becomes the new start. Without confirm buttons the swap also emits ionChange right away, so a tap meant to fix the start date commits a range the user didn't pick. The third-tap reset already matches Material, so keeping the existing new-start branch gets the requested behavior without the swap:
| * Creating a new range: first click sets start, second sets end. | |
| * Modifying an existing range: | |
| * - Before range → new start | |
| * - Within range → new end | |
| * - After range → new end | |
| * Once a full range is selected (both start and end), the next | |
| * tap always starts a new range with the clicked date as the start. | |
| * If the resulting span contains a disabled date, start a new range instead. | |
| */ | |
| private setActivePartsRange = (parts: DatetimeParts) => { | |
| const current = this.rangeParts; | |
| if (!current || isBefore(parts, current.start) || this.rangeContainsDisabledDate(current.start, parts)) { | |
| if (!current || current.end || this.rangeContainsDisabledDate(current.start, parts)) { | |
| this.activeParts = { start: parts }; | |
| } else { | |
| this.activeParts = { start: current.start, end: parts }; | |
| const start = current.start; | |
| // Normalize so start is always before end | |
| if (isBefore(parts, start)) { | |
| this.activeParts = { start: parts, end: start }; | |
| } else { | |
| this.activeParts = { start, end: parts }; | |
| } | |
| } | |
| }; | |
| * Creating a new range: first click sets start, second sets end. | |
| * If the second click is before the start, it becomes the new start. | |
| * Once a full range is selected (both start and end), the next | |
| * tap always starts a new range with the clicked date as the start. | |
| * If the resulting span contains a disabled date, start a new range instead. | |
| */ | |
| private setActivePartsRange = (parts: DatetimeParts) => { | |
| const current = this.rangeParts; | |
| if ( | |
| !current || | |
| current.end || | |
| isBefore(parts, current.start) || | |
| this.rangeContainsDisabledDate(current.start, parts) | |
| ) { | |
| this.activeParts = { start: parts }; | |
| } else { | |
| this.activeParts = { start: current.start, end: parts }; | |
| } | |
| }; |
Please also replace the tapping an earlier date second should normalize start and end test in test/range/datetime.e2e.ts with one for the new-start behavior. The fixture has no confirm buttons by default, so it also checks that the earlier tap doesn't emit ionChange:
test('tapping an earlier date second should start a new range', async ({ page }) => {
await fixture.goto(config);
const ionChangeSpy = await page.spyOnEvent('ionChange');
const day20 = fixture.getVisibleDay(20);
const day10 = fixture.getVisibleDay(10);
// First tap: set start to day 20
await day20.click();
await page.waitForChanges();
// Second tap: day 10 is before the start, so it becomes the new start
await day10.click();
await page.waitForChanges();
await expect(day10).toHaveClass(/calendar-day-range-start/);
await expect(day20).not.toHaveClass(/calendar-day-range-start/);
await expect(day20).not.toHaveClass(/calendar-day-range-end/);
await expect(ionChangeSpy).not.toHaveReceivedEvent();
});There was a problem hiding this comment.
That makes sense to me! Updated as a part of 3683a1d
| z-index: 1; | ||
| } | ||
|
|
||
| /** | ||
| * Range start and end circles can overflow their grid row. | ||
| * Lift them above adjacent in-range wrappers so the next | ||
| * row's range track does not paint over the circle. | ||
| */ | ||
| :host .calendar-day-wrapper-range-start, | ||
| :host .calendar-day-wrapper-range-end { | ||
| z-index: 1; | ||
| } |
There was a problem hiding this comment.
Giving range start and end the same z-index: 1 as :focus-within lets DOM order decide, so a range-end or range-start wrapper later in the DOM paints over the focus ring of the day before it. In the md theme, keyboard-focusing Jun 19 on the range test page's "Programmatic Default Value" (Jun 10 to 20) shows the right edge of its ring cut flat by Jun 20's track. ios uses the same ring but its wider cells keep it clear at that width, and ionic draws its ring inside the button. Raising the focused day to z-index: 2 keeps it on top:
| z-index: 1; | |
| } | |
| /** | |
| * Range start and end circles can overflow their grid row. | |
| * Lift them above adjacent in-range wrappers so the next | |
| * row's range track does not paint over the circle. | |
| */ | |
| :host .calendar-day-wrapper-range-start, | |
| :host .calendar-day-wrapper-range-end { | |
| z-index: 1; | |
| } | |
| /** | |
| * Range start and end wrappers use z-index: 1 below. Matching them | |
| * would let DOM order decide, so a later range edge would paint over | |
| * this day's focus ring. Stay one level above them. | |
| */ | |
| z-index: 2; | |
| } | |
| /** | |
| * Range start and end circles can overflow their grid row. | |
| * Lift them above adjacent in-range wrappers so the next | |
| * row's range track does not paint over the circle. | |
| */ | |
| :host .calendar-day-wrapper-range-start, | |
| :host .calendar-day-wrapper-range-end { | |
| z-index: 1; | |
| } |
The in-range-focus screenshot focuses Jun 15, which has no range edge next to it, so it doesn't catch this. Moving focus to Jun 17, the day before the end, would cover it:
await page.keyboard.press('ArrowDown');
await page.waitForChanges();
await page.keyboard.press('ArrowRight');
await page.waitForChanges();
await page.keyboard.press('ArrowRight');
await page.waitForChanges();
const day17 = datetime.locator('[data-month="6"][data-day="17"]');
await expect(day17).toBeFocused();… range The previous same-day check after clamping missed ranges that touch min or max boundaries. Compare the original start/end against bounds first so a range ending exactly on min is kept and a single-date range outside bounds is correctly cleared.
…el seams Each day in a range previously drew its own piece of the track with a ::before using two background layers. At fractional device pixel ratios (e.g. 2.625× on Pixel 9), the browser anti-aliased each layer's edge independently, letting the white background layer show through at every column boundary. The first range day of each run within a week now draws the track for the whole run via --range-track-span, so there are no edges between days. The track uses a single background color instead of two layers, which means both opaque and translucent --range-background values work without overlap artifacts. The grid uses isolation: isolate as a single stacking context, replacing the per-wrapper z-index rules. The ionic grid padding and edge overflow are removed so the grid matches the design.
Add back focus-within (z-index: 2) and range-start/range-end (z-index: 1) rules, excluding range-track wrappers whose wide ::before would cover adjacent day numbers if elevated. Also change range selection so tapping an earlier date as the second click starts a new range instead of swapping, matching Material's range picker. The focus screenshot now targets day 17 (adjacent to range-end) to catch focus ring clipping.
# Conflicts: # core/src/components/datetime/test/show-adjacent-days/datetime.e2e.ts-snapshots/datetime-show-adjacent-days-display-ionic-md-ltr-light-Mobile-Firefox-linux.png # core/src/components/datetime/test/show-adjacent-days/datetime.e2e.ts-snapshots/datetime-show-adjacent-days-weekends-disabled-ionic-md-ltr-light-Mobile-Firefox-linux.png
Issue number: resolves internal
What is the current behavior?
::before, and the per-cell boundaries left visible seams.min/maxrenders those out-of-bounds dates as highlighted.What is the new behavior?
--range-track-span, eliminating seams between days. The grid usesisolation: isolateas a single stacking context, replacing the per-wrapperz-index: 0.z-index: 1and the existing focus-within rule is raised toz-index: 2, so the focus ring and range circles are never covered by an adjacent track. Wrappers that draw the track (calendar-day-wrapper-range-track) are excluded to avoid elevating the wide::beforeover adjacent day numbers.min/maxbounds. If the entire range falls outside bounds, the selection is cleared.Does this introduce a breaking change?
Other information
Manual test page
Range test page
New min/max scenarios added: