@weser/hooks 0.0.14 → 0.0.16

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/README.md CHANGED
@@ -1,22 +1,3 @@
1
1
  # @weser/hooks
2
2
 
3
- <img alt="npm version" src="https://badge.fury.io/js/@weser%2Fhooks.svg"> <img alt="npm downloads" src="https://img.shields.io/npm/dm/@weser/hooks.svg"> <a href="https://bundlephobia.com/result?p=@weser/hooks@latest"><img alt="Bundlephobia" src="https://img.shields.io/bundlephobia/minzip/@weser/hooks.svg"></a>
4
-
5
- ## Installation
6
-
7
- ```sh
8
- # npm
9
- npm i --save @weser/hooks
10
- # yarn
11
- yarn add @weser/hooks
12
- # pnpm
13
- pnpm add @weser/hooks
14
- ```
15
-
16
- ## Documentation
17
-
18
- ## License
19
-
20
- @weser/hooks is licensed under the [MIT License](http://opensource.org/licenses/MIT).<br>
21
- Documentation is licensed under [Creative Common License](http://creativecommons.org/licenses/by/4.0/).<br>
22
- Created with ♥ by [@robinweser](http://weser.io).
3
+ [Documentation](https://packages.weser.io/hooks)
package/dist/index.d.ts CHANGED
@@ -5,6 +5,7 @@ export { default as useDisclosure } from './useDisclosure.js';
5
5
  export { default as useFirstRender } from './useFirstRender.js';
6
6
  export { default as useFocusTrap } from './useFocusTrap.js';
7
7
  export { default as useKeyDown } from './useKeyDown.js';
8
+ export { default as useList } from './useList.js';
8
9
  export { default as useRerender } from './useRerender.js';
9
10
  export { default as useRouteChange } from './useRouteChange.js';
10
11
  export { default as useScrollBlocking } from './useScrollBlocking.js';
package/dist/index.js CHANGED
@@ -5,6 +5,7 @@ export { default as useDisclosure } from './useDisclosure.js';
5
5
  export { default as useFirstRender } from './useFirstRender.js';
6
6
  export { default as useFocusTrap } from './useFocusTrap.js';
7
7
  export { default as useKeyDown } from './useKeyDown.js';
8
+ export { default as useList } from './useList.js';
8
9
  export { default as useRerender } from './useRerender.js';
9
10
  export { default as useRouteChange } from './useRouteChange.js';
10
11
  export { default as useScrollBlocking } from './useScrollBlocking.js';
@@ -1,15 +1,15 @@
1
1
  export default function useDisclosure(defaultExpanded?: boolean): {
2
2
  toggleProps: {
3
- id: string;
4
- onClick: () => void;
5
- type: "button";
6
- 'aria-expanded': boolean;
7
- 'aria-controls': string;
3
+ readonly id: string;
4
+ readonly onClick: () => void;
5
+ readonly type: "button";
6
+ readonly 'aria-expanded': boolean;
7
+ readonly 'aria-controls': string;
8
8
  };
9
9
  contentProps: {
10
- id: string;
11
- 'aria-hidden': boolean;
12
- 'aria-labelledby': string;
10
+ readonly id: string;
11
+ readonly 'aria-hidden': boolean;
12
+ readonly 'aria-labelledby': string;
13
13
  };
14
14
  isExpanded: boolean;
15
15
  toggle: () => void;
@@ -0,0 +1,8 @@
1
+ export default function useList<T>(defaultList?: Array<T>): readonly [T[], {
2
+ set: (list: Array<T>) => void;
3
+ add: (item: T) => void;
4
+ removeAt: (index: number) => void;
5
+ updateAt: (index: number, item: T) => void;
6
+ insertAt: (index: number, item: T) => void;
7
+ clear: () => void;
8
+ }];
@@ -0,0 +1,31 @@
1
+ import { useState } from 'react';
2
+ export default function useList(defaultList = []) {
3
+ const [list, setList] = useState(defaultList);
4
+ function set(list) {
5
+ setList(list);
6
+ }
7
+ function add(item) {
8
+ setList([...list, item]);
9
+ }
10
+ function removeAt(index) {
11
+ setList(list.filter((_, i) => i !== index));
12
+ }
13
+ function updateAt(index, item) {
14
+ setList(list.map((entry, i) => (i === index ? item : entry)));
15
+ }
16
+ function insertAt(index, item) {
17
+ setList([...list.slice(0, index), item, ...list.slice(index)]);
18
+ }
19
+ function clear() {
20
+ setList([]);
21
+ }
22
+ const actions = {
23
+ set,
24
+ add,
25
+ removeAt,
26
+ updateAt,
27
+ insertAt,
28
+ clear,
29
+ };
30
+ return [list, actions];
31
+ }
@@ -1 +1 @@
1
- export default function useRouteChange(onRouteChange: (path: string) => void, pathname?: string): void;
1
+ export default function useRouteChange(onRouteChange: (path: string) => void, pathname?: string, dependencies?: any[]): void;
@@ -1,10 +1,10 @@
1
1
  import { useEffect } from 'react';
2
- export default function useRouteChange(onRouteChange, pathname) {
2
+ export default function useRouteChange(onRouteChange, pathname, dependencies = []) {
3
3
  useEffect(() => {
4
4
  if (pathname) {
5
5
  onRouteChange(pathname);
6
6
  }
7
- }, [pathname]);
7
+ }, [pathname, ...dependencies]);
8
8
  // track clicks on links with the current path
9
9
  useEffect(() => {
10
10
  const onClick = (e) => {
@@ -17,7 +17,7 @@ export default function useRouteChange(onRouteChange, pathname) {
17
17
  };
18
18
  window.addEventListener('click', onClick);
19
19
  return () => window.removeEventListener('click', onClick);
20
- }, [pathname]);
20
+ }, [pathname, ...dependencies]);
21
21
  }
22
22
  function getContainingAnchor(node) {
23
23
  if (!node || !(node instanceof Node))
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@weser/hooks",
3
- "version": "0.0.14",
3
+ "version": "0.0.16",
4
4
  "description": "React hooks for building apps",
5
5
  "author": "Robin Weser <robin@weser.io>",
6
6
  "license": "MIT",
@@ -52,5 +52,5 @@
52
52
  "rimraf": "^3.0.2",
53
53
  "typescript": "^5.4.5"
54
54
  },
55
- "gitHead": "2844a44c54a0ada9f675baf40226f87fe894c29f"
55
+ "gitHead": "7b2bc4fa318330eb2031b0d1835554a8a9f0f6bd"
56
56
  }