RangeCalendar provides a customizable calendar interface for date range
selection.
The component is built with accessibility in mind and follows ARIA best practices for calendar widgets. It provides keyboard navigation, screen reader support, and customizable labels for internationalization.
import { RangeCalendar } from '@wordpress/ui';
LinksPermalink to this section
PropsPermalink to this section
| Name | Default | Description |
|---|---|---|
role | 'application' |
The ARIA role for the calendar’s root element. The default Changing this role can affect screen-reader keyboard navigation. Only
override it for a tested composition. Apply roles that require additional
behavior, such as |
required | false |
Whether the selection is required.
When |
defaultMonth | The current month |
The initial month to show in the calendar view (uncontrolled). |
month |
The month displayed in the calendar view (controlled). Use together with
| |
numberOfMonths | 1 |
The number of months displayed at once. |
showOutsideDays | false |
When |
fixedWeeks | false |
When |
startMonth |
The earliest month to start the month navigation. | |
endMonth |
The latest month to end the month navigation. | |
autoFocus |
Focus the first selected day (if set) or today’s date (if not disabled). Use this prop when the calendar should receive initial focus as it opens, such as in a calendar popover. Do not use it to move focus after updates to an open calendar. | |
disabled |
Specify which days are disabled. Using | |
disableNavigation |
Disable the navigation buttons. | |
labels |
Use custom labels, useful for translating the component. For a correct localized experience, consumers should make sure the locale used for translated labels and date text is consistent. | |
locale | The `enUS` locale from `date-fns/locale` |
A BCP 47 locale code or date-fns locale object used to localize date text, numerals, the default text direction, and the first day of the week. The locale code derives the first day of the week when the browser provides
that information, whether passed directly or through a date-fns locale
object. Use Invalid or unsupported locale codes fall back to For a correct localized experience, consumers should make sure the locale used for translated labels and date text is consistent. The calendar always uses a Gregorian date grid. The locale does not change the underlying calendar system. |
weekStartsOn | Based on the `locale` prop when available |
The index of the first day of the week (0 – Sunday). Overrides the locale’s one. |
onMonthChange |
Event fired when the user navigates between months. | |
timeZone |
The time zone (IANA or UTC offset) to use in the calendar. See Wikipedia for the possible values. When working with time zones, use the | |
render |
Replaces the component’s default HTML element using a given React element, or a function that returns a React element. | |
style |
CSS style to apply to the element. | |
className |
CSS class name to apply to the element. | |
excludeDisabled |
When | |
resetOnSelect | true |
When |
min |
The minimum number of nights to include in the range. | |
max |
The maximum number of nights to include in the range. | |
value |
The selected range (controlled). Use | |
onValueChange |
Event handler called when the selected range changes. | |
defaultValue |
The initially selected range (uncontrolled). |
ExamplesPermalink to this section
DefaultPermalink to this section
const Default = () => <RangeCalendar onMonthChange={fn()} onValueChange={fn()} endMonth={ toDate( endMonth ) } />;
Disabled DatesPermalink to this section
const DisabledDates = () => <RangeCalendar
onMonthChange={fn()}
onValueChange={fn()}
disabled={DISABLED_DATES_SAMPLE}
endMonth={ toDate( endMonth ) } />;
With Selected Range And MonthPermalink to this section
const WithSelectedRangeAndMonth = () => <RangeCalendar
onMonthChange={fn()}
onValueChange={fn()}
defaultValue={{
from: firstDayOfNextMonth,
to: fourthDayOfNextMonth,
}}
defaultMonth={firstDayOfNextMonth}
endMonth={ toDate( endMonth ) } />;
With Range ConstraintsPermalink to this section
Use min and max to constrain the number of nights the range may span, and
excludeDisabled to reset the range when it would include a disabled day.
const WithRangeConstraints = () => <RangeCalendar
onMonthChange={fn()}
onValueChange={fn()}
min={2}
max={7}
excludeDisabled
disabled={{ dayOfWeek: [ 0, 6 ] }}
endMonth={ toDate( endMonth ) } />;
With Outside DaysPermalink to this section
Shows days from adjacent months in the grid. Outside days use a lighter style
and are still interactive. Use fixedWeeks to keep the grid height constant.
const WithOutsideDays = () => <RangeCalendar
onMonthChange={fn()}
onValueChange={fn()}
showOutsideDays
fixedWeeks
endMonth={ toDate( endMonth ) } />;
With Time ZonePermalink to this section
When working with time zones, use the TZDate object from the
@date-fns/tz package instead
of the native Date object.
const WithTimeZone = () => {
const [ range, setRange ] = useState< typeof args.value >( null );
useEffect( () => {
setRange(
// Select from one week from today to two weeks from today
// every time the timezone changes.
{
from: new TZDate(
new Date().setDate( new Date().getDate() + 7 ),
args.timeZone
),
to: new TZDate(
new Date().setDate( new Date().getDate() + 14 ),
args.timeZone
),
}
);
}, [ args.timeZone ] );
return (
<>
<RangeCalendar
onMonthChange={fn()}
timeZone="Pacific/Auckland"
endMonth={ toDate( endMonth ) }
value={ range }
onValueChange={ ( selectedDate, ...rest ) => {
setRange(
// Set controlled state to null if there's no selection
! selectedDate ||
( selectedDate.from === undefined &&
selectedDate.to === undefined )
? null
: selectedDate
);
args.onValueChange?.( selectedDate, ...rest );
} }
disabled={ [
{
// Disable any date before today
before: new TZDate( new Date(), args.timeZone ),
},
] } />
<p>Calendar set to { args.timeZone ?? 'current' }timezone,
disabling selection for all dates before today, and starting
with a default date range of 1 week from today to 2 weeks
from today.
</p>
</>
);
};