Skip to content

Commit 08b1549

Browse files
authored
fix: silence state_referenced_locally compiler warnings in consumer builds (#2179)
1 parent 4a6a892 commit 08b1549

31 files changed

Lines changed: 148 additions & 89 deletions

File tree

‎.changeset/tidy-geese-untrack.md‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"bits-ui": patch
3+
---
4+
5+
fix: silence `state_referenced_locally` compiler warnings emitted in consumer builds by wrapping intentional initialization-time prop reads in `untrack`

‎packages/bits-ui/src/lib/bits/accordion/components/accordion.svelte‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
<script lang="ts">
22
import { type WritableBox, boxWith, mergeProps } from "svelte-toolbelt";
3+
import { untrack } from "svelte";
34
import { AccordionRootState } from "../accordion.svelte.js";
45
import type { AccordionRootProps } from "../types.js";
56
import { noop } from "$lib/internal/noop.js";
@@ -38,7 +39,7 @@
3839
);
3940
4041
const rootState = AccordionRootState.create({
41-
type,
42+
type: untrack(() => type),
4243
value: boxWith(
4344
() => value!,
4445
(v) => {

‎packages/bits-ui/src/lib/bits/calendar/components/calendar.svelte‎

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
<script lang="ts">
22
import { watch } from "runed";
33
import { boxWith, mergeProps } from "svelte-toolbelt";
4+
import { untrack } from "svelte";
45
import { type DateValue } from "@internationalized/date";
56
import { CalendarRootState } from "../calendar.svelte.js";
67
import type { CalendarRootProps } from "../types.js";
@@ -41,11 +42,13 @@
4142
...restProps
4243
}: CalendarRootProps = $props();
4344
44-
const defaultPlaceholder = getDefaultDate({
45-
defaultValue: value,
46-
minValue,
47-
maxValue,
48-
});
45+
const defaultPlaceholder = untrack(() =>
46+
getDefaultDate({
47+
defaultValue: value,
48+
minValue,
49+
maxValue,
50+
})
51+
);
4952
5053
function handleDefaultPlaceholder() {
5154
if (placeholder !== undefined) return;

‎packages/bits-ui/src/lib/bits/checkbox/components/checkbox.svelte‎

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
<script lang="ts">
22
import { boxWith, mergeProps } from "svelte-toolbelt";
3+
import { untrack } from "svelte";
34
import type { CheckboxRootProps } from "../types.js";
45
import { CheckboxGroupContext, CheckboxRootState } from "../checkbox.svelte.js";
56
import CheckboxInput from "./checkbox-input.svelte";
@@ -29,13 +30,15 @@
2930
3031
const group = CheckboxGroupContext.getOr(null);
3132
32-
if (group && value) {
33-
if (group.opts.value.current.includes(value)) {
34-
checked = true;
35-
} else {
36-
checked = false;
33+
untrack(() => {
34+
if (group && value) {
35+
if (group.opts.value.current.includes(value)) {
36+
checked = true;
37+
} else {
38+
checked = false;
39+
}
3740
}
38-
}
41+
});
3942
4043
watch.pre(
4144
() => value,

‎packages/bits-ui/src/lib/bits/combobox/components/combobox-input.svelte‎

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
<script lang="ts">
22
import { boxWith, mergeProps } from "svelte-toolbelt";
3+
import { untrack } from "svelte";
34
import type { ComboboxInputProps } from "../types.js";
45
import { useId } from "$lib/internal/use-id.js";
56
import { FloatingLayer } from "$lib/bits/utilities/floating-layer/index.js";
@@ -23,9 +24,11 @@
2324
clearOnDeselect: boxWith(() => clearOnDeselect),
2425
});
2526
26-
if (defaultValue) {
27-
inputState.root.opts.inputValue.current = defaultValue;
28-
}
27+
untrack(() => {
28+
if (defaultValue) {
29+
inputState.root.opts.inputValue.current = defaultValue;
30+
}
31+
});
2932
3033
const mergedProps = $derived(
3134
mergeProps(restProps, inputState.props, { value: inputState.root.opts.inputValue.current })

‎packages/bits-ui/src/lib/bits/combobox/components/combobox.svelte‎

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
<script lang="ts">
22
import { type WritableBox, boxWith } from "svelte-toolbelt";
3+
import { untrack } from "svelte";
34
import type { ComboboxRootProps } from "../types.js";
45
import { noop } from "$lib/internal/noop.js";
56
import FloatingLayer from "$lib/bits/utilities/floating-layer/components/floating-layer.svelte";
@@ -26,7 +27,7 @@
2627
}: ComboboxRootProps = $props();
2728
2829
if (value === undefined) {
29-
const defaultValue = type === "single" ? "" : [];
30+
const defaultValue = untrack(() => (type === "single" ? "" : []));
3031
value = defaultValue;
3132
}
3233
@@ -39,7 +40,7 @@
3940
);
4041
4142
const rootState = SelectRootState.create({
42-
type,
43+
type: untrack(() => type),
4344
value: boxWith(
4445
() => value!,
4546
(v) => {

‎packages/bits-ui/src/lib/bits/date-field/components/date-field-segment.svelte‎

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
<script lang="ts">
22
import { boxWith, mergeProps } from "svelte-toolbelt";
3+
import { untrack } from "svelte";
34
import { DateFieldSegmentState } from "../date-field.svelte.js";
45
import type { DateFieldSegmentProps } from "../types.js";
56
import { createId } from "$lib/internal/create-id.js";
@@ -15,13 +16,16 @@
1516
...restProps
1617
}: DateFieldSegmentProps = $props();
1718
18-
const segmentState = DateFieldSegmentState.create(part, {
19-
id: boxWith(() => id),
20-
ref: boxWith(
21-
() => ref,
22-
(v) => (ref = v)
23-
),
24-
});
19+
const segmentState = DateFieldSegmentState.create(
20+
untrack(() => part),
21+
{
22+
id: boxWith(() => id),
23+
ref: boxWith(
24+
() => ref,
25+
(v) => (ref = v)
26+
),
27+
}
28+
);
2529
2630
const mergedProps = $derived(
2731
mergeProps(restProps, segmentState.props as Record<string, unknown>)

‎packages/bits-ui/src/lib/bits/date-picker/components/date-picker.svelte‎

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
// Date Picker composes the DateField, Popover, and Calendar components
33
import { watch } from "runed";
44
import { boxWith } from "svelte-toolbelt";
5+
import { untrack } from "svelte";
56
import type { DateValue } from "@internationalized/date";
67
import { DatePickerRootState } from "../date-picker.svelte.js";
78
import type { DatePickerRootProps } from "../types.js";
@@ -50,12 +51,14 @@
5051
yearFormat = "numeric",
5152
}: DatePickerRootProps = $props();
5253
53-
const defaultPlaceholder = getDefaultDate({
54-
granularity,
55-
defaultValue: value,
56-
minValue,
57-
maxValue,
58-
});
54+
const defaultPlaceholder = untrack(() =>
55+
getDefaultDate({
56+
granularity,
57+
defaultValue: value,
58+
minValue,
59+
maxValue,
60+
})
61+
);
5962
6063
function handleDefaultPlaceholder() {
6164
if (placeholder !== undefined) return;

‎packages/bits-ui/src/lib/bits/date-range-field/components/date-range-field-input.svelte‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
<script lang="ts">
22
import { boxWith, mergeProps } from "svelte-toolbelt";
3+
import { untrack } from "svelte";
34
import type { DateRangeFieldInputProps } from "../types.js";
45
import { DateRangeFieldInputState } from "../date-range-field.svelte.js";
56
import { createId } from "$lib/internal/create-id.js";
@@ -26,7 +27,7 @@
2627
),
2728
name: boxWith(() => name),
2829
},
29-
type
30+
untrack(() => type)
3031
);
3132
3233
const mergedProps = $derived(mergeProps(restProps, inputState.props, { role: "presentation" }));

‎packages/bits-ui/src/lib/bits/date-range-picker/components/date-range-picker.svelte‎

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
<script lang="ts">
22
import { watch } from "runed";
33
import { boxWith, mergeProps } from "svelte-toolbelt";
4+
import { untrack } from "svelte";
45
import type { DateValue } from "@internationalized/date";
56
import { DateRangePickerRootState } from "../date-range-picker.svelte.js";
67
import type { DateRangePickerRootProps } from "../types.js";
@@ -82,12 +83,14 @@
8283
}
8384
);
8485
85-
const defaultPlaceholder = getDefaultDate({
86-
granularity,
87-
defaultValue: value?.start,
88-
minValue,
89-
maxValue,
90-
});
86+
const defaultPlaceholder = untrack(() =>
87+
getDefaultDate({
88+
granularity,
89+
defaultValue: value?.start,
90+
minValue,
91+
maxValue,
92+
})
93+
);
9194
9295
function handleDefaultPlaceholder() {
9396
if (placeholder !== undefined) return;

0 commit comments

Comments
 (0)