Skip to content

Commit

Permalink
test: test that css works well with rsc and cc
Browse files Browse the repository at this point in the history
- rsc: react server component
- cc: client component
  • Loading branch information
hee-suh committed Jun 12, 2024
1 parent 7097144 commit c497190
Show file tree
Hide file tree
Showing 14 changed files with 166 additions and 142 deletions.
31 changes: 31 additions & 0 deletions src/app/actions.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
const getMessage = async () => {
const data = await fetch('http://localhost:3000/api/delay?delay=1000', {
headers: {
'Content-Type': 'application/json',
},
}).then((res) => res.json())

const message =
Reflect.has(data, 'ok') && Reflect.has(data, 'message')
? data.message
: 'API Error'

return message
}

const getData = async (url: string) => {
if (url === '/api/delay') {
return getMessage()
} else {
throw Error('Not implemented')
}
}

let cache = new Map()

export const fetchData = (url: string) => {
if (!cache.has(url)) {
cache.set(url, getData(url))
}
return cache.get(url)
}
9 changes: 9 additions & 0 deletions src/app/cc/layout.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
const Layout = ({ children }: React.PropsWithChildren) => {
return (
<div className="w-full min-h-screen flex flex-col justify-center items-center gap-4">
{children}
</div>
)
}

export default Layout
30 changes: 30 additions & 0 deletions src/app/cc/page.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
'use client'

import React, { Suspense, use } from 'react'
import { fetchData } from '@/app/actions'
import { Button } from '@/components/Button'
import LoadingIndicator from '@/components/loading-indicator'

const Message = () => {
const message: string = use(fetchData('/api/delay'))

return <div>{message}</div>
}

const Page = () => {
return (
<>
<h1 className="text-lg font-semibold">Data received during SSR</h1>
<Suspense fallback={<LoadingIndicator />}>
<Message />
</Suspense>
<Button
label="클릭"
className="bg-sky-400"
onClick={() => alert('ok!')}
/>
</>
)
}

export default Page
6 changes: 0 additions & 6 deletions src/app/globals.css
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,6 @@

body {
color: rgb(var(--foreground-rgb));
background: linear-gradient(
to bottom,
transparent,
rgb(var(--background-end-rgb))
)
rgb(var(--background-start-rgb));
}

@layer utilities {
Expand Down
127 changes: 18 additions & 109 deletions src/app/page.tsx
Original file line number Diff line number Diff line change
@@ -1,113 +1,22 @@
import Image from "next/image";
import Link from 'next/link'

export default function Home() {
const Home = () => {
return (
<main className="flex min-h-screen flex-col items-center justify-between p-24">
<div className="z-10 w-full max-w-5xl items-center justify-between font-mono text-sm lg:flex">
<p className="fixed left-0 top-0 flex w-full justify-center border-b border-gray-300 bg-gradient-to-b from-zinc-200 pb-6 pt-8 backdrop-blur-2xl dark:border-neutral-800 dark:bg-zinc-800/30 dark:from-inherit lg:static lg:w-auto lg:rounded-xl lg:border lg:bg-gray-200 lg:p-4 lg:dark:bg-zinc-800/30">
Get started by editing&nbsp;
<code className="font-mono font-bold">src/app/page.tsx</code>
</p>
<div className="fixed bottom-0 left-0 flex h-48 w-full items-end justify-center bg-gradient-to-t from-white via-white dark:from-black dark:via-black lg:static lg:size-auto lg:bg-none">
<a
className="pointer-events-none flex place-items-center gap-2 p-8 lg:pointer-events-auto lg:p-0"
href="https://vercel.com?utm_source=create-next-app&utm_medium=appdir-template&utm_campaign=create-next-app"
target="_blank"
rel="noopener noreferrer"
>
By{" "}
<Image
src="/vercel.svg"
alt="Vercel Logo"
className="dark:invert"
width={100}
height={24}
priority
/>
</a>
</div>
</div>

<div className="relative z-[-1] flex place-items-center before:absolute before:h-[300px] before:w-full before:-translate-x-1/2 before:rounded-full before:bg-gradient-radial before:from-white before:to-transparent before:blur-2xl before:content-[''] after:absolute after:-z-20 after:h-[180px] after:w-full after:translate-x-1/3 after:bg-gradient-conic after:from-sky-200 after:via-blue-200 after:blur-2xl after:content-[''] before:dark:bg-gradient-to-br before:dark:from-transparent before:dark:to-blue-700 before:dark:opacity-10 after:dark:from-sky-900 after:dark:via-[#0141ff] after:dark:opacity-40 sm:before:w-[480px] sm:after:w-[240px] before:lg:h-[360px]">
<Image
className="relative dark:drop-shadow-[0_0_0.3rem_#ffffff70] dark:invert"
src="/next.svg"
alt="Next.js Logo"
width={180}
height={37}
priority
/>
</div>

<div className="mb-32 grid text-center lg:mb-0 lg:w-full lg:max-w-5xl lg:grid-cols-4 lg:text-left">
<a
href="https://nextjs.org/docs?utm_source=create-next-app&utm_medium=appdir-template&utm_campaign=create-next-app"
className="group rounded-lg border border-transparent px-5 py-4 transition-colors hover:border-gray-300 hover:bg-gray-100 hover:dark:border-neutral-700 hover:dark:bg-neutral-800/30"
target="_blank"
rel="noopener noreferrer"
>
<h2 className="mb-3 text-2xl font-semibold">
Docs{" "}
<span className="inline-block transition-transform group-hover:translate-x-1 motion-reduce:transform-none">
-&gt;
</span>
</h2>
<p className="m-0 max-w-[30ch] text-sm opacity-50">
Find in-depth information about Next.js features and API.
</p>
</a>

<a
href="https://nextjs.org/learn?utm_source=create-next-app&utm_medium=appdir-template-tw&utm_campaign=create-next-app"
className="group rounded-lg border border-transparent px-5 py-4 transition-colors hover:border-gray-300 hover:bg-gray-100 hover:dark:border-neutral-700 hover:dark:bg-neutral-800/30"
target="_blank"
rel="noopener noreferrer"
>
<h2 className="mb-3 text-2xl font-semibold">
Learn{" "}
<span className="inline-block transition-transform group-hover:translate-x-1 motion-reduce:transform-none">
-&gt;
</span>
</h2>
<p className="m-0 max-w-[30ch] text-sm opacity-50">
Learn about Next.js in an interactive course with&nbsp;quizzes!
</p>
</a>

<a
href="https://vercel.com/templates?framework=next.js&utm_source=create-next-app&utm_medium=appdir-template&utm_campaign=create-next-app"
className="group rounded-lg border border-transparent px-5 py-4 transition-colors hover:border-gray-300 hover:bg-gray-100 hover:dark:border-neutral-700 hover:dark:bg-neutral-800/30"
target="_blank"
rel="noopener noreferrer"
>
<h2 className="mb-3 text-2xl font-semibold">
Templates{" "}
<span className="inline-block transition-transform group-hover:translate-x-1 motion-reduce:transform-none">
-&gt;
</span>
</h2>
<p className="m-0 max-w-[30ch] text-sm opacity-50">
Explore starter templates for Next.js.
</p>
</a>

<a
href="https://vercel.com/new?utm_source=create-next-app&utm_medium=appdir-template&utm_campaign=create-next-app"
className="group rounded-lg border border-transparent px-5 py-4 transition-colors hover:border-gray-300 hover:bg-gray-100 hover:dark:border-neutral-700 hover:dark:bg-neutral-800/30"
target="_blank"
rel="noopener noreferrer"
>
<h2 className="mb-3 text-2xl font-semibold">
Deploy{" "}
<span className="inline-block transition-transform group-hover:translate-x-1 motion-reduce:transform-none">
-&gt;
</span>
</h2>
<p className="m-0 max-w-[30ch] text-balance text-sm opacity-50">
Instantly deploy your Next.js site to a shareable URL with Vercel.
</p>
</a>
</div>
<main className="w-full min-h-screen flex flex-col justify-center items-center">
<ul className="list-disc text-xl space-y-4">
<li>
<Link href="/cc" className="underline hover:text-sky-400">
Client Components Example
</Link>
</li>
<li>
<Link href="/rsc" className="underline hover:text-sky-400">
React Server Components Example
</Link>
</li>
</ul>
</main>
);
)
}

export default Home
9 changes: 9 additions & 0 deletions src/app/rsc/layout.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
const Layout = ({ children }: React.PropsWithChildren) => {
return (
<div className="w-full min-h-screen flex flex-col justify-center items-center gap-4">
{children}
</div>
)
}

export default Layout
16 changes: 16 additions & 0 deletions src/app/rsc/page.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import { fetchData } from '@/app/actions'
import { Button } from '@/components/Button'

const Page = async () => {
const message: string = fetchData('/api/delay')

return (
<>
<h1 className="text-lg font-semibold">Data received during RSC render</h1>
<div>{message}</div>
<Button variant="secondary" label="아무 일도 일어나지 않는 버튼" />
</>
)
}

export default Page
6 changes: 0 additions & 6 deletions src/stories/Button.tsx → src/components/Button.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -28,10 +28,6 @@ const ButtonVariants = cva(
interface ButtonProps
extends ButtonHTMLAttributes<HTMLButtonElement>,
VariantProps<typeof ButtonVariants> {
/**
* What background color to use
*/
bgColor?: string
/**
* Button contents
*/
Expand All @@ -52,15 +48,13 @@ interface ButtonProps
export const Button = ({
size,
variant,
bgColor,
label,
className,
...props
}: ButtonProps) => {
return (
<button
type="button"
style={{ backgroundColor: bgColor }}
className={cn(ButtonVariants({ size, variant }), className)}
{...props}
>
Expand Down
File renamed without changes.
12 changes: 12 additions & 0 deletions src/components/loading-indicator.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import Spinner from './spinner'

const LoadingIndicator = () => {
return (
<div role="status" className="flex items-center gap-2">
<Spinner />
<span>Loading...</span>
</div>
)
}

export default LoadingIndicator
22 changes: 22 additions & 0 deletions src/components/spinner.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
const Spinner = () => {
return (
<svg
aria-hidden="true"
className="w-4 h-4 text-gray-200 animate-spin fill-sky-500"
viewBox="0 0 100 101"
fill="none"
xmlns="http://www.w3.org/2000/svg"
>
<path
d="M100 50.5908C100 78.2051 77.6142 100.591 50 100.591C22.3858 100.591 0 78.2051 0 50.5908C0 22.9766 22.3858 0.59082 50 0.59082C77.6142 0.59082 100 22.9766 100 50.5908ZM9.08144 50.5908C9.08144 73.1895 27.4013 91.5094 50 91.5094C72.5987 91.5094 90.9186 73.1895 90.9186 50.5908C90.9186 27.9921 72.5987 9.67226 50 9.67226C27.4013 9.67226 9.08144 27.9921 9.08144 50.5908Z"
fill="currentColor"
/>
<path
d="M93.9676 39.0409C96.393 38.4038 97.8624 35.9116 97.0079 33.5539C95.2932 28.8227 92.871 24.3692 89.8167 20.348C85.8452 15.1192 80.8826 10.7238 75.2124 7.41289C69.5422 4.10194 63.2754 1.94025 56.7698 1.05124C51.7666 0.367541 46.6976 0.446843 41.7345 1.27873C39.2613 1.69328 37.813 4.19778 38.4501 6.62326C39.0873 9.04874 41.5694 10.4717 44.0505 10.1071C47.8511 9.54855 51.7191 9.52689 55.5402 10.0491C60.8642 10.7766 65.9928 12.5457 70.6331 15.2552C75.2735 17.9648 79.3347 21.5619 82.5849 25.841C84.9175 28.9121 86.7997 32.2913 88.1811 35.8758C89.083 38.2158 91.5421 39.6781 93.9676 39.0409Z"
fill="currentFill"
/>
</svg>
)
}

export default Spinner
8 changes: 3 additions & 5 deletions src/stories/Button.stories.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import type { Meta, StoryObj } from '@storybook/react'
import { fn } from '@storybook/test'
import { Button } from './Button'
import { Button } from '@/components/Button'

// More on how to set up stories at: https://storybook.js.org/docs/writing-stories#default-export
const meta = {
Expand All @@ -13,9 +13,7 @@ const meta = {
// This component will have an automatically generated Autodocs entry: https://storybook.js.org/docs/writing-docs/autodocs
tags: ['autodocs'],
// More on argTypes: https://storybook.js.org/docs/api/argtypes
argTypes: {
bgColor: { control: 'color' },
},
argTypes: {},
// Use `fn` to spy on the onClick arg, which will appear in the actions panel once invoked: https://storybook.js.org/docs/essentials/actions#action-args
args: { onClick: fn() },
} satisfies Meta<typeof Button>
Expand Down Expand Up @@ -56,6 +54,6 @@ export const Small: Story = {
export const Warning: Story = {
args: {
label: 'Delete now',
bgColor: 'red',
className: 'bg-[red]',
},
}
2 changes: 1 addition & 1 deletion src/stories/Header.stories.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import type { Meta, StoryObj } from '@storybook/react'
import { fn } from '@storybook/test'
import { Header } from './Header'
import { Header } from '../components/Header'

const meta = {
title: 'Example/Header',
Expand Down
Loading

0 comments on commit c497190

Please sign in to comment.