Skip to content

Commit

Permalink
Merge pull request #276 from SaekKkanDa/develop
Browse files Browse the repository at this point in the history
v1.0.0 배포
  • Loading branch information
seoltang authored Sep 3, 2024
2 parents 363fa55 + 7baa3b5 commit d633997
Show file tree
Hide file tree
Showing 98 changed files with 3,176 additions and 1,297 deletions.
4 changes: 4 additions & 0 deletions @types/error.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
interface ErrorResponse {
code: number;
message: string;
}
12 changes: 12 additions & 0 deletions base/components/Backdrop.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import styled from 'styled-components';

export const Backdrop = styled.div`
position: fixed;
top: 0;
left: 0;
width: 100%;
height: 100%;
background-color: ${({ theme }) => theme.gray[900]};
opacity: 0.6;
z-index: 10;
`;
66 changes: 33 additions & 33 deletions base/components/Hidden.tsx
Original file line number Diff line number Diff line change
@@ -1,33 +1,33 @@
import { CSSProperties, ReactNode } from 'react';
import styled from 'styled-components';

export interface HiddenProps {
isHidden: boolean;
isUseVisibility?: boolean;
style?: CSSProperties;
children?: ReactNode;
}
export function Hidden({
isHidden,
isUseVisibility = false,
style,
children,
}: HiddenProps) {
return (
<HiddenContainer
isHidden={isHidden}
isUseVisibility={isUseVisibility}
style={style}
>
{children}
</HiddenContainer>
);
}

const HiddenContainer = styled.div<{
isHidden: boolean;
isUseVisibility: boolean;
}>`
${({ isUseVisibility, isHidden }) =>
isHidden && (isUseVisibility ? 'visiblity : hidden' : 'display : none')}
`;
import { CSSProperties, ReactNode } from 'react';
import styled from 'styled-components';

export interface HiddenProps {
isHidden: boolean;
isUseVisibility?: boolean;
style?: CSSProperties;
children?: ReactNode;
}
export function Hidden({
isHidden,
isUseVisibility = false,
style,
children,
}: HiddenProps) {
return (
<HiddenContainer
isHidden={isHidden}
isUseVisibility={isUseVisibility}
style={style}
>
{children}
</HiddenContainer>
);
}

const HiddenContainer = styled.div<{
isHidden: boolean;
isUseVisibility: boolean;
}>`
${({ isUseVisibility, isHidden }) =>
isHidden && (isUseVisibility ? 'visiblity : hidden' : 'display : none')}
`;
27 changes: 12 additions & 15 deletions base/components/ModalBase.tsx
Original file line number Diff line number Diff line change
@@ -1,24 +1,32 @@
import { ReactElement, ReactNode, cloneElement } from 'react';
import { createPortal } from 'react-dom';
import { CSSProperties, ReactElement, ReactNode, cloneElement } from 'react';
import styled from 'styled-components';

import { Hidden } from '@Base/components/Hidden';
import useCreatePortal from '@Base/hooks/useCreatePortal';
import { isNil } from '@Base/utils/check';
import { Backdrop } from '@Base/components/Backdrop';

export interface ModalBaseProps {
backdropComponent?: ReactElement;
children?: ReactNode;
isOpen: boolean;
onClose?: () => void;
hiddenStyle?: CSSProperties;
}

export function ModalBase({
backdropComponent = <DefaultBackdrop />,
backdropComponent = <Backdrop />,
children,
isOpen,
onClose,
hiddenStyle,
}: ModalBaseProps) {
const createPortal = useCreatePortal();

if (isNil(createPortal)) return <></>;

return createPortal(
<Hidden isHidden={!isOpen}>
<Hidden isHidden={!isOpen} style={hiddenStyle}>
<Container>
{cloneElement(backdropComponent, { onClick: onClose })}
{children}
Expand All @@ -34,14 +42,3 @@ export const Container = styled.div`
max-height: 100dvh;
overflow: hidden;
`;

export const DefaultBackdrop = styled.div`
position: fixed;
top: 0;
left: 0;
width: 100%;
height: 100%;
background-color: #27272a;
opacity: 0.5;
z-index: 10;
`;
10 changes: 8 additions & 2 deletions src/hooks/useCountUp.ts → base/hooks/useCountUp.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,16 @@ const easeOutExpo = (rate: number) => {
};

// 점차 느려지는 count up 함수
export const useCountUp = (targetNumber: number, duration: number) => {
export const useCountUp = (
targetNumber: number,
duration: number,
isInView: boolean
) => {
const [count, setCount] = useState(0);

useEffect(() => {
if (!isInView) return;

const frameRate = 1000 / 60;
const totalFrame = Math.round(duration / frameRate);

Expand All @@ -27,7 +33,7 @@ export const useCountUp = (targetNumber: number, duration: number) => {
return () => {
clearInterval(counter);
};
}, [duration, targetNumber]);
}, [duration, isInView, targetNumber]);

return count;
};
15 changes: 15 additions & 0 deletions base/hooks/useCreatePortal.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import { useEffect, useState } from 'react';
import { createPortal } from 'react-dom';

// next js client component should be rendered after window object is created
// https://stackoverflow.com/questions/75408148/referenceerror-document-is-not-defined-inside-next-js-client-component
export default function useCreatePortal() {
const [isMounted, setIsMounted] = useState(false);
useEffect(() => {
setIsMounted(true);
}, []);

if (!isMounted) return null;

return createPortal;
}
31 changes: 31 additions & 0 deletions base/hooks/useIntersectionObserver.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
import { useCallback, useEffect, useRef } from 'react';

export const useIntersectionObserver = (
onIntersect: (
entry: IntersectionObserverEntry,
observer: IntersectionObserver
) => void,
options?: IntersectionObserverInit
) => {
const ref = useRef<HTMLDivElement>(null);

const callback = useCallback(
(entries: IntersectionObserverEntry[], observer: IntersectionObserver) => {
entries.forEach((entry) => {
if (entry.isIntersecting) onIntersect(entry, observer);
});
},
[onIntersect]
);

useEffect(() => {
if (!ref.current) return;

const observer = new IntersectionObserver(callback, options);
observer.observe(ref.current);

return () => observer.disconnect();
}, [ref, options, callback]);

return { ref };
};
24 changes: 24 additions & 0 deletions base/hooks/useLazyUpdate.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
import { useEffect, useRef, useState } from 'react';

export interface UseLazyUpateProps<T> {
state: T;
time?: number;
}

export default function useLazyUpdate<T>({
state,
time = 500,
}: UseLazyUpateProps<T>) {
const timeRef = useRef(time);
const [localState, setLocalState] = useState(state);

useEffect(() => {
const timoutId = setTimeout(() => setLocalState(state), timeRef.current);

return () => {
clearTimeout(timoutId);
};
}, [state]);

return localState;
}
4 changes: 2 additions & 2 deletions base/hooks/useModal.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { useCallback, useState } from 'react';

import { isTrue } from '@Base/utils/check';
import { isNotNil } from '@Base/utils/check';

export interface UseModalProps {
defaultMessage?: string;
Expand All @@ -15,7 +15,7 @@ export function useModal({
const [message, setMessage] = useState(defaultMessage);

const open = useCallback((message?: string) => {
if (isTrue(message)) setMessage(message);
if (isNotNil(message)) setMessage(message);

setIsOpen(true);
}, []);
Expand Down
18 changes: 9 additions & 9 deletions base/hooks/useTrigger.tsx
Original file line number Diff line number Diff line change
@@ -1,31 +1,31 @@
import { useCallback, useEffect, useState } from 'react';
import { useCallback, useEffect, useRef } from 'react';

import { isFalse } from '@Base/utils/check';
import { isNil } from '@Base/utils/check';

export interface UseTriggerProps {
triggerFn?: () => boolean;
onTrigger?: (reset: () => void) => void;
}

export function useTrigger({ triggerFn, onTrigger }: UseTriggerProps) {
const [isTriggered, setIsTriggered] = useState(false);
const isTrigredRef = useRef(false);

const reset = useCallback(() => {
setIsTriggered(false);
isTrigredRef.current = false;
}, []);

const trigger = useCallback(() => {
setIsTriggered(true);
isTrigredRef.current = true;
}, []);

useEffect(() => {
if (isFalse(triggerFn)) return;
if (isNil(triggerFn)) return;

if (triggerFn()) {
setIsTriggered(true);
if (!isTrigredRef.current && triggerFn()) {
isTrigredRef.current = true;
onTrigger?.(reset);
}
}, [onTrigger, reset, triggerFn]);

return { isTriggered, reset, trigger };
return { isTriggered: isTrigredRef.current, reset, trigger };
}
44 changes: 22 additions & 22 deletions base/utils/check.ts
Original file line number Diff line number Diff line change
@@ -1,22 +1,22 @@
/**
* checking object if is null + undefined or not
* if object is 0, this functions consider as true
*/
export function isFalse<T>(obj: T | undefined | null): obj is undefined | null {
if (obj === undefined || obj === null) return true;

return false;
}

/**
* checking object if is null + undefined or not
* if object is 0, this functions consider as true
*/
export function isTrue<T>(obj: T | undefined | null): obj is T {
return !isFalse(obj);
}

export function isEmpty<T extends string | Array<unknown>>(obj: T) {
if (obj.length === 0) return true;
return false;
}
/**
* checking object if is null + undefined or not
* if object is 0, this functions consider as true
*/
export function isNil<T>(obj: T | undefined | null): obj is undefined | null {
if (obj === undefined || obj === null) return true;

return false;
}

/**
* checking object if is null + undefined or not
* if object is 0, this functions consider as true
*/
export function isNotNil<T>(obj: T | undefined | null): obj is T {
return !isNil(obj);
}

export function isEmpty<T extends string | Array<unknown>>(obj: T) {
if (obj.length === 0) return true;
return false;
}
11 changes: 11 additions & 0 deletions base/utils/dataExtension.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import { isNil } from '@Base/utils/check';

export function getRandomId() {
return Math.random().toString(16).slice(2);
}

export function withDefault<T>(obj: T | null | undefined, defaultValue: T): T {
if (isNil(obj)) return defaultValue;

return obj;
}
12 changes: 12 additions & 0 deletions base/utils/mediaExtension.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
export async function loadImg(src: string) {
return new Promise<HTMLImageElement>((resolve, reject) => {
const img = new Image();
img.onload = () => {
resolve(img);
};
img.onerror = (err) => {
reject(err);
};
img.src = src;
});
}
Loading

0 comments on commit d633997

Please sign in to comment.