npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

react-copilot-autocomplete

v0.0.13

Published

Copilot inspired autocomplete textarea for React

Downloads

99

Readme

React copilot autocomplete

This is a copilot like autosuggest in react based on native html input or textarea (not on content editable). All props are passed directly to the elements (including ref, className and style) so this component should be compatible with any form handling. Use Tab or click/tap the textarea to autocomplete the current suggestion.

Live preview available at https://jankor.github.io/react-copilot-autocomplete/

You can pass a dictionary list and word autocompletion will work out of the box based on Trie index lookup:

react-copilot-autocomplete

Please install trie peer dependency if you are using the built in autocomplete:

npm i trie-typed --save
<Autocomplete as="textarea" words={['New York', 'London', 'Berlin', 'Hong Kong']}/>

You can also pass your own suggestion function and ignore the built-in word completion. Keep in mind that this way you will have to handle:

  • Debouncing and throttling
  • Api race conditions
  • Correctly slice/discard currentSuggestion as user types

react-copilot-autocomplete

<Autocomplete 
  as="textarea"
  handleCompletion={async ({value, currentSuggestion, setSuggestion, onChangeEvent}) => {
  const suggestion = await getAIPoweredSuggestion(value);
  if (suggestion !== currentSuggestion) {
    setSuggestion(suggestion);
  }
}}/>

You can also pass your input component that will be enhanced with autocomplete. For example an input from UI library:

import { TextField } from '@radix-ui/themes';

const EnhancedTextField = forwardRef<AutocompleteInputRef>((props, fwdRef) => 
  <TextField.Root size="2" {...props} ref={fwdRef} placeholder="Get your city" />
);

<Autocomplete
  asChild
  words={['New York', 'London', 'Berlin', 'Hong Kong']}
  styles={{suggestion: {top: '1px', left: '1px', height: '29.500px', opacity: 0.8}}}>
  <EnhancedTextField />
</Autocomplete>

Configurable Props

Note: All props are optional but if you don't pass either dictionary or handleCompletion there will be nothing to suggest.

as : string - input | textarea

Default value: input

Select the html element to render: input or textarea are supported. Cannot be used in combination with asChild.

asChild : boolean

Default value: false

Change the default rendered element for the one passed as a child, merging their props and behavior. Use this when you want to pass custom input or textarea (eg one from a UI library). Keep in mind to spread props and forward ref of the custom element. You can learn more about the concept from radix documentation: https://www.radix-ui.com/primitives/docs/guides/composition

autocompleteEnabled : boolean

Default value: true

Disables autocompletion

dictionary : array of strings

Default value: []

Dictionary of words used for the built-in autocomplete, won't be used if custom handleCompletion is passed

caseSensitive : boolean

Default value: false

Sets case sensitivity for the built-in autocomplete.

handleCompletion : func

Default value: ({value, currentSuggestion, setSuggestion, onChangeEvent}) => void

Custom handler for external autosuggestion - allows you to set any suggestion based on current input and current suggestion. The handler is only called when cursor is at the end of the current text.

completionShortcut : Set of strings

Default value: Set(['Tab'])

Set of keys that can be pressed to set current suggestion, use modifier keys with (Shift, Ctrl, Alt, Meta) +

Set(['Tab']) // pressing Tab will set the current suggestion
Set(['Shift+Tab']) // pressing Shift and Tab will set the current suggestion
Set(['Tab', 'Shift+Tab']) // pressing Shift and Tab or Tab will set the current suggestion

completionOnClick : boolean

Default value: false

Allows completion on click as well, by default only touch devices with tap can set current suggestion

classNames : object

Default value: {wrapper: undefined, suggestion: undefined}

You can set class names for the support elements - wrapper and suggestion overlay

styles : object

Default value: {wrapper: undefined, suggestion: undefined}

You can set react inline styles for the support elements - wrapper and suggestion overlay Be careful not to override important functional styles, such as

  • make sure the suggestion z index is below the form
  • make sure that the form and the suggestion have the same overlap size