Skip to content

Commit 78d2908

Browse files
committed
fix: disallow focus of outside month calendar cells
1 parent e4ec6b4 commit 78d2908

File tree

3 files changed

+12
-6
lines changed

3 files changed

+12
-6
lines changed

packages/@react-aria/calendar/src/useCalendarCell.ts

+7-2
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,12 @@ export interface AriaCalendarCellProps {
2828
* Whether the cell is disabled. By default, this is determined by the
2929
* Calendar's `minValue`, `maxValue`, and `isDisabled` props.
3030
*/
31-
isDisabled?: boolean
31+
isDisabled?: boolean,
32+
33+
/**
34+
* Whether the cell is outside of the current month.
35+
*/
36+
isOutsideMonth?: boolean
3237
}
3338

3439
export interface CalendarCellAria {
@@ -85,7 +90,7 @@ export function useCalendarCell(props: AriaCalendarCellProps, state: CalendarSta
8590
timeZone: state.timeZone
8691
});
8792
let isSelected = state.isSelected(date);
88-
let isFocused = state.isCellFocused(date);
93+
let isFocused = state.isCellFocused(date) && !props.isOutsideMonth;
8994
isDisabled = isDisabled || state.isCellDisabled(date);
9095
let isUnavailable = state.isCellUnavailable(date);
9196
let isSelectable = !isDisabled && !isUnavailable;

packages/react-aria-components/src/Calendar.tsx

+3-2
Original file line numberDiff line numberDiff line change
@@ -496,17 +496,18 @@ export const CalendarCell = /*#__PURE__*/ (forwardRef as forwardRefType)(functio
496496
let rangeCalendarState = useContext(RangeCalendarStateContext);
497497
let state = calendarState ?? rangeCalendarState!;
498498
let {startDate: currentMonth} = useContext(InternalCalendarGridContext) ?? {startDate: state.visibleRange.start};
499+
let isOutsideMonth = !isSameMonth(currentMonth, date);
500+
499501
let buttonRef = useRef<HTMLDivElement>(null);
500502
let {cellProps, buttonProps, ...states} = useCalendarCell(
501-
{date},
503+
{date, isOutsideMonth},
502504
state,
503505
buttonRef
504506
);
505507

506508
let {hoverProps, isHovered} = useHover({...otherProps, isDisabled: states.isDisabled});
507509
let {focusProps, isFocusVisible} = useFocusRing();
508510
isFocusVisible &&= states.isFocused;
509-
let isOutsideMonth = !isSameMonth(currentMonth, date);
510511
let isSelectionStart = false;
511512
let isSelectionEnd = false;
512513
if ('highlightedRange' in state && state.highlightedRange) {

packages/react-aria-components/stories/Calendar.stories.tsx

+2-2
Original file line numberDiff line numberDiff line change
@@ -72,10 +72,10 @@ export const CalendarMultiMonth = () => (
7272
</div>
7373
<div style={{display: 'flex', gap: 20}}>
7474
<CalendarGrid style={{flex: 1}}>
75-
{date => <CalendarCell date={date} style={({isSelected, isOutsideMonth}) => ({display: isOutsideMonth ? 'none' : '', textAlign: 'center', cursor: 'default', background: isSelected ? 'blue' : ''})} />}
75+
{date => <CalendarCell date={date} style={({isSelected, isOutsideMonth}) => ({opacity: isOutsideMonth ? '0.5' : '', textAlign: 'center', cursor: 'default', background: isSelected ? 'blue' : ''})} />}
7676
</CalendarGrid>
7777
<CalendarGrid style={{flex: 1}} offset={{months: 1}}>
78-
{date => <CalendarCell date={date} style={({isSelected, isOutsideMonth}) => ({display: isOutsideMonth ? 'none' : '', textAlign: 'center', cursor: 'default', background: isSelected ? 'blue' : ''})} />}
78+
{date => <CalendarCell date={date} style={({isSelected, isOutsideMonth}) => ({opacity: isOutsideMonth ? '0.5' : '', textAlign: 'center', cursor: 'default', background: isSelected ? 'blue' : ''})} />}
7979
</CalendarGrid>
8080
</div>
8181
</Calendar>

0 commit comments

Comments
 (0)