Activates a suggestion menu to expand text snippets as you type.
Перейти к файлу
dependabot[bot] 3a74d317a3
Bump the npm_and_yarn group with 4 updates
Bumps the npm_and_yarn group with 4 updates: [body-parser](https://github.com/expressjs/body-parser), [nanoid](https://github.com/ai/nanoid), [mocha](https://github.com/mochajs/mocha) and [qs](https://github.com/ljharb/qs).


Updates `body-parser` from 1.19.0 to 1.20.3
- [Release notes](https://github.com/expressjs/body-parser/releases)
- [Changelog](https://github.com/expressjs/body-parser/blob/master/HISTORY.md)
- [Commits](https://github.com/expressjs/body-parser/compare/1.19.0...1.20.3)

Removes `nanoid`

Updates `mocha` from 8.3.2 to 10.7.3
- [Release notes](https://github.com/mochajs/mocha/releases)
- [Changelog](https://github.com/mochajs/mocha/blob/main/CHANGELOG.md)
- [Commits](https://github.com/mochajs/mocha/compare/v8.3.2...v10.7.3)

Updates `qs` from 6.7.0 to 6.13.0
- [Changelog](https://github.com/ljharb/qs/blob/main/CHANGELOG.md)
- [Commits](https://github.com/ljharb/qs/compare/v6.7.0...v6.13.0)

---
updated-dependencies:
- dependency-name: body-parser
  dependency-type: indirect
  dependency-group: npm_and_yarn
- dependency-name: nanoid
  dependency-type: indirect
  dependency-group: npm_and_yarn
- dependency-name: mocha
  dependency-type: direct:development
  dependency-group: npm_and_yarn
- dependency-name: qs
  dependency-type: indirect
  dependency-group: npm_and_yarn
...

Signed-off-by: dependabot[bot] <support@github.com>
2024-09-11 07:41:16 +00:00
.devcontainer Update other dependencies & test files 2024-05-09 16:55:49 +00:00
.github/workflows Update publish.yml 2023-11-14 13:03:59 +00:00
examples Add floating menu to example 2024-05-22 20:44:20 +00:00
src Delete log statement 2024-05-23 19:32:11 +00:00
test Update other dependencies & test files 2024-05-09 16:55:49 +00:00
.eslintrc.json Set eslintrc file as root 2021-04-12 13:56:43 +01: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
CODEOWNERS move AOR to primer 2022-09-23 18:49:53 +01:00
LICENSE Extract <text-expander> custom element 2019-06-12 15:56:13 -06:00
README.md add toplayer support, plus active/deactivate events 2023-11-14 12:31:03 +00:00
package-lock.json Bump the npm_and_yarn group with 4 updates 2024-09-11 07:41:16 +00:00
package.json Bump the npm_and_yarn group with 4 updates 2024-09-11 07:41:16 +00: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=": @ #" 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
  • suffix is a string that is appended to the value during expansion, default is a single space character

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.
    • If the key is specified in the multiword attribute then the matched text can contain multiple words; for example cat and dog for :cat and dog.
  • 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.
  • continue: A boolean value to specify whether to continue autocompletion after inserting a value. Defaults to false. If set to true, will not add a space after inserted value and will keep firing the text-expander-change event.
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')
  }
})

text-expander-committed is fired after the underlying input value has been updated in the DOM. In event.detail you can find:

  • input: The HTMLInputElement or HTMLTextAreaElement that just had value changes committed to the DOM.
const expander = document.querySelector('text-expander')

expander.addEventListener('text-expander-committed', function(event) {
  const {input}  = event.detail
})

text-expander-activate is fired just after the menu has been assigned and appended to the DOM, and just before it is about to be positioned near the text to expand. This is useful for assigning classes or calling imperative methods to show the menu, such as .showPopover().

text-expander-dectivate is fired just before the menu is goind to be unassigned and removed from the DOM. This is useful for removing classes or running cleanup like removing from caches.

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.