remirror/remirror

View on GitHub
packages/remirror__react-components/src/buttons/undo-button.tsx

Summary

Maintainability
A
0 mins
Test Coverage
import React, { FC, useCallback } from 'react';
import { HistoryExtension } from '@remirror/extension-history';
import { useCommands, useHelpers } from '@remirror/react-core';

import { CommandButton, CommandButtonProps } from './command-button';

export interface UndoButtonProps
  extends Omit<CommandButtonProps, 'commandName' | 'active' | 'enabled' | 'attrs' | 'onSelect'> {}

export const UndoButton: FC<UndoButtonProps> = (props) => {
  const { undo } = useCommands<HistoryExtension>();
  const { undoDepth } = useHelpers<HistoryExtension>(true);

  const handleSelect = useCallback(() => {
    if (undo.enabled()) {
      undo();
    }
  }, [undo]);

  const enabled = undoDepth() > 0;

  return (
    <CommandButton
      {...props}
      commandName='undo'
      active={false}
      enabled={enabled}
      onSelect={handleSelect}
    />
  );
};