Skip to content

Commit

Permalink
fix: scroll jump - move scrollIntoView to ComboBoxOption component
Browse files Browse the repository at this point in the history
  • Loading branch information
jxjj committed Sep 23, 2024
1 parent 962c197 commit 0465145
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 30 deletions.
28 changes: 0 additions & 28 deletions resources/js/components/ComboBox/ComboBox.vue
Original file line number Diff line number Diff line change
Expand Up @@ -339,34 +339,6 @@ watch(query, () => {
: first(filteredOptions.value) ?? null;
});
// make sure the highlighted option is always in view
watch(
[areOptionsOpen, highlightedOption],
() => {
nextTick(() => {
if (!areOptionsOpen.value || !highlightedOption.value) {
return;
}
const highlightedOptionRef = document
.getElementById(`combobox-${props.label}__options`)
?.querySelector(`[data-highlighted-option="true"]`);
if (!highlightedOptionRef) {
return;
}
if (highlightedOptionRef) {
highlightedOptionRef.scrollIntoView({
block: "nearest",
inline: "nearest",
});
}
});
},
{ immediate: true },
);
const { floatingStyles } = useFloating(
comboboxContainerRef,
comboboxOptionsRef,
Expand Down
25 changes: 23 additions & 2 deletions resources/js/components/ComboBox/ComboBoxOption.vue
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
<template>
<li :id="`option-${option.id ?? option.label}`" class="tw-list-none">
<li
:id="`option-${option.id ?? option.label}`"
ref="currentOption"
class="tw-list-none"
>
<div
class="tw-flex tw-justify-between tw-items-center tw-rounded-md tw-cursor-pointer"
:class="{
Expand Down Expand Up @@ -30,11 +34,28 @@
<script setup lang="ts">
import CircleCheckIcon from "@/icons/CircleCheckIcon.vue";
import { ComboBoxOptionType } from ".";
import { watch, ref } from "vue";
defineProps<{
const props = defineProps<{
option: ComboBoxOptionType;
isSelected: boolean;
isHighlighted: boolean;
}>();
const currentOption = ref<HTMLElement | null>(null);
watch(
[() => props.isHighlighted],
() => {
if (!props.isHighlighted) return;
// make sure if the current option is highlighted, it's in vue of the scroll container
currentOption.value?.scrollIntoView({
behavior: "smooth",
block: "nearest",
});
},
{ immediate: true },
);
</script>
<style scoped></style>

0 comments on commit 0465145

Please sign in to comment.