Beautiful Open-Source Form Builder
Implement resizable sidebars in FormEditor and EditorRightSidebar com… (#835)
Chirag Chhatralaadb69b71c796
diff --git a/client/components/global/ResizeHandle.vue b/client/components/global/ResizeHandle.vuenew file mode 100644index 000000000..4f97ea016--- /dev/null+++ b/client/components/global/ResizeHandle.vue@@ -0,0 +1,37 @@+<template>+ <div+ v-if="show"+ ref="handleRef"+ :class="[+ 'absolute top-0 h-full w-0.5 hover:bg-blue-300 cursor-col-resize transition-colors duration-150 group z-10',+ direction === 'left'+ ? 'right-0'+ : 'left-0'+ ]"+ @mousedown="$emit('start-resize', $event)"+ >+ <div class="absolute inset-y-0 -left-1 -right-1 group-hover:bg-blue-500 group-hover:opacity-20" />+ </div>+</template>++<script setup>+defineEmits(['start-resize'])++defineProps({+ show: {+ type: Boolean,+ default: true+ },+ direction: {+ type: String,+ default: 'right',+ validator: (value) => ['left', 'right'].includes(value)+ }+})++const handleRef = ref(null)++defineExpose({+ handleRef+})+</script>\ No newline at end of filediff --git a/client/components/open/editors/EditorRightSidebar.vue b/client/components/open/editors/EditorRightSidebar.vueindex e3eb35629..584bce8a6 100644--- a/client/components/open/editors/EditorRightSidebar.vue+++ b/client/components/open/editors/EditorRightSidebar.vue@@ -2,10 +2,20 @@<transition @leave="(el, done) => sidebarMotion?.leave(done)"><divv-if="show"- ref="sidebar"- :class="widthClass"- class="absolute shadow-lg shadow-neutral-800/30 top-0 h-[calc(100vh-53px)] right-0 lg:shadow-none lg:relative bg-white w-full md:w-1/2 lg:w-2/5 border-l overflow-y-scroll flex-shrink-0 z-30"+ ref="elementRef"+ class="absolute shadow-lg shadow-neutral-800/30 top-0 h-[calc(100vh-53px)] right-0 lg:shadow-none lg:relative bg-white border-l overflow-y-scroll flex-shrink-0 z-30"+ :class="[+ isResizable ? '' : 'w-full md:w-1/2 lg:w-2/5',+ widthClass+ ]"+ :style="isResizable ? dynamicStyles : {}">+ <ResizeHandle+ :show="isResizable"+ direction="right"+ @start-resize="startResize"+ />+<slot /></div></transition>@@ -13,7 +23,9 @@<script setup>import { slideRight, useMotion } from '@vueuse/motion'-import { watch } from 'vue'+import { watch, computed } from 'vue'+import { useResizable } from '~/composables/components/useResizable'+import ResizeHandle from '@/components/global/ResizeHandle.vue'const props = defineProps({show: {@@ -24,16 +36,38 @@ const props = defineProps({type: String,default: 'md:max-w-[20rem]',},+ resizable: {+ type: Boolean,+ default: false,+ },})-const sidebar = ref(null)+// Sidebar resizing using composable+const {+ elementRef,+ isResizable: isResizableBase,+ dynamicStyles,+ startResize+} = useResizable({+ storageKey: 'formEditorRightSidebarWidth',+ defaultWidth: 350,+ direction: 'right',+ maxWidth: () => Math.min(600, window.innerWidth * 0.6)+})++// Motion animationconst sidebarMotion = ref(null)++// Enable resizing only when prop is true and breakpoint allows it+const isResizable = computed(() => props.resizable && isResizableBase.value)++// Watch for show prop changes (existing functionality)watch(() => props.show,(newVal) => {if (newVal) {nextTick(() => {- sidebarMotion.value = useMotion(sidebar.value, slideRight)+ sidebarMotion.value = useMotion(elementRef.value, slideRight)})}},diff --git a/client/components/open/forms/components/FormEditor.vue b/client/components/open/forms/components/FormEditor.vueindex faca76097..fd7eb78ad 100644--- a/client/components/open/forms/components/FormEditor.vue+++ b/client/components/open/forms/components/FormEditor.vue@@ -50,7 +50,19 @@<FormEditorErrorHandler><div class="w-full flex grow overflow-y-scroll relative bg-white">- <div class="relative w-full shrink-0 overflow-y-scroll border-r md:w-1/2 md:max-w-xs lg:w-2/5">+ <div+ ref="elementRef"+ class="relative shrink-0 overflow-y-scroll border-r"+ :class="isResizable ? '' : 'w-full md:w-1/2 md:max-w-xs lg:w-2/5'"+ :style="isResizable ? dynamicStyles : {}"+ >+ <ResizeHandle+ :show="isResizable"+ direction="left"+ @start-resize="startResize"+ class="z-20"+ />+<VFormsize="sm"@submit.prevent=""@@ -113,6 +125,8 @@ import { setFormDefaults } from '~/composables/forms/initForm.js'import { breakpointsTailwind, useBreakpoints } from '@vueuse/core'import LogicConfirmationModal from '~/components/forms/heavy/LogicConfirmationModal.vue'import { formsApi } from "~/api"+import { useResizable } from '~/composables/components/useResizable'+import ResizeHandle from '~/components/global/ResizeHandle.vue'// Define propsconst props = defineProps({@@ -153,6 +167,19 @@ const validationErrorResponse = ref(null)const createdFormSlug = ref(null)const logicErrors = ref([])+// Sidebar resizing using composable+const {+ elementRef,+ isResizable,+ dynamicStyles,+ startResize+} = useResizable({+ storageKey: 'formEditorSidebarWidth',+ defaultWidth: 320,+ direction: 'left',+ maxWidth: () => Math.min(600, window.innerWidth * 0.6)+})+// Check if the editor is visible on smaller screens then send an emailconst breakpoints = useBreakpoints(breakpointsTailwind)const isVisible = ref(breakpoints.smaller("md"))@@ -364,6 +391,8 @@ const saveFormGuest = () => {emit("openRegister")}++defineExpose({saveFormCreate})@@ -373,6 +402,7 @@ onMounted(() => {emit("mounted")workingFormStore.activeTab = 'build'amplitude.logEvent('form_editor_viewed')+if (!props.isEdit) {nextTick(() => {workingFormStore.openAddFieldSidebar()diff --git a/client/components/open/forms/components/form-components/FormEditorSidebar.vue b/client/components/open/forms/components/form-components/FormEditorSidebar.vueindex 9b37e9744..a1415db06 100644--- a/client/components/open/forms/components/form-components/FormEditorSidebar.vue+++ b/client/components/open/forms/components/form-components/FormEditorSidebar.vue@@ -2,6 +2,7 @@<editor-right-sidebar:width-class="showAddFieldSidebar ? 'md:max-w-[15rem]' : 'md:max-w-[20rem]'":show="isOpen"+ :resizable="true"><VFormsize="sm"diff --git a/client/composables/components/useResizable.js b/client/composables/components/useResizable.jsnew file mode 100644index 000000000..43f0a8f16--- /dev/null+++ b/client/composables/components/useResizable.js@@ -0,0 +1,136 @@+import { ref, computed, readonly, onBeforeUnmount } from 'vue'+import { useLocalStorage, useEventListener, breakpointsTailwind, useBreakpoints } from '@vueuse/core'++/**+ * Composable for making elements resizable with mouse drag+ * @param {Object} options - Configuration options+ * @param {string} options.storageKey - localStorage key for persisting width+ * @param {number} options.defaultWidth - Default width in pixels+ * @param {number} options.minWidth - Minimum width in pixels+ * @param {number} options.maxWidth - Maximum width in pixels or function that returns max width+ * @param {'left' | 'right'} options.direction - Resize direction (left = drag to resize right, right = drag to resize left)+ * @param {string} options.breakpoint - Minimum breakpoint where resizing is enabled (default: 'lg')+ */+export function useResizable(options = {}) {+ const {+ storageKey,+ defaultWidth = 350,+ minWidth = 280,+ maxWidth = 600,+ direction = 'right',+ breakpoint = 'lg'+ } = options++ // Breakpoint detection+ const breakpoints = useBreakpoints(breakpointsTailwind)+ const isResizableBreakpoint = computed(() => breakpoints.greater(breakpoint).value)++ // Persisted width using VueUse+ const width = storageKey+ ? useLocalStorage(storageKey, defaultWidth)+ : ref(defaultWidth)++ // Resize state+ const isResizing = ref(false)+ const elementRef = ref(null)+ const handleRef = ref(null)++ // Computed properties+ const isResizable = computed(() => isResizableBreakpoint.value)++ const dynamicStyles = computed(() => {+ if (!isResizable.value) return {}++ return {+ width: `${width.value}px`,+ minWidth: `${minWidth}px`,+ maxWidth: typeof maxWidth === 'function' ? `${maxWidth()}px` : `${maxWidth}px`+ }+ })++ // Calculate new width based on mouse position and direction+ const calculateNewWidth = (clientX, containerRect) => {+ let newWidth++ if (direction === 'left') {+ // Left sidebar: drag right to increase width+ newWidth = clientX - containerRect.left+ } else {+ // Right sidebar: drag left to increase width+ newWidth = containerRect.right - clientX+ }++ return newWidth+ }++ // Get max width value (handle function or number)+ const getMaxWidth = (containerRect) => {+ const baseMax = typeof maxWidth === 'function' ? maxWidth() : maxWidth+ // Ensure it doesn't exceed 60% of container width+ return Math.min(baseMax, containerRect.width * 0.6)+ }++ // Resize handlers+ const handleMouseMove = (event) => {+ if (!isResizing.value || !elementRef.value) return++ const containerRect = elementRef.value.parentElement?.getBoundingClientRect()+ if (!containerRect) return++ const newWidth = calculateNewWidth(event.clientX, containerRect)+ const maxWidthValue = getMaxWidth(containerRect)++ // Apply constraints+ width.value = Math.max(minWidth, Math.min(maxWidthValue, newWidth))+ }++ const handleMouseUp = () => {+ if (!isResizing.value) return++ isResizing.value = false+ document.body.style.cursor = ''+ document.body.style.userSelect = ''+ }++ const startResize = (event) => {+ if (!isResizable.value) return++ isResizing.value = true+ document.body.style.cursor = 'col-resize'+ document.body.style.userSelect = 'none'+ event.preventDefault()+ }++ // Auto-cleanup event listeners using VueUse+ useEventListener(document, 'mousemove', handleMouseMove, { passive: true })+ useEventListener(document, 'mouseup', handleMouseUp, { passive: true })++ // Internal cleanup on component unmount - handle edge case where component unmounts while resizing+ onBeforeUnmount(() => {+ if (isResizing.value) {+ handleMouseUp()+ }+ })++ return {+ // Refs+ elementRef,+ handleRef,++ // Reactive state+ width: readonly(width),+ isResizing: readonly(isResizing),+ isResizable,++ // Computed styles+ dynamicStyles,++ // Methods+ startResize,++ // For advanced usage+ setWidth: (newWidth) => {+ width.value = Math.max(minWidth, Math.min(getMaxWidth({ width: window.innerWidth }), newWidth))+ }+ }+}\ No newline at end of file