├── .babelrc ├── .gitattributes ├── .github └── workflows │ ├── deploy-docs.yml │ └── test.yml ├── .gitignore ├── CHANGELOG.md ├── LICENSE ├── README.md ├── package.json ├── src ├── globals.d.ts ├── useAnimationFrame.ts ├── useBreakpoint.ts ├── useCallbackRef.ts ├── useCommittedRef.ts ├── useCustomEffect.ts ├── useDebouncedCallback.ts ├── useDebouncedState.ts ├── useDebouncedValue.ts ├── useEventCallback.ts ├── useEventListener.ts ├── useFocusManager.ts ├── useForceUpdate.ts ├── useGlobalListener.ts ├── useImage.ts ├── useImmediateUpdateEffect.ts ├── useIntersectionObserver.ts ├── useInterval.ts ├── useIsInitialRenderRef.ts ├── useIsomorphicEffect.ts ├── useMap.ts ├── useMediaQuery.ts ├── useMergeState.ts ├── useMergeStateFromProps.ts ├── useMergedRefs.ts ├── useMountEffect.ts ├── useMounted.ts ├── useMutationObserver.ts ├── usePrevious.ts ├── useRafInterval.ts ├── useRefWithInitialValueFactory.ts ├── useResizeObserver.ts ├── useSafeState.ts ├── useSet.ts ├── useStableMemo.ts ├── useStateAsync.ts ├── useThrottledEventHandler.ts ├── useTimeout.ts ├── useToggleState.ts ├── useUpdateEffect.ts ├── useUpdateImmediateEffect.ts ├── useUpdateLayoutEffect.ts ├── useUpdatedRef.ts └── useWillUnmount.ts ├── test ├── helpers.tsx ├── setup.js ├── tsconfig.json ├── useAnimationFrame.test.tsx ├── useBreakpoint.ssr.test.tsx ├── useBreakpoint.test.tsx ├── useCallbackRef.test.tsx ├── useCommittedRef.test.tsx ├── useCustomEffect.test.tsx ├── useDebouncedCallback.test.tsx ├── useDebouncedState.test.tsx ├── useDebouncedValue.test.tsx ├── useForceUpdate.test.tsx ├── useImmediateUpdateEffect.test.tsx ├── useIntersectionObserver.test.tsx ├── useInterval.test.tsx ├── useIsInitialRenderRef.test.tsx ├── useIsomorphicEffect.ssr.test.tsx ├── useIsomorphicEffect.test.tsx ├── useMap.test.tsx ├── useMediaQuery.ssr.test.tsx ├── useMediaQuery.test.tsx ├── useMergeStateFromProps.test.tsx ├── useMergedRefs.test.tsx ├── useMountEffect.test.tsx ├── useMounted.test.tsx ├── useMutationObserver.test.tsx ├── usePrevious.test.tsx ├── useRefWithInitialValueFactory.test.tsx ├── useSafeState.test.tsx ├── useSet.test.tsx ├── useStateAsync.test.tsx ├── useThrottledEventHandler.test.tsx ├── useTimeout.test.tsx ├── useToggleState.test.tsx ├── useUpdateEffect.test.tsx ├── useUpdateLayoutEffect.test.tsx └── useWillUnmount.test.tsx ├── tsconfig.json ├── vitest.config.mts ├── www ├── .babelrc ├── .gitignore ├── gatsby-config.js ├── gatsby-node.js ├── package.json ├── src │ ├── @docpocalypse │ │ └── gatsby-theme │ │ │ └── components │ │ │ └── .gitkeep │ ├── examples │ │ └── .gitkeep │ └── pages │ │ └── index.mdx ├── tailwind.config.js ├── templates │ └── component.js └── yarn.lock └── yarn.lock /.babelrc: -------------------------------------------------------------------------------- 1 | { 2 | "presets": ["@4c", "@babel/typescript"], 3 | "env": { 4 | "esm": { 5 | "presets": [ 6 | [ 7 | "@4c", 8 | { 9 | "modules": false 10 | } 11 | ] 12 | 13 | ] 14 | }, 15 | "test": { 16 | "presets": [["@4c", { "development": true }], "@babel/typescript"] 17 | } 18 | } 19 | } 20 | -------------------------------------------------------------------------------- /.gitattributes: -------------------------------------------------------------------------------- 1 | # Auto detect text files and perform LF normalization 2 | * text=auto 3 | -------------------------------------------------------------------------------- /.github/workflows/deploy-docs.yml: -------------------------------------------------------------------------------- 1 | name: Deploy Documentation 2 | on: 3 | push: 4 | branches: 5 | - master 6 | jobs: 7 | build_docs: 8 | name: Build and Deploy Documentation 9 | runs-on: ubuntu-latest 10 | steps: 11 | - name: Checking out Repository 12 | uses: actions/checkout@v1 13 | - name: Setup Node.js 14 | uses: actions/setup-node@v1 15 | with: 16 | node-version: 12.x 17 | - name: Install Dependencies 18 | run: yarn bootstrap 19 | - name: Build and Deploy Production Files 20 | env: 21 | GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} 22 | run: yarn deploy-docs 23 | -------------------------------------------------------------------------------- /.github/workflows/test.yml: -------------------------------------------------------------------------------- 1 | name: Run Tests 2 | on: 3 | push: 4 | branches: 5 | - master 6 | pull_request: 7 | branches: 8 | - master 9 | jobs: 10 | test: 11 | name: Run Testing Suite 12 | runs-on: ubuntu-latest 13 | steps: 14 | - name: Checking out Repository 15 | uses: actions/checkout@v2 16 | - name: Setup Node.js 17 | uses: actions/setup-node@v1 18 | with: 19 | node-version: 18.x 20 | - name: Install Dependencies 21 | run: yarn bootstrap 22 | - name: Run Tests 23 | run: yarn test 24 | - name: Build Project 25 | run: yarn build 26 | - name: Upload Test Coverage to CodeCov 27 | uses: codecov/codecov-action@v1.0.2 28 | with: 29 | token: ${{ secrets.CODECOV_TOKEN }} 30 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | lib/ 2 | cjs/ 3 | 4 | # Logs 5 | logs 6 | *.log 7 | npm-debug.log* 8 | yarn-debug.log* 9 | yarn-error.log* 10 | 11 | # Runtime data 12 | pids 13 | *.pid 14 | *.seed 15 | *.pid.lock 16 | 17 | # Directory for instrumented libs generated by jscoverage/JSCover 18 | lib-cov 19 | 20 | # Coverage directory used by tools like istanbul 21 | coverage 22 | 23 | # nyc test coverage 24 | .nyc_output 25 | 26 | # Grunt intermediate storage (http://gruntjs.com/creating-plugins#storing-task-files) 27 | .grunt 28 | 29 | # Bower dependency directory (https://bower.io/) 30 | bower_components 31 | 32 | # node-waf configuration 33 | .lock-wscript 34 | 35 | # Compiled binary addons (http://nodejs.org/api/addons.html) 36 | build/Release 37 | 38 | # Dependency directories 39 | node_modules/ 40 | jspm_packages/ 41 | 42 | # Typescript v1 declaration files 43 | typings/ 44 | 45 | # Optional npm cache directory 46 | .npm 47 | 48 | # Optional eslint cache 49 | .eslintcache 50 | 51 | # Optional REPL history 52 | .node_repl_history 53 | 54 | # Output of 'npm pack' 55 | *.tgz 56 | 57 | # Yarn Integrity file 58 | .yarn-integrity 59 | 60 | # dotenv environment variables file 61 | .env 62 | 63 | -------------------------------------------------------------------------------- /CHANGELOG.md: -------------------------------------------------------------------------------- 1 | ## [0.6.2](https://github.com/react-restart/hooks/compare/v0.6.1...v0.6.2) (2025-01-07) 2 | 3 | 4 | ### Bug Fixes 5 | 6 | * exports definition again ([364e4a6](https://github.com/react-restart/hooks/commit/364e4a68ccad6e30d59c1ec6fd305019a521f912)) 7 | 8 | 9 | 10 | 11 | 12 | ## [0.6.1](https://github.com/react-restart/hooks/compare/v0.6.0...v0.6.1) (2025-01-07) 13 | 14 | 15 | ### Bug Fixes 16 | 17 | * exports definition ([523780d](https://github.com/react-restart/hooks/commit/523780d5bd7ce9647753c284d84f4f59e624af70)) 18 | 19 | 20 | 21 | 22 | 23 | # [0.6.0](https://github.com/react-restart/hooks/compare/v0.5.1...v0.6.0) (2025-01-07) 24 | 25 | 26 | * build!: Update build, export real ESM (#100) ([9e272d3](https://github.com/react-restart/hooks/commit/9e272d3c5e89c7cd6547c6c66afac6b800b6c7ff)), closes [#100](https://github.com/react-restart/hooks/issues/100) 27 | 28 | 29 | ### BREAKING CHANGES 30 | 31 | * exports native esm modules and removes the index file in favor of sub imports 32 | 33 | 34 | 35 | 36 | 37 | ## [0.5.1](https://github.com/jquense/react-common-hooks/compare/v0.5.0...v0.5.1) (2025-01-02) 38 | 39 | 40 | ### Bug Fixes 41 | 42 | * **useTimeout:** isPending never updates ([51e14ec](https://github.com/jquense/react-common-hooks/commit/51e14ec0d40c70b4cf09ce02b58beaa740b6b0cf)) 43 | 44 | 45 | 46 | 47 | 48 | # [0.5.0](https://github.com/jquense/react-common-hooks/compare/v0.4.16...v0.5.0) (2024-11-25) 49 | 50 | 51 | * A number of StrictMode fixes and updates (#99) ([1511129](https://github.com/jquense/react-common-hooks/commit/1511129898f9b95659bab7f964fe33528d04b938)), closes [#99](https://github.com/jquense/react-common-hooks/issues/99) 52 | 53 | 54 | ### BREAKING CHANGES 55 | 56 | * no longer supports `cancelPrevious` this is always true 57 | 58 | * fix:(useDebouncedCallback): Clean up timeout logic in strict mode 59 | 60 | * chore: deprecate useWillUnmount 61 | 62 | This hook is not possible in StrictMode, and can cause bugs 63 | 64 | * fix(useForceUpdate): ensure that chained calls produce an update 65 | 66 | * Update useCustomEffect.ts 67 | 68 | * address feedback 69 | 70 | 71 | 72 | 73 | 74 | ## [0.4.16](https://github.com/jquense/react-common-hooks/compare/v0.4.15...v0.4.16) (2024-02-10) 75 | 76 | 77 | ### Bug Fixes 78 | 79 | * **useDebouncedCallback:** fix behavior in strict mode ([#98](https://github.com/jquense/react-common-hooks/issues/98)) ([9a01792](https://github.com/jquense/react-common-hooks/commit/9a01792e025d35bc6af92582af5f8dd928227f2d)) 80 | 81 | 82 | 83 | 84 | 85 | ## [0.4.15](https://github.com/jquense/react-common-hooks/compare/v0.4.14...v0.4.15) (2023-12-07) 86 | 87 | 88 | ### Bug Fixes 89 | 90 | * **useDebouncedState:** Add factory function overload to type, clarify setter purity ([9aa18fb](https://github.com/jquense/react-common-hooks/commit/9aa18fb48d06ab2bd4d64b1b862a80d5d4d5cdd5)) 91 | 92 | 93 | 94 | 95 | 96 | ## [0.4.14](https://github.com/jquense/react-common-hooks/compare/v0.4.13...v0.4.14) (2023-12-07) 97 | 98 | 99 | ### Bug Fixes 100 | 101 | * **useDebouncedCallback:** wrap handler in useEventCallback so it's not stale ([63f897a](https://github.com/jquense/react-common-hooks/commit/63f897aa6972f66fd5d076c5edb4da6cda5031c0)) 102 | 103 | 104 | 105 | 106 | 107 | ## [0.4.13](https://github.com/jquense/react-common-hooks/compare/v0.4.12...v0.4.13) (2023-12-06) 108 | 109 | 110 | ### Features 111 | 112 | * **useDebouncedCallback:** return value from debounced function ([d1ead47](https://github.com/jquense/react-common-hooks/commit/d1ead4728fb14949a00d937f72a0657e0552d5b9)) 113 | 114 | 115 | 116 | 117 | 118 | ## [0.4.12](https://github.com/jquense/react-common-hooks/compare/v0.4.11...v0.4.12) (2023-12-06) 119 | 120 | 121 | ### Features 122 | 123 | * add leading, and maxWait options to debounce hooks ([#97](https://github.com/jquense/react-common-hooks/issues/97)) ([c8d69b2](https://github.com/jquense/react-common-hooks/commit/c8d69b23dd22a802f1c918f791478e841a9d0b99)) 124 | 125 | 126 | 127 | 128 | 129 | ## [0.4.11](https://github.com/jquense/react-common-hooks/compare/v0.4.10...v0.4.11) (2023-07-17) 130 | 131 | 132 | ### Bug Fixes 133 | 134 | * **useIsInitialRenderRef:** clarify usage and fix ordering ([437c417](https://github.com/jquense/react-common-hooks/commit/437c417806e501c17e89b7252168e790077bb322)) 135 | 136 | 137 | 138 | 139 | 140 | ## [0.4.10](https://github.com/jquense/react-common-hooks/compare/v0.4.9...v0.4.10) (2023-07-13) 141 | 142 | 143 | ### Features 144 | 145 | * **useFocusManager:** fire focused change immediately ([#92](https://github.com/jquense/react-common-hooks/issues/92)) ([7bb6d5a](https://github.com/jquense/react-common-hooks/commit/7bb6d5ae74c40ff80b2fa5db06e21b7e970f7455)) 146 | 147 | 148 | 149 | 150 | 151 | ## [0.4.9](https://github.com/jquense/react-common-hooks/compare/v0.4.8...v0.4.9) (2023-02-10) 152 | 153 | 154 | ### Features 155 | 156 | * add exports field in package.json ([#88](https://github.com/jquense/react-common-hooks/issues/88)) ([1c325b0](https://github.com/jquense/react-common-hooks/commit/1c325b08a693ae9efa8cbfac6cc8d5884d2d238f)) 157 | 158 | 159 | 160 | 161 | 162 | ## [0.4.8](https://github.com/jquense/react-common-hooks/compare/v0.4.7...v0.4.8) (2023-01-25) 163 | 164 | 165 | ### Bug Fixes 166 | 167 | * **useDebouncedCallback:** fix return type ([#85](https://github.com/jquense/react-common-hooks/issues/85)) ([f2e26a0](https://github.com/jquense/react-common-hooks/commit/f2e26a0cbf8753f3c5964452b05b50721e5b3ca3)) 168 | 169 | 170 | 171 | 172 | 173 | ## [0.4.7](https://github.com/jquense/react-common-hooks/compare/v0.4.6...v0.4.7) (2022-04-27) 174 | 175 | 176 | ### Features 177 | 178 | * **useBreakpoint:** add xxl breakpoint ([#75](https://github.com/jquense/react-common-hooks/issues/75)) ([8021cea](https://github.com/jquense/react-common-hooks/commit/8021ceacaf34304e460193faf1ff8c5e31f43db1)) 179 | 180 | 181 | 182 | 183 | 184 | ## [0.4.6](https://github.com/jquense/react-common-hooks/compare/v0.4.5...v0.4.6) (2022-04-01) 185 | 186 | 187 | ### Bug Fixes 188 | 189 | * **useMounted:** adjust to work with strict effects ([#70](https://github.com/jquense/react-common-hooks/issues/70)) ([8deba46](https://github.com/jquense/react-common-hooks/commit/8deba46157fd8d73194cbd6ad81e5a6d575bf5d4)) 190 | 191 | 192 | 193 | 194 | 195 | ## [0.4.5](https://github.com/jquense/react-common-hooks/compare/v0.4.4...v0.4.5) (2021-12-10) 196 | 197 | 198 | ### Features 199 | 200 | * add useUpdateLayoutEffect ([1914f7e](https://github.com/jquense/react-common-hooks/commit/1914f7e55449505e88e8e93a06ad71bd58b7e9a4)) 201 | 202 | 203 | 204 | 205 | 206 | ## [0.4.4](https://github.com/jquense/react-common-hooks/compare/v0.4.3...v0.4.4) (2021-12-07) 207 | 208 | 209 | ### Bug Fixes 210 | 211 | * use targetWindow ([6c8fc0d](https://github.com/jquense/react-common-hooks/commit/6c8fc0d05ee94eda479dc11b4d1ee6ed4dbf8fc2)) 212 | 213 | 214 | 215 | 216 | 217 | ## [0.4.3](https://github.com/jquense/react-common-hooks/compare/v0.4.2...v0.4.3) (2021-12-07) 218 | 219 | 220 | ### Bug Fixes 221 | 222 | * store matchers per window ([6509df8](https://github.com/jquense/react-common-hooks/commit/6509df84abf35cc20faceea3db8a37121e3b7a61)) 223 | 224 | 225 | 226 | 227 | 228 | ## [0.4.2](https://github.com/jquense/react-common-hooks/compare/v0.4.1...v0.4.2) (2021-12-07) 229 | 230 | 231 | ### Bug Fixes 232 | 233 | * minor typo in useCommittedRef ([#59](https://github.com/jquense/react-common-hooks/issues/59)) ([0ceb834](https://github.com/jquense/react-common-hooks/commit/0ceb8347527e46dba115c62578a0485da8f46e22)) 234 | 235 | 236 | ### Features 237 | 238 | * allow specifying window with useMedia/useBreakpoint ([#61](https://github.com/jquense/react-common-hooks/issues/61)) ([bf9716d](https://github.com/jquense/react-common-hooks/commit/bf9716da21aff239baf02da11671cb5caf081ccf)) 239 | 240 | 241 | 242 | 243 | 244 | ## [0.4.1](https://github.com/jquense/react-common-hooks/compare/v0.4.0...v0.4.1) (2021-09-27) 245 | 246 | 247 | ### Bug Fixes 248 | 249 | * **useFocusManager:** memo returned handlers ([410bd5a](https://github.com/jquense/react-common-hooks/commit/410bd5a10588754319a8d0a8717399aca85b1947)) 250 | 251 | 252 | ### Features 253 | 254 | * add useDebouncedValue ([362dedf](https://github.com/jquense/react-common-hooks/commit/362dedf9ace6244b4766a0128f3afe1c1305ffb6)) 255 | 256 | 257 | 258 | 259 | 260 | # [0.4.0](https://github.com/jquense/react-common-hooks/compare/v0.3.27...v0.4.0) (2021-06-17) 261 | 262 | 263 | ### Features 264 | 265 | * **useIntersectionObserver:** allow lazy roots ([4c8b77a](https://github.com/jquense/react-common-hooks/commit/4c8b77acdabeef2e2eef199e0f418ee4117b6d08)) 266 | * **useIntersectionObserver:** allow lazy roots ([#53](https://github.com/jquense/react-common-hooks/issues/53)) ([6b035cf](https://github.com/jquense/react-common-hooks/commit/6b035cf74868b381e23e69a144fd60ce06f72264)) 267 | 268 | 269 | ### BREAKING CHANGES 270 | 271 | * **useIntersectionObserver:** `null` in the native API means "the window", this is a departure to allow a consumer to hold off setting up the observer until the have a ref to the root. This was possible before by explicitly setting element to `null` until the root is available but still created an extra observer 272 | 273 | 274 | 275 | 276 | 277 | ## [0.3.27](https://github.com/jquense/react-common-hooks/compare/v0.3.26...v0.3.27) (2021-06-16) 278 | 279 | 280 | ### Bug Fixes 281 | 282 | * **types:** fix ts errors ([#47](https://github.com/jquense/react-common-hooks/issues/47)) ([310e1ec](https://github.com/jquense/react-common-hooks/commit/310e1ec605835f245e0a6fa65c5ea9ce3a07d5ae)) 283 | 284 | 285 | ### Features 286 | 287 | * **useIntersectionObserver:** add more performant overload ([6c532d8](https://github.com/jquense/react-common-hooks/commit/6c532d8e1033579f23a6fec31c98be374d8ab4e1)) 288 | * **useMutationObserver:** add overload to return records and trigger updates ([1eaa621](https://github.com/jquense/react-common-hooks/commit/1eaa621a5da1545345a7c8df79ca09ec16dcf435)) 289 | * **useToggleState:** add useToggleState hook ([#48](https://github.com/jquense/react-common-hooks/issues/48)) ([5f95dd0](https://github.com/jquense/react-common-hooks/commit/5f95dd07dde261f73b8ff658f54dcae9b6034321)) 290 | * add useDebouncedCallback hook ([05fb8da](https://github.com/jquense/react-common-hooks/commit/05fb8da28ddd8c3afdcdd5e2250b16b539edc4a3)) 291 | * add useDebouncedState hook ([a87dd3b](https://github.com/jquense/react-common-hooks/commit/a87dd3b9788102106869bcada7b87dcf9eb984d2)) 292 | * remove lodash ([#35](https://github.com/jquense/react-common-hooks/issues/35)) ([fefa63c](https://github.com/jquense/react-common-hooks/commit/fefa63c4d2115d7880423fe5d26d5413efdc7d58)) 293 | 294 | 295 | 296 | 297 | 298 | ## [0.3.26](https://github.com/jquense/react-common-hooks/compare/v0.3.25...v0.3.26) (2021-01-05) 299 | 300 | 301 | ### Bug Fixes 302 | 303 | * Allow React 17 as peer dependency ([#42](https://github.com/jquense/react-common-hooks/issues/42)) ([d0fea56](https://github.com/jquense/react-common-hooks/commit/d0fea56dda0ea7139eca4899ce67123e68dd43d2)) 304 | 305 | 306 | 307 | 308 | 309 | ## [0.3.25](https://github.com/jquense/react-common-hooks/compare/v0.3.24...v0.3.25) (2020-05-14) 310 | 311 | 312 | ### Bug Fixes 313 | 314 | * set crossOrigin prior to src ([#31](https://github.com/jquense/react-common-hooks/issues/31)) ([1d585ec](https://github.com/jquense/react-common-hooks/commit/1d585eca5a062e70405f90fd1b4ffd469dd041be)) 315 | 316 | 317 | 318 | 319 | 320 | ## [0.3.24](https://github.com/jquense/react-common-hooks/compare/v0.3.23...v0.3.24) (2020-05-13) 321 | 322 | 323 | ### Features 324 | 325 | * add useThrottledEventHandler ([#30](https://github.com/jquense/react-common-hooks/issues/30)) ([f03d52b](https://github.com/jquense/react-common-hooks/commit/f03d52b096ed95f0614728acc243a2d0b6123970)) 326 | 327 | 328 | 329 | 330 | 331 | ## [0.3.23](https://github.com/jquense/react-common-hooks/compare/v0.3.22...v0.3.23) (2020-05-13) 332 | 333 | 334 | ### Bug Fixes 335 | 336 | * improve initial state type ([#29](https://github.com/jquense/react-common-hooks/issues/29)) ([35613e4](https://github.com/jquense/react-common-hooks/commit/35613e4aac832f9a09bd63f441f7c43a0df01b8c)) 337 | 338 | 339 | 340 | 341 | 342 | ## [0.3.22](https://github.com/jquense/react-common-hooks/compare/v0.3.21...v0.3.22) (2020-03-18) 343 | 344 | 345 | ### Features 346 | 347 | * **useImmediateUpdateEffect:** allow teardown from last effect like builtin effect hooks ([b85ad85](https://github.com/jquense/react-common-hooks/commit/b85ad852a0a9efed32183be8ce0b93386c46fe95)) 348 | * add useCustomEffect ([76b18d0](https://github.com/jquense/react-common-hooks/commit/76b18d048d70beba574c70f54684fdf0b41eb4fc)) 349 | * add useMutationObserver ([c9df89c](https://github.com/jquense/react-common-hooks/commit/c9df89cc5a1e1b83eb249e291b79a6bf3b32203b)) 350 | 351 | 352 | 353 | 354 | 355 | ## [0.3.21](https://github.com/jquense/react-common-hooks/compare/v0.3.20...v0.3.21) (2020-02-25) 356 | 357 | 358 | ### Features 359 | 360 | * add useMountEffect ([f4c4753](https://github.com/jquense/react-common-hooks/commit/f4c4753f15691b4ff6f44afd2b0a8e0e8e5b28e1)) 361 | * add useRefWithInitialValueFactory ([31188e2](https://github.com/jquense/react-common-hooks/commit/31188e28dc5b68fb6e030b855ce0491fbc0c1a70)) 362 | * add useUpdateEffect ([a0d17b1](https://github.com/jquense/react-common-hooks/commit/a0d17b1dcbb4f910468bd9ae60256318fe218ecc)) 363 | 364 | 365 | 366 | 367 | 368 | ## [0.3.20](https://github.com/jquense/react-common-hooks/compare/v0.3.19...v0.3.20) (2020-01-23) 369 | 370 | 371 | ### Features 372 | 373 | * **useTimeout:** handle large delays ([#21](https://github.com/jquense/react-common-hooks/issues/21)) ([32d6951](https://github.com/jquense/react-common-hooks/commit/32d69518295b6a620afad8c495ee357a35ffeb1f)) 374 | 375 | 376 | 377 | 378 | 379 | ## [0.3.19](https://github.com/jquense/react-common-hooks/compare/v0.3.18...v0.3.19) (2019-11-26) 380 | 381 | 382 | ### Features 383 | 384 | * Add option in useInterval to run immediately ([#15](https://github.com/jquense/react-common-hooks/issues/15)) ([899369f](https://github.com/jquense/react-common-hooks/commit/899369febaaff7f60ed449b07838ef199a35b5c1)) 385 | 386 | 387 | 388 | 389 | 390 | ## [0.3.18](https://github.com/jquense/react-common-hooks/compare/v0.3.17...v0.3.18) (2019-11-22) 391 | 392 | 393 | ### Bug Fixes 394 | 395 | * Fix React peer dependency ([ea56c3d](https://github.com/jquense/react-common-hooks/commit/ea56c3d9f37f45a85d18e7b6d4092faf459bfef8)) 396 | 397 | 398 | 399 | 400 | 401 | ## [0.3.17](https://github.com/jquense/react-common-hooks/compare/v0.3.16...v0.3.17) (2019-11-22) 402 | 403 | 404 | ### Bug Fixes 405 | 406 | * Clean up package.json ([#16](https://github.com/jquense/react-common-hooks/issues/16)) ([d167136](https://github.com/jquense/react-common-hooks/commit/d1671360eed0b8137d8ca32271bef7c498b5faef)) 407 | 408 | 409 | 410 | 411 | 412 | ## [0.3.16](https://github.com/jquense/react-common-hooks/compare/v0.3.15...v0.3.16) (2019-11-22) 413 | 414 | 415 | ### Features 416 | 417 | * add useImmediateUpdateEffect ([#14](https://github.com/jquense/react-common-hooks/issues/14)) ([b6f5870](https://github.com/jquense/react-common-hooks/commit/b6f5870)) 418 | 419 | 420 | 421 | 422 | 423 | ## [0.3.15](https://github.com/jquense/react-common-hooks/compare/v0.3.14...v0.3.15) (2019-10-23) 424 | 425 | 426 | ### Features 427 | 428 | * add useSafeState ([#13](https://github.com/jquense/react-common-hooks/issues/13)) ([88c53cb](https://github.com/jquense/react-common-hooks/commit/88c53cb)) 429 | 430 | 431 | 432 | 433 | 434 | ## [0.3.14](https://github.com/jquense/react-common-hooks/compare/v0.3.13...v0.3.14) (2019-09-20) 435 | 436 | 437 | ### Features 438 | 439 | * add useAsyncSetState ([#12](https://github.com/jquense/react-common-hooks/issues/12)) ([0db4bb4](https://github.com/jquense/react-common-hooks/commit/0db4bb4)) 440 | 441 | 442 | 443 | 444 | 445 | ## [0.3.13](https://github.com/jquense/react-common-hooks/compare/v0.3.12...v0.3.13) (2019-09-06) 446 | 447 | 448 | ### Bug Fixes 449 | 450 | * useStableMemo infinite loop ([ad6fc5a](https://github.com/jquense/react-common-hooks/commit/ad6fc5a)) 451 | 452 | 453 | 454 | 455 | 456 | ## [0.3.12](https://github.com/jquense/react-common-hooks/compare/v0.3.11...v0.3.12) (2019-08-12) 457 | 458 | 459 | ### Bug Fixes 460 | 461 | * **useMediaQuery:** matches immediately in a DOM environment ([#11](https://github.com/jquense/react-common-hooks/issues/11)) ([5d1053d](https://github.com/jquense/react-common-hooks/commit/5d1053d)) 462 | 463 | 464 | 465 | 466 | 467 | ## [0.3.11](https://github.com/jquense/react-common-hooks/compare/v0.3.10...v0.3.11) (2019-08-09) 468 | 469 | 470 | ### Features 471 | 472 | * add useMergedRef ([#9](https://github.com/jquense/react-common-hooks/issues/9)) ([83bd6e1](https://github.com/jquense/react-common-hooks/commit/83bd6e1)) 473 | 474 | 475 | 476 | 477 | 478 | ## [0.3.10](https://github.com/jquense/react-common-hooks/compare/v0.3.9...v0.3.10) (2019-08-09) 479 | 480 | 481 | ### Features 482 | 483 | * add useMap and useSet ([#8](https://github.com/jquense/react-common-hooks/issues/8)) ([c21429c](https://github.com/jquense/react-common-hooks/commit/c21429c)) 484 | 485 | 486 | 487 | 488 | 489 | ## [0.3.9](https://github.com/jquense/react-common-hooks/compare/v0.3.8...v0.3.9) (2019-08-05) 490 | 491 | 492 | ### Features 493 | 494 | * add useForceUpdate() ([ba91b5f](https://github.com/jquense/react-common-hooks/commit/ba91b5f)) 495 | 496 | 497 | 498 | 499 | 500 | ## [0.3.8](https://github.com/jquense/react-common-hooks/compare/v0.3.7...v0.3.8) (2019-07-18) 501 | 502 | 503 | ### Bug Fixes 504 | 505 | * **useInterval:** don't call callback in useEffect ([#7](https://github.com/jquense/react-common-hooks/issues/7)) ([264fbeb](https://github.com/jquense/react-common-hooks/commit/264fbeb)) 506 | 507 | 508 | 509 | 510 | 511 | ## [0.3.7](https://github.com/jquense/react-common-hooks/compare/v0.3.6...v0.3.7) (2019-07-11) 512 | 513 | 514 | ### Bug Fixes 515 | 516 | * **useIntersectionObserver:** fix internal set state call triggering warnings ([7bc1d0c](https://github.com/jquense/react-common-hooks/commit/7bc1d0c)) 517 | 518 | 519 | 520 | 521 | 522 | ## [0.3.6](https://github.com/jquense/react-common-hooks/compare/v0.3.5...v0.3.6) (2019-07-08) 523 | 524 | 525 | ### Bug Fixes 526 | 527 | * wrong useStableMemo export ([16bda32](https://github.com/jquense/react-common-hooks/commit/16bda32)) 528 | * **useAnimationFrame:** stable return value ([5cca2d4](https://github.com/jquense/react-common-hooks/commit/5cca2d4)) 529 | 530 | 531 | 532 | 533 | 534 | ## [0.3.5](https://github.com/jquense/react-common-hooks/compare/v0.3.4...v0.3.5) (2019-07-06) 535 | 536 | 537 | ### Bug Fixes 538 | 539 | * **useIntersectionObserver:** return an array consistently ([ad3bb35](https://github.com/jquense/react-common-hooks/commit/ad3bb35)) 540 | 541 | 542 | ### Features 543 | 544 | * add useMediaQuery and useBreakpoint ([0d165ec](https://github.com/jquense/react-common-hooks/commit/0d165ec)) 545 | 546 | 547 | 548 | 549 | 550 | ## [0.3.4](https://github.com/jquense/react-common-hooks/compare/v0.3.3...v0.3.4) (2019-07-04) 551 | 552 | 553 | ### Bug Fixes 554 | 555 | * **useIntersectionObserver:** make SSR compatible ([fc56257](https://github.com/jquense/react-common-hooks/commit/fc56257)) 556 | 557 | 558 | 559 | 560 | 561 | ## [0.3.3](https://github.com/jquense/react-common-hooks/compare/v0.3.2...v0.3.3) (2019-07-03) 562 | 563 | 564 | ### Bug Fixes 565 | 566 | * **useEventListener:** correct listener options type ([96503dc](https://github.com/jquense/react-common-hooks/commit/96503dc)) 567 | 568 | 569 | ### Features 570 | 571 | * **useAnimationFrame:** add the ability to not clear previous callbacks ([541b82c](https://github.com/jquense/react-common-hooks/commit/541b82c)) 572 | * **useIntersectionObserver:** add an IntersectionObserver hook ([5791b22](https://github.com/jquense/react-common-hooks/commit/5791b22)) 573 | * **useIsomorphicEffect:** add hook to avoid SSR warning with useLayoutEffect ([6e6b5fa](https://github.com/jquense/react-common-hooks/commit/6e6b5fa)) 574 | * **useResizeObserver:** use with isomorphic safe effect ([4a5d976](https://github.com/jquense/react-common-hooks/commit/4a5d976)) 575 | * **useStableMemo:** add useStableMemo for a stable...useMemo ([ff981fb](https://github.com/jquense/react-common-hooks/commit/ff981fb)) 576 | 577 | 578 | 579 | 580 | 581 | ## [0.3.2](https://github.com/jquense/react-common-hooks/compare/v0.3.1...v0.3.2) (2019-06-26) 582 | 583 | 584 | ### Features 585 | 586 | * add esm support ([#5](https://github.com/jquense/react-common-hooks/issues/5)) ([ec456c1](https://github.com/jquense/react-common-hooks/commit/ec456c1)) 587 | * useEventListener allows a function, useGlobalListener works in SSR ([c67b9bc](https://github.com/jquense/react-common-hooks/commit/c67b9bc)) 588 | 589 | 590 | 591 | 592 | 593 | ## [0.3.1](https://github.com/jquense/react-common-hooks/compare/v0.3.0...v0.3.1) (2019-06-07) 594 | 595 | 596 | ### Bug Fixes 597 | 598 | * **useMergeState:** calling setState twice ([#4](https://github.com/jquense/react-common-hooks/issues/4)) ([ec7d569](https://github.com/jquense/react-common-hooks/commit/ec7d569)) 599 | 600 | 601 | 602 | 603 | 604 | # [0.3.0](https://github.com/jquense/react-common-hooks/compare/v0.2.14...v0.3.0) (2019-05-28) 605 | 606 | 607 | ### Features 608 | 609 | * better name ([dca4d54](https://github.com/jquense/react-common-hooks/commit/dca4d54)) 610 | 611 | 612 | ### BREAKING CHANGES 613 | 614 | * useRequestAnimationFrame -> useAnimationFrame 615 | 616 | 617 | 618 | 619 | 620 | ## [0.2.14](https://github.com/jquense/react-common-hooks/compare/v0.2.13...v0.2.14) (2019-05-28) 621 | 622 | 623 | ### Features 624 | 625 | * add useRequestAnimationFrame ([5c8dff1](https://github.com/jquense/react-common-hooks/commit/5c8dff1)) 626 | 627 | 628 | 629 | 630 | 631 | ## [0.2.13](https://github.com/jquense/react-common-hooks/compare/v0.2.12...v0.2.13) (2019-05-15) 632 | 633 | 634 | ### Bug Fixes 635 | 636 | * **types:** useInterval overloads ([f592102](https://github.com/jquense/react-common-hooks/commit/f592102)) 637 | 638 | 639 | 640 | 641 | 642 | ## [0.2.12](https://github.com/jquense/react-common-hooks/compare/v0.2.11...v0.2.12) (2019-05-08) 643 | 644 | 645 | ### Bug Fixes 646 | 647 | * **types:** make useFocusManger options optional ([c1ee506](https://github.com/jquense/react-common-hooks/commit/c1ee506)) 648 | * **useMounted:** provides a stable isMounted function ([3703508](https://github.com/jquense/react-common-hooks/commit/3703508)) 649 | 650 | 651 | 652 | 653 | 654 | ## [0.2.11](https://github.com/jquense/react-common-hooks/compare/v0.2.10...v0.2.11) (2019-05-02) 655 | 656 | 657 | ### Bug Fixes 658 | 659 | * allow nullable handlers in event callback ([c423cb8](https://github.com/jquense/react-common-hooks/commit/c423cb8)) 660 | 661 | 662 | 663 | 664 | 665 | ## [0.2.10](https://github.com/jquense/react-common-hooks/compare/v0.2.9...v0.2.10) (2019-05-02) 666 | 667 | 668 | ### Features 669 | 670 | * add useFocusManager() ([1cbeb66](https://github.com/jquense/react-common-hooks/commit/1cbeb66)) 671 | * add useTimeout() ([4cb8fa1](https://github.com/jquense/react-common-hooks/commit/4cb8fa1)) 672 | * add useUpdatedRef ([79b3a49](https://github.com/jquense/react-common-hooks/commit/79b3a49)) 673 | * add useWillUnmount() ([ab3e4df](https://github.com/jquense/react-common-hooks/commit/ab3e4df)) 674 | 675 | 676 | 677 | 678 | 679 | ## [0.2.9](https://github.com/jquense/react-common-hooks/compare/v0.2.8...v0.2.9) (2019-04-22) 680 | 681 | 682 | 683 | 684 | 685 | ## [0.2.8](https://github.com/jquense/react-common-hooks/compare/v0.2.7...v0.2.8) (2019-04-22) 686 | 687 | 688 | ### Features 689 | 690 | * add useCallbackRef ([722f6f5](https://github.com/jquense/react-common-hooks/commit/722f6f5)) 691 | 692 | 693 | 694 | 695 | 696 | ## [0.2.7](https://github.com/jquense/react-common-hooks/compare/v0.2.6...v0.2.7) (2019-04-16) 697 | 698 | 699 | ### Bug Fixes 700 | 701 | * **types:** allow null or undefined for useImage ([6806fd4](https://github.com/jquense/react-common-hooks/commit/6806fd4)) 702 | * **useResizeObserver:** allow element to be null or undefined ([d441fa2](https://github.com/jquense/react-common-hooks/commit/d441fa2)) 703 | 704 | 705 | 706 | 707 | 708 | ## [0.2.6](https://github.com/jquense/react-common-hooks/compare/v0.2.5...v0.2.6) (2019-04-15) 709 | 710 | 711 | 712 | 713 | 714 | ## [0.2.5](https://github.com/jquense/react-common-hooks/compare/v0.2.3...v0.2.5) (2019-04-10) 715 | 716 | 717 | ### Features 718 | 719 | * support Images directly in useImage ([f16210b](https://github.com/jquense/react-common-hooks/commit/f16210b)) 720 | 721 | 722 | 723 | 724 | 725 | ## [0.2.3](https://github.com/jquense/react-common-hooks/compare/v0.2.2...v0.2.3) (2019-03-28) 726 | 727 | 728 | ### Features 729 | 730 | * allow return null from useMergeState updater function ([c14a4e5](https://github.com/jquense/react-common-hooks/commit/c14a4e5)) 731 | 732 | 733 | 734 | 735 | 736 | ## [0.2.2](https://github.com/jquense/react-common-hooks/compare/v0.2.1...v0.2.2) (2019-03-28) 737 | 738 | 739 | ### Bug Fixes 740 | 741 | * optional crossOrigin on useImage ([2e08a7d](https://github.com/jquense/react-common-hooks/commit/2e08a7d)) 742 | 743 | 744 | 745 | 746 | 747 | ## [0.2.1](https://github.com/jquense/react-common-hooks/compare/v0.2.0...v0.2.1) (2019-03-28) 748 | 749 | 750 | ### Features 751 | 752 | * add useImage ([5b5de01](https://github.com/jquense/react-common-hooks/commit/5b5de01)) 753 | 754 | 755 | 756 | 757 | 758 | # [0.2.0](https://github.com/jquense/react-common-hooks/compare/v0.1.6...v0.2.0) (2019-03-28) 759 | 760 | 761 | ### Bug Fixes 762 | 763 | * Target ES5 in build ([#1](https://github.com/jquense/react-common-hooks/issues/1)) ([1f49c80](https://github.com/jquense/react-common-hooks/commit/1f49c80)) 764 | 765 | 766 | ### Features 767 | 768 | * add use*Interval overloads ([94dea26](https://github.com/jquense/react-common-hooks/commit/94dea26)) 769 | * add useEventListener ([bc3f520](https://github.com/jquense/react-common-hooks/commit/bc3f520)) 770 | * useEffect over LayoutEffect ([95c3416](https://github.com/jquense/react-common-hooks/commit/95c3416)) 771 | 772 | 773 | ### BREAKING CHANGES 774 | 775 | * changed arg order around on useInterval and useRafInterval 776 | * useCommittedRef fires latter now on useEffect 777 | 778 | 779 | 780 | 781 | 782 | ## [0.1.6](https://github.com/jquense/react-common-hooks/compare/v0.1.5...v0.1.6) (2019-03-27) 783 | 784 | 785 | ### Features 786 | 787 | * better globalListener types ([b0ae1ed](https://github.com/jquense/react-common-hooks/commit/b0ae1ed)) 788 | 789 | 790 | 791 | 792 | 793 | ## [0.1.5](https://github.com/jquense/react-common-hooks/compare/v0.1.4...v0.1.5) (2019-03-26) 794 | 795 | 796 | ### Bug Fixes 797 | 798 | * remove usePrevious console.log ([aeb56d8](https://github.com/jquense/react-common-hooks/commit/aeb56d8)) 799 | 800 | 801 | 802 | 803 | 804 | ## [0.1.4](https://github.com/jquense/react-common-hooks/compare/v0.1.3...v0.1.4) (2019-03-26) 805 | 806 | 807 | ### Bug Fixes 808 | 809 | * deploy new code ([be0689c](https://github.com/jquense/react-common-hooks/commit/be0689c)) 810 | 811 | 812 | 813 | 814 | 815 | ## [0.1.3](https://github.com/jquense/react-common-hooks/compare/v0.1.1...v0.1.3) (2019-03-26) 816 | 817 | 818 | ### Features 819 | 820 | * add useMounted ([36ed090](https://github.com/jquense/react-common-hooks/commit/36ed090)) 821 | 822 | 823 | 824 | 825 | 826 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2018 Jason Quense 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # @restart/hooks [![npm][npm-badge]][npm] 2 | 3 | A set of utility and general-purpose React hooks. 4 | 5 | ## Install 6 | 7 | ```sh 8 | npm install @restart/hooks 9 | ``` 10 | 11 | ## Usage 12 | 13 | ```js 14 | import useInterval from '@restart/hooks/useInterval' 15 | 16 | useInterval(() => loop(), 300, false) 17 | ``` 18 | 19 | [npm-badge]: https://img.shields.io/npm/v/@restart/hooks.svg 20 | [npm]: https://www.npmjs.org/package/@restart/hooks 21 | -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "@restart/hooks", 3 | "version": "0.6.2", 4 | "type": "module", 5 | "exports": { 6 | "./*": { 7 | "require": { 8 | "types": "./cjs/*.d.ts", 9 | "default": "./cjs/*.js" 10 | }, 11 | "import": { 12 | "types": "./lib/*.d.ts", 13 | "default": "./lib/*.js" 14 | } 15 | } 16 | }, 17 | "files": [ 18 | "cjs", 19 | "lib", 20 | "CHANGELOG.md" 21 | ], 22 | "repository": { 23 | "type": "git", 24 | "url": "git+https://github.com/react-restart/hooks.git" 25 | }, 26 | "author": { 27 | "name": "Jason Quense", 28 | "email": "monastic.panic@gmail.com" 29 | }, 30 | "license": "MIT", 31 | "bugs": { 32 | "url": "https://github.com/react-restart/hooks/issues" 33 | }, 34 | "homepage": "https://github.com/react-restart/hooks#readme", 35 | "scripts": { 36 | "bootstrap": "yarn && yarn --cwd www", 37 | "test": "vitest run --coverage", 38 | "tdd": "vitest", 39 | "build": "rimraf lib cjs && concurrently --names 'esm,cjs' 'yarn build:esm' 'yarn build:cjs' && concurrently --names 'esm types,cjs types' 'yarn build:esm:types' 'yarn build:cjs:types'", 40 | "build:esm": "babel src --env-name esm --out-dir lib --extensions '.ts' --ignore='**/*.d.ts'", 41 | "build:esm:types": "tsc -p . --emitDeclarationOnly --declaration --outDir lib", 42 | "build:cjs": "babel src --out-dir cjs --extensions '.ts' --ignore='**/*.d.ts' && echo '{\"type\": \"commonjs\"}' > cjs/package.json", 43 | "build:cjs:types": "tsc -p . --emitDeclarationOnly --declaration --outDir cjs --module commonjs --moduleResolution node", 44 | "deploy-docs": "yarn --cwd www build --prefix-paths && gh-pages -d www/public", 45 | "prepublishOnly": "yarn build", 46 | "typecheck": "tsc -p . --noEmit", 47 | "release": "rollout" 48 | }, 49 | "jest": { 50 | "preset": "@4c", 51 | "testEnvironment": "jsdom", 52 | "setupFilesAfterEnv": [ 53 | "./test/setup.js" 54 | ] 55 | }, 56 | "prettier": { 57 | "singleQuote": true, 58 | "semi": false, 59 | "trailingComma": "all" 60 | }, 61 | "publishConfig": { 62 | "access": "public" 63 | }, 64 | "release": { 65 | "conventionalCommits": true 66 | }, 67 | "peerDependencies": { 68 | "react": ">=16.8.0" 69 | }, 70 | "devDependencies": { 71 | "@4c/babel-preset": "^10.2.1", 72 | "@4c/cli": "^4.0.4", 73 | "@4c/rollout": "^4.0.2", 74 | "@4c/tsconfig": "^0.4.1", 75 | "@babel/cli": "^7.26.4", 76 | "@babel/core": "^7.26.0", 77 | "@babel/preset-typescript": "^7.26.0", 78 | "@testing-library/dom": "^10.4.0", 79 | "@testing-library/react": "^16.1.0", 80 | "@types/lodash": "^4.14.195", 81 | "@types/react": "^19.0.2", 82 | "@vitest/coverage-v8": "2.1.8", 83 | "babel-plugin-transform-rename-import": "^2.3.0", 84 | "cherry-pick": "^0.5.0", 85 | "codecov": "^3.8.3", 86 | "concurrently": "^9.1.2", 87 | "eslint": "^8.44.0", 88 | "gh-pages": "^3.1.0", 89 | "husky": "^4.3.6", 90 | "jsdom": "^25.0.1", 91 | "lint-staged": "^13.2.3", 92 | "mq-polyfill": "^1.1.8", 93 | "prettier": "^3.0.0", 94 | "react": "^19.0.0", 95 | "react-dom": "^19.0.0", 96 | "rimraf": "^5.0.1", 97 | "typescript": "^5.1.6", 98 | "vitest": "^2.1.8" 99 | }, 100 | "dependencies": { 101 | "dequal": "^2.0.3" 102 | }, 103 | "packageManager": "yarn@1.22.22+sha512.a6b2f7906b721bba3d67d4aff083df04dad64c399707841b7acf00f6b133b7ac24255f2652fa22ae3534329dc6180534e98d17432037ff6fd140556e2bb3137e" 104 | } 105 | -------------------------------------------------------------------------------- /src/globals.d.ts: -------------------------------------------------------------------------------- 1 | interface Window { 2 | ResizeObserver: ResizeObserver 3 | } 4 | 5 | /** 6 | * The ResizeObserver interface is used to observe changes to Element's content 7 | * rect. 8 | * 9 | * It is modeled after MutationObserver and IntersectionObserver. 10 | */ 11 | interface ResizeObserver { 12 | new (callback: ResizeObserverCallback) 13 | 14 | /** 15 | * Adds target to the list of observed elements. 16 | */ 17 | observe: (target: Element) => void 18 | 19 | /** 20 | * Removes target from the list of observed elements. 21 | */ 22 | unobserve: (target: Element) => void 23 | 24 | /** 25 | * Clears both the observationTargets and activeTargets lists. 26 | */ 27 | disconnect: () => void 28 | } 29 | 30 | /** 31 | * This callback delivers ResizeObserver's notifications. It is invoked by a 32 | * broadcast active observations algorithm. 33 | */ 34 | interface ResizeObserverCallback { 35 | (entries: ResizeObserverEntry[], observer: ResizeObserver): void 36 | } 37 | 38 | interface ResizeObserverEntry { 39 | /** 40 | * @param target The Element whose size has changed. 41 | */ 42 | new (target: Element) 43 | 44 | /** 45 | * The Element whose size has changed. 46 | */ 47 | readonly target: Element 48 | 49 | /** 50 | * Element's content rect when ResizeObserverCallback is invoked. 51 | */ 52 | readonly contentRect: DOMRectReadOnly 53 | } 54 | 55 | interface DOMRectReadOnly { 56 | fromRect(other: DOMRectInit | undefined): DOMRectReadOnly 57 | 58 | readonly x: number 59 | readonly y: number 60 | readonly width: number 61 | readonly height: number 62 | readonly top: number 63 | readonly right: number 64 | readonly bottom: number 65 | readonly left: number 66 | 67 | toJSON: () => any 68 | } 69 | -------------------------------------------------------------------------------- /src/useAnimationFrame.ts: -------------------------------------------------------------------------------- 1 | import { useEffect, useState } from 'react' 2 | import useMounted from './useMounted.js' 3 | 4 | export interface UseAnimationFrameReturn { 5 | cancel(): void 6 | 7 | /** 8 | * Request for the provided callback to be called on the next animation frame. 9 | * Previously registered callbacks will be cancelled 10 | */ 11 | request(callback: FrameRequestCallback): void 12 | } 13 | type AnimationFrameState = { 14 | fn: FrameRequestCallback 15 | } 16 | /** 17 | * Returns a controller object for requesting and cancelling an animation frame that is properly cleaned up 18 | * once the component unmounts. New requests cancel and replace existing ones. 19 | * 20 | * ```ts 21 | * const [style, setStyle] = useState({}); 22 | * const animationFrame = useAnimationFrame(); 23 | * 24 | * const handleMouseMove = (e) => { 25 | * animationFrame.request(() => { 26 | * setStyle({ top: e.clientY, left: e.clientY }) 27 | * }) 28 | * } 29 | * 30 | * const handleMouseUp = () => { 31 | * animationFrame.cancel() 32 | * } 33 | * 34 | * return ( 35 | *