### Installing Rooks Library with NPM Source: https://github.com/imbhargav5/rooks/blob/main/apps/website/src/pages/getting-started.md This command installs the 'rooks' library as a dependency in your project using npm. The '-s' flag (or '--save') adds it to the 'dependencies' section of your 'package.json' file, making it available for use in your application. ```Shell npm i -s rooks ``` -------------------------------- ### Importing useDidMount Hook in JSX Source: https://github.com/imbhargav5/rooks/blob/main/apps/website/src/pages/getting-started.md This line demonstrates how to import a specific hook, 'useDidMount', from the 'rooks' library. This is a standard ES module import syntax used to bring modular functionality into a JavaScript or React component file. ```JSX import { useDidMount } from "rooks"; ``` -------------------------------- ### Using useDidMount Hook in a React Component Source: https://github.com/imbhargav5/rooks/blob/main/apps/website/src/pages/getting-started.md This React functional component 'App' demonstrates the usage of the 'useDidMount' hook from 'rooks'. The callback function passed to 'useDidMount' will execute only once after the component has been mounted to the DOM, providing a lifecycle equivalent to 'componentDidMount' in class components. ```JSX function App() { useDidMount(() => { alert("mounted"); }); return (

Hello CodeSandbox

Start editing to see some magic happen!

); } ``` -------------------------------- ### Installing Dependencies - Yarn Command Source: https://github.com/imbhargav5/rooks/blob/main/CONTRIBUTING.md This command installs all required project dependencies. It should be run from the project root to ensure all packages are correctly linked and available for development or building, resolving any missing modules. ```shell yarn install ``` -------------------------------- ### Creating New Hook - Yarn Command Source: https://github.com/imbhargav5/rooks/blob/main/CONTRIBUTING.md This command initiates the process for creating a new hook within the project. It sets up the necessary package structure and files, streamlining the development of new functionalities by guiding the user through the setup. ```shell yarn new ``` -------------------------------- ### Basic Usage of useVideo Hook in React (TypeScript) Source: https://github.com/imbhargav5/rooks/blob/main/apps/website/content/docs/hooks/useVideo.mdx This example demonstrates the basic integration of the `useVideo` hook from the 'rooks' library into a React functional component. It shows how to import and call the hook within an `App` component, which in this minimal example, simply returns null. ```TypeScript import {useVideo} from "rooks" export default function App() { useVideo(); return null } ``` -------------------------------- ### Installing rooks package with npm Source: https://github.com/imbhargav5/rooks/blob/main/data/docs/rooks.md This command demonstrates how to install the 'rooks' package as a project dependency using npm. The npm i -s syntax is a shorthand for npm install --save, which adds the package to your package.json file, making it available for use in your application. ```Shell npm i - s rooks ``` -------------------------------- ### Installing Rooks Library via npm Source: https://github.com/imbhargav5/rooks/blob/main/packages/rooks/README.md This command installs the Rooks library as a production dependency in your project using npm. The '-s' flag is a shorthand for '--save', ensuring the package is listed in your 'dependencies' in 'package.json'. ```Bash npm i -s rooks ``` -------------------------------- ### Initializing useToggle Hook with a Start Value in React Source: https://github.com/imbhargav5/rooks/blob/main/apps/website/content/docs/hooks/useToggle.mdx This example shows how to initialize the `useToggle` hook with a specific starting boolean value (e.g., `true`). The state will initially be `true`, and subsequent clicks will toggle it between `true` and `false`. It requires the `rooks` library. ```jsx import \"./styles.css\";\ import { useToggle } from \"rooks\";\ \ export default function App() {\ const [state, toggle] = useToggle(true);\ \ return (\
\

Rooks: useToggle Example <\/h1>\
<\/br>\

); } export default App; ``` -------------------------------- ### Compiling Project - Yarn Command Source: https://github.com/imbhargav5/rooks/blob/main/CONTRIBUTING.md This command compiles the entire project, transforming source code into distributable artifacts. It's essential to run this after installing dependencies and before testing or deploying changes to ensure the latest code is built. ```shell yarn build ``` -------------------------------- ### Initializing useInput Hook in React (Simple Example) Source: https://github.com/imbhargav5/rooks/blob/main/data/docs/useInput.md This snippet demonstrates the basic usage of the `useInput` hook from the 'rooks' library. It initializes an input field with a default value 'hello' and dynamically displays its current value, providing a simple way to manage input state. ```jsx import { useInput } from "rooks"; export default function App() { const myInput = useInput("hello"); return (

Value is {myInput.value}

); } ``` -------------------------------- ### useToggle Hook with Initial Value in React Source: https://github.com/imbhargav5/rooks/blob/main/data/docs/useToggle.md This example illustrates how to initialize the `useToggle` hook with a starting boolean value, in this case, `true`. It demonstrates setting the initial state and then toggling it on button clicks, showing how the component's display reacts to the state changes. ```jsx import \"./styles.css\";\ import { useToggle } from \"rooks\";\ \ export default function App() {\ const [state, toggle] = useToggle(true);\ \ return (\
\

Rooks: useToggle Example <\/h1>\
<\/br>\

); } export default App; ``` -------------------------------- ### Basic Usage of useWindowSize Hook in React Source: https://github.com/imbhargav5/rooks/blob/main/apps/website/content/docs/hooks/useWindowSize.mdx This example demonstrates how to integrate and use the `useWindowSize` hook from the 'rooks' library within a React functional component. It shows how to destructure the returned object to access `innerWidth`, `innerHeight`, `outerHeight`, and `outerWidth` and display them dynamically in the UI. ```JSX import "./styles.css"; import { useWindowSize } from "rooks"; function App() { const { innerWidth, innerHeight, outerHeight, outerWidth } = useWindowSize(); return (

Rooks: useWindowSize Example


innerHeight - {innerHeight}

innerWidth - {innerWidth}

outerHeight - {outerHeight}

outerWidth - {outerWidth}

); } export default App; ``` -------------------------------- ### Basic Integration of a Rooks Utility/Hook (TypeScript) Source: https://github.com/imbhargav5/rooks/blob/main/template/README.md This snippet illustrates the fundamental way to import and utilize a Rooks utility or hook (represented by "%name%") within a React functional component. It shows the import statement and a direct call to the utility within the component's body, assuming it's a hook or a function that can be invoked without arguments for this basic example. ```TypeScript import {%name%} from "rooks" export default function App() { %name%(); return null } ``` -------------------------------- ### Basic Usage of useIsDroppingFiles Hook in React Source: https://github.com/imbhargav5/rooks/blob/main/data/docs/useIsDroppingFiles.md This example demonstrates the basic integration of the `useIsDroppingFiles` hook from the `rooks` library into a React functional component. The hook is called directly within the `App` component, providing a mechanism to track file drop events across the application. It requires the `rooks` package as a dependency. ```tsx import { useIsDroppingFiles } from "rooks"; export default function App() { useIsDroppingFiles(); return null; } ``` -------------------------------- ### Basic Usage of useTimeoutWhen Hook in React Source: https://github.com/imbhargav5/rooks/blob/main/apps/website/content/docs/hooks/useTimeoutWhen.mdx This example demonstrates the basic usage of the `useTimeoutWhen` hook within a React functional component. It uses a state variable `start` to control a button's disabled state. Clicking the button sets `start` to `true`, which triggers the `useTimeoutWhen` hook. After a 2-second delay, the hook's callback sets `start` back to `false`, re-enabling the button. ```jsx import "./styles.css"; import { useTimeoutWhen } from "rooks"; import { useState } from "react"; function App() { const [start, setStart] = useState(false); useTimeoutWhen(() => setStart(false), 2000, start); return ( <>

Rooks: useTimeoutWhen example


Click the button below to disable it for 2 seconds

); } export default App; ``` -------------------------------- ### Handling Vowel Keydown Events with useKeyRef (JSX) Source: https://github.com/imbhargav5/rooks/blob/main/apps/website/content/docs/hooks/useKeyRef.mdx This example demonstrates the basic usage of `useKeyRef` to listen for specific keydown events on an input element. It attaches a ref to the input, and when any of the specified vowel keys are pressed, a callback function logs a message to the console. ```jsx import { useKeyRef } from "rooks"; export default function App() { function vowelsEntered(e) { console.log("[Demo 1] You typed a vowel"); } // window is the target const inputRef = useKeyRef(["a", "e", "i", "o", "u"], vowelsEntered); return ( <>

Press a,e,i,o,u in the input to trigger a console.log statement

); } ``` -------------------------------- ### Implementing useKeys Hook for Multiple Key Combinations in React Source: https://github.com/imbhargav5/rooks/blob/main/data/docs/useKeys.md This example demonstrates how to use the `useKeys` hook to trigger callbacks when specific key combinations are pressed. It shows two instances: one for 'ControlLeft + S' on the document, and another for 'M + R' on an input field, illustrating the `target` and `when` options. It also includes state management for event activation and callback count. ```JSX import * as React from "react"; import { useKeys } from "rooks"; export default function App() { const containerRef = React.useRef(document); const inputRef = React.useRef(null); const [isEventActive, setIsEventActive] = React.useState(true); const [firstCallbackCallCount, setFirstCallbackCallCount] = React.useState(0); useKeys( ["ControlLeft", "KeyS"], () => { alert("you presses ctrlLeft + s"); setFirstCallbackCallCount(curr => curr + 1); }, { target: containerRef, when: isEventActive, preventLostKeyup: true, } ); useKeys( ["m", "r"], event => { // event.stopPropagation(); console.log("here you go m and r"); }, { when: isEventActive, target: inputRef, } ); return (

Callback Run Count: {firstCallbackCallCount}

Is events enabled ? ==> {isEventActive ? "Yes" : "No"}

Press CtrlLeft + s to see update in count

); } ``` -------------------------------- ### Initializing useUndoRedoState Hook in TypeScript Source: https://github.com/imbhargav5/rooks/blob/main/apps/website/content/docs/hooks/useUndoRedoState.mdx This snippet demonstrates the basic initialization of the `useUndoRedoState` hook from the 'rooks' library within a React functional component. It shows how to import and call the hook without any specific state management, serving as a minimal example of its integration. ```tsx import {useUndoRedoState} from "rooks" export default function App() { useUndoRedoState(); return null } ``` -------------------------------- ### Basic Usage of useDebounceFn in React (TypeScript) Source: https://github.com/imbhargav5/rooks/blob/main/data/docs/useDebounceFn.md This snippet illustrates the fundamental way to integrate the `useDebounceFn` hook into a React functional component. It shows the necessary import statement from the 'rooks' library and a simple invocation of the hook within the `App` component, serving as a minimal working example. ```TypeScript import {useDebounceFn} from "rooks" export default function App() { useDebounceFn(); return null } ``` -------------------------------- ### Basic Usage of useResizeObserverRef Hook in React Source: https://github.com/imbhargav5/rooks/blob/main/apps/website/content/docs/hooks/useResizeObserverRef.mdx This example demonstrates the fundamental application of the `useResizeObserverRef` hook. It attaches a ref to a resizable div, and a callback function is executed to increment a counter every time the div's dimensions change. The example also includes buttons to dynamically adjust the position of the parent container, showcasing how the hook responds to layout shifts. ```jsx import { useState } from "react"; import { useResizeObserverRef } from "rooks"; export default function App() { const [resizeCount, setResizeCount] = useState(0); const incrementResizeCount = () => { return setResizeCount(resizeCount + 1); }; const [myRef] = useResizeObserverRef(incrementResizeCount); const [XOffset, setXOffset] = useState(0); const [YOffset, setYOffset] = useState(300); return ( <>

Resize this div as you see fit. To demonstrate that it also updates on child dom nodes resize

Bounds

Resize count: {resizeCount}
); } ``` -------------------------------- ### Basic Usage of usePromise Hook in React Source: https://github.com/imbhargav5/rooks/blob/main/data/docs/usePromise.md This example demonstrates the basic integration of the `usePromise` hook into a React functional component. It imports the hook from 'rooks' and calls it within the `App` component, returning `null` as a placeholder. ```tsx import {usePromise} from "rooks" export default function App() { usePromise(); return null } ``` -------------------------------- ### Using useIntervalWhen Hook for Conditional Intervals - Immediate Callback (JSX) Source: https://github.com/imbhargav5/rooks/blob/main/apps/website/content/docs/hooks/useIntervalWhen.mdx This example illustrates how to use the `useIntervalWhen` hook to fire the callback function immediately when the condition becomes true, without waiting for the first interval duration. It increments a state variable `value` every second, starting instantly when the `when` state is true. ```jsx import { useIntervalWhen } from "rooks"; import { useState } from "react"; function App() { const [value, setValue] = useState(0); const [when, setWhen] = useState(false); useIntervalWhen( () => { setValue(value + 1); }, 1000, // run callback every 1 second when, // start the timer when it's true true // no need to wait for the first interval ); return (

Rooks: useIntervalWhen example

Notice how the first increment is instantly after interval is enabled. If n intervals run, the callback fires n+1 times.

Value: {value}

); } export default App; ``` -------------------------------- ### Handling Basic Keydown Events with useKey (JSX) Source: https://github.com/imbhargav5/rooks/blob/main/data/docs/useKey.md This example demonstrates the basic usage of the `useKey` hook to listen for keydown events. It shows how to attach listeners to the global window object for the 'Enter' key and to a specific input element for both lowercase and uppercase vowels. The `target` option allows specifying the DOM element to listen on. ```jsx import React, { useRef, useState } from "react"; import "./styles.css"; import { useDebounce, useKey, useThrottle, useTimeoutWhen } from "rooks"; export default function App() { const inputRef = useRef(); function windowEnter(e) { console.log("[Demo 1] Enter key was pressed on window"); } function vowelsEntered(e) { console.log("[Demo 1] You typed a vowel"); } function capitalVowelsEntered(e) { console.log("[Demo 1] You typed a capital vowel"); } // window is the target useKey(["Enter"], windowEnter); useKey(["a", "e", "i", "o", "u"], vowelsEntered, { target: inputRef, }); useKey(["A", "E", "I", "O", "U"], capitalVowelsEntered, { target: inputRef, }); return ( <>

Press enter anywhere to trigger a console.log statement

Press a,e,i,o,u in the input to trigger a console.log statement

Press A,E,I,O,U in the input to trigger a different log statement

); } ``` -------------------------------- ### Initializing useSpeech Hook in React Source: https://github.com/imbhargav5/rooks/blob/main/apps/website/content/docs/hooks/useSpeech.mdx This example demonstrates the basic initialization of the `useSpeech` hook within a React functional component. It imports the hook from the 'rooks' library and calls it, returning `null` as the component's render output, indicating it's primarily for side effects. ```TypeScript import {useSpeech} from "rooks" export default function App() { useSpeech(); return null } ``` -------------------------------- ### Basic Usage of useFreshCallback in TypeScript React Source: https://github.com/imbhargav5/rooks/blob/main/apps/website/content/docs/hooks/useFreshCallback.mdx This example demonstrates the basic import and usage of the `useFreshCallback` hook within a React functional component. It shows how to include the hook in an application, although it doesn't illustrate a specific problem it solves. ```tsx import {useFreshCallback} from "rooks" export default function App() { useFreshCallback(); return null } ``` -------------------------------- ### Basic Usage of useDeepCompareEffect Hook in TypeScript Source: https://github.com/imbhargav5/rooks/blob/main/apps/website/content/docs/hooks/useDeepCompareEffect.mdx This example demonstrates the basic usage of the `useDeepCompareEffect` hook from the 'rooks' library. It imports the hook and calls it within a functional component, illustrating its direct application without specific dependencies shown in this minimal example. This hook is useful when you need to trigger an effect only when the deep equality of its dependencies changes. ```TypeScript import {useDeepCompareEffect} from "rooks" export default function App() { useDeepCompareEffect(); return null } ``` -------------------------------- ### Initializing File Drop with useFileDropRef Hook in TypeScript Source: https://github.com/imbhargav5/rooks/blob/main/data/docs/useFileDropRef.md This example demonstrates how to integrate the `useFileDropRef` hook from the 'rooks' library into a React component. The hook simplifies the process of handling file drops on an element, making it easy to add drag-and-drop file upload functionality. It requires the 'rooks' library as a dependency. ```TypeScript import { useFileDropRef } from "rooks"; export default function App() { useFileDropRef(); return null; } ``` -------------------------------- ### Getting Geolocation in a React Component Source: https://github.com/imbhargav5/rooks/blob/main/apps/website/content/docs/hooks/useGeolocation.mdx This snippet demonstrates the basic usage of the `useGeolocation` hook to fetch and display a user's geolocation data immediately when the component mounts. It imports the hook and uses it to get a `geoObj` which is then stringified and rendered in a paragraph. ```jsx import { useGeolocation } from "rooks"; import React from "react"; import { useState } from "react"; function App() { const geoObj = useGeolocation(); return (

Rooks : useGeolocation Example

{geoObj && JSON.stringify(geoObj)}

); } export default App; ``` -------------------------------- ### Setting Conditional Interval with useIntervalWhen (Immediate Start) - React JSX Source: https://github.com/imbhargav5/rooks/blob/main/data/docs/useIntervalWhen.md This example shows how to use the `useIntervalWhen` hook to immediately invoke the callback function when the `when` condition becomes true, without waiting for the first interval duration to pass. It increments a state variable `value` every second, with the first increment happening instantly upon activation. ```jsx import { useIntervalWhen } from "rooks"; import { useState } from "react"; function App() { const [value, setValue] = useState(0); const [when, setWhen] = useState(false); useIntervalWhen( () => { setValue(value + 1); }, 1000, // run callback every 1 second when, // start the timer when it's true true // no need to wait for the first interval ); return (

Rooks: useIntervalWhen example

Notice how the first increment is instantly after interval is enabled. If n intervals run, the callback fires n+1 times.

Value: {value}

); } export default App; ``` -------------------------------- ### Basic useToggle Hook in React Source: https://github.com/imbhargav5/rooks/blob/main/data/docs/useToggle.md This example demonstrates the fundamental usage of the `useToggle` hook without an initial value, defaulting to `false`. It shows how to toggle a boolean state and update the UI accordingly when a button is clicked, displaying 'Yes!' or 'No!' based on the state. ```jsx import \"./styles.css\";\ import { useToggle } from \"rooks\";\ \ export default function App() {\ const [state, toggle] = useToggle();\ \ return (\
\

Rooks: useToggle Example <\/h1>\
<\/br>\

); } ``` -------------------------------- ### Importing Dependencies and Defining Constants in React Source: https://github.com/imbhargav5/rooks/blob/main/apps/website/content/docs/hooks/useKey.mdx This snippet imports all necessary React hooks, utility hooks from the 'rooks' library, 'random' for number generation, 'styled-components' for CSS-in-JS, and 'framer-motion' for animations. It also defines a 'colors' array for styling and an 'Alphabets' array used for key press detection. ```jsx import React, { useRef, useState } from "react"; import "./styles.css"; import { useDebounce, useKey, useThrottle, useTimeoutWhen } from "rooks"; import random from "random"; import styled from "styled-components"; import { AnimatePresence, motion } from "framer-motion"; const colors = [ "#1D4ED8", "#6D28D9", "#92400E", "#047857", "#DC2626", "#374151", "##BE185D" ]; const Alphabets = [ "A", "B", "C", "D", "E", "F", "G", "H", "I", "J", "K", "L", "M", "N", "O", "P", "Q", "R", "S", "T", "U", "V", "W", "X", "Y", "Z" ]; ```