Skip to content

Commit

Permalink
feat: Persist custom sidebar size in localstorage
Browse files Browse the repository at this point in the history
  • Loading branch information
wa0x6e committed Dec 12, 2024
1 parent d7ebc56 commit 58692d6
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 12 deletions.
26 changes: 15 additions & 11 deletions apps/ui/src/components/Ui/ResizableHorizontal.vue
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
<script lang="ts" setup>
import { lsGet, lsSet } from '@/helpers/utils';
const CACHE_KEYNAME = 'proposal.sidebarWidth';
const props = defineProps<{
width: number;
default: number;
max?: number;
min?: number;
}>();
Expand All @@ -9,26 +13,22 @@ const containerEl = ref<HTMLElement | null>(null);
const sliderEl = ref<HTMLElement | null>(null);
const initialized = ref(false);
const sliderOriginalPositionX = ref(0);
const width = ref(lsGet(CACHE_KEYNAME) || props.default);
const { x, y } = useDraggable(sliderEl, {
axis: 'x'
});
const containerWidth = computed(() => {
const width = getWidth(x.value);
const offset = sliderOriginalPositionX.value - x.value;
const newWidth = width.value + offset;
if (props.max && width > props.max) return props.max;
if (props.min && width < props.min) return props.min;
if (props.max && newWidth > props.max) return props.max;
if (props.min && newWidth < props.min) return props.min;
return width;
return Math.round(newWidth);
});
function getWidth(newSliderPositionX: number): number {
const offset = sliderOriginalPositionX.value - newSliderPositionX;
return props.width + offset;
}
function initResizer() {
if (!sliderEl.value || !containerEl.value) return;
Expand All @@ -40,6 +40,10 @@ function initResizer() {
initialized.value = true;
}
watch(containerWidth, w => {
lsSet(CACHE_KEYNAME, w);
});
onMounted(() => {
initResizer();
});
Expand Down
2 changes: 1 addition & 1 deletion apps/ui/src/views/Proposal.vue
Original file line number Diff line number Diff line change
Expand Up @@ -238,7 +238,7 @@ watchEffect(() => {
<router-view :proposal="proposal" />
</div>
<UiResizableHorizontal
:width="340"
:default="340"
:max="400"
:min="340"
:class="[
Expand Down

0 comments on commit 58692d6

Please sign in to comment.