Skip to content

Commit

Permalink
feat(usetoggle): added useToggle
Browse files Browse the repository at this point in the history
  • Loading branch information
trinhthinh388 committed Feb 1, 2024
1 parent f5ad0d0 commit e2e0dc6
Show file tree
Hide file tree
Showing 13 changed files with 152 additions and 3 deletions.
2 changes: 1 addition & 1 deletion apps/docs/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
"clean": "rimraf .turbo && rimraf node_modules && rimraf .next"
},
"dependencies": {
"@react-awesome/components": "1.0.7",
"@react-awesome/components": "1.0.8",
"classnames": "^2.5.1",
"lucide-react": "^0.315.0",
"next": "^14.0.4",
Expand Down
7 changes: 7 additions & 0 deletions packages/components/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,12 @@
# @react-awesome/components

## 1.0.8

### Patch Changes

- Updated dependencies
- @react-awesome/use-toggle@0.0.1

## 1.0.7

### Patch Changes
Expand Down
5 changes: 3 additions & 2 deletions packages/components/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@react-awesome/components",
"version": "1.0.7",
"version": "1.0.8",
"main": "./dist/index.cjs",
"module": "./dist/index.js",
"types": "./dist/index.d.ts",
Expand Down Expand Up @@ -43,6 +43,7 @@
"@react-awesome/use-click-outside": "0.0.3",
"@react-awesome/use-preserve-input-caret-position": "0.0.3",
"@react-awesome/use-selection-range": "0.0.3",
"@react-awesome/use-previous": "0.0.3"
"@react-awesome/use-previous": "0.0.3",
"@react-awesome/use-toggle": "0.0.1"
}
}
1 change: 1 addition & 0 deletions packages/components/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,3 +7,4 @@ export * from '@react-awesome/use-click-outside'
export * from '@react-awesome/use-preserve-input-caret-position'
export * from '@react-awesome/use-previous'
export * from '@react-awesome/use-selection-range'
export * from '@react-awesome/use-toggle'
8 changes: 8 additions & 0 deletions packages/use-toggle/.eslintrc.js
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,
},
};
7 changes: 7 additions & 0 deletions packages/use-toggle/CHANGELOG.md
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
32 changes: 32 additions & 0 deletions packages/use-toggle/README.md
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).
43 changes: 43 additions & 0 deletions packages/use-toggle/package.json
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"
}
}
1 change: 1 addition & 0 deletions packages/use-toggle/src/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export * from './useToggle/useToggle'

Check warning on line 1 in packages/use-toggle/src/index.ts

View workflow job for this annotation

GitHub Actions / ⬣ ESLint

Definition for rule '@typescript-eslint/no-unused-vars' was not found
24 changes: 24 additions & 0 deletions packages/use-toggle/src/useToggle/useToggle.spec.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
import { act, renderHook } from '@testing-library/react'

Check warning on line 1 in packages/use-toggle/src/useToggle/useToggle.spec.tsx

View workflow job for this annotation

GitHub Actions / ⬣ ESLint

Definition for rule '@typescript-eslint/no-unused-vars' was not found
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)
})
})
16 changes: 16 additions & 0 deletions packages/use-toggle/src/useToggle/useToggle.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import React, { useCallback, useState } from 'react'

Check warning on line 1 in packages/use-toggle/src/useToggle/useToggle.tsx

View workflow job for this annotation

GitHub Actions / ⬣ ESLint

Definition for rule '@typescript-eslint/no-unused-vars' was not found

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,
}
}
8 changes: 8 additions & 0 deletions packages/use-toggle/tsconfig.json
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"],
}
1 change: 1 addition & 0 deletions turbo.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
},
"@react-awesome/components#build": {
"dependsOn": [
"@react-awesome/use-toggle#build",
"@react-awesome/use-previous#build",
"@react-awesome/use-selection-range#build",
"@react-awesome/use-preserve-input-caret-position#build",
Expand Down

0 comments on commit e2e0dc6

Please sign in to comment.