Skip to content

Commit

Permalink
feat: support disabling list types in toolbar component
Browse files Browse the repository at this point in the history
  • Loading branch information
petyosi committed Feb 8, 2024
1 parent 9244ab3 commit 5216136
Showing 1 changed file with 22 additions and 13 deletions.
35 changes: 22 additions & 13 deletions src/plugins/toolbar/components/ListsToggle.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,24 +4,33 @@ import { SingleChoiceToggleGroup } from '.././primitives/toolbar'
import { useCellValues, usePublisher } from '@mdxeditor/gurx'
import { iconComponentFor$ } from '../../core'

const LIST_TITLE_MAP = {
bullet: 'Bulleted list',
number: 'Numbered list',
check: 'Check list'
} as const

const ICON_NAME_MAP = {
bullet: 'format_list_bulleted',
number: 'format_list_numbered',
check: 'format_list_checked'
} as const

/**
* A toolbar toggle that allows the user to toggle between bulleted and numbered lists.
* A toolbar toggle that allows the user to toggle between bulleted, numbered, and check lists.
* Pressing the selected button will convert the current list to the other type. Pressing it again will remove the list.
* For this button to work, you need to have the `listsPlugin` plugin enabled.
* @group Toolbar Components
* @param options - The list types that the user can toggle between. Defaults to `['bullet', 'number', 'check']`.
*/
export const ListsToggle: React.FC = () => {
export const ListsToggle: React.FC<{ options?: Array<'bullet' | 'number' | 'check'> }> = ({ options = ['bullet', 'number', 'check'] }) => {
const [currentListType, iconComponentFor] = useCellValues(currentListType$, iconComponentFor$)
const applyListType = usePublisher(applyListType$)
return (
<SingleChoiceToggleGroup
value={currentListType || ''}
items={[
{ title: 'Bulleted list', contents: iconComponentFor('format_list_bulleted'), value: 'bullet' },
{ title: 'Numbered list', contents: iconComponentFor('format_list_numbered'), value: 'number' },
{ title: 'Check list', contents: iconComponentFor('format_list_checked'), value: 'check' }
]}
onChange={applyListType}
/>
)
const items = options.map((type) => ({
value: type,
title: LIST_TITLE_MAP[type],
contents: iconComponentFor(ICON_NAME_MAP[type])
}))

return <SingleChoiceToggleGroup value={currentListType || ''} items={items} onChange={applyListType} />
}

0 comments on commit 5216136

Please sign in to comment.