1.1.0 • Published 2 years ago

cedit v1.1.0

Weekly downloads
-
License
MIT
Repository
github
Last release
2 years ago

About

Cedit is a component for react that uses the Content Editable feature for free editing of content within an HTML element. It is light and minimalist and aims to make the most of this wonderful resource in a clean and performative way.

Motivation

The content editable is an excellent and super useful feature that I needed to implement many times but I always faced some basic usage problems such as caret positioning, etc. And even with some components in the community implementing this feature, the same issues still exist! The objective of this implementation is to skip this step and deliver a simple component, at the same time powerful and with the basic functioning expected.

Installation

Cedit is a lightweight package with 0 dependencies.

Using NPM

npm i cedit

Using Yarn

yarn add cedit

Use

The component has a simple interface, you can import the CeditProps interface for better specification.

All handlers return a value of type Maybe<HTMLDivElement> with keys text, html and event.

Cedit

You can check a practical example in Demo.

import { useState } from 'react'
import { Cedit, CeditProps, Maybe } from 'cedit'

const App: React.FC = () => {
  const [value, setValue] = useState('')

  return (
    <Cedit
      value={value}
      placeholder="Type here..."
      onChange={({ text, html, event }) => setValue(html)}
    />
  )
}

Provider

If you want to control more than one editor, you can use CeditProvider to wrap all components and take advantage of features like autofocus. For now the provider does not offer extra features, but it will be implemented in the future.

import { Cedit, CeditProps, CeditProvider, Maybe } from 'cedit'

const App: React.FC = () => {
  return (
    <CeditProvider>
      {/* Editors */}
    <CeditProvider>
  )
}
PropertyTypeDescription
idstringSpecifies a unique id for an HTML element.
valuestringCan be plain text or html.
spellCheckbooleanSpell check.
editablebooleanEnable editing when focusing on component
multiLinebooleanWhen true allows line wrapping.
styleReact.CSSPropertiesCustom styles.
classNamestringCSS class for styling. By default no styles are defined for the component, only resets basic settings.
placeholderstringA small tip, phrase, word, which is intended to help the user understand how to fill out that form.
autoFocusbooleanIf enabled, the element receives focus automatically when displayed.
placementstringEditable content centering, the accepted values are: topStart, topCenter, topEnd, middleStart, middleCenter,middleEnd,bottomStart,bottomCenter and bottomEnd.
onKeyUpMaybeTThe keyup event fires when a key is released.
onKeyDownMaybeTThe keydown event is fired when a key is pressed.
onKeyPressMaybeTThe onkeypress event occurs when the user presses a key.
onBlurMaybeTThe onblur event occurs when an object loses focus.
onFocusMaybeTThe onfocus event occurs when an element gets focus.
onChangeMaybeTThe onchange event occurs when the value of an element has been changed.
onPasteMaybeTThe onpaste event occurs when the user pastes some content in an element.

Style

The component has no predefined styling, but exposes two main classes that you can use to style.

/* Container */
.cedit {
}

/* Content */
.cedit__content {
}

Contributing

Cedit is in an initial version without many features yet, but you can feel free to send your suggestion or open a PR.