Skip to content
TheChatArchiveThe Chat Archive
o1-preview-2024-09-12· OpenAIAi2 WildChat-4.8M (AI2)

Write code for a NextJS App router application using TailwindCSS that does the…

  1. User

    Write code for a NextJS App router application using TailwindCSS that does the following:

    1. in the layout.tsx file should have background of very dark grey nearly black with light light grey dots in regular grid pattern with spacing of 5px between each.
    2. the page.tsx should be a draggable window e.g. user can move it around anywhere on the page for now should just display hello world title it should be 20% page height and 35% page width.

    Write the code out in full for both files.

  2. Assistant

    layout.tsx

    // app/layout.tsx
    
    export const metadata = {
      title: 'My Next.js App',
      description: 'A Next.js App with a custom background',
    }
    
    import './globals.css'
    
    export default function RootLayout({
      children,
    }: {
      children: React.ReactNode
    }) {
      return (
        <html lang="en">
          <body className="bg-dark-pattern w-full h-full">
            {children}
          </body>
        </html>
      )
    }
    

    globals.css

    /* globals.css */
    
    @tailwind base;
    @tailwind components;
    @tailwind utilities;
    
    body {
      @apply min-h-screen;
    }
    
    .bg-dark-pattern {
      background-color: #111; /* Very dark grey nearly black */
      background-image: radial-gradient(circle, #eee 1px, transparent 1px);
      background-size: 5px 5px;
    }
    

    page.tsx

    // app/page.tsx
    
    'use client';
    
    import React, { useState } from 'react';
    
    export default function Home() {
      const [position, setPosition] = useState({ x: 100, y: 100 });
      const [dragging, setDragging] = useState(false);
      const [rel, setRel] = useState<{ x: number, y: number }>({ x: 0, y: 0 });
    
      const onMouseDown = (e: React.MouseEvent) => {
        if (e.button !== 0) return;
    
        const pos = e.currentTarget.getBoundingClientRect();
    
        setDragging(true);
        setRel({
          x: e.pageX - pos.left,
          y: e.pageY - pos.top
        });
    
        e.stopPropagation();
        e.preventDefault();
      };
    
      const onMouseUp = (e: React.MouseEvent) => {
        setDragging(false);
        e.stopPropagation();
        e.preventDefault();
      };
    
      const onMouseMove = (e: React.MouseEvent) => {
        if (!dragging) return;
    
        setPosition({
          x: e.pageX - rel.x,
          y: e.pageY - rel.y
        });
    
        e.stopPropagation();
        e.preventDefault();
      };
    
      return (
        <div
          onMouseMove={onMouseMove}
          onMouseUp={onMouseUp}
          className="w-full h-full fixed top-0 left-0">
          <div
            onMouseDown={onMouseDown}
            style={{
              width: '35%',
              height: '20%',
              position: 'absolute',
              left: position.x,
              top: position.y,
            }}
            className="bg-white border border-gray-500 cursor-move flex items-center justify-center"
          >
            <h1 className="text-center">Hello World</h1>
          </div>
        </div>
      );
    }