Activates a suggestion menu to expand text snippets as you type.
Перейти к файлу
Mu-An Chiou bae88b8298
1.1.1
2020-05-12 12:27:42 -04:00
.github/workflows Remove metrics as we only care about the latest 2020-04-29 16:21:53 -04:00
examples Remove browser key and document bundle.js in the readme 2020-04-30 13:00:51 -04:00
src Convert to TypeScript 2020-04-28 18:37:15 -04:00
test Remove browser key and document bundle.js in the readme 2020-04-30 13:00:51 -04:00
.eslintrc.json Convert to TypeScript 2020-04-28 18:37:15 -04:00
.gitignore Extract <text-expander> custom element 2019-06-12 15:56:13 -06:00
.travis.yml Configure Travis builds 2019-06-12 16:16:19 -06:00
LICENSE Extract <text-expander> custom element 2019-06-12 15:56:13 -06:00
README.md Remove browser key and document bundle.js in the readme 2020-04-30 13:00:51 -04:00
package-lock.json 1.1.1 2020-05-12 12:27:42 -04:00
package.json 1.1.1 2020-05-12 12:27:42 -04:00
rollup.config.js Remove browser key and document bundle.js in the readme 2020-04-30 13:00:51 -04:00
rollup.config.test.js Build module file for test as well 2020-04-29 16:43:43 -04:00
tsconfig.json Convert to TypeScript 2020-04-28 18:37:15 -04:00

README.md

<text-expander> element

Activates a suggestion menu to expand text snippets as you type.

Installation

$ npm install --save @github/text-expander-element

Usage

Script

Import as ES modules:

import '@github/text-expander-element'

With a script tag:

<script type="module" src="./node_modules/@github/text-expander-element/dist/bundle.js">

Markup

<text-expander keys=": @ #">
  <textarea></textarea>
</text-expander>

Attributes

  • keys is a space separated list of menu activation keys

Events

text-expander-change is fired when a key is matched. In event.detail you can find:

  • key: The matched key; for example: :.
  • text: The matched text; for example: cat, for :cat.
  • provide: A function to be called when you have the menu results. Takes a Promise with {matched: boolean, fragment: HTMLElement} where matched tells the element whether a suggestion is available, and fragment is the menu content to be displayed on the page.
const expander = document.querySelector('text-expander')

expander.addEventListener('text-expander-change', function(event) {
  const {key, provide, text} = event.detail
  if (key !== ':') return

  const suggestions = document.querySelector('.emoji-suggestions').cloneNode(true)
  suggestions.hidden = false
  for (const suggestion of suggestions.children) {
    if (!suggestion.textContent.match(text)) {
      suggestion.remove()
    }
  }
  provide(Promise.resolve({matched: suggestions.childElementCount > 0, fragment: suggestions}))
})

The returned fragment should be consisted of filtered [role=option] items to be selected. For example:

<ul class="emoji-suggestions" hidden>
  <li role="option" data-value="🐈">🐈 :cat2:</li>
  <li role="option" data-value="🐕">🐕 :dog:</li>
</ul>

text-expander-value is fired when an item is selected. In event.detail you can find:

  • key: The matched key; for example: :.
  • item: The selected item. This would be one of the [role=option]. Use this to work out the value.
  • value: A null value placeholder to replace the query. To replace the text query, simply re-assign this value.
const expander = document.querySelector('text-expander')

expander.addEventListener('text-expander-value', function(event) {
  const {key, item}  = event.detail
  if (key === ':') {
    event.detail.value = item.getAttribute('data-value')
  }
})

Browser support

Browsers without native custom element support require a polyfill.

  • Chrome
  • Firefox
  • Safari
  • Microsoft Edge

Development

npm install
npm test

License

Distributed under the MIT license. See LICENSE for details.