-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
b9248d5
commit adcd2ec
Showing
17 changed files
with
284 additions
and
4 deletions.
There are no files selected for viewing
This file contains 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 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,16 @@ | ||
import { Autosizer } from '@react-awesome/components' | ||
|
||
export const AutosizerExample = () => { | ||
return ( | ||
<div className="w-full h-[600px]"> | ||
<Autosizer> | ||
{({ width, height }) => ( | ||
<div> | ||
<div className="flex">Width: {width}px</div> | ||
<div className="flex">Height: {height}px</div> | ||
</div> | ||
)} | ||
</Autosizer> | ||
</div> | ||
) | ||
} |
This file contains 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,2 +1,3 @@ | ||
export * from './PhoneInput/Default' | ||
export * from './PhoneInput/SupportCountry' | ||
export * from './Autosizer' |
This file contains 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 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,60 @@ | ||
--- | ||
title: Autosizer | ||
--- | ||
|
||
# Autosizer | ||
|
||
**Autosizer** keeps tracking of the parent size and reports it to the children. | ||
|
||
## Install | ||
|
||
To start using **Autosizer** component, you can install the `@react-awesome/autosizer` library or you can import it directly from `@react-awesome/components` if you have installed it before. In your project directory, run | ||
the following command to install the dependencies: | ||
|
||
```sh npm2yarn | ||
npm i @react-awesome/autosizer | ||
``` | ||
|
||
import { Container } from '../../components/Container' | ||
import { AutosizerExample } from '../../components/examples' | ||
|
||
```jsx | ||
import { Autosizer } from '@react-awesome/components' | ||
|
||
const Example = () => { | ||
return ( | ||
<div> | ||
<Autosizer> | ||
{({ width, height }) => ( | ||
<div> | ||
<div className="flex">Width: {width}px</div> | ||
<div className="flex">Height: {height}px</div> | ||
</div> | ||
)} | ||
</Autosizer> | ||
</div> | ||
) | ||
} | ||
``` | ||
|
||
<Container showSizeHandler> | ||
<AutosizerExample /> | ||
</Container> | ||
|
||
## API | ||
|
||
**Autosizer** accepts the following props: | ||
|
||
#### `children` (optional) | ||
|
||
Children of `Autosizer` is rendered as render props. | ||
|
||
- Type: `(size : { width: number, height: number }) => React.ReactNode` | ||
- Default: `undefined` | ||
|
||
#### `initialSize` (optional) | ||
|
||
Initial size of the parent component. | ||
|
||
- Type: `{ width: number, height: number }` | ||
- Default: `undefined` |
This file contains 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,8 @@ | ||
module.exports = { | ||
root: true, | ||
extends: ["@react-awesome/eslint-config/library.js"], | ||
parser: "@typescript-eslint/parser", | ||
parserOptions: { | ||
project: true, | ||
}, | ||
}; |
This file contains 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,32 @@ | ||
# @react-awesome/autosizer | ||
|
||
<p align="center"> | ||
<img alt="version" src="https://img.shields.io/npm/v/%40react-awesome%2Fautosizer" /> | ||
<img alt="coverage" src="https://img.shields.io/codecov/c/github/trinhthinh388/react-awesome-components/master?token=VQ8VJ7OECQ&flag=autosizer" /> | ||
<img alt="license" src="https://img.shields.io/github/license/trinhthinh388/react-awesome-components" /> | ||
<img alt="build status" src="https://img.shields.io/github/actions/workflow/status/trinhthinh388/react-awesome-components/release.yml" /> | ||
<img alt="size" src="https://img.shields.io/bundlejs/size/%40react-awesome/autosizer" /> | ||
</p> | ||
|
||
**autosizer** tracks the previous value of a variable. | ||
|
||
Please refer to the [documentation](https://react-awesome-components.vercel.app/docs/autosizer) for more information. | ||
|
||
## Installation | ||
|
||
```sh | ||
yarn add @react-awesome/autosizer | ||
# or | ||
npm i @react-awesome/autosizer | ||
``` | ||
|
||
## Contribution | ||
|
||
Yes please! See the | ||
[contributing guidelines](https://github.com/trinhthinh388/react-awesome-components/blob/master/CONTRIBUTING.md) | ||
for details. | ||
|
||
## Licence | ||
|
||
This project is licensed under the terms of the | ||
[MIT license](https://github.com/trinhthinh388/react-awesome-components/blob/master/LICENSE). |
This file contains 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,45 @@ | ||
{ | ||
"name": "@react-awesome/autosizer", | ||
"version": "0.0.0", | ||
"main": "./dist/index.cjs", | ||
"module": "./dist/index.js", | ||
"types": "./dist/index.d.ts", | ||
"sideEffects": false, | ||
"license": "MIT", | ||
"description": "Autosizer keeps tracking the parent size and report back to its children.", | ||
"keywords": [ | ||
"react", | ||
"autosizer", | ||
"resize" | ||
], | ||
"repository": { | ||
"url": "https://github.com/trinhthinh388/react-awesome-components" | ||
}, | ||
"files": [ | ||
"dist/**" | ||
], | ||
"scripts": { | ||
"build": "vite --config ../../vite.config.ts build", | ||
"dev": "vite --watch --config ../../vite.config.ts build", | ||
"lint": "eslint \"src/**/*.ts*\"", | ||
"clean": "rimraf .turbo && rimraf node_modules && rimraf dist", | ||
"test": "vitest --config ../../vite.config.ts --coverage --run", | ||
"test:ui": "vitest --config ../../vite.config.ts --coverage --ui", | ||
"typecheck": "tsc --noEmit" | ||
}, | ||
"devDependencies": { | ||
"@react-awesome/eslint-config": "*", | ||
"@react-awesome/tsconfig": "*", | ||
"@types/react": "^18.2.46", | ||
"@types/react-dom": "^18.2.18", | ||
"eslint": "^8.56.0", | ||
"react": "^18.2.0", | ||
"typescript": "^5.3.3" | ||
}, | ||
"publishConfig": { | ||
"access": "public" | ||
}, | ||
"dependencies": { | ||
"react-merge-refs": "^2.1.1" | ||
} | ||
} |
This file contains 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,34 @@ | ||
import { Mock } from 'vitest' | ||
import { Autosizer } from './autosizer' | ||
import { render } from '@testing-library/react' | ||
|
||
describe('useBreakpoint', () => { | ||
let MockObserverInstance: ResizeObserver | ||
let GlobalResizeObserver: Mock | ||
|
||
beforeEach(() => { | ||
MockObserverInstance = { | ||
observe: vitest.fn(), | ||
unobserve: vitest.fn(), | ||
disconnect: vitest.fn(), | ||
} | ||
GlobalResizeObserver = vitest | ||
.fn() | ||
.mockImplementation(() => MockObserverInstance) | ||
global.ResizeObserver = GlobalResizeObserver | ||
}) | ||
|
||
afterEach(() => { | ||
vitest.resetAllMocks() | ||
}) | ||
|
||
it('Should observe resize event', async () => { | ||
const { unmount } = render(<Autosizer />) | ||
|
||
expect(MockObserverInstance.observe).toHaveBeenCalled() | ||
|
||
unmount() | ||
|
||
expect(MockObserverInstance.disconnect).toHaveBeenCalled() | ||
}) | ||
}) |
This file contains 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,58 @@ | ||
import React, { | ||
forwardRef, | ||
useCallback, | ||
useEffect, | ||
useRef, | ||
useState, | ||
} from 'react' | ||
import { mergeRefs } from 'react-merge-refs' | ||
|
||
export type AutosizerProps = { | ||
children?: (_: { width: number; height: number }) => React.ReactNode | ||
initialSize?: { | ||
width?: number | ||
height?: number | ||
} | ||
} & Omit<React.ComponentPropsWithRef<'div'>, 'children'> | ||
|
||
export const Autosizer = forwardRef<HTMLDivElement, AutosizerProps>( | ||
({ children = () => void 0, initialSize = {}, ...props }, ref) => { | ||
const elRef = useRef<HTMLDivElement>(null) | ||
const [size, setSize] = useState({ | ||
width: initialSize.width || 0, | ||
height: initialSize.height || 0, | ||
}) | ||
|
||
const watch = useCallback( | ||
/* istanbul ignore next */ () => { | ||
if (!elRef.current) return | ||
|
||
const observer = new ResizeObserver( | ||
/* istanbul ignore next */ (entries) => { | ||
if (!entries.length) return | ||
const [entry] = entries | ||
setSize({ | ||
width: entry.contentRect.width, | ||
height: entry.contentRect.height, | ||
}) | ||
}, | ||
) | ||
|
||
observer.observe(elRef.current) | ||
|
||
return () => { | ||
observer.disconnect() | ||
} | ||
}, | ||
[], | ||
) | ||
|
||
useEffect(() => watch(), [watch]) | ||
|
||
return ( | ||
<div ref={mergeRefs([elRef, ref])} {...props}> | ||
{children(size)} | ||
</div> | ||
) | ||
}, | ||
) |
This file contains 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 @@ | ||
export * from './autosizer' |
This file contains 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,8 @@ | ||
{ | ||
"extends": "@react-awesome/tsconfig/react-library.json", | ||
"compilerOptions": { | ||
"types": ["vitest/globals"], | ||
}, | ||
"include": ["."], | ||
"exclude": ["dist", "build", "node_modules"], | ||
} |
This file contains 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,5 +1,11 @@ | ||
# @react-awesome/components | ||
|
||
## 1.0.21 | ||
|
||
### Patch Changes | ||
|
||
- Added autosizer | ||
|
||
## 1.0.20 | ||
|
||
### Patch Changes | ||
|
This file contains 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 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,4 +1,5 @@ | ||
export * from '@react-awesome/phone-input' | ||
export * from '@react-awesome/autosizer' | ||
|
||
/** | ||
* Hooks | ||
|
This file contains 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 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