bits-ui 0.10.1 → 0.10.3

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,7 +1,7 @@
1
1
  /// <reference types="svelte" />
2
2
  import { type Accordion as AccordionReturn, type CreateAccordionProps } from "@melt-ui/svelte";
3
3
  import type { ItemProps } from "./types.js";
4
- export declare const getAttrs: (part: "content" | "root" | "header" | "item" | "trigger") => Record<string, string>;
4
+ export declare const getAttrs: (part: "content" | "header" | "root" | "item" | "trigger") => Record<string, string>;
5
5
  type GetReturn = AccordionReturn;
6
6
  export declare function setCtx<Multiple extends boolean>(props: CreateAccordionProps<Multiple>): {
7
7
  updateOption: <K extends string, V extends unknown>(key: K, value: V | undefined) => void;
@@ -1,6 +1,6 @@
1
1
  /// <reference types="svelte" />
2
2
  import { type CreateAvatarProps, type Avatar as AvatarReturn } from "@melt-ui/svelte";
3
- export declare const getAttrs: (part: "root" | "image" | "fallback") => Record<string, string>;
3
+ export declare const getAttrs: (part: "image" | "root" | "fallback") => Record<string, string>;
4
4
  export declare function setCtx(props: CreateAvatarProps): {
5
5
  updateOption: <K extends string, V extends unknown>(key: K, value: V | undefined) => void;
6
6
  elements: {
@@ -33,6 +33,14 @@ type Props<Multiple extends boolean = false> = Expand<OmitCalendarProps<CreateCa
33
33
  * A callback function called when the placeholder changes.
34
34
  */
35
35
  onPlaceholderChange?: OnChangeFn<DateValue>;
36
+ /**
37
+ * If `true`, the calendar will focus the selected day,
38
+ * today, or the first day of the month in that order depending
39
+ * on what is visible when the calendar is mounted.
40
+ *
41
+ * @default false
42
+ */
43
+ initialFocus?: boolean;
36
44
  } & AsChild>;
37
45
  type PrevButtonProps = AsChild;
38
46
  type NextButtonProps = AsChild;
@@ -1,5 +1,7 @@
1
- <script>import { createDispatcher } from "../../../internal/events.js";
1
+ <script>import { handleCalendarInitialFocus } from "../../../internal/focus.js";
2
+ import { createDispatcher } from "../../../internal/events.js";
2
3
  import { melt } from "@melt-ui/svelte";
4
+ import { onMount } from "svelte";
3
5
  import { setCtx, getAttrs } from "../ctx.js";
4
6
  export let placeholder = void 0;
5
7
  export let onPlaceholderChange = void 0;
@@ -22,6 +24,13 @@ export let multiple = false;
22
24
  export let asChild = false;
23
25
  export let id = void 0;
24
26
  export let numberOfMonths = void 0;
27
+ export let initialFocus = false;
28
+ let el = void 0;
29
+ onMount(() => {
30
+ if (!initialFocus || !el)
31
+ return;
32
+ handleCalendarInitialFocus(el);
33
+ });
25
34
  const {
26
35
  elements: { calendar },
27
36
  states: {
@@ -118,7 +127,12 @@ $:
118
127
  {#if asChild}
119
128
  <slot {...slotProps} />
120
129
  {:else}
121
- <div {...builder} use:builder.action {...$$restProps} on:m-keydown={dispatch}>
130
+ <div
131
+ {...builder} use:builder.action
132
+ {...$$restProps}
133
+ on:m-keydown={dispatch}
134
+ bind:this={el}
135
+ >
122
136
  <slot {...slotProps} />
123
137
  </div>
124
138
  {/if}
@@ -1,6 +1,6 @@
1
1
  /// <reference types="svelte" />
2
2
  import { type CreateCalendarProps } from "@melt-ui/svelte";
3
- export declare const getAttrs: (part: "date" | "root" | "header" | "cell" | "grid" | "heading" | "prev-button" | "next-button" | "grid-head" | "head-cell" | "grid-body" | "grid-row") => Record<string, string>;
3
+ export declare const getAttrs: (part: "date" | "header" | "root" | "cell" | "grid" | "heading" | "prev-button" | "next-button" | "grid-head" | "head-cell" | "grid-body" | "grid-row") => Record<string, string>;
4
4
  export declare function setCtx<Multiple extends boolean>(props: CreateCalendarProps<Multiple>): {
5
5
  updateOption: <K extends string, V extends unknown>(key: K, value: V | undefined) => void;
6
6
  elements: {
@@ -1,6 +1,6 @@
1
1
  /// <reference types="svelte" />
2
2
  import { type Checkbox as CheckboxReturn, type CreateCheckboxProps } from "@melt-ui/svelte";
3
- export declare const getAttrs: (part: "root" | "input" | "indicator") => Record<string, string>;
3
+ export declare const getAttrs: (part: "input" | "root" | "indicator") => Record<string, string>;
4
4
  export declare function setCtx(props: CreateCheckboxProps): {
5
5
  updateOption: <K extends string, V extends unknown>(key: K, value: V | undefined) => void;
6
6
  elements: {
@@ -1,7 +1,7 @@
1
1
  /// <reference types="svelte" />
2
2
  import { type CreateRadioGroupProps, type RadioGroup as RadioGroupReturn } from "@melt-ui/svelte";
3
3
  import type { Readable } from "svelte/store";
4
- export declare const getAttrs: (part: "root" | "item" | "input" | "item-indicator") => Record<string, string>;
4
+ export declare const getAttrs: (part: "input" | "root" | "item" | "item-indicator") => Record<string, string>;
5
5
  export declare function setCtx(props: CreateRadioGroupProps): {
6
6
  updateOption: <K extends string, V extends unknown>(key: K, value: V | undefined) => void;
7
7
  elements: {
@@ -8,10 +8,39 @@ import type { DateRange } from "../../shared/index.js";
8
8
  import type { DateValue } from "@internationalized/date";
9
9
  import type { CreateRangeCalendarProps } from "@melt-ui/svelte";
10
10
  type Props = Expand<Omit<CreateRangeCalendarProps, "placeholder" | "defaultPlaceholder" | "value" | "defaultValue" | "onPlaceholderChange" | "onValueChange" | "ids"> & {
11
- placeholder?: DateValue;
11
+ /**
12
+ * The selected date range. This updates as the user selects
13
+ * date ranges in the calendar.
14
+ *
15
+ * You can bind this to a value to programmatically control the
16
+ * value state.
17
+ */
12
18
  value?: DateRange;
13
- onPlaceholderChange?: OnChangeFn<DateValue>;
19
+ /**
20
+ * A callback function called when the value changes.
21
+ */
14
22
  onValueChange?: OnChangeFn<DateRange>;
23
+ /**
24
+ * The placeholder date, used to display the calendar when no
25
+ * date is selected. This updates as the user navigates
26
+ * the calendar.
27
+ *
28
+ * You can bind this to a value to programmatically control the
29
+ * placeholder state.
30
+ */
31
+ placeholder?: DateValue;
32
+ /**
33
+ * A callback function called when the placeholder changes.
34
+ */
35
+ onPlaceholderChange?: OnChangeFn<DateValue>;
36
+ /**
37
+ * If `true`, the calendar will focus the selected day,
38
+ * today, or the first day of the month in that order depending
39
+ * on what is visible when the calendar is mounted.
40
+ *
41
+ * @default false
42
+ */
43
+ initialFocus?: boolean;
15
44
  } & AsChild>;
16
45
  type PrevButtonProps = AsChild;
17
46
  type NextButtonProps = AsChild;
@@ -1,6 +1,8 @@
1
1
  <script>import { melt } from "@melt-ui/svelte";
2
2
  import { setCtx, getAttrs } from "../ctx.js";
3
3
  import { createDispatcher } from "../../../internal/events.js";
4
+ import { onMount } from "svelte";
5
+ import { handleCalendarInitialFocus } from "../../../internal/focus.js";
4
6
  export let placeholder = void 0;
5
7
  export let onPlaceholderChange = void 0;
6
8
  export let value = void 0;
@@ -20,6 +22,13 @@ export let calendarLabel = void 0;
20
22
  export let asChild = false;
21
23
  export let id = void 0;
22
24
  export let weekdayFormat = void 0;
25
+ export let initialFocus = false;
26
+ let el = void 0;
27
+ onMount(() => {
28
+ if (!initialFocus || !el)
29
+ return;
30
+ handleCalendarInitialFocus(el);
31
+ });
23
32
  const {
24
33
  elements: { calendar },
25
34
  states: {
@@ -112,7 +121,12 @@ $:
112
121
  {#if asChild}
113
122
  <slot {...slotProps} />
114
123
  {:else}
115
- <div {...builder} use:builder.action {...$$restProps} on:m-keydown={dispatch}>
124
+ <div
125
+ {...builder} use:builder.action
126
+ {...$$restProps}
127
+ on:m-keydown={dispatch}
128
+ bind:this={el}
129
+ >
116
130
  <slot {...slotProps} />
117
131
  </div>
118
132
  {/if}
@@ -1,6 +1,6 @@
1
1
  /// <reference types="svelte" />
2
2
  import { type CreateRangeCalendarProps } from "@melt-ui/svelte";
3
- export declare const getAttrs: (part: "date" | "root" | "header" | "cell" | "grid" | "heading" | "prev-button" | "next-button" | "grid-head" | "head-cell" | "grid-body" | "grid-row") => Record<string, string>;
3
+ export declare const getAttrs: (part: "date" | "header" | "root" | "cell" | "grid" | "heading" | "prev-button" | "next-button" | "grid-head" | "head-cell" | "grid-body" | "grid-row") => Record<string, string>;
4
4
  export declare function setCtx(props: CreateRangeCalendarProps): {
5
5
  updateOption: <K extends string, V extends unknown>(key: K, value: V | undefined) => void;
6
6
  elements: {
@@ -16,7 +16,7 @@ declare const __propDef: {
16
16
  } & {
17
17
  [x: `data-melt-${string}`]: "";
18
18
  } & {
19
- action: (node: HTMLElement) => import("@melt-ui/svelte/internal/types").MeltActionReturn<"keydown" | "click" | "input">;
19
+ action: (node: HTMLElement) => import("@melt-ui/svelte/internal/types").MeltActionReturn<"input" | "keydown" | "click">;
20
20
  };
21
21
  };
22
22
  };
@@ -2,7 +2,7 @@
2
2
  import { type CreateSelectProps, type Select as SelectReturn } from "@melt-ui/svelte";
3
3
  import type { Writable } from "svelte/store";
4
4
  import type { FloatingProps } from "../floating/_types.js";
5
- export declare const getAttrs: (part: "content" | "value" | "arrow" | "label" | "group" | "item" | "trigger" | "input" | "indicator") => Record<string, string>;
5
+ export declare const getAttrs: (part: "content" | "value" | "arrow" | "label" | "group" | "input" | "item" | "trigger" | "indicator") => Record<string, string>;
6
6
  type GetReturn = SelectReturn;
7
7
  export declare function getCtx(): GetReturn;
8
8
  type Items<T> = {
@@ -26,7 +26,7 @@ export declare function setCtx<T = unknown, M extends boolean = false>(props: Pr
26
26
  update: (updater: import("svelte/store").Updater<boolean>, sideEffect?: ((newValue: boolean) => void) | undefined) => void;
27
27
  set: (this: void, value: boolean) => void;
28
28
  subscribe(this: void, run: import("svelte/store").Subscriber<boolean>, invalidate?: import("svelte/store").Invalidator<boolean> | undefined): import("svelte/store").Unsubscriber;
29
- }, Writable<HTMLElement | null>, Writable<boolean>, Writable<string>, Writable<string>, Writable<string>], (node: HTMLElement) => import("@melt-ui/svelte/internal/types").MeltActionReturn<"keydown" | "click" | "input">, ([$open, $highlightedItem, $disabled, $menuId, $triggerId, $labelId]: [boolean, HTMLElement | null, boolean, string, string, string]) => {
29
+ }, Writable<HTMLElement | null>, Writable<boolean>, Writable<string>, Writable<string>, Writable<string>], (node: HTMLElement) => import("@melt-ui/svelte/internal/types").MeltActionReturn<"input" | "keydown" | "click">, ([$open, $highlightedItem, $disabled, $menuId, $triggerId, $labelId]: [boolean, HTMLElement | null, boolean, string, string, string]) => {
30
30
  readonly 'aria-activedescendant': string | undefined;
31
31
  readonly 'aria-autocomplete': "list";
32
32
  readonly 'aria-controls': string;
@@ -1,6 +1,6 @@
1
1
  /// <reference types="svelte" />
2
2
  import { type CreateSliderProps, type Slider as SliderReturn } from "@melt-ui/svelte";
3
- export declare const getAttrs: (part: "root" | "input" | "range" | "thumb" | "tick") => Record<string, string>;
3
+ export declare const getAttrs: (part: "input" | "root" | "range" | "thumb" | "tick") => Record<string, string>;
4
4
  export declare function setCtx(props: CreateSliderProps): {
5
5
  updateOption: <K extends string, V extends unknown>(key: K, value: V | undefined) => void;
6
6
  elements: {
@@ -1,6 +1,6 @@
1
1
  /// <reference types="svelte" />
2
2
  import { type CreateSwitchProps, type Switch as SwitchReturn } from "@melt-ui/svelte";
3
- export declare const getAttrs: (part: "root" | "input" | "thumb") => Record<string, string>;
3
+ export declare const getAttrs: (part: "input" | "root" | "thumb") => Record<string, string>;
4
4
  export declare function setCtx(props: CreateSwitchProps): {
5
5
  updateOption: <K extends string, V extends unknown>(key: K, value: V | undefined) => void;
6
6
  elements: {
@@ -1,6 +1,6 @@
1
1
  /// <reference types="svelte" />
2
2
  import { type CreateToggleProps, type Toggle as ToggleReturn } from "@melt-ui/svelte";
3
- export declare const getAttrs: (part: "root" | "input") => Record<string, string>;
3
+ export declare const getAttrs: (part: "input" | "root") => Record<string, string>;
4
4
  export declare function setCtx(props: CreateToggleProps): {
5
5
  updateOption: <K extends string, V extends unknown>(key: K, value: V | undefined) => void;
6
6
  elements: {
@@ -0,0 +1,6 @@
1
+ /**
2
+ * Handles `initialFocus` prop behavior for the
3
+ * Calendar & RangeCalendar components.
4
+ */
5
+ export declare function handleCalendarInitialFocus(calendar: HTMLElement): void;
6
+ export declare function focusWithoutScroll(element: HTMLElement): void;
@@ -0,0 +1,26 @@
1
+ import { isBrowser } from ".";
2
+ /**
3
+ * Handles `initialFocus` prop behavior for the
4
+ * Calendar & RangeCalendar components.
5
+ */
6
+ export function handleCalendarInitialFocus(calendar) {
7
+ if (!isBrowser)
8
+ return;
9
+ const selectedDay = calendar.querySelector("[data-selected]");
10
+ if (selectedDay)
11
+ return focusWithoutScroll(selectedDay);
12
+ const today = calendar.querySelector("[data-today]");
13
+ if (today)
14
+ return focusWithoutScroll(today);
15
+ const firstDay = calendar.querySelector("[data-calendar-date]");
16
+ if (firstDay)
17
+ return focusWithoutScroll(firstDay);
18
+ }
19
+ export function focusWithoutScroll(element) {
20
+ const scrollPosition = {
21
+ x: window.pageXOffset || document.documentElement.scrollLeft,
22
+ y: window.pageYOffset || document.documentElement.scrollTop
23
+ };
24
+ element.focus();
25
+ window.scrollTo(scrollPosition.x, scrollPosition.y);
26
+ }
@@ -8,3 +8,4 @@ export * from "./sleep.js";
8
8
  export * from "./style.js";
9
9
  export * from "./types.js";
10
10
  export * from "./updater.js";
11
+ export * from "./focus.js";
@@ -8,3 +8,4 @@ export * from "./sleep.js";
8
8
  export * from "./style.js";
9
9
  export * from "./types.js";
10
10
  export * from "./updater.js";
11
+ export * from "./focus.js";
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "bits-ui",
3
- "version": "0.10.1",
3
+ "version": "0.10.3",
4
4
  "license": "MIT",
5
5
  "repository": "github:huntabyte/bits-ui",
6
6
  "exports": {
@@ -74,7 +74,7 @@
74
74
  "type": "module",
75
75
  "dependencies": {
76
76
  "@internationalized/date": "^3.5.0",
77
- "@melt-ui/svelte": "0.64.3",
77
+ "@melt-ui/svelte": "0.64.4",
78
78
  "nanoid": "^5.0.4"
79
79
  },
80
80
  "peerDependencies": {