feat: implement scroll to bottom

This commit is contained in:
shadcn 2023-06-11 22:58:00 +04:00
parent f15cd0c936
commit 857a99736f
5 changed files with 62 additions and 12 deletions

View file

@ -40,10 +40,10 @@ export default function RootLayout({ children }: RootLayoutProps) {
)} )}
> >
<ThemeProvider attribute="class" defaultTheme="system" enableSystem> <ThemeProvider attribute="class" defaultTheme="system" enableSystem>
<div className="flex h-screen flex-col"> <div className="flex min-h-screen flex-col">
{/* @ts-ignore */} {/* @ts-ignore */}
<Header /> <Header />
<main className="flex-1">{children}</main> <main className="flex-1 bg-muted/50">{children}</main>
</div> </div>
<TailwindIndicator /> <TailwindIndicator />
</ThemeProvider> </ThemeProvider>

View file

@ -0,0 +1,48 @@
'use client'
import * as React from 'react'
import { ArrowDown } from 'lucide-react'
import { cn } from '@/lib/utils'
import { Button, type ButtonProps } from '@/components/ui/button'
export function ButtonScrollToBottom({ className, ...props }: ButtonProps) {
const [isAtBottom, setIsAtBottom] = React.useState(false)
React.useEffect(() => {
const handleScroll = () => {
setIsAtBottom(
window.innerHeight + window.scrollY > document.body.offsetHeight - 100
)
}
window.addEventListener('scroll', handleScroll, { passive: true })
handleScroll()
return () => {
window.removeEventListener('scroll', handleScroll)
}
}, [])
return (
<Button
variant="outline"
size="icon"
className={cn(
'absolute right-4 top-1 z-10 bg-background transition-opacity duration-300 sm:right-8 md:top-2',
isAtBottom ? 'opacity-0' : 'opacity-100',
className
)}
onClick={() =>
window.scrollTo({
top: document.body.offsetHeight,
behavior: 'smooth'
})
}
{...props}
>
<ArrowDown className="h-4 w-4" />
<span className="sr-only">Scroll to bottom</span>
</Button>
)
}

View file

@ -6,6 +6,7 @@ import { RefreshCcw, StopCircle } from 'lucide-react'
import { Button } from '@/components/ui/button' import { Button } from '@/components/ui/button'
import { ExternalLink } from '@/components/external-link' import { ExternalLink } from '@/components/external-link'
import { PromptForm } from '@/components/prompt-form' import { PromptForm } from '@/components/prompt-form'
import { ButtonScrollToBottom } from '@/components/button-scroll-to-bottom'
export interface ChatPanelProps export interface ChatPanelProps
extends Pick< extends Pick<
@ -24,15 +25,16 @@ export function ChatPanel({
}: ChatPanelProps) { }: ChatPanelProps) {
return ( return (
<div className="fixed inset-x-0 bottom-0 bg-gradient-to-b from-muted/10 from-10% to-muted/30 to-50%"> <div className="fixed inset-x-0 bottom-0 bg-gradient-to-b from-muted/10 from-10% to-muted/30 to-50%">
<ButtonScrollToBottom />
<div className="mx-auto sm:max-w-2xl sm:px-4"> <div className="mx-auto sm:max-w-2xl sm:px-4">
<div className="flex items-center justify-center py-2"> <div className="flex h-10 items-center justify-center">
{isLoading ? ( {isLoading ? (
<Button <Button
variant="outline" variant="outline"
onClick={() => stop()} onClick={() => stop()}
className="bg-background" className="bg-background"
> >
<StopCircle className="w-4 h-4 mr-2" /> <StopCircle className="mr-2 h-4 w-4" />
Stop generating Stop generating
</Button> </Button>
) : ( ) : (
@ -42,13 +44,13 @@ export function ChatPanel({
onClick={() => reload()} onClick={() => reload()}
className="bg-background" className="bg-background"
> >
<RefreshCcw className="w-4 h-4 mr-2" /> <RefreshCcw className="mr-2 h-4 w-4" />
Regenerate response Regenerate response
</Button> </Button>
) )
)} )}
</div> </div>
<div className="p-4 space-y-4 border-t shadow-lg bg-background sm:rounded-t-xl sm:border"> <div className="space-y-4 border-t bg-background px-4 py-2 shadow-lg sm:rounded-t-xl sm:border md:py-4">
<PromptForm <PromptForm
onSubmit={value => { onSubmit={value => {
append({ append({
@ -58,7 +60,7 @@ export function ChatPanel({
}} }}
isLoading={isLoading} isLoading={isLoading}
/> />
<p className="hidden px-2 text-xs leading-normal text-center text-muted-foreground sm:block"> <p className="hidden px-2 text-center text-xs leading-normal text-muted-foreground sm:block">
Open source AI chatbot app built with{' '} Open source AI chatbot app built with{' '}
<ExternalLink href="https://nextjs.org">Next.js</ExternalLink> and{' '} <ExternalLink href="https://nextjs.org">Next.js</ExternalLink> and{' '}
<ExternalLink href="https://vercel.com/storage/kv"> <ExternalLink href="https://vercel.com/storage/kv">

View file

@ -18,7 +18,7 @@ export function Chat({ id, initialMessages }: ChatProps) {
}) })
return ( return (
<div className="h-full w-full overflow-auto bg-muted/50 pb-[200px] pt-4 md:pt-10"> <div className="pb-[200px] pt-4 md:pt-10">
<ChatList messages={messages} /> <ChatList messages={messages} />
<ChatPanel <ChatPanel
id={id} id={id}

View file

@ -50,14 +50,14 @@ export function PromptForm({ onSubmit, isLoading }: PromptProps) {
ref={formRef} ref={formRef}
> >
<TooltipProvider> <TooltipProvider>
<div className="relative flex w-full grow flex-col overflow-hidden bg-background px-12 sm:rounded-md sm:border"> <div className="relative flex w-full grow flex-col overflow-hidden bg-background px-8 sm:rounded-md sm:border sm:px-12">
<Tooltip> <Tooltip>
<TooltipTrigger asChild> <TooltipTrigger asChild>
<Link <Link
href="/" href="/"
className={cn( className={cn(
buttonVariants({ size: 'sm', variant: 'outline' }), buttonVariants({ size: 'sm', variant: 'outline' }),
'absolute left-4 top-4 h-8 w-8 rounded-full bg-background p-0' 'absolute left-0 top-4 h-8 w-8 rounded-full bg-background p-0 sm:left-4'
)} )}
> >
<Plus className="h-4 w-4" /> <Plus className="h-4 w-4" />
@ -75,9 +75,9 @@ export function PromptForm({ onSubmit, isLoading }: PromptProps) {
onChange={e => setInput(e.target.value)} onChange={e => setInput(e.target.value)}
placeholder="Send a message." placeholder="Send a message."
spellCheck={false} spellCheck={false}
className="min-h-[60px] w-full resize-none bg-transparent px-4 py-[1.4rem] focus-within:outline-none sm:text-sm" className="min-h-[60px] w-full resize-none bg-transparent px-4 py-[1.3rem] focus-within:outline-none sm:text-sm"
/> />
<div className="absolute right-4 top-4"> <div className="absolute right-0 top-4 sm:right-4">
<Tooltip> <Tooltip>
<TooltipTrigger asChild> <TooltipTrigger asChild>
<Button <Button