notestamp v2.0.0
notestamp
Made with create-react-library
Description
A rich-text editor library for React that supports clickable stamps.
A stamp is automatically inserted at the start of a line when the Enter
key is pressed. You can define an arbitrary state to be stored inside a stamp as well as a function to execute when a stamp is clicked.
A common use case of this component is to synchronize text to some entity e.g. an audio file. See https://notestamp.com as an example.
Install
npm install notestamp
Usage
import React, { useRef } from 'react'
import { Notestamp, useEditor } from 'notestamp'
const App = () => {
const { editor } = useEditor()
const setStampData = dateStampRequested => {
return { label: 'three', value: 3 }
}
const printStampLabel = (label, _) => console.log(`Clicked stamp: ${label}`)
return (
<Notestamp
editor={editor}
onStampInsert={setStampData}
onStampClick={printStampLabel}
borderSize='1px'
borderColor='lightgray'
borderStyle='solid'
toolbarBackgroundColor='whitesmoke'
/>
)
}
Usage
Extract the editor
object from the useEditor()
hook and pass it as a prop to the Notestamp
component.
Editor Object
The editor
object returned by useEditor()
extends Slate's editor prototype with additional methods and behaviors, allowing you to use it without requiring deep knowledge of Slate.
Warning: Only the methods defined below have been tested for this particular React library. If you wish to use all the other properties and methods available on the editor
object, you should read up on Slate's official documentation.
Methods
Use these methods to interact with the editor:
getChildren()
: Returns the editor's content (also known as children in Slate). This is a JSON object conforming to Slate’sNode[]
interface.setChildren(children)
: Replaces the editor’s content withchildren
. This is the only way to set content that includes stamps. Thechildren
must adhere to [Slate’sNode[]
interface. Read more on Slate's official documentation.getTextContent(options)
: Returns the editor’s text as a single string, excluding stamps. To include stamps, pass{ withStamps: true }
asoptions
.setTextContent(content)
: Replaces the editor’s content withcontent
, which must be a string.clear()
: Clears the editor’s content.
Notestamp Component
Render the Notestamp
component and pass the editor
as a prop. Additional props allow you to listen for events, customize the UI, and define stamp behavior.
Props
editor
: Expects theeditor
object returned byuseEditor
.onStampInsert
: Called when theEnter
key is pressed. ReceivesdateEnterKeyPressed
(aDate
object) as an argument and should return an object{ label: string, value: any }
. Thelabel
is displayed inside the stamp, andvalue
holds the stamp’s state. Returningnull
forvalue
cancels the insertion.onStampClick
: A callback function that executes when a stamp is clicked. Receiveslabel: string
andvalue: any
as arguments.placeholder
: Sets a custom placeholder text. Pass a string to override the default orfalse
to disable it.toolbarBackgroundColor
: Sets the toolbar’s background color.borderColor
,borderSize
,borderStyle
: Customize the color, thickness, and style of the editor’s border, including the separator between the toolbar and the text area.onChange
: A callback function that executes when the editor’s content changes. Receivesvalue: Node[]
, representing the updated content.
Credits
This editor was built using Slate.
License
MIT © fortyoneplustwo