Skip to content

Commit aa7747f

Browse files
authored
Improve date range UI (#663)
* wip improve date range ui * simplify * update classic calendar also
1 parent 08c527e commit aa7747f

File tree

7 files changed

+37
-17
lines changed

7 files changed

+37
-17
lines changed

resources/js/components/ui/calendar/CalendarCell.vue

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ const forwardedProps = useForwardProps(delegatedProps)
1616

1717
<template>
1818
<CalendarCell
19-
:class="cn('relative h-9 w-9 p-0 text-center text-sm focus-within:relative focus-within:z-20 [&:has([data-selected])]:rounded-md [&:has([data-selected])]:bg-accent [&:has([data-selected][data-outside-month])]:bg-accent/50', props.class)"
19+
:class="cn('relative p-0 text-center text-sm focus-within:relative focus-within:z-20 [&:has([data-selected])]:rounded-md [&:has([data-selected])]:bg-accent', props.class)"
2020
v-bind="forwardedProps"
2121
>
2222
<slot />

resources/js/components/ui/calendar/CalendarCellTrigger.vue

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -19,16 +19,16 @@ const forwardedProps = useForwardProps(delegatedProps)
1919
<CalendarCellTrigger
2020
:class="cn(
2121
buttonVariants({ variant: 'ghost' }),
22-
'h-9 w-9 p-0 font-normal',
22+
'size-8 p-0 font-normal aria-selected:opacity-100 cursor-default',
2323
'[&[data-today]:not([data-selected])]:bg-accent [&[data-today]:not([data-selected])]:text-accent-foreground',
2424
// Selected
25-
'data-[selected]:bg-primary data-[selected]:text-primary-foreground data-selected:opacity-100 data-[selected]:hover:bg-primary data-[selected]:hover:text-primary-foreground data-[selected]:focus:bg-primary data-[selected]:focus:text-primary-foreground',
25+
'data-[selected]:bg-primary data-[selected]:text-primary-foreground data-[selected]:opacity-100 data-[selected]:hover:bg-primary data-[selected]:hover:text-primary-foreground data-[selected]:focus:bg-primary data-[selected]:focus:text-primary-foreground',
2626
// Disabled
27-
'data-[disabled]:text-muted-foreground data-disabled:opacity-50',
27+
'data-[disabled]:text-muted-foreground data-[disabled]:opacity-50',
2828
// Unavailable
29-
'data-[unavailable]:text-destructive-foreground data-unavailable:line-through',
29+
'data-[unavailable]:text-destructive-foreground data-[unavailable]:line-through',
3030
// Outside months
31-
'data-outside-month:pointer-events-none data-[outside-month]:text-muted-foreground data-outside-month:opacity-50 [&[data-outside-month][data-selected]]:bg-accent/50 [&[data-outside-month][data-selected]]:text-muted-foreground [&[data-outside-month][data-selected]]:opacity-30',
31+
'data-[outside-view]:invisible data-[outside-view]:text-muted-foreground',
3232
props.class,
3333
)"
3434
v-bind="forwardedProps"

resources/js/components/ui/calendar/CalendarHeadCell.vue

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ const forwardedProps = useForwardProps(delegatedProps)
1515
</script>
1616

1717
<template>
18-
<CalendarHeadCell :class="cn('w-9 rounded-md text-[0.8rem] font-normal text-muted-foreground', props.class)" v-bind="forwardedProps">
18+
<CalendarHeadCell :class="cn('w-8 rounded-md text-[0.8rem] font-normal text-muted-foreground', props.class)" v-bind="forwardedProps">
1919
<slot />
2020
</CalendarHeadCell>
2121
</template>

resources/js/components/ui/range-calendar/RangeCalendar.vue

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ const forwarded = useForwardPropsEmits(delegatedProps, emits)
3030
</RangeCalendarHeader>
3131

3232
<div class="flex flex-col gap-y-4 mt-4 sm:flex-row sm:gap-x-4 sm:gap-y-0">
33-
<RangeCalendarGrid v-for="month in grid" :key="month.value.toString()">
33+
<RangeCalendarGrid v-for="(month, i) in grid" :key="month.value.toString()">
3434
<RangeCalendarGridHead>
3535
<RangeCalendarGridRow>
3636
<RangeCalendarHeadCell
@@ -46,6 +46,8 @@ const forwarded = useForwardPropsEmits(delegatedProps, emits)
4646
v-for="weekDate in weekDates"
4747
:key="weekDate.toString()"
4848
:date="weekDate"
49+
:month="month.value"
50+
:month-index="i"
4951
>
5052
<RangeCalendarCellTrigger
5153
:day="weekDate"

resources/js/components/ui/range-calendar/RangeCalendarCell.vue

Lines changed: 22 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,15 @@
11
<script lang="ts" setup>
22
import { type HTMLAttributes, computed } from 'vue'
3-
import { RangeCalendarCell, type RangeCalendarCellProps, useForwardProps } from 'reka-ui'
3+
import {
4+
injectRangeCalendarRootContext,
5+
RangeCalendarCell,
6+
type RangeCalendarCellProps,
7+
useForwardProps
8+
} from 'reka-ui'
49
import { cn } from '@/utils/cn'
10+
import { isSameMonth, DateValue } from '@internationalized/date';
511
6-
const props = defineProps<RangeCalendarCellProps & { class?: HTMLAttributes['class'] }>()
12+
const props = defineProps<RangeCalendarCellProps & { class?: HTMLAttributes['class'], month: DateValue, monthIndex: number }>()
713
814
const delegatedProps = computed(() => {
915
const { class: _, ...delegated } = props
@@ -12,12 +18,24 @@ const delegatedProps = computed(() => {
1218
})
1319
1420
const forwardedProps = useForwardProps(delegatedProps)
21+
const rootContext = injectRangeCalendarRootContext();
22+
const isNextMonthOutsideSelected = computed(() => isSameMonth(props.date, props.month.add({ months: 1 }))
23+
&& rootContext.startValue.value?.set({ day: 0 }).compare(props.month.set({ day: 0 })) <= 0
24+
&& rootContext.endValue.value?.set({ day: 0 }).compare(props.month.set({ day: 0 })) > 0);
25+
const isPreviousMonthOutsideSelected = computed(() => isSameMonth(props.date, props.month.subtract({ months: 1 }))
26+
&& rootContext.startValue.value?.set({ day: 0 }).compare(props.month.set({ day: 0 })) < 0
27+
&& rootContext.endValue.value?.set({ day: 0 }).compare(props.month.set({ day: 0 })) >= 0);
1528
</script>
1629

1730
<template>
1831
<RangeCalendarCell
19-
:class="cn('relative h-9 w-9 p-0 text-center text-sm focus-within:relative focus-within:z-20 [&:has([data-selected])]:bg-accent first:[&:has([data-selected])]:rounded-l-md last:[&:has([data-selected])]:rounded-r-md [&:has([data-selected][data-outside-month])]:bg-accent/50 [&:has([data-selected][data-selection-end])]:rounded-r-md [&:has([data-selected][data-selection-start])]:rounded-l-md', props.class)"
20-
v-bind="forwardedProps"
32+
:class="cn('relative p-0 text-center text-sm focus-within:relative focus-within:z-20 [&:has([data-selected][data-selection-end]:not([data-outside-view]))]:rounded-r-md [&:has([data-selected][data-selection-start]:not([data-outside-view]))]:rounded-l-md',
33+
rootContext.isSelected(props.date) ? 'bg-accent' : '',
34+
isNextMonthOutsideSelected ? 'bg-accent last:bg-transparent last:bg-linear-to-r last:from-accent last:to-transparent' : '',
35+
isPreviousMonthOutsideSelected ? 'bg-accent first:bg-transparent first:bg-linear-to-l first:from-accent first:to-transparent' : '',
36+
props.class
37+
)"
38+
v-bind="forwardedProps"
2139
>
2240
<slot />
2341
</RangeCalendarCell>

resources/js/components/ui/range-calendar/RangeCalendarCellTrigger.vue

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -19,18 +19,18 @@ const forwardedProps = useForwardProps(delegatedProps)
1919
<RangeCalendarCellTrigger
2020
:class="cn(
2121
buttonVariants({ variant: 'ghost' }),
22-
'h-9 w-9 p-0 font-normal data-selected:opacity-100',
22+
'size-8 p-0 font-normal data-[selected]:opacity-100 outline -outline-offset-1 outline-transparent hover:outline-primary',
2323
'[&[data-today]:not([data-selected])]:bg-accent [&[data-today]:not([data-selected])]:text-accent-foreground',
2424
// Selection Start
2525
'data-[selection-start]:bg-primary data-[selection-start]:text-primary-foreground data-[selection-start]:hover:bg-primary data-[selection-start]:hover:text-primary-foreground data-[selection-start]:focus:bg-primary data-[selection-start]:focus:text-primary-foreground',
2626
// Selection End
2727
'data-[selection-end]:bg-primary data-[selection-end]:text-primary-foreground data-[selection-end]:hover:bg-primary data-[selection-end]:hover:text-primary-foreground data-[selection-end]:focus:bg-primary data-[selection-end]:focus:text-primary-foreground',
2828
// Outside months
29-
'data-outside-month:pointer-events-none data-[outside-month]:text-muted-foreground data-outside-month:opacity-50 [&[data-outside-month][data-selected]]:bg-accent/50 [&[data-outside-month][data-selected]]:text-muted-foreground [&[data-outside-month][data-selected]]:opacity-30',
29+
'data-[outside-view]:invisible data-[outside-view]:text-muted-foreground/50! data-outside-view:bg-transparent!',
3030
// Disabled
31-
'data-[disabled]:text-muted-foreground data-disabled:opacity-50',
31+
'data-[disabled]:text-muted-foreground data-[disabled]:opacity-50',
3232
// Unavailable
33-
'data-[unavailable]:text-destructive-foreground data-unavailable:line-through',
33+
'data-[unavailable]:text-destructive-foreground data-[unavailable]:line-through',
3434
props.class,
3535
)"
3636
v-bind="forwardedProps"

resources/js/components/ui/range-calendar/RangeCalendarHeadCell.vue

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ const forwardedProps = useForwardProps(delegatedProps)
1515
</script>
1616

1717
<template>
18-
<RangeCalendarHeadCell :class="cn('w-9 rounded-md text-[0.8rem] font-normal text-muted-foreground', props.class)" v-bind="forwardedProps">
18+
<RangeCalendarHeadCell :class="cn('w-8 rounded-md text-[0.8rem] font-normal text-muted-foreground', props.class)" v-bind="forwardedProps">
1919
<slot />
2020
</RangeCalendarHeadCell>
2121
</template>

0 commit comments

Comments
 (0)