Skip to content

Commit 3c6f338

Browse files
authored
fix: Combobox/Select item registration (#919)
1 parent fcc4c95 commit 3c6f338

File tree

6 files changed

+142
-112
lines changed

6 files changed

+142
-112
lines changed

.changeset/green-drinks-sell.md

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"bits-ui": patch
3+
---
4+
5+
fix: ensure highlighted item is reset when items mount/unmount

packages/bits-ui/src/lib/bits/select/components/select-item.svelte

+7
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
import type { SelectItemProps } from "../types.js";
55
import { useId } from "$lib/internal/use-id.js";
66
import { noop } from "$lib/internal/noop.js";
7+
import Mounted from "$lib/bits/utilities/mounted.svelte";
78
89
let {
910
id = useId(),
@@ -41,3 +42,9 @@
4142
{@render children?.(itemState.snippetProps)}
4243
</div>
4344
{/if}
45+
46+
<Mounted
47+
onMountedChange={(m) => {
48+
itemState.mounted = m;
49+
}}
50+
/>

packages/bits-ui/src/lib/bits/select/select.svelte.ts

+11-5
Original file line numberDiff line numberDiff line change
@@ -187,7 +187,7 @@ class SelectSingleRootState extends SelectBaseRootState {
187187
$effect(() => {
188188
if (!this.open.current) return;
189189
afterTick(() => {
190-
this.#setInitialHighlightedNode();
190+
this.setInitialHighlightedNode();
191191
});
192192
});
193193
}
@@ -201,8 +201,8 @@ class SelectSingleRootState extends SelectBaseRootState {
201201
this.inputValue = itemLabel;
202202
};
203203

204-
#setInitialHighlightedNode = () => {
205-
if (this.highlightedNode) return;
204+
setInitialHighlightedNode = () => {
205+
if (this.highlightedNode && document.contains(this.highlightedNode)) return;
206206
if (this.value.current !== "") {
207207
const node = this.getNodeByValue(this.value.current);
208208
if (node) {
@@ -235,7 +235,7 @@ class SelectMultipleRootState extends SelectBaseRootState {
235235
if (!this.open.current) return;
236236
afterTick(() => {
237237
if (!this.highlightedNode) {
238-
this.#setInitialHighlightedNode();
238+
this.setInitialHighlightedNode();
239239
}
240240
});
241241
});
@@ -254,7 +254,7 @@ class SelectMultipleRootState extends SelectBaseRootState {
254254
this.inputValue = itemLabel;
255255
};
256256

257-
#setInitialHighlightedNode = () => {
257+
setInitialHighlightedNode = () => {
258258
if (this.highlightedNode) return;
259259
if (this.value.current.length && this.value.current[0] !== "") {
260260
const node = this.getNodeByValue(this.value.current[0]!);
@@ -815,6 +815,7 @@ class SelectItemState {
815815
isHighlighted = $derived.by(() => this.root.highlightedValue === this.value.current);
816816
prevHighlighted = new Previous(() => this.isHighlighted);
817817
textId = $state("");
818+
mounted = $state(false);
818819

819820
constructor(props: SelectItemStateProps, root: SelectRootState) {
820821
this.root = root;
@@ -838,6 +839,11 @@ class SelectItemState {
838839
id: this.#id,
839840
ref: this.#ref,
840841
});
842+
843+
$effect(() => {
844+
if (!this.mounted) return;
845+
this.root.setInitialHighlightedNode();
846+
});
841847
}
842848

843849
snippetProps = $derived.by(() => ({

pnpm-lock.yaml

+13-85
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

sites/docs/package.json

+1
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@
4040
"rehype-slug": "^6.0.0",
4141
"remark-gfm": "^4.0.0",
4242
"remove-markdown": "^0.5.5",
43+
"runed": "^0.15.2",
4344
"shiki": "^1.1.1",
4445
"svelte": "^5.1.0",
4546
"svelte-check": "4.0.3",

0 commit comments

Comments
 (0)