Skip to content

Commit

Permalink
refactor: move hooks to packages (#6458)
Browse files Browse the repository at this point in the history
* feat: add use-size hook

* chore: update lockfile

* chore: deprecate use dimensions

* chore: add storybook for use size

* fix: update hook deps

* refactor: move more hooks to specific package
  • Loading branch information
segunadebayo committed Aug 10, 2022
1 parent 04ff824 commit 70872f2
Show file tree
Hide file tree
Showing 14 changed files with 319 additions and 0 deletions.
7 changes: 7 additions & 0 deletions .changeset/thirty-spiders-pay.md
@@ -0,0 +1,7 @@
---
"@chakra-ui/react-use-callback-ref": patch
"@chakra-ui/react-use-controllable-state": patch
"@chakra-ui/react-context": patch
---

Initial release
24 changes: 24 additions & 0 deletions hooks/context/README.md
@@ -0,0 +1,24 @@
# @chakra-ui/react-use-callback-ref

A Quick description of the component

> This is an internal utility, not intended for public usage.
## Installation

```sh
yarn add @chakra-ui/react-use-callback-ref
# or
npm i @chakra-ui/react-use-callback-ref
```

## Contribution

Yes please! See the
[contributing guidelines](https://github.com/chakra-ui/chakra-ui/blob/master/CONTRIBUTING.md)
for details.

## Licence

This project is licensed under the terms of the
[MIT license](https://github.com/chakra-ui/chakra-ui/blob/master/LICENSE).
1 change: 1 addition & 0 deletions hooks/context/index.ts
@@ -0,0 +1 @@
export * from "./src"
36 changes: 36 additions & 0 deletions hooks/context/package.json
@@ -0,0 +1,36 @@
{
"name": "@chakra-ui/react-context",
"version": "1.0.0",
"description": "",
"keywords": [
"react-use-callback-ref"
],
"author": "Segun Adebayo <sage@adebayosegun.com>",
"homepage": "https://github.com/chakra-ui/chakra-ui#readme",
"license": "MIT",
"main": "dist/index.cjs.js",
"module": "dist/index.esm.js",
"types": "dist/index.d.ts",
"sideEffects": false,
"files": [
"dist"
],
"publishConfig": {
"access": "public"
},
"repository": {
"type": "git",
"url": "git+https://github.com/chakra-ui/chakra-ui.git",
"directory": "packages/react-use-callback-ref"
},
"bugs": {
"url": "https://github.com/chakra-ui/chakra-ui/issues"
},
"scripts": {
"build": "tsup src/index.ts --format=esm,cjs --dts",
"dev": "pnpm build -- --watch",
"clean": "rimraf dist .turbo",
"typecheck": "tsc --noEmit",
"build:fast": "tsup src/index.ts --format=esm,cjs"
}
}
49 changes: 49 additions & 0 deletions hooks/context/src/index.ts
@@ -0,0 +1,49 @@
import {
createContext as createReactContext,
useContext as useReactContext,
} from "react"

export interface CreateContextOptions {
strict?: boolean
hookName?: string
providerName?: string
name?: string
}

export type CreateContextReturn<T> = [
React.Provider<T>,
() => T,
React.Context<T>,
]

function getErrorMessage(hook: string, provider: string) {
return `${hook} returned \`undefined\`. Seems you forgot to wrap component within the ${provider}`
}

export function createContext<T>(options: CreateContextOptions = {}) {
const {
strict = true,
hookName = "useContext",
providerName = "Provider",
name,
} = options

const Context = createReactContext<T | undefined>(undefined)

Context.displayName = name

function useContext() {
const context = useReactContext(Context)

if (!context && strict) {
const error = new Error(getErrorMessage(hookName, providerName))
error.name = "ContextError"
Error.captureStackTrace?.(error, useContext)
throw error
}

return context
}

return [Context.Provider, useContext, Context] as CreateContextReturn<T>
}
24 changes: 24 additions & 0 deletions hooks/use-callback-ref/README.md
@@ -0,0 +1,24 @@
# @chakra-ui/react-use-callback-ref

A Quick description of the component

> This is an internal utility, not intended for public usage.
## Installation

```sh
yarn add @chakra-ui/react-use-callback-ref
# or
npm i @chakra-ui/react-use-callback-ref
```

## Contribution

Yes please! See the
[contributing guidelines](https://github.com/chakra-ui/chakra-ui/blob/master/CONTRIBUTING.md)
for details.

## Licence

This project is licensed under the terms of the
[MIT license](https://github.com/chakra-ui/chakra-ui/blob/master/LICENSE).
1 change: 1 addition & 0 deletions hooks/use-callback-ref/index.ts
@@ -0,0 +1 @@
export * from "./src"
36 changes: 36 additions & 0 deletions hooks/use-callback-ref/package.json
@@ -0,0 +1,36 @@
{
"name": "@chakra-ui/react-use-callback-ref",
"version": "1.0.0",
"description": "",
"keywords": [
"react-use-callback-ref"
],
"author": "Segun Adebayo <sage@adebayosegun.com>",
"homepage": "https://github.com/chakra-ui/chakra-ui#readme",
"license": "MIT",
"main": "dist/index.cjs.js",
"module": "dist/index.esm.js",
"types": "dist/index.d.ts",
"sideEffects": false,
"files": [
"dist"
],
"publishConfig": {
"access": "public"
},
"repository": {
"type": "git",
"url": "git+https://github.com/chakra-ui/chakra-ui.git",
"directory": "packages/react-use-callback-ref"
},
"bugs": {
"url": "https://github.com/chakra-ui/chakra-ui/issues"
},
"scripts": {
"build": "tsup src/index.ts --format=esm,cjs --dts",
"dev": "pnpm build -- --watch",
"clean": "rimraf dist .turbo",
"typecheck": "tsc --noEmit",
"build:fast": "tsup src/index.ts --format=esm,cjs"
}
}
13 changes: 13 additions & 0 deletions hooks/use-callback-ref/src/index.ts
@@ -0,0 +1,13 @@
import { useEffect, useMemo, useRef } from "react"

export function useCallbackRef<T extends (...args: any[]) => any>(
callback: T | undefined,
) {
const callbackRef = useRef(callback)

useEffect(() => {
callbackRef.current = callback
})

return useMemo<T>((...args) => callbackRef.current?.(...args), [])
}
24 changes: 24 additions & 0 deletions hooks/use-controllable-state/README.md
@@ -0,0 +1,24 @@
# @chakra-ui/react-use-callback-ref

A Quick description of the component

> This is an internal utility, not intended for public usage.
## Installation

```sh
yarn add @chakra-ui/react-use-callback-ref
# or
npm i @chakra-ui/react-use-callback-ref
```

## Contribution

Yes please! See the
[contributing guidelines](https://github.com/chakra-ui/chakra-ui/blob/master/CONTRIBUTING.md)
for details.

## Licence

This project is licensed under the terms of the
[MIT license](https://github.com/chakra-ui/chakra-ui/blob/master/LICENSE).
1 change: 1 addition & 0 deletions hooks/use-controllable-state/index.ts
@@ -0,0 +1 @@
export * from "./src"
39 changes: 39 additions & 0 deletions hooks/use-controllable-state/package.json
@@ -0,0 +1,39 @@
{
"name": "@chakra-ui/react-use-controllable-state",
"version": "1.0.0",
"description": "",
"keywords": [
"react-use-callback-ref"
],
"author": "Segun Adebayo <sage@adebayosegun.com>",
"homepage": "https://github.com/chakra-ui/chakra-ui#readme",
"license": "MIT",
"main": "dist/index.cjs.js",
"module": "dist/index.esm.js",
"types": "dist/index.d.ts",
"sideEffects": false,
"files": [
"dist"
],
"publishConfig": {
"access": "public"
},
"repository": {
"type": "git",
"url": "git+https://github.com/chakra-ui/chakra-ui.git",
"directory": "packages/react-use-callback-ref"
},
"bugs": {
"url": "https://github.com/chakra-ui/chakra-ui/issues"
},
"scripts": {
"build": "tsup src/index.ts --format=esm,cjs --dts",
"dev": "pnpm build -- --watch",
"clean": "rimraf dist .turbo",
"typecheck": "tsc --noEmit",
"build:fast": "tsup src/index.ts --format=esm,cjs"
},
"dependencies": {
"@chakra-ui/react-use-callback-ref": "workspace:*"
}
}
52 changes: 52 additions & 0 deletions hooks/use-controllable-state/src/index.ts
@@ -0,0 +1,52 @@
import { useCallback, useMemo, useState } from "react"
import { useCallbackRef } from "@chakra-ui/react-use-callback-ref"

export function useControllableProp<T>(prop: T | undefined, state: T) {
const controlled = typeof prop !== "undefined"
const value = controlled ? prop : state
return useMemo<[boolean, T]>(() => [controlled, value], [controlled, value])
}

export interface UseControllableStateProps<T> {
value?: T
defaultValue?: T | (() => T)
onChange?: (value: T) => void
shouldUpdate?: (prev: T, next: T) => boolean
}

export function useControllableState<T>(props: UseControllableStateProps<T>) {
const {
value: valueProp,
defaultValue,
onChange,
shouldUpdate = (prev, next) => prev !== next,
} = props

const onChangeProp = useCallbackRef(onChange)
const shouldUpdateProp = useCallbackRef(shouldUpdate)

const [uncontrolledState, setUncontrolledState] = useState(defaultValue as T)

const controlled = valueProp !== undefined
const value = controlled ? valueProp : uncontrolledState

const setValue = useCallback(
(next: React.SetStateAction<T>) => {
const setter = next as (prevState?: T) => T
const nextValue = typeof next === "function" ? setter(value) : next

if (!shouldUpdateProp(value, nextValue)) {
return
}

if (!controlled) {
setUncontrolledState(nextValue)
}

onChangeProp(nextValue)
},
[controlled, onChangeProp, value, shouldUpdateProp],
)

return [value, setValue] as [T, React.Dispatch<React.SetStateAction<T>>]
}
12 changes: 12 additions & 0 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 comment on commit 70872f2

@vercel
Copy link

@vercel vercel bot commented on 70872f2 Aug 10, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.