Ecosyste.ms: Awesome

An open API service indexing awesome lists of open source software.

Awesome Lists | Featured Topics | Projects

https://github.com/yuanqing/charming

:tophat: Lettering.js in vanilla JavaScript
https://github.com/yuanqing/charming

dom javascript typography

Last synced: 25 days ago
JSON representation

:tophat: Lettering.js in vanilla JavaScript

Awesome Lists containing this project

README

        

# charming [![npm Version](https://badgen.net/npm/v/charming)](https://www.npmjs.org/package/charming) [![Build Status](https://badgen.net/travis/yuanqing/charming?label=build)](https://travis-ci.org/yuanqing/charming) [![Coverage Status](https://badgen.net/coveralls/c/github/yuanqing/charming)](https://coveralls.io/r/yuanqing/charming) [![Bundle Size](https://badgen.net/bundlephobia/minzip/charming)](https://bundlephobia.com/result?p=charming)

> [Lettering.js](https://github.com/davatron5000/Lettering.js) in vanilla JavaScript

- Supports changing the tag name or class name of the inserted DOM elements
- Supports controlling how the contents of the element are wrapped

## Usage

> [**Editable demo (CodePen)**](https://codepen.io/lyuanqing/pen/YeYdrm)

HTML:

```html

foo


```

JavaScript:

```js
const charming = require('charming')

const element = document.querySelector('h1')
charming(element)
```

Boom:

```html


f
o
o


```

- Charming also works when the given element contains other (possibly nested) DOM elements; any character that is inside a [text node](https://developer.mozilla.org/en-US/docs/Web/API/Text) in the given element will be wrapped.
- For accessibility, Charming adds an `aria-label` attribute on the given element and `aria-hidden` attributes on each of the inserted DOM elements.

## API

```js
const charming = require('charming')
```

### charming(element [, options])

- `element` is a DOM element
- `options` is an optional configuration object

Use `options.tagName` to change the tag name of the wrapper element:

```js
charming(element, {
tagName: 'b'
})
```

Use `options.setClassName` to change the class name on each wrapper element:

```js
charming(element, {
setClassName: function (index, letter) {
return `index-${index} letter-${letter}`
}
})
```

Use `options.split` to control how the contents of the element are wrapped:

```js
charming(element, {
split: function (string) {
return string.split(/(\s+)/)
},
setClassName: function (index) {
return `word-${index}`
}
})
```

## Installation

```sh
$ yarn add charming
```

## License

[MIT](LICENSE.md)