# @yukimoto/use-clamp-text

> This package provide a custome hook that can limit the text in the container in specified line number.

Latest version **2.0.5** (published 2023-03-13) · MIT license · 0 weekly downloads

## Install

```sh
npm install @yukimoto/use-clamp-text
pnpm add @yukimoto/use-clamp-text
yarn add @yukimoto/use-clamp-text
bun add @yukimoto/use-clamp-text
```

## Health

**Score 20/100 (F)** — status: abandoned.

Positive: has types; no vulnerabilities.

Warnings: low downloads; no esm support.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 2.0.5 |
| Published | 2023-03-13 |
| First published | 2023-01-23 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | bundled |
| Module format | CommonJS |
| Dependencies | 2 |
| Unpacked size | 18.6 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 1 |
| Author | Yukimoto |
| Maintainers | yukimoto |
| Keywords | React, Hook, Clamp, Line Number, See more, See less, clamp text, read more, read less |

## Links

- npm: https://www.npmjs.com/package/@yukimoto/use-clamp-text
- Repository: https://github.com/Yukimotochern/useClampText
- Homepage: https://github.com/Yukimotochern/useClampText#readme
- Issues: https://github.com/Yukimotochern/useClampText/issues
- npm.io page: https://npm.io/package/@yukimoto/use-clamp-text

## Dependencies (2)

- [react](https://npm.io/package/react.md) >=16.8.0
- [lodash](https://npm.io/package/lodash.md) ^4.17.21

## Alternatives

- [mobx-react](https://npm.io/package/mobx-react.md) — 2.8M weekly downloads
- [rc-tree](https://npm.io/package/rc-tree.md) — 2.6M weekly downloads
- [@react-oauth/google](https://npm.io/package/@react-oauth/google.md) — 1.3M weekly downloads
- [@wagmi/connectors](https://npm.io/package/@wagmi/connectors.md) — 877.0K weekly downloads
- [vee-validate](https://npm.io/package/vee-validate.md) — 836.4K weekly downloads

## Recent versions

- 2.0.5 (latest) — 2023-03-13
- 2.0.4 — 2023-02-19
- 2.0.3 — 2023-02-19
- 2.0.2 — 2023-02-12
- 2.0.1 — 2023-02-12
- 2.0.0 — 2023-02-12
- 1.0.1 — 2023-01-28
- 1.0.0 — 2023-01-23

## README

# use-clamp-text hook: useClampText

Custom react hook that clamp multiline text to a specified line number. Can optionally specify trailing spaces.
Fix some bugs and use cleaner implementation. Modified from:

- https://github.com/drenther/use-clamp-text/blob/main/src/index.ts
- https://github.com/zoltantothcom/react-clamp-lines/blob/master/src/index.js

## Install

```bash
npm i @yukimoto/use-clamp-text
```

## Usage

```tsx
const component = () => {
  const {
    longEnoughToClamp,
    textContainerRef,
    addOnsContainerRef,
    wrapperContainerRef,
    clamped,
    clampedText,
    toggleClamp,
  } = useClampText<{
    WrapperContainer: HTMLDivElement;
    TextContainer: HTMLSpanElement;
    AddonsContainer: HTMLButtonElement;
  }>({
    originalText: 'some potentially really long text...',
    lines: 2, // At most how many lines
    endSpaceNumber: 4, // how many trailing space,
    unitSpaceChar: '*', // single trailing space should be the width of this char
    debounceTime: 100, // in miliseconds
  });
  return (
    <div ref={wrapperContainerRef}>
      <span ref={textContainerRef}>{clampedText}</span>
      {longEnoughToClamp ? (
        <button ref={addOnsContainerRef} onClick={toggleClamp}>
          {clamped ? '...show more' : '...show less'}
        </button>
      ) : null}
    </div>
  );
};
```

## API reference

### Arguments

| property        | type     | required | default | description                                                                                                                |
| --------------- | -------- | -------- | ------- | -------------------------------------------------------------------------------------------------------------------------- |
| originalText    | `string` | `false`  | `''`    | Text you wish to clamp.                                                                                                    |
| lines           | `number` | `false`  | `2`     | Number of visible lines when collapsed.                                                                                    |
| debounceTime    | `number` | `false`  | `300`   | How many time in miliseconds before the clamp action will be triggered when window resizes.                                |
| minSpaceCharNum | `number` | `false`  | `0`     | The minium trailing space number, will make the process of adding space to the end of the addons component more efficient. |
| unitSpaceChar   | `string` | `false`  | `'.'`   | The width of single trailing space, pass a char for the measurement of width.                                              |
| endSpaceNumber  | `number` | `false`  | `0`     | How many trailing spaces. Each of them will be the width of `unitSpaceChar`.                                               |

### Returns

| property                                                  | type                                     | description                                                                                |
| --------------------------------------------------------- | ---------------------------------------- | ------------------------------------------------------------------------------------------ |
| longEnoughtToClamp                                        | `boolean`                                | Whether the `originalText` is longer enough to exceeds the given `line` number by it self. |
| clampedText                                               | `string`                                 | The string to be rendered.                                                                 |
| clamped                                                   | `boolean`                                | A state indicating whether the text should be clamped if long enough.                      |
| toggleClamp                                               | `() => void`                             | A callback which togger the `clamped` state.                                               |
| wrapperContainerRef, textContainerRef, addOnsContainerRef | `React.RefObject<E extends HTMLElement>` | Ref for corresponding containers.                                                          |

---
_Source: https://npm.io/package/@yukimoto/use-clamp-text · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
