Skip to content

Commit

Permalink
Update react documentation
Browse files Browse the repository at this point in the history
  • Loading branch information
ifiokjr committed Aug 24, 2020
1 parent dd811f4 commit febad9e
Show file tree
Hide file tree
Showing 4 changed files with 80 additions and 19 deletions.
49 changes: 31 additions & 18 deletions docs/concepts/react-controlled-editor.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,13 +20,20 @@ Get started in the usual way.

```tsx
import React from 'react';
import { RemirrorProvider, useRemirror, createReactManager } from 'remirror/react';
import { RemirrorProvider, useRemirror, useManager } from 'remirror/react';
import { BoldExtension } from 'remirror/extension/bold';

const manager = createReactManager([new BoldExtension()]);
// This is a function that returns the list of extensions and presets we want to
// use. It's helpful to set up this way since the template can be reused
// multiple times in your app.
const extensionTemplate = () => [new BoldExtension()];

// Adds the `RemirrorProvider` which is responsible for wrapping the editor with
// the context and state of the rendered editor.
const EditorWrapper = () => {
// This will be changed
// A convenient hooks for creating the manager in a react editor.
const manager = useManager(extensionTemplate);

return (
<RemirrorProvider manager={manager}>
<Editor />
Expand All @@ -50,22 +57,26 @@ the `RemirrorProvider`.
```tsx
// Add the `useState` hook to keep track of the state.
import React, { useState } from 'react';
import { RemirrorProvider, useRemirror, createReactManager } from 'remirror/react';
import { RemirrorProvider, useRemirror, useManager } from 'remirror/react';
import { BoldExtension } from 'remirror/extension/bold';

// Add the `fromHtml` string handler import so that the initial state can be a html string.
// Add the `fromHtml` string handler import so that the initial state can be a
// html string.
import { fromHtml } from 'remirror/core';

const manager = createReactManager([new BoldExtension()]);
const extensionTemplate = () => [new BoldExtension()];

const EditorWrapper = () => {
// Create the initial value for the manager.
const initialValue = manager.createState({
content: '<p>This is the initial value</p>',
stringHandler: fromHtml,
});

const [value, setValue] = useState(initialValue);
const manager = useManager(extensionTemplate);

// Store the editor value in a state variable.
const [value, setValue] = useState(() =>
// Use the `remirror` manager to create the state.
manager.createState({
content: '<p>This is the initial value</p>',
stringHandler: fromHtml,
}),
);

// Add the value and change handler to the editor.
return (
Expand Down Expand Up @@ -98,12 +109,14 @@ into the editor whenever the user types any content.

```tsx
const EditorWrapper = () => {
const initialValue = manager.createState({
content: '<p>This is the initial value</p>',
stringHandler: fromHtml,
});
const manager = useManager(extensionTemplate);

const [value, setValue] = useState(initialValue);
const [value, setValue] = useState(() =>
manager.createState({
content: '<p>This is the initial value</p>',
stringHandler: fromHtml,
}),
);

return (
<RemirrorProvider
Expand Down
27 changes: 27 additions & 0 deletions docs/concepts/react-refs.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
---
title: React Refs
---

## Extract context as a `ref`

In order to access the context properties of your editor from externally the following snippet
should work.

It makes use of the less well known `useImperativeHandle` to attach the context from the editor to
the `ref` within a forward ref component.

```tsx
import { forwardRef, useImperativeHandle, FC } from 'react';
import { useRemirror } from 'remirror/react';

let EditorWithRef = (props: {}, ref: RemirrorContext) => {
const context = useRemirror();
const { getRootProps } = context;

useImperativeHandle(ref, () => context);

return <div {...getRootProps} />;
};

EditorWithRef = forwardRef(EditorWithRef);
```
20 changes: 20 additions & 0 deletions docs/faq.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
---
title: 'Frequently asked questions'
---

Some of the answers outlined here may be helpful to you if you're stuck somewhere. They're questions
that are asked quite frequently on GitHub and in our [discord](https://remirror.io/chat) channel.

### Is there any way to get the value of the editor already parsed to HTML?

There's are methods available in `remirror/core`. `toHtml` which converts the provided node to a
HTML string, and `fromHtml` which takes the html string you've provided and converts it into a value
that can be used within remirror.

To get the html string from the editor the following should work well for you.

```tsx
import { toHtml } from 'remirror/core';

const htmlString = toHtml({ node: state.doc, schema: state.schema });
```
3 changes: 2 additions & 1 deletion support/website/sidebars.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ const sidebarConfig = {
{
type: 'category',
label: 'Remirror',
items: ['introduction', 'contributing', 'tooling', 'errors', 'projects'],
items: ['introduction', 'contributing', 'tooling', 'errors', 'projects', 'faq'],
},
{
type: 'category',
Expand All @@ -20,6 +20,7 @@ const sidebarConfig = {
'concepts/presets',
'concepts/remirror-manager',
'concepts/react-controlled-editor',
'concepts/react-refs',
'concepts/priority',
'concepts/keymap',
],
Expand Down

1 comment on commit febad9e

@github-actions
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🎉 Published on https://remirror.io as production
🚀 Deployed on https://5f44022a804ed29a1c61b101--remirror.netlify.app

Please sign in to comment.