Giter Site home page Giter Site logo

charming's Introduction

charming npm Version Build Status Coverage Status Bundle Size

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)

HTML:

<h1>foo</h1>

JavaScript:

const charming = require('charming')

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

Boom:

<h1 aria-label="foo">
  <span class="char1" aria-hidden="true">f</span>
  <span class="char2" aria-hidden="true">o</span>
  <span class="char3" aria-hidden="true">o</span>
</h1>
  • Charming also works when the given element contains other (possibly nested) DOM elements; any character that is inside a text node 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

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:

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

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

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

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

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

Installation

$ yarn add charming

License

MIT

charming's People

Contributors

mburakerman avatar uroybd avatar yuanqing avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

charming's Issues

Splitting text nodes into words

Hey, thanks for making this! There doesn't seem to be an option, but I thought I would ask. Can you wrap words with spans similar to .lettering('words') in Lettering.js?

Letter name/callback for class prefix

For styling individual/particular letters, it would be great if the wrapper elements have
a class for each letter, e.g. letter-W for all capital Ws.

use inline-block to enable css transforms

I suggest to use inline-block in order to enable the use of css transformations.
when settings the span elements zu inline block via css all the blank spaces disappear.

Split by letters break words

Splitting long phrases letters break-words in wrong manner e.g:

Hello
World

Hello Wo
rld

Any ideas to fix this? wrap each word with a div?

Split emoji

emoji is splited, can we use "runes" by default for split ?

codepen issue

Kindly take a look at the codepen demo, for whatever reason it doesn't seem to work!

Word split removes spacing

When I use

charming(element, {
  splitRegex: /(\s+)/,
  classPrefix: 'word'
})

to split words, spaces are removed. It looks like they are still in DOM, but aren't shown.

Is there a way to split words and keep spaces ?

splitRegex doesn't seem to work with japanese based regex

Hello

thank you for this cool library!

I am trying to split a Japanese text 2 characters at a time with the option splitRegx, but it doesn't seem to work...

I think my regex is fine, but I am not Japanese so I may be wrong somewhere...

Any clues?

here is the regex
/[\uD800-\uDBFF][\uDC00-\uDFFF]|[\s\S]{2}/
the test

console.log("過去顔ここコカ".match(/[\uD800-\uDBFF][\uDC00-\uDFFF]|[\s\S]{2}/));
// 過去

my HTML code

<div class="prBox prBox2" id="js-prBox2">
          <p class="js-txtNode is-hidden">営業資料を作成する時、ブログの記事を書く時、<br>
          学術論文を執筆する時、判断や選択を迫られる時。</p>
          <p class="js-txtNode is-hidden">論理的な思考にはそれを裏付けるデータが欲しいです。<br>
          新たな発想を得るには見たことのないデータが必要です。</p>
          <p class="prTxtBig js-txtNode is-hidden">「マーケティングデータをアイディア化する」</p>
          <p class="js-txtNode is-hidden">互いにデータを開放することで新たな発見が生まれ、新たな視点で次のリサーチが始まります。<br>そうして集まった過去のアーカイブから簡単に自分のリサーチが作れるような仕組みをつくります。<br>そしてリサーチが誰でも気軽に実施できるツールとなり、</p>
          <p class="prTxtBig js-txtNode is-hidden">たくさんのアイディアが具現化して行きます。</p>
          <p class="js-txtNode is-hidden">データが開放された世界で何が起こるか、一緒に体験してください。</p>
</div>

and How I am instancing charming

  function setCharming() {
    return new Promise(function(resolve, reject) {
      var heroTxt = Array.from(document.querySelectorAll('.js-txtNode'));
      heroTxt.map(function (txt) {
        charming(txt, {
          // splitRegex: /(\u4e00\u9faf{4})/,
          // splitRegex: /^([^\.]+).([^\.]+)$/,
          classRefix: false
        });
        setTimeout(resolve , 100);
      });
    }); 
  }

How to contribute?

Hello, I am excited that someone try to create the vanilla version of Lettering Js, actually I want to contribute to this but I don't know how to do it in this repository.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.