Skip to content

Commit 42d4101

Browse files
Merge pull request #309 from selbekk/make-indentation-a-prop
Add tabMode prop
2 parents e6e9a73 + 19d6dcd commit 42d4101

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
@@ -20,26 +20,20 @@
2020

2121
The library is structured modularly and lets you style and compose its components freely.
2222

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

2625
## Usage
2726

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

3029
```js
31-
import {
32-
LiveProvider,
33-
LiveEditor,
34-
LiveError,
35-
LivePreview
36-
} from 'react-live'
30+
import { LiveProvider, LiveEditor, LiveError, LivePreview } from "react-live";
3731

3832
<LiveProvider code="<strong>Hello World!</strong>">
3933
<LiveEditor />
4034
<LiveError />
4135
<LivePreview />
42-
</LiveProvider>
36+
</LiveProvider>;
4337
```
4438

4539
## Demo
@@ -57,10 +51,10 @@ is a React component.
5751

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

60-
|Version|Supported React version|Editor |Transpiler
61-
|-------|-----------------------|--------------------------|----------
62-
|v3.x.x |v17.x.x |`use-editable` |`Sucrase`
63-
|v2.x.x |v16.x.x |`react-simple-code-editor`|`Bublé`
54+
| Version | Supported React version | Editor | Transpiler |
55+
| ------- | ----------------------- | -------------------------- | ---------- |
56+
| v3.x.x | v17.x.x | `use-editable` | `Sucrase` |
57+
| v2.x.x | v16.x.x | `react-simple-code-editor` | `Bublé` |
6458

6559
Please see also the related Formidable libraries:-
6660

@@ -88,7 +82,7 @@ means that the user can use it in their code like this:
8882
// ↓↓↓↓↓
8983
class Example extends React.Component {
9084
render() {
91-
return <strong>Hello World!</strong>
85+
return <strong>Hello World!</strong>;
9286
}
9387
}
9488
```
@@ -149,16 +143,13 @@ This means that while you may be used to destructuring `useState` when importing
149143

150144
return (
151145
<>
152-
<p>
153-
{`${likes} likes`}
154-
</p>
146+
<p>{`${likes} likes`}</p>
155147
<button onClick={() => increaseLikes(likes + 1)}>Like</button>
156148
</>
157149
);
158-
}
150+
};
159151
```
160152

161-
162153
### What bundle size can I expect?
163154

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

181-
|Name|PropType|Description|
182-
|---|---|---|
183-
|code|PropTypes.string|The code that should be rendered, apart from the user’s edits
184-
|scope|PropTypes.object|Accepts custom globals that the `code` can use
185-
|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}`
186-
|transformCode|PropTypes.func|Accepts and returns the code to be transpiled, affording an opportunity to first transform it
187-
|language|PropTypes.string|What language you're writing for correct syntax highlighting. (Default: `jsx`)
188-
|disabled|PropTypes.bool|Disable editing on the `<LiveEditor />` (Default: `false`)
189-
|theme|PropTypes.object|A `prism-react-renderer` theme object. See more [here](https://github.com/FormidableLabs/prism-react-renderer#theming)
190-
172+
| Name | PropType | Description |
173+
| ------------- | ---------------- | --------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
174+
| code | PropTypes.string | The code that should be rendered, apart from the user’s edits |
175+
| scope | PropTypes.object | Accepts custom globals that the `code` can use |
176+
| 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}` |
177+
| transformCode | PropTypes.func | Accepts and returns the code to be transpiled, affording an opportunity to first transform it |
178+
| language | PropTypes.string | What language you're writing for correct syntax highlighting. (Default: `jsx`) |
179+
| disabled | PropTypes.bool | Disable editing on the `<LiveEditor />` (Default: `false`) |
180+
| theme | PropTypes.object | A `prism-react-renderer` theme object. See more [here](https://github.com/FormidableLabs/prism-react-renderer#theming) |
191181

192182
All subsequent components must be rendered inside a provider, since they communicate
193183
using one.
@@ -200,10 +190,10 @@ with valid JSX elements.
200190

201191
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).
202192

203-
|Name|PropType|Description|
204-
|---|---|---|
205-
|style|PropTypes.object|Allows overriding default styles on the `LiveEditor` component.
206-
193+
| Name | PropType | Description |
194+
| ------- | ----------------------------------------- | ----------------------------------------------------------------- |
195+
| style | PropTypes.object | Allows overriding default styles on the `LiveEditor` component. |
196+
| tabMode | PropTypes.oneOf(["indentation", "focus"]) | Sets how you want the tab key to work. (Default: `"indentation"`) |
207197

208198
### &lt;LiveError /&gt;
209199

@@ -216,10 +206,9 @@ It passes through any props to a `pre`.
216206

217207
This component renders the actual component that the code generates inside an error boundary.
218208

219-
|Name|PropType|Description|
220-
|---|---|---|
221-
|Component|PropTypes.node|Element that wraps the generated code. (Default: `div`)
222-
209+
| Name | PropType | Description |
210+
| --------- | -------------- | ------------------------------------------------------- |
211+
| Component | PropTypes.node | Element that wraps the generated code. (Default: `div`) |
223212

224213
### withLive
225214

@@ -229,26 +218,23 @@ by `LiveProvider` into a component.
229218
Using this HOC allows you to add new components to react-live, or replace the default ones, with a new
230219
desired behavior.
231220

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

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

243231
> Note: The code prop doesn't reflect the up-to-date code, but the `code` prop, that is passed to the `LiveProvider`.
244232
245-
246233
## FAQ
247-
> **I want to use experimental feature x but Sucrase doesn't support it! Can I use babel instead?**
248-
249-
`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`.
250234

235+
> **I want to use experimental feature x but Sucrase doesn't support it! Can I use babel instead?**
251236
237+
`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`.
252238

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

@@ -269,5 +255,4 @@ Here are the various factors at play:
269255

270256
## Maintenance Status
271257

272-
**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.
273-
258+
**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(() => {
@@ -83,7 +83,12 @@ CodeEditor.propTypes = {
8383
onChange: PropTypes.func,
8484
prism: PropTypes.object,
8585
style: PropTypes.object,
86+
tabMode: PropTypes.oneOf(["focus", "indentation"]),
8687
theme: PropTypes.object,
8788
};
8889

90+
CodeEditor.defaultProps = {
91+
tabMode: "indentation",
92+
};
93+
8994
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)