bits-ui 2.4.0 → 2.5.0

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.
Files changed (56) hide show
  1. package/dist/bits/alert-dialog/components/alert-dialog-content.svelte +1 -0
  2. package/dist/bits/calendar/calendar.svelte.d.ts +77 -1
  3. package/dist/bits/calendar/calendar.svelte.js +169 -9
  4. package/dist/bits/calendar/components/calendar-month-select.svelte +54 -0
  5. package/dist/bits/calendar/components/calendar-month-select.svelte.d.ts +4 -0
  6. package/dist/bits/calendar/components/calendar-year-select.svelte +51 -0
  7. package/dist/bits/calendar/components/calendar-year-select.svelte.d.ts +4 -0
  8. package/dist/bits/calendar/components/calendar.svelte +6 -0
  9. package/dist/bits/calendar/exports.d.ts +3 -1
  10. package/dist/bits/calendar/exports.js +2 -0
  11. package/dist/bits/calendar/types.d.ts +94 -12
  12. package/dist/bits/combobox/types.d.ts +1 -1
  13. package/dist/bits/date-field/date-field.svelte.js +5 -1
  14. package/dist/bits/date-picker/components/date-picker-calendar.svelte +3 -0
  15. package/dist/bits/date-picker/components/date-picker.svelte +4 -0
  16. package/dist/bits/date-picker/date-picker.svelte.d.ts +2 -0
  17. package/dist/bits/date-picker/exports.d.ts +3 -1
  18. package/dist/bits/date-picker/exports.js +2 -0
  19. package/dist/bits/date-picker/types.d.ts +13 -1
  20. package/dist/bits/date-range-field/date-range-field.svelte.js +5 -1
  21. package/dist/bits/date-range-picker/components/date-range-picker-calendar.svelte +5 -0
  22. package/dist/bits/date-range-picker/components/date-range-picker.svelte +10 -0
  23. package/dist/bits/date-range-picker/date-range-picker.svelte.d.ts +5 -0
  24. package/dist/bits/date-range-picker/exports.d.ts +3 -1
  25. package/dist/bits/date-range-picker/exports.js +2 -0
  26. package/dist/bits/date-range-picker/types.d.ts +33 -1
  27. package/dist/bits/dialog/components/dialog-content.svelte +1 -0
  28. package/dist/bits/navigation-menu/components/navigation-menu-content-impl.svelte +1 -0
  29. package/dist/bits/range-calendar/components/range-calendar.svelte +10 -0
  30. package/dist/bits/range-calendar/exports.d.ts +3 -1
  31. package/dist/bits/range-calendar/exports.js +2 -0
  32. package/dist/bits/range-calendar/range-calendar.svelte.d.ts +20 -0
  33. package/dist/bits/range-calendar/range-calendar.svelte.js +120 -6
  34. package/dist/bits/range-calendar/types.d.ts +44 -12
  35. package/dist/bits/scroll-area/scroll-area.svelte.js +1 -1
  36. package/dist/bits/select/components/select-hidden-input.svelte +6 -2
  37. package/dist/bits/select/components/select-hidden-input.svelte.d.ts +2 -1
  38. package/dist/bits/select/components/select.svelte +3 -2
  39. package/dist/bits/select/types.d.ts +5 -0
  40. package/dist/bits/slider/slider.svelte.d.ts +2 -1
  41. package/dist/bits/slider/slider.svelte.js +7 -5
  42. package/dist/bits/utilities/escape-layer/escape-layer.svelte +2 -0
  43. package/dist/bits/utilities/escape-layer/types.d.ts +2 -0
  44. package/dist/bits/utilities/escape-layer/use-escape-layer.svelte.d.ts +5 -1
  45. package/dist/bits/utilities/escape-layer/use-escape-layer.svelte.js +4 -1
  46. package/dist/bits/utilities/floating-layer/use-floating-layer.svelte.js +2 -0
  47. package/dist/bits/utilities/popper-layer/popper-layer-inner.svelte +1 -1
  48. package/dist/internal/date-time/calendar-helpers.svelte.d.ts +8 -2
  49. package/dist/internal/date-time/calendar-helpers.svelte.js +47 -15
  50. package/dist/internal/date-time/formatter.d.ts +8 -1
  51. package/dist/internal/date-time/formatter.js +16 -3
  52. package/dist/internal/floating-svelte/types.d.ts +12 -0
  53. package/dist/internal/floating-svelte/use-floating.svelte.js +18 -12
  54. package/dist/internal/use-grace-area.svelte.js +5 -2
  55. package/dist/shared/attributes.d.ts +3 -1
  56. package/package.json +1 -1
@@ -309,21 +309,23 @@ export function getWeekdays({ months, formatter, weekdayFormat }) {
309
309
  * which determines the month to show in the calendar.
310
310
  */
311
311
  export function useMonthViewOptionsSync(props) {
312
- const weekStartsOn = props.weekStartsOn.current;
313
- const locale = props.locale.current;
314
- const fixedWeeks = props.fixedWeeks.current;
315
- const numberOfMonths = props.numberOfMonths.current;
316
- untrack(() => {
317
- const placeholder = props.placeholder.current;
318
- if (!placeholder)
319
- return;
320
- const defaultMonthProps = {
321
- weekStartsOn,
322
- locale,
323
- fixedWeeks,
324
- numberOfMonths,
325
- };
326
- props.setMonths(createMonths({ ...defaultMonthProps, dateObj: placeholder }));
312
+ $effect(() => {
313
+ const weekStartsOn = props.weekStartsOn.current;
314
+ const locale = props.locale.current;
315
+ const fixedWeeks = props.fixedWeeks.current;
316
+ const numberOfMonths = props.numberOfMonths.current;
317
+ untrack(() => {
318
+ const placeholder = props.placeholder.current;
319
+ if (!placeholder)
320
+ return;
321
+ const defaultMonthProps = {
322
+ weekStartsOn,
323
+ locale,
324
+ fixedWeeks,
325
+ numberOfMonths,
326
+ };
327
+ props.setMonths(createMonths({ ...defaultMonthProps, dateObj: placeholder }));
328
+ });
327
329
  });
328
330
  }
329
331
  /**
@@ -529,5 +531,35 @@ export const calendarAttrs = createBitsAttrs({
529
531
  "head-cell",
530
532
  "header",
531
533
  "heading",
534
+ "month-select",
535
+ "year-select",
532
536
  ],
533
537
  });
538
+ export function getDefaultYears(opts) {
539
+ const currentYear = new Date().getFullYear();
540
+ const latestYear = Math.max(opts.placeholderYear, currentYear);
541
+ // use minValue/maxValue as boundaries if provided, otherwise calculate default range
542
+ let minYear;
543
+ let maxYear;
544
+ if (opts.minValue) {
545
+ minYear = opts.minValue.year;
546
+ }
547
+ else {
548
+ // (111 years: latestYear - 100 to latestYear + 10)
549
+ const initialMinYear = latestYear - 100;
550
+ minYear =
551
+ opts.placeholderYear < initialMinYear ? opts.placeholderYear - 10 : initialMinYear;
552
+ }
553
+ if (opts.maxValue) {
554
+ maxYear = opts.maxValue.year;
555
+ }
556
+ else {
557
+ maxYear = latestYear + 10;
558
+ }
559
+ // ensure we have at least one year and minYear <= maxYear
560
+ if (minYear > maxYear) {
561
+ minYear = maxYear;
562
+ }
563
+ const totalYears = maxYear - minYear + 1;
564
+ return Array.from({ length: totalYears }, (_, i) => minYear + i);
565
+ }
@@ -1,7 +1,13 @@
1
1
  import { type DateValue } from "@internationalized/date";
2
2
  import type { HourCycle, TimeValue } from "../../shared/date/types.js";
3
+ import type { ReadableBox } from "svelte-toolbelt";
3
4
  export type Formatter = ReturnType<typeof createFormatter>;
4
5
  export type TimeFormatter = ReturnType<typeof createTimeFormatter>;
6
+ type CreateFormatterOptions = {
7
+ initialLocale: string;
8
+ monthFormat: ReadableBox<Intl.DateTimeFormatOptions["month"] | ((month: number) => string)>;
9
+ yearFormat: ReadableBox<Intl.DateTimeFormatOptions["year"] | ((year: number) => string)>;
10
+ };
5
11
  /**
6
12
  * Creates a wrapper around the `DateFormatter`, which is
7
13
  * an improved version of the {@link Intl.DateTimeFormat} API,
@@ -10,7 +16,7 @@ export type TimeFormatter = ReturnType<typeof createTimeFormatter>;
10
16
  *
11
17
  * @see [DateFormatter](https://react-spectrum.adobe.com/internationalized/date/DateFormatter.html)
12
18
  */
13
- export declare function createFormatter(initialLocale: string): {
19
+ export declare function createFormatter(opts: CreateFormatterOptions): {
14
20
  setLocale: (newLocale: string) => void;
15
21
  getLocale: () => string;
16
22
  fullMonth: (date: Date) => string;
@@ -32,3 +38,4 @@ export declare function createTimeFormatter(initialLocale: string): {
32
38
  dayPeriod: (date: Date, hourCycle?: HourCycle | undefined) => "AM" | "PM";
33
39
  selectedTime: (date: TimeValue) => string;
34
40
  };
41
+ export {};
@@ -17,8 +17,8 @@ const defaultPartOptions = {
17
17
  *
18
18
  * @see [DateFormatter](https://react-spectrum.adobe.com/internationalized/date/DateFormatter.html)
19
19
  */
20
- export function createFormatter(initialLocale) {
21
- let locale = initialLocale;
20
+ export function createFormatter(opts) {
21
+ let locale = opts.initialLocale;
22
22
  function setLocale(newLocale) {
23
23
  locale = newLocale;
24
24
  }
@@ -42,7 +42,20 @@ export function createFormatter(initialLocale) {
42
42
  }
43
43
  }
44
44
  function fullMonthAndYear(date) {
45
- return new DateFormatter(locale, { month: "long", year: "numeric" }).format(date);
45
+ if (typeof opts.monthFormat.current !== "function" &&
46
+ typeof opts.yearFormat.current !== "function") {
47
+ return new DateFormatter(locale, {
48
+ month: opts.monthFormat.current,
49
+ year: opts.yearFormat.current,
50
+ }).format(date);
51
+ }
52
+ const formattedMonth = typeof opts.monthFormat.current === "function"
53
+ ? opts.monthFormat.current(date.getMonth() + 1)
54
+ : new DateFormatter(locale, { month: opts.monthFormat.current }).format(date);
55
+ const formattedYear = typeof opts.yearFormat.current === "function"
56
+ ? opts.yearFormat.current(date.getFullYear())
57
+ : new DateFormatter(locale, { year: opts.yearFormat.current }).format(date);
58
+ return `${formattedMonth} ${formattedYear}`;
46
59
  }
47
60
  function fullMonth(date) {
48
61
  return new DateFormatter(locale, { month: "long" }).format(date);
@@ -41,6 +41,18 @@ export type UseFloatingOptions = {
41
41
  * @default undefined
42
42
  */
43
43
  whileElementsMounted?: (reference: ReferenceElement, floating: FloatingElement, update: () => void) => () => void;
44
+ /**
45
+ * The offset from the reference element along the side axis.
46
+ * Used to detect bad coordinates during transitions.
47
+ * @default undefined
48
+ */
49
+ sideOffset?: ValueOrGetValue<number | undefined>;
50
+ /**
51
+ * The offset from the reference element along the alignment axis.
52
+ * Used to detect bad coordinates during transitions.
53
+ * @default undefined
54
+ */
55
+ alignOffset?: ValueOrGetValue<number | undefined>;
44
56
  };
45
57
  export type UseFloatingReturn = {
46
58
  /**
@@ -9,6 +9,8 @@ export function useFloating(options) {
9
9
  const transformOption = $derived(get(options.transform) ?? true);
10
10
  const placementOption = $derived(get(options.placement) ?? "bottom");
11
11
  const strategyOption = $derived(get(options.strategy) ?? "absolute");
12
+ const sideOffsetOption = $derived(get(options.sideOffset) ?? 0);
13
+ const alignOffsetOption = $derived(get(options.alignOffset) ?? 0);
12
14
  const reference = options.reference;
13
15
  /** State */
14
16
  let x = $state(0);
@@ -19,21 +21,17 @@ export function useFloating(options) {
19
21
  let middlewareData = $state({});
20
22
  let isPositioned = $state(false);
21
23
  const floatingStyles = $derived.by(() => {
22
- const initialStyles = {
23
- position: strategy,
24
- left: "0",
25
- top: "0",
26
- };
27
- if (!floating.current) {
28
- return initialStyles;
29
- }
30
- const xVal = roundByDPR(floating.current, x);
31
- const yVal = roundByDPR(floating.current, y);
24
+ // preserve last known position when floating ref is null (during transitions)
25
+ const xVal = floating.current ? roundByDPR(floating.current, x) : x;
26
+ const yVal = floating.current ? roundByDPR(floating.current, y) : y;
32
27
  if (transformOption) {
33
28
  return {
34
- ...initialStyles,
29
+ position: strategy,
30
+ left: "0",
31
+ top: "0",
35
32
  transform: `translate(${xVal}px, ${yVal}px)`,
36
- ...(getDPR(floating.current) >= 1.5 && {
33
+ ...(floating.current &&
34
+ getDPR(floating.current) >= 1.5 && {
37
35
  willChange: "transform",
38
36
  }),
39
37
  };
@@ -54,6 +52,14 @@ export function useFloating(options) {
54
52
  placement: placementOption,
55
53
  strategy: strategyOption,
56
54
  }).then((position) => {
55
+ // ignore bad coordinates that cause jumping during close transitions
56
+ if (!openOption && x !== 0 && y !== 0) {
57
+ // if we had a good position and now getting coordinates near
58
+ // the expected offset bounds during close, ignore it
59
+ const maxExpectedOffset = Math.max(Math.abs(sideOffsetOption), Math.abs(alignOffsetOption), 15);
60
+ if (position.x <= maxExpectedOffset && position.y <= maxExpectedOffset)
61
+ return;
62
+ }
57
63
  x = position.x;
58
64
  y = position.y;
59
65
  strategy = position.strategy;
@@ -1,4 +1,4 @@
1
- import { executeCallbacks, getWindow } from "svelte-toolbelt";
1
+ import { executeCallbacks, getDocument, getWindow } from "svelte-toolbelt";
2
2
  import { on } from "svelte/events";
3
3
  import { watch } from "runed";
4
4
  import { boxAutoReset } from "./box-auto-reset.svelte.js";
@@ -60,7 +60,10 @@ export function useGraceArea(opts) {
60
60
  opts.onPointerExit();
61
61
  }
62
62
  };
63
- return on(document, "pointermove", handleTrackPointerGrace);
63
+ const doc = getDocument(opts.triggerNode() ?? opts.contentNode());
64
+ if (!doc)
65
+ return;
66
+ return on(doc, "pointermove", handleTrackPointerGrace);
64
67
  });
65
68
  return {
66
69
  isPointerInTransit,
@@ -1,4 +1,4 @@
1
- import type { HTMLAnchorAttributes, HTMLAttributes, HTMLButtonAttributes, HTMLImgAttributes, HTMLInputAttributes, HTMLLabelAttributes, HTMLLiAttributes, HTMLTableAttributes, HTMLTdAttributes, HTMLThAttributes, SVGAttributes } from "svelte/elements";
1
+ import type { HTMLAnchorAttributes, HTMLAttributes, HTMLButtonAttributes, HTMLImgAttributes, HTMLInputAttributes, HTMLLabelAttributes, HTMLLiAttributes, HTMLSelectAttributes, HTMLTableAttributes, HTMLTdAttributes, HTMLThAttributes, SVGAttributes } from "svelte/elements";
2
2
  export type BitsDivAttributes = HTMLAttributes<HTMLDivElement>;
3
3
  export type BitsSpanAttributes = HTMLAttributes<HTMLSpanElement>;
4
4
  export type BitsHeadingAttributes = HTMLAttributes<HTMLHeadingElement>;
@@ -7,6 +7,7 @@ export type BitsElementAttributes = HTMLAttributes<HTMLElement>;
7
7
  export type BitsTableSectionAttributes = HTMLAttributes<HTMLTableSectionElement>;
8
8
  export type BitsTableRowAttributes = HTMLAttributes<HTMLTableRowElement>;
9
9
  export type BitsSVGElementAttributes = SVGAttributes<SVGElement>;
10
+ export type BitsSelectAttributes = HTMLSelectAttributes;
10
11
  /**
11
12
  * We override the `id` prop type to not allow it to be `null`. We rely on the
12
13
  * `id` heavily in the internals of Bits UI for node references. We also override
@@ -35,4 +36,5 @@ export type BitsPrimitiveTbodyAttributes = BitsPrimitive<BitsTableSectionAttribu
35
36
  export type BitsPrimitiveTrAttributes = BitsPrimitive<BitsTableRowAttributes>;
36
37
  export type BitsPrimitiveTheadAttributes = BitsPrimitive<BitsTableSectionAttributes>;
37
38
  export type BitsPrimitiveHeaderAttributes = BitsPrimitive<BitsElementAttributes>;
39
+ export type BitsPrimitiveSelectAttributes = BitsPrimitive<BitsSelectAttributes>;
38
40
  export {};
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "bits-ui",
3
- "version": "2.4.0",
3
+ "version": "2.5.0",
4
4
  "license": "MIT",
5
5
  "repository": "github:huntabyte/bits-ui",
6
6
  "funding": "https://github.com/sponsors/huntabyte",