Skip to content

Commit

Permalink
Allow use of react-interactive-cljs props
Browse files Browse the repository at this point in the history
  • Loading branch information
thiagooak committed Aug 29, 2024
1 parent f14704c commit be98ea6
Show file tree
Hide file tree
Showing 6 changed files with 33 additions and 58 deletions.
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
node_modules
node_modules
index.js
15 changes: 11 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,16 +10,23 @@ npm i react-interactive-cljs

```js
"use client"
import { CljsCodeBlock } from "react-interactive-cljs"
import { CodeBlock } from "react-interactive-cljs"

export function Page() {
return (<CljsCodeBlock>{`(println "Hello")
(+ 2 2)`}</CljsCodeBlock>)
return (<CodeBlock>{`(println "Hello")
(+ 2 2)`}</CodeBlock>)
)

```
The code above will render like the image below
![](before.png)
After clicking the "Run" button it will render like the image below
![](after.png)
![](after.png)
Accepts the same props as [react-syntax-highlighter](https://github.com/react-syntax-highlighter/react-syntax-highlighter?tab=readme-ov-file#props)
plus:
* `language` defaults to "clojure"
* `allowEval` a boolean that defaults to true if the language is "clojure".
40 changes: 0 additions & 40 deletions index.js

This file was deleted.

25 changes: 14 additions & 11 deletions index.jsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { Prism as SyntaxHighlighter } from 'react-syntax-highlighter';
import { darcula as style } from 'react-syntax-highlighter/dist/esm/styles/prism';
import { darcula } from 'react-syntax-highlighter/dist/esm/styles/prism';
import React from 'react';
import { evaluate } from 'eval-cljs';

Expand All @@ -9,7 +9,7 @@ function PlayIcon() {
</svg>);
}

export function CljsCodeBlock({ children }) {
export function CodeBlock({ children, language = "clojure", allowEval = true, style = darcula, ...props }) {
const [output, setOutput] = React.useState("")

function handleRun() {
Expand All @@ -21,18 +21,21 @@ export function CljsCodeBlock({ children }) {
return (
<>
<SyntaxHighlighter
language="clojure"
language={language}
style={style}
showLineNumbers={false}>
{...props}
>
{children}
</SyntaxHighlighter>
<button onClick={() => (handleRun())} style={{minWidth: 30, minHeight: 30}}><PlayIcon /></button>
{output && (<SyntaxHighlighter
language="clojure"
style={style}
showLineNumbers={false}>
{output}
</SyntaxHighlighter>)}
{allowEval && language === "clojure" && (<>
<button onClick={() => (handleRun())} style={{minWidth: 30, minHeight: 30}}><PlayIcon /></button>
{output && (<SyntaxHighlighter
language={language}
style={style}
showLineNumbers={false}>
{output}
</SyntaxHighlighter>)}
</>)}
</>
);
}
6 changes: 5 additions & 1 deletion package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "react-interactive-cljs",
"version": "0.0.6",
"version": "0.1.0",
"description": "Interactive cljs code blocks",
"main": "index.js",
"scripts": {
Expand Down

0 comments on commit be98ea6

Please sign in to comment.