feat: init project && add useOnClickOutside demo

This commit is contained in:
cheliangzhao 2023-08-21 17:11:33 +08:00
parent d9833ddb3d
commit fa6cee4643
17 changed files with 4273 additions and 3444 deletions

View File

@ -1,34 +1,15 @@
This is a [Next.js](https://nextjs.org/) project bootstrapped with [`create-next-app`](https://github.com/vercel/next.js/tree/canary/packages/create-next-app).
# NextJs + NextUI
## Getting Started
First, run the development server:
1. 为什么 `pages/*` 添加NextUI后无效
```bash
npm run dev
# or
yarn dev
# or
pnpm dev
后来发现,在`pages/*`中使用tailwind也无效原来原因是没有在css文件中引入tailwind
添加 `globals.css` 文件,在`_app.tsx`中导入
```css
@tailwind base;
@tailwind components;
@tailwind utilities;
```
Open [http://localhost:3000](http://localhost:3000) with your browser to see the result.
You can start editing the page by modifying `app/page.tsx`. The page auto-updates as you edit the file.
This project uses [`next/font`](https://nextjs.org/docs/basic-features/font-optimization) to automatically optimize and load Inter, a custom Google Font.
## Learn More
To learn more about Next.js, take a look at the following resources:
- [Next.js Documentation](https://nextjs.org/docs) - learn about Next.js features and API.
- [Learn Next.js](https://nextjs.org/learn) - an interactive Next.js tutorial.
You can check out [the Next.js GitHub repository](https://github.com/vercel/next.js/) - your feedback and contributions are welcome!
## Deploy on Vercel
The easiest way to deploy your Next.js app is to use the [Vercel Platform](https://vercel.com/new?utm_medium=default-template&filter=next.js&utm_source=create-next-app&utm_campaign=create-next-app-readme) from the creators of Next.js.
Check out our [Next.js deployment documentation](https://nextjs.org/docs/deployment) for more details.

Binary file not shown.

Before

Width:  |  Height:  |  Size: 25 KiB

View File

@ -1,22 +0,0 @@
import './globals.css'
import type { Metadata } from 'next'
import { Inter } from 'next/font/google'
const inter = Inter({ subsets: ['latin'] })
export const metadata: Metadata = {
title: 'Create Next App',
description: 'Generated by create next app',
}
export default function RootLayout({
children,
}: {
children: React.ReactNode
}) {
return (
<html lang="en">
<body className={inter.className}>{children}</body>
</html>
)
}

View File

@ -1,113 +0,0 @@
import Image from 'next/image'
export default function Home() {
return (
<main className="flex min-h-screen flex-col items-center justify-between p-24">
<div className="z-10 max-w-5xl w-full 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">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:h-auto lg:w-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 flex place-items-center before:absolute before:h-[300px] before:w-[480px] 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-[240px] 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 before:lg:h-[360px] z-[-1]">
<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:max-w-5xl lg:w-full lg:mb-0 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 the Next.js 13 playground.
</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-sm opacity-50`}>
Instantly deploy your Next.js site to a shareable URL with Vercel.
</p>
</a>
</div>
</main>
)
}

View File

@ -0,0 +1,39 @@
import React from "react";
export const UserIcon = ({
fill = 'currentColor',
filled,
size,
height,
width,
label,
...props
}: any) => {
return (
<svg
data-name="Iconly/Curved/Profile"
xmlns="http://www.w3.org/2000/svg"
viewBox="0 0 24 24"
width={size || width || 24}
height={size || height || 24}
{...props}
>
<g
fill="none"
stroke={fill}
strokeLinecap="round"
strokeLinejoin="round"
strokeMiterlimit={10}
strokeWidth={1.5}
>
<path
data-name="Stroke 1"
d="M11.845 21.662C8.153 21.662 5 21.088 5 18.787s3.133-4.425 6.845-4.425c3.692 0 6.845 2.1 6.845 4.4s-3.134 2.9-6.845 2.9z"
/>
<path
data-name="Stroke 3"
d="M11.837 11.174a4.372 4.372 0 10-.031 0z"
/>
</g>
</svg>
);
};

View File

@ -1,5 +1,5 @@
{
"name": "next-nextui-demo",
"name": "usehooks-ts--demo",
"version": "0.1.0",
"private": true,
"scripts": {
@ -9,17 +9,21 @@
"lint": "next lint"
},
"dependencies": {
"@nextui-org/react": "^2.0.22",
"@types/node": "20.5.1",
"@types/react": "18.2.20",
"@types/react-dom": "18.2.7",
"autoprefixer": "10.4.15",
"classnames": "^2.3.2",
"eslint": "8.47.0",
"eslint-config-next": "13.4.19",
"framer-motion": "^10.16.0",
"next": "13.4.19",
"postcss": "8.4.28",
"react": "18.2.0",
"react-dom": "18.2.0",
"tailwindcss": "3.3.3",
"typescript": "5.1.6"
"typescript": "5.1.6",
"usehooks-ts": "^2.9.1"
}
}

11
pages/_app.tsx Normal file
View File

@ -0,0 +1,11 @@
import './globals.css'
import type { AppProps } from 'next/app'
import { NextUIProvider } from '@nextui-org/react'
export default function App({ Component, pageProps }: AppProps) {
return (
<NextUIProvider>
<Component {...pageProps} />
</NextUIProvider>
)
}

13
pages/_document.tsx Normal file
View File

@ -0,0 +1,13 @@
import { Html, Head, Main, NextScript } from 'next/document'
export default function Document() {
return (
<Html>
<Head />
<body >
<Main />
<NextScript />
</body>
</Html>
)
}

View File

@ -2,6 +2,10 @@
@tailwind components;
@tailwind utilities;
body, html {
height: 100vh;
}
:root {
--foreground-rgb: 0, 0, 0;
--background-start-rgb: 214, 219, 220;

10
pages/index.tsx Normal file
View File

@ -0,0 +1,10 @@
import { Button } from "@nextui-org/react"
export default function Page() {
return (
<>
<Button> test</Button>
<h1 className="bg-red-500">Hello, Next.js!</h1>
</>
)
}

20
pages/use/boolean.tsx Normal file
View File

@ -0,0 +1,20 @@
import { useBoolean } from "usehooks-ts";
export default function Boolean() {
const { value, setValue, setTrue, setFalse, toggle } = useBoolean(false)
// Just an example to use "setValue"
const customToggle = () => setValue((x: boolean) => !x)
return (
<>
<p>
Value is <code>{value.toString()}</code>
</p>
<button onClick={setTrue}>set true</button>
<button onClick={setFalse}>set false</button>
<button onClick={toggle}>toggle</button>
<button onClick={customToggle}>custom toggle</button>
</>
)
}

View File

@ -0,0 +1,45 @@
import { useEffect, useRef } from 'react';
import { useEventListener, useSsr } from 'usehooks-ts';
export default function EventListener() {
const getDocument = () => (
typeof document !== 'undefined' ?
document :
null
);
// Define button ref
const buttonRef = useRef<HTMLButtonElement>(null)
const documentRef = useRef<Document>(getDocument());
const onScroll = (event: Event) => {
console.log('window scrolled!', event)
}
const onClick = (event: Event) => {
console.log('button clicked!', event)
}
const onVisibilityChange = (event: Event) => {
console.log('doc visibility changed!', {
isVisible: !document.hidden,
event,
})
}
// example with window based event
useEventListener('scroll', onScroll)
// example with document based event
useEventListener('visibilitychange', onVisibilityChange, documentRef)
// example with element based event
useEventListener('click', onClick, buttonRef)
return (
<div style={{ minHeight: '200vh' }}>
<button ref={buttonRef}>Click me</button>
</div>
)
}

View File

@ -0,0 +1,34 @@
import { UserIcon } from "@/components/icons/userIcon";
import { Button } from "@nextui-org/react";
import { useRef, useState } from "react";
import { useOnClickOutside } from "usehooks-ts";
export default function OnClickOutSide() {
const ref = useRef(null);
const [edit, setEdit] = useState(false);
const [inputValue] = useState('User Name');
const handleOnClick = () => {
setEdit(true);
}
const handleOnClickOutside = () => {
setEdit(false);
}
useOnClickOutside(ref, handleOnClickOutside);
return (
<>
<Button
color="danger"
variant="bordered"
isIconOnly={!edit}
ref={ref}
onClick={handleOnClick}
>
<UserIcon />
{edit && inputValue}
</Button>
</>
);
};

File diff suppressed because it is too large Load Diff

View File

@ -1,3 +1,4 @@
import { nextui } from '@nextui-org/react'
import type { Config } from 'tailwindcss'
const config: Config = {
@ -5,6 +6,7 @@ const config: Config = {
'./pages/**/*.{js,ts,jsx,tsx,mdx}',
'./components/**/*.{js,ts,jsx,tsx,mdx}',
'./app/**/*.{js,ts,jsx,tsx,mdx}',
'./node_modules/@nextui-org/theme/dist/**/*.{js,ts,jsx,tsx}',
],
theme: {
extend: {
@ -15,6 +17,7 @@ const config: Config = {
},
},
},
plugins: [],
darkMode: 'class',
plugins: [nextui()],
}
export default config

View File

@ -23,6 +23,6 @@
"@/*": ["./*"]
}
},
"include": ["next-env.d.ts", "**/*.ts", "**/*.tsx", ".next/types/**/*.ts"],
"include": ["next-env.d.ts", "**/*.ts", "**/*.tsx", ".next/types/**/*.ts", "components/icons/userIcon.tsx"],
"exclude": ["node_modules"]
}

4075
yarn.lock Normal file

File diff suppressed because it is too large Load Diff