Skip to content

Commit

Permalink
feat: add useToggleCodeBlockButton hook to code-block package
Browse files Browse the repository at this point in the history
  • Loading branch information
vodkaslime committed Feb 21, 2024
1 parent aa5ad44 commit 48e0a83
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 0 deletions.
1 change: 1 addition & 0 deletions packages/code-block/src/hooks/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,4 @@
export * from './useCodeBlockCombobox';
export * from './useCodeBlockElement';
export * from './useCodeSyntaxLeaf';
export * from './useToggleCodeBlockButton';
18 changes: 18 additions & 0 deletions packages/code-block/src/hooks/useToggleCodeBlockButton.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import React from 'react';
import { useEditorRef } from '@udecode/plate-common';
import { toggleCodeBlock } from '../transforms';

export const useToggleCodeBlockButton = () => {
const editor = useEditorRef();

return {
props: {
onMouseDown: (e: React.MouseEvent<HTMLButtonElement>) => {
e.preventDefault();
},
onClick: () => {
toggleCodeBlock(editor);
},
},
};
};
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ import { TableDropdownMenu } from '@/components/plate-ui/table-dropdown-menu';
import { InsertDropdownMenu } from './insert-dropdown-menu';
import { MarkToolbarButton } from './mark-toolbar-button';
import { ModeDropdownMenu } from './mode-dropdown-menu';
import { ToggleCodeBlockButton } from './toggle-code-block-button';
import { ToolbarGroup } from './toolbar';
import { TurnIntoDropdownMenu } from './turn-into-dropdown-menu';

Expand Down Expand Up @@ -72,6 +73,7 @@ export function FixedToolbarButtons() {
<MarkToolbarButton tooltip="Code (⌘+E)" nodeType={MARK_CODE}>
<Icons.code />
</MarkToolbarButton>
<ToggleCodeBlockButton />
</ToolbarGroup>

<ToolbarGroup>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import { withRef } from '@udecode/cn';
import { useToggleCodeBlockButton } from '@udecode/plate-code-block';

import { Icons } from '../icons';
import { ToolbarButton } from './toolbar';

export const ToggleCodeBlockButton = withRef<typeof ToolbarButton>(
(rest, ref) => {
const { props } = useToggleCodeBlockButton();

return (
<ToolbarButton ref={ref} tooltip="Toggle Code Block" {...props} {...rest}>
<Icons.codeblock />
</ToolbarButton>
);
}
);

0 comments on commit 48e0a83

Please sign in to comment.