-
Notifications
You must be signed in to change notification settings - Fork 1.6k
Improved onboarding flow for the editor #2942
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
'use client'; | ||
|
||
import { useOnboarding } from '@/components/onboarding/onboarding-context'; | ||
import { Button } from '@onlook/ui/button'; | ||
import { observer } from 'mobx-react-lite'; | ||
import { useEffect, useState } from 'react'; | ||
|
||
export const OnboardingTrigger = observer(() => { | ||
const { startOnboarding, isActive } = useOnboarding(); | ||
|
||
return ( | ||
<div className="absolute top-20 left-1/2 transform -translate-x-1/2 z-[200]"> | ||
<Button | ||
onClick={startOnboarding} | ||
variant="outline" | ||
size="sm" | ||
className="bg-background/90 backdrop-blur-sm border-primary/30 hover:bg-primary/10 text-foreground-primary shadow-lg" | ||
> | ||
{isActive ? 'Restart Tour' : 'Start Tour'} | ||
</Button> | ||
</div> | ||
); | ||
}); |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,7 @@ | ||
'use client'; | ||
|
||
import type { SendMessage } from '@/app/project/[id]/_hooks/use-chat'; | ||
import { useOnboarding } from '@/components/onboarding/onboarding-context'; | ||
import { useEditorEngine } from '@/components/store/editor'; | ||
import { FOCUS_CHAT_INPUT_EVENT } from '@/components/store/editor/chat'; | ||
import { transKeys } from '@/i18n/keys'; | ||
|
@@ -18,6 +19,7 @@ import { compressImageInBrowser } from '@onlook/utility'; | |
import { observer } from 'mobx-react-lite'; | ||
import { useTranslations } from 'next-intl'; | ||
import { useEffect, useMemo, useRef, useState } from 'react'; | ||
import { createPortal } from 'react-dom'; | ||
import { validateImageLimit } from '../context-pills/helpers'; | ||
import { InputContextPills } from '../context-pills/input-context-pills'; | ||
import { Suggestions, type SuggestionsRef } from '../suggestions'; | ||
|
@@ -41,13 +43,17 @@ export const ChatInput = observer(({ | |
onSendMessage, | ||
}: ChatInputProps) => { | ||
const editorEngine = useEditorEngine(); | ||
const { isActive, currentStep } = useOnboarding(); | ||
const t = useTranslations(); | ||
const textareaRef = useRef<HTMLTextAreaElement>(null); | ||
const containerRef = useRef<HTMLDivElement>(null); | ||
const [isComposing, setIsComposing] = useState(false); | ||
const [actionTooltipOpen, setActionTooltipOpen] = useState(false); | ||
const [isDragging, setIsDragging] = useState(false); | ||
const [portalPosition, setPortalPosition] = useState<{ top: number; left: number; width: number; height: number } | null>(null); | ||
const chatMode = editorEngine.state.chatMode; | ||
const [inputValue, setInputValue] = useState(''); | ||
const isOnboardingChatStep = isActive && currentStep === 0; | ||
const lastUsageMessage = useMemo(() => messages.findLast(msg => msg.metadata?.usage), [messages]); | ||
|
||
const focusInput = () => { | ||
|
@@ -79,6 +85,30 @@ export const ChatInput = observer(({ | |
return () => window.removeEventListener(FOCUS_CHAT_INPUT_EVENT, focusHandler); | ||
}, []); | ||
|
||
// Track position for portal when in onboarding mode | ||
useEffect(() => { | ||
if (isOnboardingChatStep && containerRef.current) { | ||
const updatePosition = () => { | ||
const rect = containerRef.current?.getBoundingClientRect(); | ||
if (rect) { | ||
setPortalPosition({ | ||
top: rect.top, | ||
left: rect.left, | ||
width: rect.width, | ||
height: rect.height, | ||
}); | ||
} | ||
}; | ||
|
||
updatePosition(); | ||
window.addEventListener('resize', updatePosition); | ||
|
||
return () => window.removeEventListener('resize', updatePosition); | ||
} else { | ||
setPortalPosition(null); | ||
} | ||
}, [isOnboardingChatStep]); | ||
Comment on lines
+90
to
+110
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Portal clone never repositions with layout changes
Apply something like: useEffect(() => {
- if (isOnboardingChatStep && containerRef.current) {
- const updatePosition = () => {
- const rect = containerRef.current?.getBoundingClientRect();
- if (rect) {
- setPortalPosition({
- top: rect.top,
- left: rect.left,
- width: rect.width,
- height: rect.height,
- });
- }
- };
-
- updatePosition();
- window.addEventListener('resize', updatePosition);
-
- return () => window.removeEventListener('resize', updatePosition);
- } else {
- setPortalPosition(null);
- }
-}, [isOnboardingChatStep]);
+ const node = containerRef.current;
+ if (!isOnboardingChatStep || !node) {
+ setPortalPosition(null);
+ return;
+ }
+
+ const updatePosition = () => {
+ const rect = node.getBoundingClientRect();
+ setPortalPosition({
+ top: rect.top,
+ left: rect.left,
+ width: rect.width,
+ height: rect.height,
+ });
+ };
+
+ const resizeObserver = new ResizeObserver(updatePosition);
+ resizeObserver.observe(node);
+ updatePosition();
+
+ const handleScroll = () => updatePosition();
+ window.addEventListener('scroll', handleScroll, true);
+
+ return () => {
+ resizeObserver.disconnect();
+ window.removeEventListener('scroll', handleScroll, true);
+ };
+}, [isOnboardingChatStep]); Also applies to: 352-358 |
||
|
||
useEffect(() => { | ||
const handleGlobalKeyDown = (e: KeyboardEvent) => { | ||
if (e.key === 'Enter' && suggestionRef.current?.handleEnterSelection()) { | ||
|
@@ -310,12 +340,22 @@ export const ChatInput = observer(({ | |
}; | ||
|
||
|
||
return ( | ||
const chatInputContent = (isPortal = false) => ( | ||
<div | ||
ref={!isPortal ? containerRef : undefined} | ||
data-onboarding-target={!isPortal ? "chat-input" : undefined} | ||
className={cn( | ||
'flex flex-col w-full text-foreground-tertiary border-t text-small transition-colors duration-200 [&[data-dragging-image=true]]:bg-teal-500/40', | ||
'flex flex-col w-full text-foreground-tertiary border-t text-small transition-all duration-300 [&[data-dragging-image=true]]:bg-teal-500/40 bg-background', | ||
isDragging && 'cursor-copy', | ||
isPortal ? 'fixed z-[99999]' : 'relative z-[100]', | ||
isOnboardingChatStep && !isPortal && 'invisible', | ||
)} | ||
style={isPortal && portalPosition ? { | ||
top: portalPosition.top, | ||
left: portalPosition.left, | ||
width: portalPosition.width, | ||
height: portalPosition.height, | ||
} : undefined} | ||
onDrop={(e) => { | ||
handleDrop(e); | ||
setIsDragging(false); | ||
|
@@ -434,4 +474,14 @@ export const ChatInput = observer(({ | |
</div> | ||
</div> | ||
); | ||
|
||
return ( | ||
<> | ||
{chatInputContent(false)} | ||
{isOnboardingChatStep && portalPosition && typeof window !== 'undefined' && createPortal( | ||
chatInputContent(true), | ||
document.body | ||
)} | ||
</> | ||
); | ||
}); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Remove unused imports.
useEffect
anduseState
are imported but never used in this component.Apply this diff:
Or remove the line entirely if no React imports are needed:
-import { useEffect, useState } from 'react';
📝 Committable suggestion
🤖 Prompt for AI Agents