-
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.
Merge pull request #67 from trinhthinh388/master
Publish packages
- Loading branch information
Showing
17 changed files
with
256 additions
and
6 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,90 @@ | ||
--- | ||
title: useToggle | ||
--- | ||
|
||
# useToggle | ||
|
||
**useToggle** toggles a boolean value. | ||
|
||
## Install | ||
|
||
To start using **useToggle**, you can install the `@react-awesome/use-toggle` 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/use-toggle | ||
``` | ||
|
||
## Usage | ||
|
||
import { useState } from 'react' | ||
import { Container } from '../../components/Container' | ||
import { useToggle } from '@react-awesome/components' | ||
|
||
export const Example = () => { | ||
const { toggle, isOn } = useToggle(true) | ||
return ( | ||
|
||
<> | ||
<div className="inline-flex items-center"> | ||
<div className="relative inline-block w-8 h-4 rounded-full cursor-pointer"> | ||
<input | ||
checked={isOn} | ||
id="switch" | ||
type="checkbox" | ||
onClick={toggle} | ||
className="absolute bg-gray-100 w-8 h-4 transition-colors duration-300 rounded-full appearance-none cursor-pointer peer bg-blue-gray-100 checked:bg-amber-500 peer-checked:border-amber-500 peer-checked:before:bg-amber-500" | ||
/> | ||
<label | ||
htmlFor="switch" | ||
class="before:content[''] absolute top-2/4 -left-1 h-5 w-5 -translate-y-2/4 cursor-pointer rounded-full border border-blue-gray-100 bg-white shadow-md transition-all duration-300 before:absolute before:top-2/4 before:left-2/4 before:block before:h-10 before:w-10 before:-translate-y-2/4 before:-translate-x-2/4 before:rounded-full before:bg-blue-gray-500 before:opacity-0 before:transition-opacity hover:before:opacity-10 peer-checked:translate-x-full peer-checked:border-amber-500 peer-checked:before:bg-amber-500" | ||
> | ||
<div | ||
className="inline-block p-5 rounded-full top-2/4 left-2/4 -translate-x-2/4 -translate-y-2/4" | ||
data-ripple-dark="true" | ||
></div> | ||
</label> | ||
</div> | ||
<label | ||
htmlFor="switch" | ||
class="mt-px mb-0 ml-3 font-light cursor-pointer select-none" | ||
> | ||
Click to toggle | ||
</label> | ||
</div> | ||
</> | ||
); }; | ||
|
||
<Container> | ||
<Example /> | ||
</Container> | ||
|
||
```jsx | ||
import { useToggle } from '@react-awesome/use-toggle' | ||
|
||
const Example = () => { | ||
const { toggle, isOn } = useToggle(state) | ||
|
||
return <input checked={isOn} type="checkbox" onClick={toggle} /> | ||
} | ||
``` | ||
|
||
## Parameters | ||
|
||
The `useToggle` takes the following parameters: | ||
|
||
#### `value` | ||
|
||
- Type: `boolean` | ||
|
||
## API | ||
|
||
The `useToggle` returns the following props: | ||
|
||
#### `isOn` | ||
|
||
- Type: `boolean` | ||
|
||
#### `toggle` | ||
|
||
- Type: `(value?: boolean) => void` |
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
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