Skip to content

Commit 11c3a33

Browse files
author
Griko Nibras
committed
feat(docs): update jsdoc and readme
1 parent 8d50de8 commit 11c3a33

File tree

3 files changed

+45
-7
lines changed

3 files changed

+45
-7
lines changed

README.md

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,8 @@ Toggle custom hook and component wrapper for React 🔦
88

99
**Table of contents**
1010

11-
- [Install](#install)
12-
- [Usage](#usage)
11+
- [Installing](#installing)
12+
- [Example usage](#example-usage)
1313
- [Toggle hook](#toggle-hook)
1414
- [Toggle wrapper](#toggle-wrapper)
1515
- [Suggestions and/or questions](#suggestions-andor-questions)
@@ -18,7 +18,7 @@ Toggle custom hook and component wrapper for React 🔦
1818

1919
---
2020

21-
## Install
21+
## Installing
2222

2323
```sh
2424
# using npm
@@ -28,7 +28,7 @@ npm install @kodingdotninja/use-toggle
2828
yarn add @kodingdotninja/use-toggle
2929
```
3030

31-
## Usage
31+
## Example usage
3232

3333
### Toggle hook
3434

@@ -52,9 +52,9 @@ function App() {
5252
### Toggle wrapper
5353

5454
```jsx
55-
import { ToggleWrap } from "..";
55+
import { ToggleWrap } from "kodingdotninja/use-toggle";
5656

57-
export default function App() {
57+
function App() {
5858
return (
5959
<ToggleWrap>
6060
{({ state, disable, enable, set, toggle }) => (

src/toggle-wrap.tsx

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,9 @@ import { useToggle, UseToggleReturnType } from "./use-toggle";
55
export const ENABLE_ON_MOUNT_KEY = "enableOnMount";
66
export const DISABLE_ON_MOUNT_KEY = "disableOnMount";
77

8+
/**
9+
* Toggle wrap props for determining if toggle should be enabled or disabled on mount.
10+
*/
811
export type OnMountProps = { [ENABLE_ON_MOUNT_KEY]?: boolean } | { [DISABLE_ON_MOUNT_KEY]?: boolean };
912

1013
export type ToggleWrapProps = OnMountProps & {
@@ -14,6 +17,26 @@ export type ToggleWrapProps = OnMountProps & {
1417

1518
export type ToggleWrapType = (props: ToggleWrapProps) => JSX.Element;
1619

20+
/**
21+
* Toggle wrap component which accepts initial state and enable/disable on mount prop.
22+
* Returns a render function to consume the toggle hook.
23+
*
24+
* ---
25+
*
26+
* @param props Toggle wrap props
27+
*
28+
* @returns Render function to consume the toggle hook
29+
*
30+
* @example
31+
*
32+
* ```jsx
33+
* <ToggleWrap>
34+
* {({ state, disable, enable, set, toggle }) => (
35+
* <div>...</div>
36+
* )}
37+
* </ToggleWrap>
38+
* ```
39+
*/
1740
export const ToggleWrap: ToggleWrapType = ({ initialState, children, ...props }) => {
1841
const toggle = useToggle(initialState);
1942

src/use-toggle.ts

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,23 @@ export type UseToggleReturnType = {
1010

1111
export type UseToggleType = (initialState?: boolean) => UseToggleReturnType;
1212

13+
/**
14+
* Toggle hook which accepts initial state and returns the toggle state and controls.
15+
*
16+
* ---
17+
*
18+
* @param initalState Initial value for toggle hook
19+
*
20+
* @returns Toggle value and controls
21+
*
22+
* @example
23+
*
24+
* ```jsx
25+
* const { state, disable, enable, set, toggle } = useToggle(initalState);
26+
* ```
27+
*/
1328
export const useToggle: UseToggleType = (initalState = false) => {
14-
const [state, setState] = React.useState(initalState);
29+
const [state, setState] = React.useState(() => Boolean(initalState));
1530

1631
const disable = React.useCallback(() => setState(true), []);
1732
const enable = React.useCallback(() => setState(true), []);

0 commit comments

Comments
 (0)