Skip to content

Commit

Permalink
fix: handle page resize
Browse files Browse the repository at this point in the history
  • Loading branch information
wa0x6e committed Dec 16, 2024
1 parent 8349261 commit 0118df1
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 14 deletions.
42 changes: 29 additions & 13 deletions apps/ui/src/components/Ui/ResizableHorizontal.vue
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,8 @@ const sliderEl = ref<HTMLElement | null>(null);
const initialized = ref(false);
const dragging = ref(false);
const width = ref(lsGet(`${props.id}.${CACHE_KEY_SUFFIX}`, props.default));
const sliderOriginalPositionX = ref(0);
const sliderOutOfBound = ref(false);
const skipNextDragTick = ref(false);
const { x, y } = useDraggable(sliderEl, {
axis: 'x',
Expand All @@ -24,33 +25,48 @@ const { x, y } = useDraggable(sliderEl, {
},
onEnd: () => {
dragging.value = false;
if (sliderOutOfBound) {
skipNextDragTick.value = true;
}
}
});
const containerWidth = computed(() => {
const newWidth = Math.round(
width.value + sliderOriginalPositionX.value - x.value
);
function getNewWidth(width: number, delta: number) {
const newWidth = Math.round(width - delta);
if (props.max && newWidth > props.max) return props.max;
if (props.min && newWidth < props.min) return props.min;
if (props.max && newWidth > props.max) {
sliderOutOfBound.value = true;
return props.max;
}
if (props.min && newWidth < props.min) {
sliderOutOfBound.value = true;
return props.min;
}
return newWidth;
});
}
function initResizer() {
if (!sliderEl.value || !containerEl.value) return;
const position = sliderEl.value.getBoundingClientRect();
sliderOriginalPositionX.value = position.x;
x.value = position.x;
y.value = position.y;
initialized.value = true;
}
watch(containerWidth, w => {
lsSet(`${props.id}.${CACHE_KEY_SUFFIX}`, w);
watch(x, (newX, oldX) => {
if (!initialized) return;
if (skipNextDragTick.value) {
skipNextDragTick.value = false;
sliderOutOfBound.value = false;
return;
}
width.value = getNewWidth(width.value, newX - oldX);
lsSet(`${props.id}.${CACHE_KEY_SUFFIX}`, width.value);
});
onMounted(() => {
Expand All @@ -62,7 +78,7 @@ onMounted(() => {
<div
ref="containerEl"
class="relative max-md:!w-full"
:style="{ width: `${containerWidth}px` }"
:style="{ width: `${width}px` }"
>
<div
ref="sliderEl"
Expand Down
1 change: 0 additions & 1 deletion apps/ui/src/views/Proposal.vue
Original file line number Diff line number Diff line change
Expand Up @@ -240,7 +240,6 @@ watchEffect(() => {
:default="340"
:max="400"
:min="340"
resize-direction="LEFT"
:class="[
'shrink-0 md:h-full z-40 border-l-0 md:border-l',
{
Expand Down

0 comments on commit 0118df1

Please sign in to comment.