Skip to content

Commit 19d6dcd

Browse files
committed
Add tabMode prop
This commit adds a new prop for LiveEditor - - which lets you define how the tab key should work. If you pass the value indentation (the default), everything will work as it has. If you pass focus, however, you'll be able to tab on to the next or previous item in the tab order
1 parent 98925cc commit 19d6dcd

File tree

3 files changed

+46
-55
lines changed

3 files changed

+46
-55
lines changed

README.md

Lines changed: 36 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -17,26 +17,20 @@
1717

1818
The library is structured modularly and lets you style and compose its components freely.
1919

20-
2120
<p align="center"><img src="https://user-images.githubusercontent.com/17658189/63181897-1d67d380-c049-11e9-9dd2-7da2a3a57f05.gif" width=500></p>
2221

2322
## Usage
2423

2524
Install it with `npm install react-live` or `yarn add react-live` and try out this piece of JSX:
2625

2726
```js
28-
import {
29-
LiveProvider,
30-
LiveEditor,
31-
LiveError,
32-
LivePreview
33-
} from 'react-live'
27+
import { LiveProvider, LiveEditor, LiveError, LivePreview } from "react-live";
3428

3529
<LiveProvider code="<strong>Hello World!</strong>">
3630
<LiveEditor />
3731
<LiveError />
3832
<LivePreview />
39-
</LiveProvider>
33+
</LiveProvider>;
4034
```
4135

4236
## Demo
@@ -54,10 +48,10 @@ is a React component.
5448

5549
Prior to `v3.0.0`, earlier versions of the library used different internals. We recommend using the latest version you can.
5650

57-
|Version|Supported React version|Editor |Transpiler
58-
|-------|-----------------------|--------------------------|----------
59-
|v3.x.x |v17.x.x |`use-editable` |`Sucrase`
60-
|v2.x.x |v16.x.x |`react-simple-code-editor`|`Bublé`
51+
| Version | Supported React version | Editor | Transpiler |
52+
| ------- | ----------------------- | -------------------------- | ---------- |
53+
| v3.x.x | v17.x.x | `use-editable` | `Sucrase` |
54+
| v2.x.x | v16.x.x | `react-simple-code-editor` | `Bublé` |
6155

6256
Please see also the related Formidable libraries:-
6357

@@ -85,7 +79,7 @@ means that the user can use it in their code like this:
8579
// ↓↓↓↓↓
8680
class Example extends React.Component {
8781
render() {
88-
return <strong>Hello World!</strong>
82+
return <strong>Hello World!</strong>;
8983
}
9084
}
9185
```
@@ -146,16 +140,13 @@ This means that while you may be used to destructuring `useState` when importing
146140

147141
return (
148142
<>
149-
<p>
150-
{`${likes} likes`}
151-
</p>
143+
<p>{`${likes} likes`}</p>
152144
<button onClick={() => increaseLikes(likes + 1)}>Like</button>
153145
</>
154146
);
155-
}
147+
};
156148
```
157149

158-
159150
### What bundle size can I expect?
160151

161152
Our reported bundle size badges don't give you the full picture of
@@ -175,16 +166,15 @@ Sucrase for transpilation.
175166
This component provides the `context` for all the other ones. It also transpiles the user’s code!
176167
It supports these props, while passing any others through to the `children`:
177168

178-
|Name|PropType|Description|
179-
|---|---|---|
180-
|code|PropTypes.string|The code that should be rendered, apart from the user’s edits
181-
|scope|PropTypes.object|Accepts custom globals that the `code` can use
182-
|noInline|PropTypes.bool|Doesn’t evaluate and mount the inline code (Default: `false`). Note: when using `noInline` whatever code you write must be a single expression (function, class component or some `jsx`) that can be returned immediately. If you'd like to render multiple components, use `noInline={true}`
183-
|transformCode|PropTypes.func|Accepts and returns the code to be transpiled, affording an opportunity to first transform it
184-
|language|PropTypes.string|What language you're writing for correct syntax highlighting. (Default: `jsx`)
185-
|disabled|PropTypes.bool|Disable editing on the `<LiveEditor />` (Default: `false`)
186-
|theme|PropTypes.object|A `prism-react-renderer` theme object. See more [here](https://github.com/FormidableLabs/prism-react-renderer#theming)
187-
169+
| Name | PropType | Description |
170+
| ------------- | ---------------- | --------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
171+
| code | PropTypes.string | The code that should be rendered, apart from the user’s edits |
172+
| scope | PropTypes.object | Accepts custom globals that the `code` can use |
173+
| noInline | PropTypes.bool | Doesn’t evaluate and mount the inline code (Default: `false`). Note: when using `noInline` whatever code you write must be a single expression (function, class component or some `jsx`) that can be returned immediately. If you'd like to render multiple components, use `noInline={true}` |
174+
| transformCode | PropTypes.func | Accepts and returns the code to be transpiled, affording an opportunity to first transform it |
175+
| language | PropTypes.string | What language you're writing for correct syntax highlighting. (Default: `jsx`) |
176+
| disabled | PropTypes.bool | Disable editing on the `<LiveEditor />` (Default: `false`) |
177+
| theme | PropTypes.object | A `prism-react-renderer` theme object. See more [here](https://github.com/FormidableLabs/prism-react-renderer#theming) |
188178

189179
All subsequent components must be rendered inside a provider, since they communicate
190180
using one.
@@ -197,10 +187,10 @@ with valid JSX elements.
197187

198188
This component renders the editor that displays the code. It is a wrapper around [`react-simple-code-editor`](https://github.com/satya164/react-simple-code-editor) and the code highlighted using [`prism-react-renderer`](https://github.com/FormidableLabs/prism-react-renderer).
199189

200-
|Name|PropType|Description|
201-
|---|---|---|
202-
|style|PropTypes.object|Allows overriding default styles on the `LiveEditor` component.
203-
190+
| Name | PropType | Description |
191+
| ------- | ----------------------------------------- | ----------------------------------------------------------------- |
192+
| style | PropTypes.object | Allows overriding default styles on the `LiveEditor` component. |
193+
| tabMode | PropTypes.oneOf(["indentation", "focus"]) | Sets how you want the tab key to work. (Default: `"indentation"`) |
204194

205195
### &lt;LiveError /&gt;
206196

@@ -213,10 +203,9 @@ It passes through any props to a `pre`.
213203

214204
This component renders the actual component that the code generates inside an error boundary.
215205

216-
|Name|PropType|Description|
217-
|---|---|---|
218-
|Component|PropTypes.node|Element that wraps the generated code. (Default: `div`)
219-
206+
| Name | PropType | Description |
207+
| --------- | -------------- | ------------------------------------------------------- |
208+
| Component | PropTypes.node | Element that wraps the generated code. (Default: `div`) |
220209

221210
### withLive
222211

@@ -226,26 +215,23 @@ by `LiveProvider` into a component.
226215
Using this HOC allows you to add new components to react-live, or replace the default ones, with a new
227216
desired behavior.
228217

229-
The component wrapped with `withLive` gets injected the following props:
230-
231-
|Name|PropType|Description|
232-
|---|---|---|
233-
|code|PropTypes.string|Reflects the code that is passed in as the `code` prop
234-
|error|PropTypes.string|An error that the code has thrown when it was previewed
235-
|onError|PropTypes.func|A callback that, when called, changes the error to what's passed as the first argument
236-
|onChange|PropTypes.func|A callback that accepts new code and transpiles it
237-
|element|React.Element|The result of the transpiled code that is previewed
218+
The component wrapped with `withLive` gets injected the following props:
238219

220+
| Name | PropType | Description |
221+
| -------- | ---------------- | -------------------------------------------------------------------------------------- |
222+
| code | PropTypes.string | Reflects the code that is passed in as the `code` prop |
223+
| error | PropTypes.string | An error that the code has thrown when it was previewed |
224+
| onError | PropTypes.func | A callback that, when called, changes the error to what's passed as the first argument |
225+
| onChange | PropTypes.func | A callback that accepts new code and transpiles it |
226+
| element | React.Element | The result of the transpiled code that is previewed |
239227

240228
> Note: The code prop doesn't reflect the up-to-date code, but the `code` prop, that is passed to the `LiveProvider`.
241229
242-
243230
## FAQ
244-
> **I want to use experimental feature x but Sucrase doesn't support it! Can I use babel instead?**
245-
246-
`react-live` doesn't currently support configuring the transpiler and it ships with Sucrase. The current workaround for using some experimental features `Sucrase` doesn't support would be to use the `transformCode` prop on `LiveProvider` to transform your code with `babel` alongside `Sucrase`.
247231

232+
> **I want to use experimental feature x but Sucrase doesn't support it! Can I use babel instead?**
248233
234+
`react-live` doesn't currently support configuring the transpiler and it ships with Sucrase. The current workaround for using some experimental features `Sucrase` doesn't support would be to use the `transformCode` prop on `LiveProvider` to transform your code with `babel` alongside `Sucrase`.
249235

250236
## Comparison to [component-playground](https://github.com/FormidableLabs/component-playground)
251237

@@ -266,5 +252,4 @@ Here are the various factors at play:
266252

267253
## Maintenance Status
268254

269-
**Active:** Formidable is actively working on this project, and we expect to continue for work for the foreseeable future. Bug reports, feature requests and pull requests are welcome.
270-
255+
**Active:** Formidable is actively working on this project, and we expect to continue for work for the foreseeable future. Bug reports, feature requests and pull requests are welcome.

src/components/Editor/index.js

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
1-
import React, { useEffect, useState, useRef, useCallback } from "react";
1+
import Highlight, { Prism } from "prism-react-renderer";
22
import PropTypes from "prop-types";
3+
import React, { useCallback, useEffect, useRef, useState } from "react";
34
import { useEditable } from "use-editable";
4-
import Highlight, { Prism } from "prism-react-renderer";
55
import { theme as liveTheme } from "../../constants/theme";
66

77
const CodeEditor = (props) => {
@@ -18,7 +18,7 @@ const CodeEditor = (props) => {
1818

1919
useEditable(editorRef, onEditableChange, {
2020
disabled: props.disabled,
21-
indentation: 2,
21+
indentation: props.tabMode === "indentation" ? 2 : undefined,
2222
});
2323

2424
useEffect(() => {
@@ -82,7 +82,12 @@ CodeEditor.propTypes = {
8282
onChange: PropTypes.func,
8383
prism: PropTypes.object,
8484
style: PropTypes.object,
85+
tabMode: PropTypes.oneOf(["focus", "indentation"]),
8586
theme: PropTypes.object,
8687
};
8788

89+
CodeEditor.defaultProps = {
90+
tabMode: "indentation",
91+
};
92+
8893
export default CodeEditor;

typings/react-live.d.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,8 @@ export type EditorProps = Omit<PreProps, 'onChange'> & {
2828
language?: Language;
2929
onChange?: (code: string) => void;
3030
theme?: PrismTheme;
31-
prism?: unknown
31+
prism?: unknown;
32+
tabMode?: "indentation" | "focus"
3233
}
3334

3435
export const Editor: ComponentClass<EditorProps>

0 commit comments

Comments
 (0)