Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improvements on the documentation of useEditorIsActive #10

Open
kkdaisuki opened this issue Jul 6, 2024 · 0 comments
Open

Improvements on the documentation of useEditorIsActive #10

kkdaisuki opened this issue Jul 6, 2024 · 0 comments

Comments

@kkdaisuki
Copy link

It appears that in the current implementation, when only one parameter is provided, it would be always interpreted as options.

// This would always return true
useEditorIsActive(editor, 'heading')
// This would work as intended
useEditorIsActive(editor, 'heading', {})

This is different from the official Editor API in the Tiptap documentation where the only parameter may be interpreted either way as options or as the name of the node or mark, i.e.,

// Check if it’s a heading
editor.isActive('heading')
// Check if it’s a heading with a specific attribute value
editor.isActive('heading', { level: 2 })
// Check if it has a specific attribute value, doesn’t care what node/mark it is
editor.isActive({ textAlign: 'justify' })

This behavior is certainly related to

return instance?.isActive(args[0]);
where the case of only one parameter is defaulted as the options, thus not evaluated.

To make things clearer, this discrepancy would better be either documented or fixed like

// Not tested
export function useEditorIsActive<
  V extends Editor | undefined,
  R extends Record<string, any>,
>(
  editor: () => V,
  ...args: [name: () => string, options?: R] | [options: R] | [name: () => string]
): () => boolean | undefined {
  return createEditorTransaction(editor, instance => {
    if (args.length === 2) {
      return instance?.isActive(args[0](), args[1]);
    }
    return (typeof args[0] === "function") ? instance?.isActive(args[0]()) : instance?.isActive(args[0]);
  });
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant