# adaptable-text

> Adapt font size to a specified width using either binary search or linear steps.

Latest version **3.2.1** (published 2024-07-06) · MIT license · 0 weekly downloads

## Install

```sh
npm install adaptable-text
pnpm add adaptable-text
yarn add adaptable-text
bun add adaptable-text
```

## Health

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

Positive: has types; esm support; no vulnerabilities.

Warnings: low downloads.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 3.2.1 |
| Published | 2024-07-06 |
| First published | 2015-04-17 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | bundled |
| Module format | ESM + CommonJS |
| Node | >=22.0.0 |
| Dependencies | 1 |
| Unpacked size | 14.9 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 2 |
| Author | Damien Seguin |
| Maintainers | dmnsgn |
| Keywords | responsive, text, fontsize |

## Links

- npm: https://www.npmjs.com/package/adaptable-text
- Repository: https://github.com/dmnsgn/adaptable-text
- Issues: https://github.com/dmnsgn/adaptable-text/issues
- Funding: https://paypal.me/dmnsgn
- npm.io page: https://npm.io/package/adaptable-text

## Dependencies (1)

- [clamp](https://npm.io/package/clamp.md) ^1.0.1

## Alternatives

- [ext-list](https://npm.io/package/ext-list.md) — 6.3M weekly downloads
- [@lexical/selection](https://npm.io/package/@lexical/selection.md) — 3.8M weekly downloads
- [@lexical/text](https://npm.io/package/@lexical/text.md) — 3.6M weekly downloads
- [@lexical/clipboard](https://npm.io/package/@lexical/clipboard.md) — 3.0M weekly downloads
- [@tiptap/extension-mention](https://npm.io/package/@tiptap/extension-mention.md) — 3.0M weekly downloads

## Recent versions

- 3.2.1 (latest) — 2024-07-06
- 3.2.0 — 2024-01-27
- 3.1.0 — 2021-09-10
- 3.0.1 — 2021-04-11
- 3.0.0 — 2021-04-10
- 2.1.1 — 2018-02-19
- 2.1.0 — 2018-02-16
- 2.0.1 — 2017-09-28
- 0.1.2 — 2015-04-17
- 0.1.1 — 2015-04-17
- 0.1.0 — 2015-04-17

## README

# adaptable-text

[![npm version](https://img.shields.io/npm/v/adaptable-text)](https://www.npmjs.com/package/adaptable-text)
[![stability-stable](https://img.shields.io/badge/stability-stable-green.svg)](https://www.npmjs.com/package/adaptable-text)
[![npm minzipped size](https://img.shields.io/bundlephobia/minzip/adaptable-text)](https://bundlephobia.com/package/adaptable-text)
[![dependencies](https://img.shields.io/librariesio/release/npm/adaptable-text)](https://github.com/dmnsgn/adaptable-text/blob/main/package.json)
[![types](https://img.shields.io/npm/types/adaptable-text)](https://github.com/microsoft/TypeScript)
[![Conventional Commits](https://img.shields.io/badge/Conventional%20Commits-1.0.0-fa6673.svg)](https://conventionalcommits.org)
[![styled with prettier](https://img.shields.io/badge/styled_with-Prettier-f8bc45.svg?logo=prettier)](https://github.com/prettier/prettier)
[![linted with eslint](https://img.shields.io/badge/linted_with-ES_Lint-4B32C3.svg?logo=eslint)](https://github.com/eslint/eslint)
[![license](https://img.shields.io/github/license/dmnsgn/adaptable-text)](https://github.com/dmnsgn/adaptable-text/blob/main/LICENSE.md)

Adapt font size to a specified width using either binary search or linear steps.

Linear steps works best for small adjustments (eg. on resize) while binary search is most efficient when the expected font-size change is big.

[![paypal](https://img.shields.io/badge/donate-paypal-informational?logo=paypal)](https://paypal.me/dmnsgn)
[![coinbase](https://img.shields.io/badge/donate-coinbase-informational?logo=coinbase)](https://commerce.coinbase.com/checkout/56cbdf28-e323-48d8-9c98-7019e72c97f3)
[![twitter](https://img.shields.io/twitter/follow/dmnsgn?style=social)](https://twitter.com/dmnsgn)

## Installation

```bash
npm install adaptable-text
```

## Usage

```js
import AdaptableText from "adaptable-text";

const textToAdapt = new AdaptableText(document.querySelector(".TextToAdapt"), {
  step: 0.1,
  minFontSize: 10,
  width: null,
});
textToAdapt.init();

// Adapt
const adapt = () => {
  textToAdapt.setWidth();
  textToAdapt.adapt();
};

// Listen for a resize event
window.addEventListener("resize", adapt);

// Kick off
adapt(true);
```

## API

<!-- api-start -->

## Classes

<dl>
<dt><a href="#AdaptableText">AdaptableText</a></dt>
<dd></dd>
</dl>

## Typedefs

<dl>
<dt><a href="#Options">Options</a> : <code>object</code></dt>
<dd></dd>
</dl>

<a name="AdaptableText"></a>

## AdaptableText

**Kind**: global class

- [AdaptableText](#AdaptableText)
  - [new AdaptableText(element, [options])](#new_AdaptableText_new)
  - [.init()](#AdaptableText+init)
  - [.setWidth()](#AdaptableText+setWidth)
  - [.adapt([useBinarySearch])](#AdaptableText+adapt)

<a name="new_AdaptableText_new"></a>

### new AdaptableText(element, [options])

Creates an instance of AdaptableText.

| Param     | Type                             |
| --------- | -------------------------------- |
| element   | <code>HTMLElement</code>         |
| [options] | [<code>Options</code>](#Options) |

<a name="AdaptableText+init"></a>

### adaptableText.init()

Initialise the adaptor.

**Kind**: instance method of [<code>AdaptableText</code>](#AdaptableText)
<a name="AdaptableText+setWidth"></a>

### adaptableText.setWidth()

Set the desired width for adaptation from options.width or getBoundingClientRect().width

**Kind**: instance method of [<code>AdaptableText</code>](#AdaptableText)
<a name="AdaptableText+adapt"></a>

### adaptableText.adapt([useBinarySearch])

Adapt font size to a specified width.

**Kind**: instance method of [<code>AdaptableText</code>](#AdaptableText)

| Param             | Type                 |
| ----------------- | -------------------- |
| [useBinarySearch] | <code>boolean</code> |

<a name="Options"></a>

## Options : <code>object</code>

**Kind**: global typedef
**Properties**

| Name          | Type                | Default                       | Description                                                                                                                               |
| ------------- | ------------------- | ----------------------------- | ----------------------------------------------------------------------------------------------------------------------------------------- |
| [step]        | <code>number</code> | <code>0.5</code>              | The step used by the generator to calculate the width of the element.                                                                     |
| [minFontSize] | <code>number</code> | <code>0</code>                | A minimum font size for the element (max would be the size defined in a stylesheet retrieved by `window.getComputedStyle(this.element)`). |
| [width]       | <code>number</code> | <code>&quot;null&quot;</code> | A maximum width for the container.                                                                                                        |

<!-- api-end -->

## License

MIT. See [license file](https://github.com/dmnsgn/adaptable-text/blob/main/LICENSE.md).

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