Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Integrate into a react project #22

Open
smotched opened this issue May 10, 2022 · 4 comments
Open

Integrate into a react project #22

smotched opened this issue May 10, 2022 · 4 comments
Labels
enhancement New feature or request good first issue Good for newcomers

Comments

@smotched
Copy link

Thanks for osing this neat library, is there a guide to integrating ink into an existing react project?

@davidmyersdev
Copy link
Owner

@smotched I'm glad you like it! I've been meaning to put together a React component, but I haven't prioritized it yet since I primarily work in Vue. I'll try to get an example together for you soon.

@phylor
Copy link

phylor commented Sep 23, 2022

Something like the following might get you started:

import { defineOptions, ink, Instance } from "ink-mde"
import { useEffect, useRef, useState } from "react"

type Props = {
  content: string | null
  onChange: (content: string) => void
  readonly: boolean
}

const MarkdownEditor = ({ content, onChange, readonly }: Props) => {
  const ref = useRef<HTMLDivElement | null>(null)
  const [editor, setEditor] = useState<Instance | null>(null)
  const options = defineOptions({
    doc: content || undefined,
    hooks: {
      afterUpdate: onChange,
    },
    interface: {
      readonly,
    },
  })

  useEffect(() => {
    if (ref.current && ref.current.children.length <= 0) {
      const editor = ink(ref.current, options)
      setEditor(editor)
    }
  }, [ref])

  useEffect(() => {
    if (editor) {
      editor.reconfigure(options)
    }
  }, [readonly])

  return <div ref={ref}></div>
}

export default MarkdownEditor

@davidmyersdev
Copy link
Owner

Thanks for the example, @phylor!

@davidmyersdev davidmyersdev added good first issue Good for newcomers enhancement New feature or request labels Sep 28, 2022
@kadhirr
Copy link

kadhirr commented Apr 17, 2023

Hey @davidmyersdev , I have created a react project using ink-mde and the code by @phylor . Please check it, and if it seems fit, you can add it to the project for reference.
Here is the link: https://monumental-dango.netlify.app/
Source Code: Github Repo

This is my first open-source contribution, so let me know if I need to change any procedures for better convenience.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request good first issue Good for newcomers
Projects
Status: Next Up
Development

No branches or pull requests

4 participants