Skip to content

Commit

Permalink
fix(react): AspectRatio 인터페이스 및 내부 로직 개선 (#597)
Browse files Browse the repository at this point in the history
  • Loading branch information
ssi02014 authored Nov 20, 2024
1 parent 7920bff commit f7d9f93
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 74 deletions.
5 changes: 5 additions & 0 deletions .changeset/polite-apes-rush.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@modern-kit/react': patch
---

fix(react): AspectRatio 인터페이스 및 내부 로직 개선 - @ssi0214
44 changes: 7 additions & 37 deletions docs/docs/react/components/AspectRatio.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -14,21 +14,22 @@ import { AspectRatio } from '@modern-kit/react';

## Interface
```ts title="typescript"
interface AspectRatioProps extends ComponentProps<'div'> {
asChild?: boolean;
interface AspectRatioProps {
children: JSX.Element;
ratio: number;
}
```
```tsx title="typescript"
const AspectRatio: ({ ratio, children }: AspectRatioProps) => JSX.Element;
```

## Usage
### Basic
- 기본적으로 div 태그를 부모로 두고 자식 요소를 렌더링 하며, div 태그에 aspect-ratio 속성을 적용합니다.
```tsx title="typescript"
import { AspectRatio } from '@modern-kit/react'

const Example = () => {
return (
<div style={{ width: '500px' }}>
<div style={{ width: '700px' }}>
<AspectRatio ratio={427 / 640}>
<img src="https://github.com/user-attachments/assets/dd60ec12-afd7-44c9-bd6b-0069e16bf2c9" />
</AspectRatio>
Expand All @@ -39,7 +40,7 @@ const Example = () => {

export const Example1 = () => {
return (
<div style={{ width: '500px' }}>
<div style={{ width: '700px' }}>
<AspectRatio ratio={427 / 640}>
<img src="https://github.com/user-attachments/assets/dd60ec12-afd7-44c9-bd6b-0069e16bf2c9" />
</AspectRatio>
Expand All @@ -49,34 +50,3 @@ export const Example1 = () => {

<Example1 />

### asChild
- asChild 속성을 true로 설정하면 자식 요소에 aspect-ratio 속성을 적용합니다.
```tsx title="typescript"
import { AspectRatio } from '@modern-kit/react'

const Example = () => {
return (
<div style={{ width: '500px' }}>
<AspectRatio ratio={427 / 640} asChild>
<section> {/* section 태그에 aspect-ratio 속성이 적용됩니다. */}
<img src="https://github.com/user-attachments/assets/dd60ec12-afd7-44c9-bd6b-0069e16bf2c9" />
</section>
</AspectRatio>
</div>
);
};
```

export const Example2 = () => {
return (
<div style={{ width: '500px' }}>
<AspectRatio ratio={427 / 640} asChild>
<section>
<img src="https://github.com/user-attachments/assets/dd60ec12-afd7-44c9-bd6b-0069e16bf2c9" />
</section>
</AspectRatio>
</div>
);
};

<Example2 />
48 changes: 11 additions & 37 deletions packages/react/src/components/AspectRatio/index.tsx
Original file line number Diff line number Diff line change
@@ -1,16 +1,10 @@
import classNames from 'classnames';
import styles from './AspectRatio.module.css';
import { Slot } from '../Slot';
import {
CSSProperties,
Children,
ComponentProps,
PropsWithChildren,
useMemo,
} from 'react';
import { CSSProperties, Children, PropsWithChildren, useMemo } from 'react';

Check failure on line 4 in packages/react/src/components/AspectRatio/index.tsx

View workflow job for this annotation

GitHub Actions / packages-release

'PropsWithChildren' is defined but never used

interface AspectRatioProps extends ComponentProps<'div'> {
asChild?: boolean;
interface AspectRatioProps {
children: JSX.Element;
ratio: number;
}

Expand All @@ -21,53 +15,33 @@ interface AspectRatioProps extends ComponentProps<'div'> {
*
* @template T - 사용할 HTML 요소의 타입을 지정합니다. 기본값은 'div'입니다.
*
* @param {PropsWithChildren<AspectRatioProps<T>>} aspectRatioProps - 컴포넌트에 전달되는 속성들입니다.
* @param {boolean} [aspectRatioProps.asChild=false] - `true`로 설정하면 `Slot` 컴포넌트를 사용해 자식 요소에 aspect-ratio 속성을 적용합니다.
* @param {number} aspectRatioProps.ratio - 자식 요소의 가로 세로 비율을 지정합니다.
* @param {React.ReactNode} aspectRatioProps.children - 렌더링 할 자식요소 입니다.
* @param {Object} [aspectRatioProps.props] - 기타 추가할 나머지 속성들입니다.
* @param {PropsWithChildren<AspectRatioProps<T>>} props - 컴포넌트에 전달되는 속성들입니다.
* @param {number} props.ratio - 자식 요소의 가로 세로 비율을 지정합니다.
* @param {React.ReactNode} props.children - 렌더링 할 자식요소 입니다.
*
* @returns {JSX.Element} 주어진 비율에 맞춰 스타일이 적용된 래퍼 요소를 반환합니다.
*
* @example
* // 기본적으로 div를 생성 후 aspect-ratio 속성을 적용합니다.
* ```tsx
* <AspectRatio ratio={16 / 9}>
* <img src={imgUrl} alt="placeholder" />
* </AspectRatio>
* ```
*
* @example
* // asChild 속성을 true로 설정하면 자식 요소에 aspect-ratio 속성을 적용합니다.
* ```tsx
* <AspectRatio ratio={16 / 9} asChild>
* <section>
* <img src={imgUrl} alt="placeholder" />
* </section>
* </AspectRatio>
* ```
*/
export const AspectRatio = ({
asChild = false,
ratio,
children,
...props
}: PropsWithChildren<AspectRatioProps>): JSX.Element => {
const Wrapper = asChild ? Slot : 'div';
}: AspectRatioProps): JSX.Element => {
const customStyle: CSSProperties = useMemo(
() => ({
paddingTop: `calc(100% * (1 / ${ratio}))`,
...props.style,
aspectRatio: ratio,
}),
[ratio, props.style]
[ratio]
);

return (
<Wrapper
className={classNames(styles['wrapper'], props.className)}
style={customStyle}
{...props}>
<Slot className={classNames(styles['wrapper'])} style={customStyle}>
{Children.only(children)}
</Wrapper>
</Slot>
);
};

0 comments on commit f7d9f93

Please sign in to comment.