Skip to content

Commit

Permalink
fix: toolbar re-renders with state changes
Browse files Browse the repository at this point in the history
Fixes #248
  • Loading branch information
petyosi committed Jan 13, 2024
1 parent 148ac50 commit 73a87f3
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 36 deletions.
78 changes: 42 additions & 36 deletions src/examples/toolbar.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -79,43 +79,49 @@ export const CustomTheming = () => {
}

export const ConditionalToolbar = () => {
const [outsideState, setOutsideState] = React.useState('foo')
return (
<MDXEditor
markdown={'hello world'}
plugins={[
toolbarPlugin({
toolbarContents: () => (
<>
<DiffSourceToggleWrapper>
<UndoRedo />
<BoldItalicUnderlineToggles />
<ListsToggle />
<Separator />
<BlockTypeSelect />
<CreateLink />
<InsertImage />
<Separator />
</DiffSourceToggleWrapper>
</>
)
}),
listsPlugin(),
quotePlugin(),
headingsPlugin(),
linkPlugin(),
linkDialogPlugin(),
imagePlugin(),
tablePlugin(),
thematicBreakPlugin(),
frontmatterPlugin(),
// codeBlockPlugin({ defaultCodeBlockLanguage: 'txt' }),
// sandpackPlugin({ sandpackConfig: virtuosoSampleSandpackConfig }),
// codeMirrorPlugin({ codeBlockLanguages: { js: 'JavaScript', css: 'CSS', txt: 'text' } }),
directivesPlugin({ directiveDescriptors: [YoutubeDirectiveDescriptor, AdmonitionDirectiveDescriptor] }),
diffSourcePlugin({ viewMode: 'rich-text', diffMarkdown: 'boo' }),
markdownShortcutPlugin()
]}
/>
<>
<button onClick={() => setOutsideState('bar')}>Toggle outside state</button>
{outsideState}
<MDXEditor
markdown={'hello world'}
plugins={[
toolbarPlugin({
toolbarContents: () => (
<>
<DiffSourceToggleWrapper>
{outsideState}
<UndoRedo />
<BoldItalicUnderlineToggles />
<ListsToggle />
<Separator />
<BlockTypeSelect />
<CreateLink />
<InsertImage />
<Separator />
</DiffSourceToggleWrapper>
</>
)
}),
listsPlugin(),
quotePlugin(),
headingsPlugin(),
linkPlugin(),
linkDialogPlugin(),
imagePlugin(),
tablePlugin(),
thematicBreakPlugin(),
frontmatterPlugin(),
// codeBlockPlugin({ defaultCodeBlockLanguage: 'txt' }),
// sandpackPlugin({ sandpackConfig: virtuosoSampleSandpackConfig }),
// codeMirrorPlugin({ codeBlockLanguages: { js: 'JavaScript', css: 'CSS', txt: 'text' } }),
directivesPlugin({ directiveDescriptors: [YoutubeDirectiveDescriptor, AdmonitionDirectiveDescriptor] }),
diffSourcePlugin({ viewMode: 'rich-text', diffMarkdown: 'boo' }),
markdownShortcutPlugin()
]}
/>
</>
)
}

Expand Down
3 changes: 3 additions & 0 deletions src/plugins/toolbar/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -27,5 +27,8 @@ export const toolbarPlugin = realmPlugin<{ toolbarContents: () => React.ReactNod
return <Root readOnly={readOnly}>{toolbarContents()}</Root>
}
})
},
update(realm, params) {
realm.pub(toolbarContents$, params?.toolbarContents ?? DEFAULT_TOOLBAR_CONTENTS)
}
})

0 comments on commit 73a87f3

Please sign in to comment.