GithubHelp home page GithubHelp logo

isabella232 / text-expander-element Goto Github PK

View Code? Open in Web Editor NEW

This project forked from github/text-expander-element

0.0 0.0 0.0 656 KB

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

Home Page: http://github.github.io/text-expander-element/examples

License: MIT License

JavaScript 50.10% TypeScript 49.90%

text-expander-element's Introduction

<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=": @ #" multiword="#">
  <textarea></textarea>
</text-expander>

Attributes

  • keys is a space separated list of menu activation keys
  • multiword defines whether the expansion should use several words or not
    • you can provide a space separated list of activation keys that should support multi-word matching

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.

text-expander-element's People

Contributors

muan avatar koddsson avatar keithamus avatar dgraham avatar traumverloren avatar lvivski avatar dependabot[bot] avatar

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.