Skip to content

Commit

Permalink
feat(toolbar): add property to show custom hint when action is disabl…
Browse files Browse the repository at this point in the history
…ed (#193)

- property `disabledPopoverVisible` in the `ToolbarItemData` type is marked as deprecated
- add a new `hintWhenDisabled` property for the `ToolbarItemData` type, which allows to display a custom message in hint when the action is disabled
  • Loading branch information
d3m1d0v authored Feb 16, 2024
1 parent 42be1aa commit f08366a
Show file tree
Hide file tree
Showing 6 changed files with 55 additions and 9 deletions.
1 change: 1 addition & 0 deletions src/i18n/menubar/en.json
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
"olist": "Ordered list",
"list__action_sink": "Sink item",
"list__action_lift": "Lift item",
"list_action_disabled": "Contradicts the logic of the list",
"checkbox": "Checkbox",
"quote": "Quote",
"cut": "Cut",
Expand Down
1 change: 1 addition & 0 deletions src/i18n/menubar/ru.json
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
"olist": "Нумерованный список",
"list__action_sink": "Увеличить отступ",
"list__action_lift": "Уменьшить отступ",
"list_action_disabled": "Противоречит логике списка",
"checkbox": "Контрольный список",
"quote": "Цитата",
"cut": "Кат",
Expand Down
12 changes: 10 additions & 2 deletions src/toolbar/ToolbarButton.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ export function ToolbarButton<E>({
editor,
icon,
disabledPopoverVisible = true,
hintWhenDisabled,
exec,
focus,
onClick,
Expand All @@ -35,11 +36,18 @@ export function ToolbarButton<E>({
const buttonRef = useRef<HTMLElement>(null);

const titleText: string = isFunction(title) ? title() : title;
const hideHintWhenDisabled = hintWhenDisabled === false || !disabledPopoverVisible || !disabled;
const hintWhenDisabledText =
typeof hintWhenDisabled === 'string'
? hintWhenDisabled
: typeof hintWhenDisabled === 'function'
? hintWhenDisabled()
: i18n('toolbar_action_disabled');

return (
<Popover
content={i18n('toolbar_action_disabled')}
disabled={!disabledPopoverVisible || !disabled}
content={hintWhenDisabledText}
disabled={hideHintWhenDisabled}
tooltipContentClassName={b('action-disabled-tooltip')}
placement={['bottom']}
>
Expand Down
29 changes: 26 additions & 3 deletions src/toolbar/ToolbarListButton.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -93,17 +93,40 @@ export function ToolbarListButton<E>({
<Popup anchorRef={buttonRef} open={popupOpen} onClose={hide}>
<Menu size="l" className={b('menu')}>
{data
.map(({id, title, icon, hotkey, isActive, isEnable, exec, hint}) => {
.map((data) => {
const {
id,
title,
icon,
hotkey,
isActive,
isEnable,
exec,
hint,
hintWhenDisabled,
disabledPopoverVisible = true,
} = data;

const titleText = isFunction(title) ? title() : title;
const hintText = isFunction(hint) ? hint() : hint;
const disabled = !isEnable(editor);

const hideHintWhenDisabled =
hintWhenDisabled === false || !disabledPopoverVisible || !disabled;
const hintWhenDisabledText =
typeof hintWhenDisabled === 'string'
? hintWhenDisabled
: typeof hintWhenDisabled === 'function'
? hintWhenDisabled()
: i18n('toolbar_action_disabled');

return (
<Popover
className={b('action-disabled-popover')}
tooltipContentClassName={b('action-disabled-tooltip')}
content={i18n('toolbar_action_disabled')}
content={hintWhenDisabledText}
placement={'left'}
disabled={!disabled}
disabled={hideHintWhenDisabled}
key={id}
>
<Menu.Item
Expand Down
8 changes: 4 additions & 4 deletions src/toolbar/config/wysiwyg.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ export const wHistoryGroupConfig: WToolbarGroupData = [
title: i18n.bind(null, 'undo'),
icon: icons.undo,
hotkey: f.toView(A.Undo),
disabledPopoverVisible: true,
disabledPopoverVisible: false,
exec: (e) => e.actions.undo.run(),
isActive: (e) => e.actions.undo.isActive(),
isEnable: (e) => e.actions.undo.isEnable(),
Expand All @@ -40,7 +40,7 @@ export const wHistoryGroupConfig: WToolbarGroupData = [
title: i18n.bind(null, 'redo'),
icon: icons.redo,
hotkey: f.toView(A.Redo),
disabledPopoverVisible: true,
hintWhenDisabled: false,
exec: (e) => e.actions.redo.run(),
isActive: (e) => e.actions.redo.isActive(),
isEnable: (e) => e.actions.redo.isEnable(),
Expand Down Expand Up @@ -204,7 +204,7 @@ export const wListsListConfig: WToolbarListButtonData = {
title: i18n.bind(null, 'list__action_sink'),
icon: icons.sink,
hotkey: f.toView(A.SinkListItem),
disabledPopoverVisible: true,
hintWhenDisabled: () => i18n('list_action_disabled'),
exec: (e) => e.actions.sinkListItem.run(),
isActive: (e) => e.actions.sinkListItem.isActive(),
isEnable: (e) => e.actions.sinkListItem.isEnable(),
Expand All @@ -214,7 +214,7 @@ export const wListsListConfig: WToolbarListButtonData = {
title: i18n.bind(null, 'list__action_lift'),
icon: icons.lift,
hotkey: f.toView(A.LiftListItem),
disabledPopoverVisible: true,
hintWhenDisabled: () => i18n('list_action_disabled'),
exec: (e) => e.actions.liftListItem.run(),
isActive: (e) => e.actions.liftListItem.isActive(),
isEnable: (e) => e.actions.liftListItem.isEnable(),
Expand Down
13 changes: 13 additions & 0 deletions src/toolbar/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,20 @@ export type ToolbarItemData<E> = {
title: string | (() => string);
hint?: string | (() => string);
hotkey?: HotkeyProps['value'];
/** @deprecated Use _hintWhenDisabled_ setting instead */
disabledPopoverVisible?: boolean;
/**
* Show hint when _isEnable()_ returns false
*
* `false` – don't show hint;
*
* `true` – show default hint;
*
* `string` or `() => string` – show hint with custom message.
*
* @default true
*/
hintWhenDisabled?: boolean | string | (() => string);
exec(editor: E): void;
isActive(editor: E): boolean;
isEnable(editor: E): boolean;
Expand Down

0 comments on commit f08366a

Please sign in to comment.