Skip to content

feat(Calendar): add onVisibleMonthsChange callback to calendars and date pickers #1382

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
5 changes: 5 additions & 0 deletions .changeset/seven-dancers-admire.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"bits-ui": minor
---

add onVisibleMonthsChange callback to calendars and date pickers
36 changes: 21 additions & 15 deletions docs/src/routes/api/demos.json/demos.json

Large diffs are not rendered by default.

4 changes: 2 additions & 2 deletions docs/src/routes/api/demos.json/stackblitz-files.json

Large diffs are not rendered by default.

50 changes: 34 additions & 16 deletions packages/bits-ui/src/lib/bits/calendar/calendar.svelte.ts
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ type CalendarRootStateProps = WithRefProps<
WritableBoxedValues<{
value: DateValue | undefined | DateValue[];
placeholder: DateValue;
months: Month<DateValue>[];
}> &
ReadableBoxedValues<{
preventDeselect: boolean;
Expand Down Expand Up @@ -80,8 +81,7 @@ type CalendarRootStateProps = WithRefProps<
>;

export class CalendarRootState {
months: Month<DateValue>[] = $state([]);
visibleMonths = $derived.by(() => this.months.map((month) => month.value));
visibleMonths = $derived.by(() => this.#months.map((month) => month.value));
announcer: Announcer;
formatter: Formatter;
accessibleHeadingId = useId();
Expand Down Expand Up @@ -109,7 +109,7 @@ export class CalendarRootState {

useRefById(opts);

this.months = createMonths({
this.opts.months.current = createMonths({
dateObj: this.opts.placeholder.current,
weekStartsOn: this.opts.weekStartsOn.current,
locale: this.opts.locale.current,
Expand Down Expand Up @@ -154,7 +154,7 @@ export class CalendarRootState {
locale: this.opts.locale,
fixedWeeks: this.opts.fixedWeeks,
numberOfMonths: this.opts.numberOfMonths,
setMonths: (months: Month<DateValue>[]) => (this.months = months),
setMonths: (months: Month<DateValue>[]) => (this.opts.months.current = months),
});

/**
Expand Down Expand Up @@ -212,8 +212,24 @@ export class CalendarRootState {
});
}

/**
* Currently displayed months, with default value fallback for SSR,
* as boxes don't update server-side.
*/
get #months() {
return this.opts.months.current.length
? this.opts.months.current
: createMonths({
dateObj: this.opts.placeholder.current,
weekStartsOn: this.opts.weekStartsOn.current,
locale: this.opts.locale.current,
fixedWeeks: this.opts.fixedWeeks.current,
numberOfMonths: this.opts.numberOfMonths.current,
});
}

setMonths(months: Month<DateValue>[]) {
this.months = months;
this.opts.months.current = months;
}

/**
Expand All @@ -225,7 +241,7 @@ export class CalendarRootState {
*/
weekdays = $derived.by(() => {
return getWeekdays({
months: this.months,
months: this.#months,
formatter: this.formatter,
weekdayFormat: this.opts.weekdayFormat.current,
});
Expand All @@ -243,7 +259,7 @@ export class CalendarRootState {
setMonths: this.setMonths,
setPlaceholder: (date: DateValue) => (this.opts.placeholder.current = date),
weekStartsOn: this.opts.weekStartsOn.current,
months: this.months,
months: this.#months,
});
}

Expand All @@ -259,7 +275,7 @@ export class CalendarRootState {
setMonths: this.setMonths,
setPlaceholder: (date: DateValue) => (this.opts.placeholder.current = date),
weekStartsOn: this.opts.weekStartsOn.current,
months: this.months,
months: this.#months,
});
}

Expand All @@ -282,15 +298,15 @@ export class CalendarRootState {
isNextButtonDisabled = $derived.by(() => {
return getIsNextButtonDisabled({
maxValue: this.opts.maxValue.current,
months: this.months,
months: this.#months,
disabled: this.opts.disabled.current,
});
});

isPrevButtonDisabled = $derived.by(() => {
return getIsPrevButtonDisabled({
minValue: this.opts.minValue.current,
months: this.months,
months: this.#months,
disabled: this.opts.disabled.current,
});
});
Expand All @@ -315,7 +331,7 @@ export class CalendarRootState {

headingValue = $derived.by(() => {
return getCalendarHeadingValue({
months: this.months,
months: this.#months,
formatter: this.formatter,
locale: this.opts.locale.current,
});
Expand Down Expand Up @@ -356,7 +372,7 @@ export class CalendarRootState {
calendarNode: this.opts.ref.current,
isPrevButtonDisabled: this.isPrevButtonDisabled,
isNextButtonDisabled: this.isNextButtonDisabled,
months: this.months,
months: this.#months,
numberOfMonths: this.opts.numberOfMonths.current,
});
}
Expand Down Expand Up @@ -437,10 +453,12 @@ export class CalendarRootState {
});
}

snippetProps = $derived.by(() => ({
months: this.months,
weekdays: this.weekdays,
}));
snippetProps = $derived.by(() => {
return {
months: this.#months,
weekdays: this.weekdays,
};
});

getBitsAttr(part: CalendarParts) {
return `data-bits-calendar-${part}`;
Expand Down
11 changes: 11 additions & 0 deletions packages/bits-ui/src/lib/bits/calendar/components/calendar.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import { noop } from "$lib/internal/noop.js";
import { getDefaultDate } from "$lib/internal/date-time/utils.js";
import { watch } from "runed";
import type { Month } from "$lib/shared/index.js";

let {
child,
Expand Down Expand Up @@ -34,9 +35,12 @@
type,
disableDaysOutsideMonth = true,
initialFocus = false,
onVisibleMonthsChange = noop,
...restProps
}: CalendarRootProps = $props();

let months = $state<Month<DateValue>[]>([]);

const defaultPlaceholder = getDefaultDate({
defaultValue: value,
});
Expand Down Expand Up @@ -110,6 +114,13 @@
),
type: box.with(() => type),
defaultPlaceholder,
months: box.with(
() => months,
(v) => {
months = v;
onVisibleMonthsChange(v);
}
),
});

const mergedProps = $derived(mergeProps(restProps, rootState.props));
Expand Down
5 changes: 5 additions & 0 deletions packages/bits-ui/src/lib/bits/calendar/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,11 @@ type CalendarBaseRootPropsWithoutHTML = {
*/
onPlaceholderChange?: OnChangeFn<DateValue>;

/**
* A callback function called when the currently displayed month(s) changes.
*/
onVisibleMonthsChange?: OnChangeFn<Month<DateValue>[]>;

/**
* Whether or not users can deselect a date once selected
* without selecting another date.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@
minValue: datePickerRootState.opts.minValue,
placeholder: datePickerRootState.opts.placeholder,
value: datePickerRootState.opts.value,
months: datePickerRootState.opts.months,
onDateSelect: datePickerRootState.opts.onDateSelect,
initialFocus: datePickerRootState.opts.initialFocus,
defaultPlaceholder: datePickerRootState.opts.defaultPlaceholder,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
import { FloatingLayer } from "$lib/bits/utilities/floating-layer/index.js";
import { getDefaultDate } from "$lib/internal/date-time/utils.js";
import { watch } from "runed";
import type { Month } from "$lib/shared/index.js";

let {
open = $bindable(false),
Expand Down Expand Up @@ -44,8 +45,11 @@
initialFocus = false,
errorMessageId,
children,
onVisibleMonthsChange = noop,
}: DatePickerRootProps = $props();

let months = $state<Month<DateValue>[]>([]);

const defaultPlaceholder = getDefaultDate({
granularity,
defaultValue: value,
Expand Down Expand Up @@ -121,6 +125,13 @@
numberOfMonths: box.with(() => numberOfMonths),
initialFocus: box.with(() => initialFocus),
onDateSelect: box.with(() => onDateSelect),
months: box.with(
() => months,
(v) => {
months = v;
onVisibleMonthsChange(v);
}
),
defaultPlaceholder,
});

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,13 @@ import type { DateValue } from "@internationalized/date";
import { Context } from "runed";
import type { ReadableBoxedValues, WritableBoxedValues } from "$lib/internal/box.svelte.js";
import type { DateMatcher, SegmentPart } from "$lib/shared/index.js";
import type { Granularity, HourCycle, WeekStartsOn } from "$lib/shared/date/types.js";
import type { Granularity, HourCycle, Month, WeekStartsOn } from "$lib/shared/date/types.js";

type DatePickerRootStateProps = WritableBoxedValues<{
value: DateValue | undefined;
open: boolean;
placeholder: DateValue;
months: Month<DateValue>[];
}> &
ReadableBoxedValues<{
readonlySegments: SegmentPart[];
Expand Down
7 changes: 6 additions & 1 deletion packages/bits-ui/src/lib/bits/date-picker/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import type {
DateValidator,
EditableSegmentPart,
} from "$lib/shared/index.js";
import type { Granularity, WeekStartsOn } from "$lib/shared/date/types.js";
import type { Granularity, Month, WeekStartsOn } from "$lib/shared/date/types.js";

export type DatePickerRootPropsWithoutHTML = WithChildren<{
/**
Expand All @@ -23,6 +23,11 @@ export type DatePickerRootPropsWithoutHTML = WithChildren<{
*/
onValueChange?: OnChangeFn<DateValue | undefined>;

/**
* A callback function called when the currently displayed month(s) changes.
*/
onVisibleMonthsChange?: OnChangeFn<Month<DateValue>[]>;

/**
* The placeholder value of the date field. This determines the format
* and what date the field starts at when it is empty.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@
placeholder: dateRangePickerRootState.opts.placeholder,
value: dateRangePickerRootState.opts.value,
onRangeSelect: dateRangePickerRootState.opts.onRangeSelect,
months: dateRangePickerRootState.opts.months,
startValue: dateRangePickerRootState.opts.startValue,
endValue: dateRangePickerRootState.opts.endValue,
defaultPlaceholder: dateRangePickerRootState.opts.defaultPlaceholder,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
import { useDateRangeFieldRoot } from "$lib/bits/date-range-field/date-range-field.svelte.js";
import FloatingLayer from "$lib/bits/utilities/floating-layer/components/floating-layer.svelte";
import { useId } from "$lib/internal/use-id.js";
import type { DateRange } from "$lib/shared/index.js";
import type { DateRange, Month } from "$lib/shared/index.js";
import { getDefaultDate } from "$lib/internal/date-time/utils.js";
import { watch } from "runed";

Expand Down Expand Up @@ -45,6 +45,7 @@
closeOnRangeSelect = true,
onStartValueChange = noop,
onEndValueChange = noop,
onVisibleMonthsChange = noop,
validate = noop,
errorMessageId,
child,
Expand All @@ -54,6 +55,7 @@

let startValue = $state<DateValue | undefined>(value?.start);
let endValue = $state<DateValue | undefined>(value?.end);
let months = $state.raw<Month<DateValue>[]>([]);

function handleDefaultValue() {
if (value !== undefined) return;
Expand Down Expand Up @@ -149,6 +151,13 @@
fixedWeeks: box.with(() => fixedWeeks),
numberOfMonths: box.with(() => numberOfMonths),
onRangeSelect: box.with(() => onRangeSelect),
months: box.with(
() => months,
(v) => {
months = v;
onVisibleMonthsChange(v);
}
),
startValue: box.with(
() => startValue,
(v) => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,13 @@ import type { DateValue } from "@internationalized/date";
import { Context } from "runed";
import type { ReadableBoxedValues, WritableBoxedValues } from "$lib/internal/box.svelte.js";
import type { DateMatcher, DateRange, SegmentPart } from "$lib/shared/index.js";
import type { Granularity, HourCycle, WeekStartsOn } from "$lib/shared/date/types.js";
import type { Granularity, HourCycle, Month, WeekStartsOn } from "$lib/shared/date/types.js";

type DateRangePickerRootStateProps = WritableBoxedValues<{
value: DateRange;
startValue: DateValue | undefined;
endValue: DateValue | undefined;
months: Month<DateValue>[];
open: boolean;
placeholder: DateValue;
}> &
Expand Down
7 changes: 6 additions & 1 deletion packages/bits-ui/src/lib/bits/date-range-picker/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import type {
EditableSegmentPart,
} from "$lib/shared/index.js";
import type { CalendarRootSnippetProps } from "$lib/types.js";
import type { Granularity, WeekStartsOn } from "$lib/shared/date/types.js";
import type { Granularity, Month, WeekStartsOn } from "$lib/shared/date/types.js";

export type DateRangePickerRootPropsWithoutHTML = WithChild<{
/**
Expand Down Expand Up @@ -266,6 +266,11 @@ export type DateRangePickerRootPropsWithoutHTML = WithChild<{
*/
onEndValueChange?: OnChangeFn<DateValue | undefined>;

/**
* A callback function called when the currently displayed month(s) changes.
*/
onVisibleMonthsChange?: OnChangeFn<Month<DateValue>[]>;

/**
* The `id` of the element which contains the error messages for the date field when the
* date is invalid.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import { useId } from "$lib/internal/use-id.js";
import { getDefaultDate } from "$lib/internal/date-time/utils.js";
import { watch } from "runed";
import type { Month } from "$lib/shared/index.js";

let {
children,
Expand Down Expand Up @@ -34,11 +35,13 @@
disableDaysOutsideMonth = true,
onStartValueChange = noop,
onEndValueChange = noop,
onVisibleMonthsChange = noop,
...restProps
}: RangeCalendarRootProps = $props();

let startValue = $state<DateValue | undefined>(value?.start);
let endValue = $state<DateValue | undefined>(value?.end);
let months = $state.raw<Month<DateValue>[]>([]);

const defaultPlaceholder = getDefaultDate({
defaultValue: value?.start,
Expand Down Expand Up @@ -109,6 +112,13 @@
calendarLabel: box.with(() => calendarLabel),
fixedWeeks: box.with(() => fixedWeeks),
disableDaysOutsideMonth: box.with(() => disableDaysOutsideMonth),
months: box.with(
() => months,
(v) => {
months = v;
onVisibleMonthsChange(v);
}
),
startValue: box.with(
() => startValue,
(v) => {
Expand Down
Loading
Loading