Skip to content

Commit d2b41db

Browse files
committed
fix: make chat width depend of rem
1 parent 9489585 commit d2b41db

4 files changed

Lines changed: 35 additions & 17 deletions

File tree

custom/ChatSurface.vue

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@
2323
class="fixed bg-lightNavbar dark:bg-darkNavbar h-screen top-0 right-0 border-x border-b border-gray-200 dark:border-gray-700
2424
flex flex z-40"
2525
:class="[agentStore.isChatOpen ? 'translate-x-0' : 'translate-x-full', !agentStore.isTeleportedToBody ? 'shadow-2xl' : '']"
26-
:style="{ width: agentStore.chatWidth + 'px' }"
26+
:style="{ width: agentStore.chatWidth + 'rem' }"
2727
>
2828
<div
2929
v-if="!coreStore.isMobile"
@@ -103,7 +103,7 @@
103103
<div
104104
class="w-full mb-2 flex items-center justify-center px-2 bg-transparent relative translate-x-[-50%] left-1/2"
105105
:style="{
106-
maxWidth: agentStore.isFullScreen ? agentStore.MAX_WIDTH+'px' : '100%',
106+
maxWidth: agentStore.isFullScreen ? remToPx(agentStore.MAX_WIDTH)+'px' : '100%',
107107
transition: `transform ${agentTransitions.TRANSITION_DURATION}ms ease-in-out`
108108
}"
109109
>
@@ -185,6 +185,7 @@ import { useAgentStore } from './composables/useAgentStore';
185185
import { useAgentTransitions } from './composables/useAgentTransitions';
186186
import { Button } from '@/afcl';
187187
import { useCoreStore } from '@/stores/core';
188+
import { remToPx, pxToRem } from './utils';
188189
189190
const props = defineProps<{
190191
meta: {
@@ -209,7 +210,7 @@ let startWidth = 0
209210
210211
const startResize = (e: MouseEvent) => {
211212
startX = e.clientX
212-
startWidth = agentStore.chatWidth
213+
startWidth = remToPx(agentStore.chatWidth)
213214
214215
document.body.style.userSelect = 'none'
215216
document.body.style.cursor = 'ew-resize'
@@ -220,7 +221,7 @@ const startResize = (e: MouseEvent) => {
220221
221222
const onResize = (e: MouseEvent) => {
222223
const dx = startX - e.clientX
223-
agentStore.setChatWidth(Math.min(Math.max(startWidth + dx, agentStore.MIN_WIDTH), agentStore.MAX_WIDTH))
224+
agentStore.setChatWidth(Math.min(Math.max(startWidth + dx, remToPx(agentStore.MIN_WIDTH)), remToPx(agentStore.MAX_WIDTH)))
224225
agentTransitions.setChatSurfaceTransition(true);
225226
}
226227
@@ -246,7 +247,9 @@ onMounted(async () => {
246247
agentStore.setAvailableModes(props.meta.modes, props.meta.defaultModeName);
247248
agentStore.regisrerTextInput(textInput.value);
248249
textInput.value?.focus();
249-
agentStore.setIsTeleportedToBody(props.meta.stickByDefault);
250+
const isTeleportedToBodyFromLocalStorage = agentStore.getLocalStorageItem('isTeleportedToBody') === 'true';
251+
252+
agentStore.setIsTeleportedToBody(isTeleportedToBodyFromLocalStorage || props.meta.stickByDefault);
250253
await agentStore.fetchSessionsList();
251254
});
252255

custom/ConversationArea.vue

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@
2424
:threshold="10"
2525
behavior="smooth"
2626
:style="{
27-
maxWidth: agentStore.isFullScreen ? agentStore.MAX_WIDTH+'px' : '100%',
27+
maxWidth: agentStore.isFullScreen ? agentStore.MAX_WIDTH+'rem' : '100%',
2828
transition: `
2929
max-width ${agentTransitions.TRANSITION_DURATION}ms ease-in-out,
3030
transform ${agentTransitions.TRANSITION_DURATION}ms ease-in-out

custom/composables/useAgentStore.ts

Lines changed: 17 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import { DefaultChatTransport } from 'ai';
88
import { useCoreStore } from '@/stores/core';
99
import { useAgentTransitions } from './useAgentTransitions';
1010
import { useWindowSize } from '@vueuse/core';
11+
import { remToPx, pxToRem } from '../utils';
1112

1213
type AgentMode = {
1314
name: string;
@@ -19,9 +20,9 @@ const PLACEHOLDER_DELETING_DELAY_MS = 35;
1920
const PLACEHOLDER_HOLD_DELAY_MS = 3000;
2021

2122
export const useAgentStore = defineStore('agent', () => {
22-
const DEFAULT_CHAT_WIDTH = 600;
23-
const MAX_WIDTH = 800;
24-
const MIN_WIDTH = 382; //w-96
23+
const DEFAULT_CHAT_WIDTH = 30;
24+
const MAX_WIDTH = 60;
25+
const MIN_WIDTH = 25
2526
const agentTransitions = useAgentTransitions();
2627

2728
const activeSessionId = ref<string | null>(null);
@@ -71,6 +72,9 @@ export const useAgentStore = defineStore('agent', () => {
7172
})
7273
watch(chatWidth, (newVal: number) => {
7374
setLocalStorageItem('chatWidth', newVal.toString());
75+
if (!isFullScreen.value) {
76+
setLocalStorageItem('chatWidthBeforeFullScreen', newVal.toString());
77+
}
7478
})
7579
watch(activeSessionId, (newVal: string | null) => {
7680
if (newVal) {
@@ -90,14 +94,14 @@ export const useAgentStore = defineStore('agent', () => {
9094
onMounted(() => {
9195
const chatWidthBeforeFullScreen = parseInt(getLocalStorageItem('chatWidthBeforeFullScreen') || '0', 10);
9296
if (chatWidthBeforeFullScreen) {
93-
chatWidth.value = chatWidthBeforeFullScreen;
97+
setChatWidth(remToPx(chatWidthBeforeFullScreen));
9498
} else {
9599
const savedChatWidth = parseInt(getLocalStorageItem('chatWidth') || '0', 10);
96100
if (savedChatWidth) {
97101
if (savedChatWidth > MAX_WIDTH || savedChatWidth < MIN_WIDTH) {
98-
chatWidth.value = DEFAULT_CHAT_WIDTH;
102+
setChatWidth(remToPx(DEFAULT_CHAT_WIDTH));
99103
} else {
100-
chatWidth.value = savedChatWidth;
104+
setChatWidth(remToPx(savedChatWidth));
101105
}
102106
}
103107
}
@@ -110,7 +114,7 @@ export const useAgentStore = defineStore('agent', () => {
110114
isChatOpen.value = getLocalStorageItem('isChatOpen') === 'true';
111115
}
112116
if (coreStore.isMobile) {
113-
chatWidth.value = window.innerWidth;
117+
setChatWidth(window.innerWidth);
114118
}
115119
appRoot.value = document.getElementById('app');
116120
header.value = document.getElementById('af-header-nav');
@@ -135,23 +139,24 @@ export const useAgentStore = defineStore('agent', () => {
135139
const isTeleportedBeforeFullScreen = getLocalStorageItem('isTeleportedToBodyBeforeFullScreen') === 'true';
136140
agentTransitions.setAppRootTransition(true);
137141
setIsTeleportedToBody(isTeleportedBeforeFullScreen);
138-
setChatWidth(lastChatWidth, false);
142+
setChatWidth(remToPx(lastChatWidth), false);
139143
setTimeout(() => agentTransitions.setAppRootTransition(false), agentTransitions.TRANSITION_DURATION);
140144
}
141145
}
142146

147+
//takes on input width in pixels, converts to rem and sets chat width
143148
function setChatWidth(width: number, blockTransition = true) {
144149
if (blockTransition) {
145150
agentTransitions.setAppRootTransition(true);
146151
}
147-
chatWidth.value = width;
152+
chatWidth.value = pxToRem(width);
148153

149154
}
150155
watch([isTeleportedToBody, isChatOpen, chatWidth], ([newIsTeleportedToBody, newIsChatOpen, newChatWidth]: [boolean, boolean, number]) => {
151156
if (appRoot.value && header.value) {
152157
if (newIsTeleportedToBody && newIsChatOpen) {
153-
appRoot.value.style.paddingRight = `${chatWidth.value}px`;
154-
header.value.style.paddingRight = `${chatWidth.value}px`;
158+
appRoot.value.style.paddingRight = `${remToPx(chatWidth.value)}px`;
159+
header.value.style.paddingRight = `${remToPx(chatWidth.value)}px`;
155160
} else {
156161
appRoot.value.style.paddingRight = '';
157162
header.value.style.paddingRight = '';
@@ -572,5 +577,6 @@ export const useAgentStore = defineStore('agent', () => {
572577
DEFAULT_CHAT_WIDTH,
573578
MAX_WIDTH,
574579
MIN_WIDTH,
580+
getLocalStorageItem
575581
}
576582
})

custom/utils.ts

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
export function remToPx(rem: number): number {
2+
const rootFontSize = parseFloat(getComputedStyle(document.documentElement).fontSize);
3+
return rem * rootFontSize;
4+
}
5+
6+
export function pxToRem(px: number): number {
7+
const rootFontSize = parseFloat(getComputedStyle(document.documentElement).fontSize);
8+
return px / rootFontSize;
9+
}

0 commit comments

Comments
 (0)