Skip to content

Commit

Permalink
feat: Support options.theme in Input component
Browse files Browse the repository at this point in the history
  • Loading branch information
nervetattoo committed Mar 26, 2020
1 parent 9de9c21 commit 4cb65c1
Show file tree
Hide file tree
Showing 4 changed files with 13 additions and 9 deletions.
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

Add a [Mermaid](https://mermaid-js.github.io/mermaid/) graph input type for [Sanity CMS](http://sanity.io)

Read more about [using the plugin in this blog post](https://raymondjulin.com/blog/drawing-diagrams-in-sanity-with-mermaid-js)

![Screenshot](/sanity-plugin-mermaid.png)

```js
Expand Down Expand Up @@ -31,8 +33,6 @@ In order to render in your frontend you need to manually use the mermaid package

## TODO

- [ ] Document how to use in frontend
- [ ] Write a helper package providing a serialiser for portable text
- [ ] Link to mermaid docs in editor
- [ ] Syntax highlighted editor
- [ ] Polish and npm publishing
2 changes: 1 addition & 1 deletion src/components/Input/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ function Input ({
{!isSSR && (
<React.Suspense fallback={<div />}>
<FormField level={level} label="Preview">
<Mermaid graph={value} id={id} />
<Mermaid graph={value} id={id} options={type?.options} />
</FormField>
</React.Suspense>
)}
Expand Down
3 changes: 2 additions & 1 deletion src/components/Mermaid.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,10 @@ import useMermaid from '../useMermaid'
export default function Mermaid ({
graph,
id,
options,
fallback = 'Invalid graph definition'
}) {
const [valid, html] = useMermaid(graph, id)
const [valid, html] = useMermaid(graph, id, options)
const ref = useRef()

useEffect(() => {
Expand Down
13 changes: 8 additions & 5 deletions src/useMermaid.js
Original file line number Diff line number Diff line change
@@ -1,13 +1,16 @@
import { useState, useEffect } from 'react'
import mermaid from 'mermaid'

mermaid.mermaidAPI.initialize({
startOnLoad: true,
})

export default function useMermaid(graph = '', id = 'mermaid') {
export default function useMermaid(graph = '', id = 'mermaid', options = {}) {
const [svg, setSvg] = useState('')

useEffect(() => {
mermaid.mermaidAPI.initialize({
startOnLoad: true,
theme: options.theme || 'neutral',
})
}, [])

useEffect(() => {
try {
mermaid.parse(graph)
Expand Down

0 comments on commit 4cb65c1

Please sign in to comment.