Skip to content

Commit

Permalink
finish avatar generator example
Browse files Browse the repository at this point in the history
  • Loading branch information
always-maap committed Aug 11, 2022
1 parent 31b3400 commit a182f6c
Show file tree
Hide file tree
Showing 8 changed files with 73 additions and 9 deletions.
1 change: 1 addition & 0 deletions examples/avatar-generator/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
"preview": "vite preview"
},
"dependencies": {
"party-js": "^2.2.0",
"react": "^18.2.0",
"react-dom": "^18.2.0"
},
Expand Down
29 changes: 26 additions & 3 deletions examples/avatar-generator/src/App.jsx
Original file line number Diff line number Diff line change
@@ -1,12 +1,16 @@
import { useState } from "react";
import { useState, useRef } from "react";
import "./App.css";
import UserCard from "./components/UserCard";
import Button from "./components/Button";
import Header from "./components/Header";
import party from "party-js";

function App() {
const [name, setName] = useState("");
const [avatar, setAvatar] = useState("");
const [isLoading, setIsLoading] = useState(false);
const [headerValue, setHeaderValue] = useState("Avatar Generator");
const ref = useRef(null);

async function getAvatar(name, gender) {
const url = `https://avatars.dicebear.com/api/${gender}/${name}.svg`;
Expand All @@ -26,11 +30,30 @@ function App() {
getAvatar(fullName, gender);
}

function startParty() {
party.confetti(ref.current, {
count: party.variation.range(20, 40),
spread: party.variation.range(10, 20),
size: party.variation.range(1, 3),
});
setHeaderValue(name);
}

return (
<div className="wrapper">
<h1 align="center">Avatar Generator</h1>
<Button onClick={() => generateRandomUser()}>Generate User</Button>
<Header value={headerValue} setValue={setHeaderValue} />
<Button ref={ref} onClick={() => generateRandomUser()}>
Generate User
</Button>
<UserCard isLoading={isLoading} name={name} avatar={avatar} />
{avatar !== "" && (
<Button
onClick={startParty}
style={{ background: "green", color: "white" }}
>
Select as Avatar
</Button>
)}
</div>
);
}
Expand Down
9 changes: 6 additions & 3 deletions examples/avatar-generator/src/components/Button.jsx
Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@
import { forwardRef } from "react";
import styles from "./Button.module.css";

export default function Button(props) {
const Button = forwardRef((props, ref) => {
return (
<button className={styles.btn} {...props}>
<button className={styles.btn} ref={ref} {...props}>
{props.children}
</button>
);
}
});

export default Button;
25 changes: 25 additions & 0 deletions examples/avatar-generator/src/components/Header.jsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
import { useState } from "react";
import Button from "./Button";

export default function Header(props) {
const [isEditMode, setIsEditMode] = useState(false);

return (
<div>
{isEditMode ? (
<>
<input
style={{ height: 70, fontSize: 28, marginRight: 5 }}
value={props.value}
onChange={(e) => props.setValue(e.target.value)}
/>
<Button onClick={() => setIsEditMode(false)}>✔️</Button>
</>
) : (
<h1 onClick={() => setIsEditMode(true)} align="center">
{props.value}
</h1>
)}
</div>
);
}
4 changes: 2 additions & 2 deletions examples/avatar-generator/src/components/UserCard.module.css
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
.wrapper {
background: lightblue;
background: #eee;
padding: 20px;
width: 100%;
border: 1px solid black;
border: 1px solid #eee;
border-radius: 10px;
height: 200px;
display: flex;
Expand Down
2 changes: 1 addition & 1 deletion examples/avatar-generator/src/index.css
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,6 @@
flex-direction: column;
justify-content: center;
align-items: center;
background-image: linear-gradient(to right, #439bff, #2f61fe);
background-image: linear-gradient(to right, #19FD8280, #17FE5B50);
height: 100vh;
}
7 changes: 7 additions & 0 deletions examples/todo-react/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
- [JavaScript modules](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Modules)

- [Controlled vs Uncontrolled Forms](https://reactjs.org/docs/uncontrolled-components.html#:~:text=In%20a%20controlled%20component%2C%20form,form%20values%20from%20the%20DOM.)

- [vite](https://vitejs.dev/)

- [React](https://beta.reactjs.org/learn)
5 changes: 5 additions & 0 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -611,6 +611,11 @@ node-releases@^2.0.6:
resolved "https://registry.yarnpkg.com/node-releases/-/node-releases-2.0.6.tgz#8a7088c63a55e493845683ebf3c828d8c51c5503"
integrity sha512-PiVXnNuFm5+iYkLBNeq5211hvO38y63T0i2KKh2KnUs3RpzJ+JtODFjkD8yjLwnDkTYF1eKXheUwdssR+NRZdg==

party-js@^2.2.0:
version "2.2.0"
resolved "https://registry.yarnpkg.com/party-js/-/party-js-2.2.0.tgz#3340026971c9e62fd34db102daaa645fbc9130b8"
integrity sha512-50hGuALCpvDTrQLPQ1fgUgxKIWAH28ShVkmeK/3zhO0YJyCqkhrZhQEkWPxDYLvbFJ7YAXyROmFEu35gKpZLtQ==

path-parse@^1.0.7:
version "1.0.7"
resolved "https://registry.yarnpkg.com/path-parse/-/path-parse-1.0.7.tgz#fbc114b60ca42b30d9daf5858e4bd68bbedb6735"
Expand Down

1 comment on commit a182f6c

@vercel
Copy link

@vercel vercel bot commented on a182f6c Aug 11, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Successfully deployed to the following URLs:

nit-news – ./

nit-news-always-maap.vercel.app
nit-news-git-master-always-maap.vercel.app
nit-news.vercel.app

Please sign in to comment.