Skip to content

Commit

Permalink
feat: toolbar plugin toolbarClassName prop (#604)
Browse files Browse the repository at this point in the history
  • Loading branch information
WebEferen authored Oct 7, 2024
1 parent d837a1d commit dc2bfb3
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 5 deletions.
1 change: 1 addition & 0 deletions docs/customizing-toolbar.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ function App() {
markdown="Hello world"
plugins={[
toolbarPlugin({
toolbarClassName: 'my-classname',
toolbarContents: () => (
<>
{' '}
Expand Down
13 changes: 10 additions & 3 deletions src/plugins/toolbar/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import { Root } from './primitives/toolbar'
* @group Toolbar
*/
export const toolbarContents$ = Cell<() => React.ReactNode>(() => null)
export const toolbarClassName$ = Cell<string>('')

const DEFAULT_TOOLBAR_CONTENTS = () => {
return 'This is an empty toolbar. Pass `{toolbarContents: () => { return <>toolbar components</> }}` to the toolbarPlugin to customize it.'
Expand All @@ -18,17 +19,23 @@ const DEFAULT_TOOLBAR_CONTENTS = () => {
* A plugin that adds a toolbar to the editor.
* @group Toolbar
*/
export const toolbarPlugin = realmPlugin<{ toolbarContents: () => React.ReactNode }>({
export const toolbarPlugin = realmPlugin<{ toolbarContents: () => React.ReactNode; toolbarClassName?: string }>({
init(realm, params) {
realm.pubIn({
[toolbarContents$]: params?.toolbarContents ?? DEFAULT_TOOLBAR_CONTENTS,
[toolbarClassName$]: params?.toolbarClassName ?? '',
[addTopAreaChild$]: () => {
const [toolbarContents, readOnly] = useCellValues(toolbarContents$, readOnly$)
return <Root readOnly={readOnly}>{toolbarContents()}</Root>
const [toolbarContents, readOnly, toolbarClassName] = useCellValues(toolbarContents$, readOnly$, toolbarClassName$)
return (
<Root className={toolbarClassName} readOnly={readOnly}>
{toolbarContents()}
</Root>
)
}
})
},
update(realm, params) {
realm.pub(toolbarContents$, params?.toolbarContents ?? DEFAULT_TOOLBAR_CONTENTS)
realm.pub(toolbarClassName$, params?.toolbarClassName ?? '')
}
})
4 changes: 2 additions & 2 deletions src/plugins/toolbar/primitives/toolbar.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -40,10 +40,10 @@ function addTooltipToChildren<C extends React.ComponentType<{ children: React.Re
/**
* @internal
*/
export const Root: React.FC<{ readOnly: boolean; children: React.ReactNode }> = ({ readOnly, children }) => {
export const Root: React.FC<{ readOnly: boolean; children: React.ReactNode; className?: string }> = ({ readOnly, children, className }) => {
return (
<RadixToolbar.Root
className={classNames('mdxeditor-toolbar', styles.toolbarRoot, { [styles.readOnlyToolbarRoot]: readOnly })}
className={classNames('mdxeditor-toolbar', styles.toolbarRoot, { [styles.readOnlyToolbarRoot]: readOnly }, className)}
{...(readOnly ? { tabIndex: -1 } : {})}
>
{children}
Expand Down

0 comments on commit dc2bfb3

Please sign in to comment.