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

[Bug]: Default value of text-align doesn't set any style on the element #5955

Open
1 task done
kouroshezzati opened this issue Dec 22, 2024 · 1 comment
Open
1 task done
Labels
Category: Open Source The issue or pull reuqest is related to the open source packages of Tiptap. Type: Bug The issue or pullrequest is related to a bug

Comments

@kouroshezzati
Copy link

kouroshezzati commented Dec 22, 2024

Affected Packages

extension-text-align

Version(s)

2.4.0

Bug Description

When using the Tiptap rich text editor, I encountered an issue where the text-align style is not added to the output for text elements aligned to the default alignment (right in my case). This leads to a problem when the output is displayed on a page with a global text-align: center style. The text, expected to align right, is instead centered because the necessary text-align style is missing.

This issue still exist on the demo page. that's why I checked "Yes, I've updated all my dependencies."

Steps to Reproduce

  1. Set up a Tiptap editor with the default text alignment set to right.
  2. Enter some text in the editor and apply the right text align formatting.
  3. Save or render the editor's output on a page with a global text-align: center style.

Browser Used

Chrome

Code Example URL

No response

Expected Behavior

The editor should add a text-align style explicitly to the output for right-aligned text, even if right is the default alignment. Alternatively, a solution that ensures no unintended alignment issues when the text is displayed elsewhere.

Additional Context (Optional)

Suggested Solution
Add text-align: initial; to the CSS rules for default text alignment. This ensures that the text alignment property does not inherit global styles unintentionally.

By setting text-align: initial;, the text alignment falls back to the browser's default behavior unless explicitly overridden, providing a robust solution for scenarios with global alignment styles.

Dependency Updates

  • Yes, I've updated all my dependencies.
@kouroshezzati kouroshezzati added Category: Open Source The issue or pull reuqest is related to the open source packages of Tiptap. Type: Bug The issue or pullrequest is related to a bug labels Dec 22, 2024
@kouroshezzati
Copy link
Author

Here’s an improved and polished version of the solution you provided:


Solution for Ensuring TextAlign Adds Left or Right Values

To allow the TextAlign extension in Tiptap to explicitly include text-align: left or text-align: right values in the output, you can set the defaultAlignment to 'initial'. While 'initial' is not officially listed as an accepted value for defaultAlignment, it works in JavaScript. However, if you're using TypeScript, this might trigger a type-checking error.

Here’s an example configuration:

TextAlign.configure({
  types: ['heading', 'paragraph'],
  defaultAlignment: 'initial', // Ensures 'left' or 'right' values are included explicitly
});

Notes:

  1. Why Use 'initial':

    • The 'initial' value resets the text-align property to its default behavior, ensuring no unintended inheritance or alignment conflicts. It allows the explicit inclusion of left or right values when required.
  2. TypeScript Caveat:

    • If using TypeScript, you might encounter a type error because 'initial' is not part of the defined defaultAlignment type. To address this, you can cast the value as follows:
      TextAlign.configure({
        types: ['heading', 'paragraph'],
        defaultAlignment: 'initial' as 'left' | 'right' | 'center' | 'justify',
      });
      This ensures compatibility with TypeScript's type-checking.
  3. Benefits:

    • Prevents unexpected text alignment issues when the editor's output is rendered in environments with global styles.
    • Ensures consistent and predictable alignment behavior across different contexts.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Category: Open Source The issue or pull reuqest is related to the open source packages of Tiptap. Type: Bug The issue or pullrequest is related to a bug
Projects
None yet
Development

No branches or pull requests

1 participant