Skip to content

Commit

Permalink
Make useClassyInk hook public (#26)
Browse files Browse the repository at this point in the history
  • Loading branch information
DaniGuardiola authored Nov 8, 2023
1 parent 5b91ae2 commit 614b8c1
Show file tree
Hide file tree
Showing 6 changed files with 21 additions and 6 deletions.
5 changes: 5 additions & 0 deletions .changeset/six-schools-appear.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"classy-ink": minor
---

Make the useClassyInk hook public for advanced use cases
15 changes: 12 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -307,18 +307,27 @@ All colors except `gray` also have a "bright" equivalent named `<color>-bright`

## Custom usage

If you have some kind of custom use case, you can use the `compileClass` function directly. This function takes a class string and returns an object with the corresponding Ink props.
If you have some kind of custom use case, you can use the `useClassyInk` hook or the `compileClass` function directly.

Both take a class string and return an object with the corresponding Ink props. The hook wraps the function and adds memoization and caching logic on top.

Unless there's a good reason to do otherwise, the hook is recommended over the function.

```tsx
// note: incomplete example for illustration purposes
import { compileClass } from "classy-ink";
import { Box } from "ink";

function MyCustomBox({ class: className, ...props }) {
return <Box {...useClassyInk(className)} {...props} />;
}

// or

const inkProps = compileClass("border border-red");
<Box {...inkProps} />;
```

Note that this function does not have some optimizations that are built-in to the `Box` and `Text` components, like memoization and (optional) global cache. Use at your own risk.

## <a name='Contributing'></a>Contributing

Install [`bun`](https://bun.sh/) and install dependencies with `bun i`.
Expand Down
2 changes: 1 addition & 1 deletion src/components/Box.tsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { Box as InkBox, type DOMElement } from "ink";
import { type ComponentPropsWithoutRef, forwardRef } from "react";

import { useClassyInk } from "./useClassyInk.js";
import { useClassyInk } from "../lib/useClassyInk.js";

export type BoxProps = ComponentPropsWithoutRef<typeof InkBox> & {
class?: string;
Expand Down
2 changes: 1 addition & 1 deletion src/components/Text.tsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { Text as InkText } from "ink";
import { type ComponentPropsWithoutRef } from "react";

import { useClassyInk } from "./useClassyInk.js";
import { useClassyInk } from "../lib/useClassyInk.js";

export type TextProps = ComponentPropsWithoutRef<typeof InkText> & {
class?: string;
Expand Down
1 change: 1 addition & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
export * from "./components/index.js";
export * from "./lib/compile-class.js";
export * from "./lib/useClassyInk.js";
2 changes: 1 addition & 1 deletion src/components/useClassyInk.ts → src/lib/useClassyInk.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import { useMemo } from "react";

import { useClassyInkContext } from "../components/context.js";
import { compileClass } from "../index.js";
import { type Props } from "../shared.js";
import { useClassyInkContext } from "./context.js";

export function useClassyInk(className?: string): Props {
const { cache } = useClassyInkContext();
Expand Down

0 comments on commit 614b8c1

Please sign in to comment.