All Projects β†’ chrisjpatty β†’ Crooks

chrisjpatty / Crooks

Licence: mit
A collection of eclectic react hooks

Programming Languages

javascript
184084 projects - #8 most used programming language

Projects that are alternatives of or similar to Crooks

tacklebox
🎣React UX components for handling common interactions
Stars: ✭ 15 (-92.02%)
Mutual labels:  hooks, collection
Use Wallet
πŸ‘› useWallet() Β· All-in-one solution to connect a dapp to an Ethereum provider.
Stars: ✭ 182 (-3.19%)
Mutual labels:  hooks
React Use Wizard
πŸ§™ A React wizard (stepper) builder without the hassle, powered by hooks.
Stars: ✭ 162 (-13.83%)
Mutual labels:  hooks
Awesome Virtual Try On
A curated list of awesome research papers, projects, code, dataset, workshops etc. related to virtual try-on.
Stars: ✭ 175 (-6.91%)
Mutual labels:  collection
Zent
A collection of essential UI components written with React.
Stars: ✭ 2,133 (+1034.57%)
Mutual labels:  hooks
Useworker
βš›οΈ useWorker() - A React Hook for Blocking-Free Background Tasks
Stars: ✭ 2,233 (+1087.77%)
Mutual labels:  hooks
Fpgo
Monad, Functional Programming features for Golang
Stars: ✭ 165 (-12.23%)
Mutual labels:  collection
Beautiful React Diagrams
πŸ’Ž A collection of lightweight React components and hooks to build diagrams with ease πŸ’Ž
Stars: ✭ 2,326 (+1137.23%)
Mutual labels:  hooks
Appenv Kotlin
a xposed model -> AppEnv
Stars: ✭ 183 (-2.66%)
Mutual labels:  hooks
React Hooks In Svelte
React hook examples ported to Svelte
Stars: ✭ 176 (-6.38%)
Mutual labels:  hooks
Xresources Themes
A big (huge) collection of rxvt / xterm terminal themes
Stars: ✭ 174 (-7.45%)
Mutual labels:  collection
Deploy
Ansible role to deploy scripting applications like PHP, Python, Ruby, etc. in a capistrano style
Stars: ✭ 2,141 (+1038.83%)
Mutual labels:  hooks
Ansible Collection Hardening
This Ansible collection provides battle tested hardening for Linux, SSH, nginx, MySQL
Stars: ✭ 2,543 (+1252.66%)
Mutual labels:  collection
React Hooks Mobx State Tree
React Hooks + MobX State Tree + TypeScript = πŸ’›
Stars: ✭ 169 (-10.11%)
Mutual labels:  hooks
Shokoserver
Repository for Shoko Server.
Stars: ✭ 184 (-2.13%)
Mutual labels:  collection
Simpler State
The simplest app state management for React
Stars: ✭ 68 (-63.83%)
Mutual labels:  hooks
Polybar Collection
Beautiful collection of Polybar themes
Stars: ✭ 172 (-8.51%)
Mutual labels:  collection
Server Push Hooks
πŸ”₯ React hooks for Socket.io, SEE, WebSockets and more to come
Stars: ✭ 176 (-6.38%)
Mutual labels:  hooks
Asciidoctor Skins
Control how your asciidoctor powered documentation looks
Stars: ✭ 185 (-1.6%)
Mutual labels:  collection
React Form
βš›οΈ Hooks for managing form state and validation in React
Stars: ✭ 2,270 (+1107.45%)
Mutual labels:  hooks

Crooks

crooks Travis (.org) Coverage Status code style: prettier

A collection of useful react hooks by @chrisjpatty.

Installing

yarn add crooks
npm install crooks --save

Available Hooks

useLocalStorage

useLocalStorage behaves just like the native react useState hook, except that any and all state updates are automatically saved in the browser's localstorage under the provided key. The first argument is the name of the key to save it under, and the second argument is the initial value. The hook returns the current state and an updater function just like useState.

When the app reloads, the hook will first look for a previously cached value. If one is found, it will be used as the initial value instead of the provided initial value.

import { useLocalStorage } from 'crooks'

const App = () => {
  const [state, setState] = useLocalStorage("LOCAL_STORAGE_KEY", initialValue)

  return (
    <div>App</div>
  )
}

useFiler

useFiler manages a simple virtual file system using the browser's localstorage. This is especially useful for quick prototyping. Any type of data can be saved in a file provided that it's JSON-serializable.

Basic Usage

When the hook is first initialized it returns the files as an empty object.

import { useFiler } from 'crooks'

const App = () => {
  const [files, {add, remove, update, clear}] = useFiler("LOCAL_STORAGE_KEY")

  return (
    <div>App</div>
  )
}

File Structure

By default, each file has an automatically generated id generated using the shortid package. Each single file is structured as follows:

{
  id: "ogn41na",
  created: 489108491,
  modified: 489108561,
  data: "The file's data."
}

The files object returned as the first parameter of the hook represents all of the current files as an object, with each file's ID as the key, and the file as the value.

Adding Files

The first parameter of the add function may be any JSON-serializable data and is required. The data will be saved as a new file with an automatically generated ID. If you would like to override the automatically-generated ID, you may pass a String or Int as the second parameter and it will be used as the ID. If the ID already exists, the existing file will be overwritten.

add("Any JSON-serializable data to be saved as a new file.")

Updating Files

To update a file, pass as the first parameter, the ID of the file you want to update. The second parameter is the data you want to overwrite the file with.

update("jal31af", "The new data to overwrite the file with.")

As with the native useState, update() accepts a callback function injected with the previous file.

update("jal31af", file => ([...file.data, "New item"]))

Removing Files

The remove function simply accepts a file ID of the file you wish to remove.

remove("zoep31a")

Clearing all Files

clear()

useKeyboardShortcut

The useKeyboardShortcut hook listens to "keydown" events on the Document, and will call the provided action when the specified Javascript keyCode is detected. The shortcut listener is enabled by default, but can be declaratively disabled by passing disabled: true to the hook.

keyboard.info is a great resource for finding Javascript keyCodes.

Basic Usage

import { useKeyboardShortcut } from 'crooks'

const App = () => {
  const submit = () => {
    console.log('Submitted')
  }

  const {enable, disable} = useKeyboardShortcut({
    keyCode: 13,
    action: submit,
    disabled: false // This key is not required
  })

  return (
    <div>
      <button onClick={enable}>Enable</button>
      <button onClick={disable}>Disable</button>
    </div>
  )
}

With keyboard shortcuts, there are times when you may want to imperatively enable or disable the shortcut listener. For these occasions, the hook returns enable and disable functions.

useOnClickOutside

useOnClickOutside accepts a function that will be called when there's a click outside of a target element. The hook returns a ref, which you pass to the ref attribute of the element you want to target.

Basic Usage

import { useOnClickOutside } from 'crooks'

const App = () => {
  const handleClickOutside = () => {
    console.log("You clicked outside of the blue box")
  }

  const outsideRef = useOnClickOutside(handleClickOutside)

  return (
    <div>
      <div ref={outsideRef}> I'm a blue box </div>
    </div>
  )
}

Disabling the listener

For performance reasons, you may not want to always listen for clicks outside of an element. For these times you can pass a Boolean as a second parameter to this hook representing whether or not the listener should be disabled like so:

import { useState } from 'react'
import { useOnClickOutside } from 'crooks'

const App = () => {
  const [isDisabled, setIsDisabled] = useState(false)

  const disableOnOutside = () => setIsDisabled(true)

  const handleClickOutside = () => {
    console.log("You clicked outside of the blue box")
  }

  const outsideRef = useOnClickOutside(handleClickOutside, isDisabled)

  return (
    <div>
      <button onClick={disableOnOutside}>Stop listening for outside clicks</button>
      <div ref={outsideRef}> I'm a blue box </div>
    </div>
  )
}
Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].