-
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
f5ad0d0
commit e2e0dc6
Showing
13 changed files
with
152 additions
and
3 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
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
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,7 @@ | ||
# @react-awesome/use-previous | ||
|
||
## 0.0.1 | ||
|
||
### Patch Changes | ||
|
||
- Added useToggle |
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/use-toggle | ||
|
||
<p align="center"> | ||
<img alt="version" src="https://img.shields.io/npm/v/%40react-awesome%2Fuse-toggle" /> | ||
<img alt="coverage" src="https://img.shields.io/codecov/c/github/trinhthinh388/react-awesome-components/master?token=VQ8VJ7OECQ&flag=useToggle" /> | ||
<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/use-toggle" /> | ||
</p> | ||
|
||
**useToggle** tracks the previous value of a variable. | ||
|
||
Please refer to the [documentation](https://react-awesome-components.vercel.app/docs/use-toggle) for more information. | ||
|
||
## Installation | ||
|
||
```sh | ||
yarn add @react-awesome/use-toggle | ||
# or | ||
npm i @react-awesome/use-toggle | ||
``` | ||
|
||
## 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,43 @@ | ||
{ | ||
"name": "@react-awesome/use-toggle", | ||
"version": "0.0.1", | ||
"main": "./dist/index.cjs", | ||
"module": "./dist/index.js", | ||
"types": "./dist/index.d.ts", | ||
"sideEffects": false, | ||
"license": "MIT", | ||
"description": "useToggle toggles the boolean value with ease.", | ||
"keywords": [ | ||
"react", | ||
"hooks", | ||
"toggle", | ||
"useToggle" | ||
], | ||
"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" | ||
} | ||
} |
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 './useToggle/useToggle' | ||
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,24 @@ | ||
import { act, renderHook } from '@testing-library/react' | ||
import { useToggle } from './useToggle' | ||
|
||
describe('useToggle', () => { | ||
it('Should toggle the value', () => { | ||
const { result, rerender } = renderHook(() => useToggle(true)) | ||
|
||
expect(result.current.isOn).toBe(true) | ||
|
||
act(() => { | ||
result.current.toggle() | ||
rerender() | ||
}) | ||
|
||
expect(result.current.isOn).toBe(false) | ||
|
||
act(() => { | ||
result.current.toggle(true) | ||
rerender() | ||
}) | ||
|
||
expect(result.current.isOn).toBe(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,16 @@ | ||
import React, { useCallback, useState } from 'react' | ||
|
||
export const useToggle = (initialValue: boolean) => { | ||
const [value, setValue] = useState(initialValue) | ||
|
||
const toggle = useCallback( | ||
(_value?: boolean) => | ||
setValue((prev) => (typeof _value === 'boolean' ? _value : !prev)), | ||
[], | ||
) | ||
|
||
return { | ||
toggle, | ||
isOn: value, | ||
} | ||
} |
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