diff --git a/.changeset/thick-guests-brush.md b/.changeset/thick-guests-brush.md new file mode 100644 index 000000000..cb11a168d --- /dev/null +++ b/.changeset/thick-guests-brush.md @@ -0,0 +1,5 @@ +--- +"bits-ui": patch +--- + +Calendar & Range Calendar: add `initialFocus` prop to autofocus dates on mount diff --git a/src/content/api-reference/calendar.ts b/src/content/api-reference/calendar.ts index ba8a5d879..1429db581 100644 --- a/src/content/api-reference/calendar.ts +++ b/src/content/api-reference/calendar.ts @@ -117,6 +117,12 @@ export const root: APISchema = { description: "Whether or not the calendar is readonly.", default: C.FALSE }, + initialFocus: { + type: C.BOOLEAN, + description: + "If `true`, the calendar will focus the selected day, today, or the first day of the month in that order depending on what is visible when the calendar is mounted.", + default: C.FALSE + }, asChild }, slotProps: { diff --git a/src/content/api-reference/range-calendar.ts b/src/content/api-reference/range-calendar.ts index 4c4f41922..3a5fc8ce5 100644 --- a/src/content/api-reference/range-calendar.ts +++ b/src/content/api-reference/range-calendar.ts @@ -116,6 +116,12 @@ export const root: APISchema = { description: "Whether or not the calendar is readonly.", default: C.FALSE }, + initialFocus: { + type: C.BOOLEAN, + description: + "If `true`, the calendar will focus the selected day, today, or the first day of the month in that order depending on what is visible when the calendar is mounted.", + default: C.FALSE + }, asChild }, slotProps: { diff --git a/src/lib/bits/calendar/_types.ts b/src/lib/bits/calendar/_types.ts index dc9de8b4f..9c1dadcb4 100644 --- a/src/lib/bits/calendar/_types.ts +++ b/src/lib/bits/calendar/_types.ts @@ -49,6 +49,15 @@ type Props = Expand< * A callback function called when the placeholder changes. */ onPlaceholderChange?: OnChangeFn; + + /** + * If `true`, the calendar will focus the selected day, + * today, or the first day of the month in that order depending + * on what is visible when the calendar is mounted. + * + * @default false + */ + initialFocus?: boolean; } & AsChild >; diff --git a/src/lib/bits/calendar/components/calendar.svelte b/src/lib/bits/calendar/components/calendar.svelte index 61e83dd1b..97e6105f2 100644 --- a/src/lib/bits/calendar/components/calendar.svelte +++ b/src/lib/bits/calendar/components/calendar.svelte @@ -1,7 +1,8 @@