-
Notifications
You must be signed in to change notification settings - Fork 0
글로벌 컴포넌트 #19
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
11t518s
wants to merge
2
commits into
main
Choose a base branch
from
feat/#16
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
글로벌 컴포넌트 #19
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,3 @@ | ||
| export * from './src/utils' | ||
| export * from './src/manager' | ||
| export * from './src/provider' |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,10 @@ | ||
| { | ||
| "name": "@daehwahap/global-components", | ||
| "version": "0.0.1", | ||
| "private": true, | ||
| "devDependencies": { | ||
| "@types/react": "^18.2.61", | ||
| "react": "^18.2.0", | ||
| "typescript": "5.4.3" | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,33 @@ | ||
| import { GlobalComponentContextType } from '../types' | ||
|
|
||
| type GlobalComponentDisplayName = | ||
| | 'BottomSheet' | ||
| | 'LoadingIndicator' | ||
| | 'Popup' | ||
| | 'ScreenBottomSheet' | ||
| | 'Toast' | ||
|
|
||
| class GlobalComponentManager { | ||
| private map = new Map<string, React.RefObject<GlobalComponentContextType<any>>>() | ||
|
|
||
| public registerRef(name: string, ref: React.RefObject<GlobalComponentContextType<any>>): void { | ||
| if (this.map.has(name)) return | ||
| this.map.set(name, ref) | ||
| } | ||
|
|
||
| public async hideAll(): Promise<boolean> { | ||
| await Promise.all(Array.from(this.map).map(([_, popup]) => popup.current?.hide())) | ||
| return true | ||
| } | ||
|
|
||
| public async hide(params: { name: GlobalComponentDisplayName }) { | ||
| const componentRef = this.map.get(params.name) | ||
|
|
||
| if (componentRef) { | ||
| await componentRef.current?.hide() | ||
| } | ||
| return true | ||
| } | ||
| } | ||
|
|
||
| export const globalComponentManager = new GlobalComponentManager() |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,68 @@ | ||
| import { | ||
| createContext, | ||
| useCallback, | ||
| useContext, | ||
| useImperativeHandle, | ||
| useMemo, | ||
| useRef, | ||
| useState, | ||
| } from 'react' | ||
| import { | ||
| Animation, | ||
| GlobalComponentContextProviderProps, | ||
| GlobalComponentContextType, | ||
| } from '../types' | ||
|
|
||
| export const GlobalComponentContext = createContext<GlobalComponentContextType | null>(null) | ||
|
|
||
| const useGlobalComponentContext = () => { | ||
| const context = useContext(GlobalComponentContext) | ||
|
|
||
| if (!context) { | ||
| throw new Error('GlobalComponent should be used with GlobalComponentContext') | ||
| } | ||
|
|
||
| return context | ||
| } | ||
|
|
||
| const GlobalComponentContextProvider = <T extends any>({ | ||
| Component, | ||
| internalRef, | ||
| }: GlobalComponentContextProviderProps<T>) => { | ||
| const [props, setProps] = useState<T | null>(null) | ||
|
|
||
| const animations = useRef<Animation[]>([]) | ||
|
|
||
| const execHideAnimations = useCallback(async () => { | ||
| await Promise.all(animations.current.map((hideAnimationFn) => hideAnimationFn())) | ||
| animations.current = [] | ||
| }, []) | ||
|
|
||
| const hideComponent = useCallback(async () => { | ||
| await execHideAnimations() | ||
| setProps(null) | ||
| }, []) | ||
|
|
||
| const context = useMemo( | ||
| () => ({ | ||
| show: async (p: T) => { | ||
| await hideComponent() | ||
| setProps(p) | ||
| }, | ||
| hide: () => hideComponent(), | ||
| }), | ||
| [], | ||
| ) | ||
|
|
||
| useImperativeHandle(internalRef, () => context, []) | ||
|
|
||
| if (!props) return <></> | ||
|
|
||
| return ( | ||
| <GlobalComponentContext.Provider value={context}> | ||
| <Component {...props} /> | ||
| </GlobalComponentContext.Provider> | ||
| ) | ||
| } | ||
|
|
||
| export { useGlobalComponentContext, GlobalComponentContextProvider } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,11 @@ | ||
| export type Animation = () => Promise<void> | ||
|
|
||
| export interface GlobalComponentContextType<T = any> { | ||
| show: (props: T, hidePrevComponentImmediate?: boolean) => Promise<void> | ||
| hide: () => Promise<void> | ||
| } | ||
|
|
||
| export interface GlobalComponentContextProviderProps<T> { | ||
| Component: React.FC<T> | ||
| internalRef: React.RefObject<GlobalComponentContextType<T>> | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,39 @@ | ||
| import { createContext, createElement, createRef } from 'react' | ||
| import { GlobalComponentContextType } from '../types' | ||
| import { globalComponentManager } from '../manager' | ||
| import { GlobalComponentContextProvider } from '../provider' | ||
|
|
||
| export const createPortal = <T>(Component: React.FC<T>) => { | ||
| const context = createContext<GlobalComponentContextType<T>>({} as any) | ||
|
|
||
| const internalRef = createRef<GlobalComponentContextType<T>>() | ||
|
|
||
| const show = async (props: T, hidePrevComponentImmediate?: boolean) => { | ||
| if (!internalRef.current) { | ||
| return console.warn('global-components should be used with context') | ||
| } | ||
|
|
||
| await internalRef.current.show(props, hidePrevComponentImmediate) | ||
| } | ||
|
|
||
| const hide = async () => { | ||
| if (!internalRef.current) { | ||
| return console.warn('global-components should be used with context') | ||
| } | ||
|
|
||
| await internalRef.current.hide() | ||
| } | ||
|
|
||
| globalComponentManager.registerRef(Component.displayName || Component.name, internalRef) | ||
|
|
||
| return { | ||
| show, | ||
| hide, | ||
| Portal: ({ children }: { children?: React.ReactNode }) => | ||
| createElement( | ||
| context.Provider, | ||
| { value: { show, hide } }, | ||
| createElement(GlobalComponentContextProvider<T>, { Component, internalRef }, children), | ||
| ), | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,22 +1,22 @@ | ||
| import type { Metadata } from "next"; | ||
| import { Inter } from "next/font/google"; | ||
| import "./globals.css"; | ||
|
|
||
| const inter = Inter({ subsets: ["latin"] }); | ||
| import type { Metadata } from 'next' | ||
| import './globals.css' | ||
| import HOCProvider from '../global-components/hocs/HOCProvider' | ||
|
|
||
| export const metadata: Metadata = { | ||
| title: "Create Next App", | ||
| description: "Generated by create next app", | ||
| }; | ||
| title: 'Create Next App', | ||
| description: 'Generated by create next app', | ||
| } | ||
|
|
||
| export default function RootLayout({ | ||
| export function RootLayout({ | ||
| children, | ||
| }: Readonly<{ | ||
| children: React.ReactNode; | ||
| children: React.ReactNode | ||
| }>) { | ||
| return ( | ||
| <html lang="en"> | ||
| <body className={inter.className}>{children}</body> | ||
| <HOCProvider> | ||
| <body>{children}</body> | ||
| </HOCProvider> | ||
| </html> | ||
| ); | ||
| ) | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,9 +1,11 @@ | ||
| import { trpc } from '../../trpc' | ||
| import { TestButton } from '../../components/TestButton' | ||
|
|
||
| const TrpcPage = async () => { | ||
| const { greeting } = await trpc.createUser.mutate({ greeting: 'aa' }) | ||
|
|
||
| return <div>{greeting}</div> | ||
| return ( | ||
| <div style={{ backgroundColor: 'blue', height: '100vh' }}> | ||
| <TestButton /> | ||
| </div> | ||
| ) | ||
| } | ||
|
|
||
| export default TrpcPage |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,17 @@ | ||
| 'use client' | ||
|
|
||
| import Toast from '../global-components/components/Toast' | ||
|
|
||
| export const TestButton = () => { | ||
| return ( | ||
| <button | ||
| style={{ backgroundColor: 'black', width: 100, height: 100 }} | ||
| onClick={() => { | ||
| console.log('toast') | ||
| Toast.show({ text: 'toast' }) | ||
| }} | ||
| > | ||
| aaaaaaa | ||
| </button> | ||
| ) | ||
| } |
54 changes: 54 additions & 0 deletions
54
web/daehwahap-webview/src/global-components/components/Toast/index.tsx
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,54 @@ | ||
| 'use client' | ||
|
|
||
| import { motion, AnimatePresence } from 'framer-motion' | ||
| import { useEffect } from 'react' | ||
| import { createPortal, useGlobalComponentContext } from '@daehwahap/global-components' | ||
|
|
||
| interface ToastProps { | ||
| text: string | ||
| position?: 'top' | 'middle' | 'bottom' | ||
| duration?: number | ||
| } | ||
|
|
||
| const Toast = ({ text, position = 'top', duration = 2000 }: ToastProps) => { | ||
| // 이거는 Toast에만 해당하는 context임 | ||
| const { hide } = useGlobalComponentContext() | ||
|
|
||
| useEffect(() => { | ||
| setTimeout(() => { | ||
| hide() | ||
| }, duration) | ||
| }, []) | ||
|
|
||
| return ( | ||
| <AnimatePresence> | ||
| <motion.div | ||
| initial={{ opacity: 0, y: 20 }} | ||
| animate={{ opacity: 1, y: 0 }} | ||
| exit={{ opacity: 0, y: 20 }} | ||
| transition={{ duration: 0.3 }} | ||
| style={{ | ||
| position: 'fixed', | ||
| top: 60, | ||
| width: '100vw', | ||
| maxWidth: '720px', | ||
| display: 'flex', | ||
| justifyContent: 'center', | ||
| }} | ||
| > | ||
| <div | ||
| style={{ | ||
| background: '#000000BF', | ||
| color: '#fff', | ||
| padding: '12px 20px', | ||
| borderRadius: '12px', | ||
| }} | ||
| > | ||
| {text} | ||
| </div> | ||
| </motion.div> | ||
| </AnimatePresence> | ||
| ) | ||
| } | ||
|
|
||
| export default createPortal(Toast) |
19 changes: 19 additions & 0 deletions
19
web/daehwahap-webview/src/global-components/hocs/HOCProvider.tsx
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change | ||||
|---|---|---|---|---|---|---|
| @@ -0,0 +1,19 @@ | ||||||
| 'use client' | ||||||
|
|
||||||
| import { ReactNode } from 'react' | ||||||
| import Toast from '../components/Toast' | ||||||
|
|
||||||
| type HOCProviderProps = { children: ReactNode } | ||||||
|
|
||||||
| const HOCProvider = ({ children }: HOCProviderProps) => { | ||||||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 아래처럼 해도 되겠다
Suggested change
|
||||||
| return ( | ||||||
| <> | ||||||
| {children} | ||||||
| <div style={{ position: 'absolute' }}> | ||||||
| <Toast.Portal /> | ||||||
| </div> | ||||||
|
Comment on lines
+12
to
+14
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. |
||||||
| </> | ||||||
| ) | ||||||
| } | ||||||
|
|
||||||
| export default HOCProvider | ||||||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.

There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
요런식으로 packageManager 명시해줘도 좋을듯 !
