diff --git a/package.json b/package.json index 7b0d9c4..3825d6f 100644 --- a/package.json +++ b/package.json @@ -4,7 +4,7 @@ "version": "1.0.0", "scripts": { "build": "rsbuild build", - "dev": "rsbuild dev --open", + "dev": "rsbuild dev", "format": "prettier --write .", "lint": "eslint .", "preview": "rsbuild preview" diff --git a/src/App.css b/src/App.css index 164c0a6..2ea6fc0 100644 --- a/src/App.css +++ b/src/App.css @@ -1,8 +1,10 @@ body { margin: 0; - color: #fff; font-family: Inter, Avenir, Helvetica, Arial, sans-serif; - background-image: linear-gradient(to bottom, #020917, #101725); + + --color-parent: #61a5c2; + --color-child: #89c2d9; + --color-grandchild: #a9d6e5; } .content { @@ -12,15 +14,48 @@ body { text-align: center; flex-direction: column; justify-content: center; + align-items: center; } -.content h1 { - font-size: 3.6rem; - font-weight: 700; +.version__toggle { + text-transform: capitalize; } -.content p { +.parent { + width: fit-content; + display: flex; + flex-direction: column; + justify-content: center; + align-items: center; + background-color: var(--color-parent); + padding: 2rem; + border-radius: 8px; +} + +.child { + display: flex; + flex-direction: column; + gap: 1rem; + padding: 2rem; + border-radius: 8px; + background: var(--color-child); + position: relative; +} + +.card__label { + padding: 0; + margin: 0; + position: absolute; font-size: 1.2rem; - font-weight: 400; - opacity: 0.5; + line-height: 1.2rem; + top: 0.4rem; + left: 0.4rem; + color: white; +} + +.grandson { + position: relative; + padding: 1rem; + border-radius: 8px; + background: var(--color-grandchild); } diff --git a/src/App.tsx b/src/App.tsx index 994fa78..afc6806 100644 --- a/src/App.tsx +++ b/src/App.tsx @@ -1,10 +1,27 @@ +import { useState } from 'react'; import './App.css'; import { Parent } from './children-render/parent'; +import { ParentFixed } from './children-render-fixed/parent-fixed'; +import { FamilyContextProvider } from './children-render-fixed/family-context'; const App = () => { + const [version, setVersion] = useState<'normal' | 'fixed'>('normal'); + return (
- + + {version === 'normal' ? ( + + ) : ( + + + + )}
); }; diff --git a/src/children-render-fixed/child-fixed.tsx b/src/children-render-fixed/child-fixed.tsx new file mode 100644 index 0000000..8acac21 --- /dev/null +++ b/src/children-render-fixed/child-fixed.tsx @@ -0,0 +1,12 @@ +import { GrandsonFixed } from './grandson-fixed'; + +export const ChildFixed = () => { + console.log('__children render'); + + return ( +
+
Child
+ +
+ ); +}; diff --git a/src/children-render-fixed/context-input.tsx b/src/children-render-fixed/context-input.tsx new file mode 100644 index 0000000..11eea02 --- /dev/null +++ b/src/children-render-fixed/context-input.tsx @@ -0,0 +1,13 @@ +import { useFamily } from './family-context'; + +export const ContextInput = () => { + const { name, setName } = useFamily(); + return ( + setName(e.target.value)} + /> + ); +}; diff --git a/src/children-render-fixed/family-context.tsx b/src/children-render-fixed/family-context.tsx new file mode 100644 index 0000000..4277c1d --- /dev/null +++ b/src/children-render-fixed/family-context.tsx @@ -0,0 +1,28 @@ +import { createContext, useContext, useState } from 'react'; + +interface FamilyContextType { + name: string; + setName: (newName: string) => void; +} + +const FamilyContext = createContext(undefined); + +const FamilyContextProvider: React.FC = ({ + children, +}) => { + const [name, setName] = useState(''); + return ( + + {children} + + ); +}; + +const useFamily = () => { + const context = useContext(FamilyContext); + if (!context) + throw new Error('useFamily must be used within a FamilyContextProvider'); + return context; +}; + +export { FamilyContextProvider, useFamily }; diff --git a/src/children-render-fixed/grandson-fixed.tsx b/src/children-render-fixed/grandson-fixed.tsx new file mode 100644 index 0000000..5fe9153 --- /dev/null +++ b/src/children-render-fixed/grandson-fixed.tsx @@ -0,0 +1,14 @@ +import { useFamily } from './family-context'; + +export const GrandsonFixed = () => { + console.log('__grandson render'); + + const { name } = useFamily(); + + return ( +
+
Grandson
+

Name: {name}

+
+ ); +}; diff --git a/src/children-render-fixed/parent-fixed.tsx b/src/children-render-fixed/parent-fixed.tsx new file mode 100644 index 0000000..435f516 --- /dev/null +++ b/src/children-render-fixed/parent-fixed.tsx @@ -0,0 +1,14 @@ +import { ChildFixed } from './child-fixed'; +import { ContextInput } from './context-input'; + +export const ParentFixed = () => { + console.log('__parent render'); + + return ( +
+

I'm the parent

+ + +
+ ); +}; diff --git a/src/children-render/children.tsx b/src/children-render/children.tsx index ace5078..2da568f 100644 --- a/src/children-render/children.tsx +++ b/src/children-render/children.tsx @@ -1,13 +1,29 @@ +import { useRef, useState } from 'react'; +import { Grandson } from './grandson'; + type ChildrenProps = { counter: number; }; export const Children = (props: ChildrenProps) => { console.log('__children render'); + const [myOwnState, setMyOwnState] = useState(0); + const myRef = useRef(0); + return ( -
-
I'm its child
-

My counter is {props.counter}

+
+
Child
+

My parent counter is {props.counter}

+ + + + + +
); }; diff --git a/src/children-render/grandson.tsx b/src/children-render/grandson.tsx new file mode 100644 index 0000000..5670867 --- /dev/null +++ b/src/children-render/grandson.tsx @@ -0,0 +1,15 @@ +type GrandsonProps = { + grandparentState: number; + parentState: number; +}; + +export const Grandson = (props: GrandsonProps) => { + console.log('__grandson render'); + return ( +
+
Grandson
+

Parent state: {props.parentState}

+

Grandparent state: {props.grandparentState}

+
+ ); +}; diff --git a/src/children-render/parent.tsx b/src/children-render/parent.tsx index 4839186..6d0d488 100644 --- a/src/children-render/parent.tsx +++ b/src/children-render/parent.tsx @@ -3,21 +3,28 @@ import { Children } from './children'; export const Parent = () => { console.log('__parent render'); - const [counter, setCounter] = useState(0); - const [childrenCounter, setChildrenCounter] = useState(0); + const [ownState, setOwnState] = useState(0); + const [childrenPropState, setChildrenPropState] = useState(0); + const [name, setName] = useState(''); return ( -
+

I'm the parent

- -
-
These are my children
- +
);