Skip to content

fix: disallow focus of calendar cells outside the month #8129

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 7 additions & 2 deletions packages/@react-aria/calendar/src/useCalendarCell.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,12 @@ export interface AriaCalendarCellProps {
* Whether the cell is disabled. By default, this is determined by the
* Calendar's `minValue`, `maxValue`, and `isDisabled` props.
*/
isDisabled?: boolean
isDisabled?: boolean,

/**
* Whether the cell is outside of the current month.
*/
isOutsideMonth?: boolean
}

export interface CalendarCellAria {
Expand Down Expand Up @@ -85,7 +90,7 @@ export function useCalendarCell(props: AriaCalendarCellProps, state: CalendarSta
timeZone: state.timeZone
});
let isSelected = state.isSelected(date);
let isFocused = state.isCellFocused(date);
let isFocused = state.isCellFocused(date) && !props.isOutsideMonth;
isDisabled = isDisabled || state.isCellDisabled(date);
let isUnavailable = state.isCellUnavailable(date);
let isSelectable = !isDisabled && !isUnavailable;
Expand Down
5 changes: 3 additions & 2 deletions packages/react-aria-components/src/Calendar.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -496,17 +496,18 @@ export const CalendarCell = /*#__PURE__*/ (forwardRef as forwardRefType)(functio
let rangeCalendarState = useContext(RangeCalendarStateContext);
let state = calendarState ?? rangeCalendarState!;
let {startDate: currentMonth} = useContext(InternalCalendarGridContext) ?? {startDate: state.visibleRange.start};
let isOutsideMonth = !isSameMonth(currentMonth, date);

let buttonRef = useRef<HTMLDivElement>(null);
let {cellProps, buttonProps, ...states} = useCalendarCell(
{date},
{date, isOutsideMonth},
state,
buttonRef
);

let {hoverProps, isHovered} = useHover({...otherProps, isDisabled: states.isDisabled});
let {focusProps, isFocusVisible} = useFocusRing();
isFocusVisible &&= states.isFocused;
let isOutsideMonth = !isSameMonth(currentMonth, date);
let isSelectionStart = false;
let isSelectionEnd = false;
if ('highlightedRange' in state && state.highlightedRange) {
Expand Down
4 changes: 2 additions & 2 deletions packages/react-aria-components/stories/Calendar.stories.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -72,10 +72,10 @@ export const CalendarMultiMonth = () => (
</div>
<div style={{display: 'flex', gap: 20}}>
<CalendarGrid style={{flex: 1}}>
{date => <CalendarCell date={date} style={({isSelected, isOutsideMonth}) => ({display: isOutsideMonth ? 'none' : '', textAlign: 'center', cursor: 'default', background: isSelected ? 'blue' : ''})} />}
{date => <CalendarCell date={date} style={({isSelected, isOutsideMonth}) => ({opacity: isOutsideMonth ? '0.5' : '', textAlign: 'center', cursor: 'default', background: isSelected ? 'blue' : ''})} />}
</CalendarGrid>
<CalendarGrid style={{flex: 1}} offset={{months: 1}}>
{date => <CalendarCell date={date} style={({isSelected, isOutsideMonth}) => ({display: isOutsideMonth ? 'none' : '', textAlign: 'center', cursor: 'default', background: isSelected ? 'blue' : ''})} />}
{date => <CalendarCell date={date} style={({isSelected, isOutsideMonth}) => ({opacity: isOutsideMonth ? '0.5' : '', textAlign: 'center', cursor: 'default', background: isSelected ? 'blue' : ''})} />}
</CalendarGrid>
</div>
</Calendar>
Expand Down