Skip to content

[lexical-list] Bullet item color matches text color - #7024

Merged
etrepum merged 17 commits into
meta:mainfrom
ivailop7:bullet_colors
Feb 24, 2025
Merged

etrepum merged 17 commits into
meta:mainfrom
ivailop7:bullet_colors

Conversation

@ivailop7

@ivailop7 ivailop7 commented Jan 7, 2025 •

Copy link
Copy Markdown
Collaborator

Most text editors do bullet lists with the marker color matching the font color in that list item. Investigated a few editors and how they handle different colors in the same list item.
They do differ in behaviour quite a bit apparently.

One of the popular editors, follows the color of the last text node. Another editor, follows the color only if all the text nodes inside the bullet have the same font color. A third one doesn't support it at all.

In the end, I decided to go with the first one, it's kind of the standard behaviour most people expect. Also because it's easiest to reason about and implement. I'm also unsure if we should do first node or last node. Personally, I think the first text node makes more sense vs the last.

Before:

Screen.Recording.2025-01-07.at.23.31.29.mov

After:

Screen.Recording.2025-01-12.at.22.45.22.mov

@vercel

vercel Bot commented Jan 7, 2025 •

Copy link
Copy Markdown

The latest updates on your projects. Learn more about Vercel for Git ↗︎

Name Status Preview Comments Updated (UTC)
lexical ✅ Ready (Inspect) Visit Preview 💬 Add feedback Feb 24, 2025 3:08am
lexical-playground ✅ Ready (Inspect) Visit Preview 💬 Add feedback Feb 24, 2025 3:08am

@facebook-github-bot facebook-github-bot added the CLA Signed This label is managed by the Facebook bot. Authors need to sign the CLA before a PR can be reviewed. label Jan 7, 2025
@github-actions

github-actions Bot commented Jan 7, 2025 •

Copy link
Copy Markdown

size-limit report 📦

Path Size
lexical - cjs 29.08 KB (+0.03% 🔺)
lexical - esm 28.87 KB (+0.03% 🔺)
@lexical/rich-text - cjs 38.04 KB (-0.01% 🔽)
@lexical/rich-text - esm 30.94 KB (+0.07% 🔺)
@lexical/plain-text - cjs 36.6 KB (+0.14% 🔺)
@lexical/plain-text - esm 28.19 KB (-0.13% 🔽)
@lexical/react - cjs 39.85 KB (+0.02% 🔺)
@lexical/react - esm 32.36 KB (+0.26% 🔺)

@ivailop7

ivailop7 commented Jan 7, 2025

Copy link
Copy Markdown
Collaborator Author

There is the pseudoclass '::marker' that can isolate the color change to the marker only, but I didn't find a nice way to set this via javascript.

@ivailop7

ivailop7 commented Jan 7, 2025

Copy link
Copy Markdown
Collaborator Author

@etrepum @zurfyx for ideas and comments

@etrepum

etrepum commented Jan 8, 2025

Copy link
Copy Markdown
Collaborator

This strategy is easy to implement but I don't think it's correct, IIRC updateDOM for parent nodes happens before the child nodes.

You couldn't do this with CSS and something like color: var(--marker-color) and set that from js (possibly from a mutation listener I guess, that would happen after everything is reconciled including children)?

@ivailop7

ivailop7 commented Jan 8, 2025

Copy link
Copy Markdown
Collaborator Author

This strategy is easy to implement but I don't think it's correct, IIRC updateDOM for parent nodes happens before the child nodes.

You couldn't do this with CSS and something like color: var(--marker-color) and set that from js (possibly from a mutation listener I guess, that would happen after everything is reconciled including children)?

This sounds like a better idea. I wanted to avoid another listener just for this, but I guess even updateDom itself is just another listener implementation. Will give it a go.

@etrepum

etrepum commented Jan 8, 2025

Copy link
Copy Markdown
Collaborator

You may also want to use getComputedStyle since the text could be colored by means other than setting the style attribute directly (although of course that's not how the playground works today, it may be a bit more robust in other settings).

@fantactuka

Copy link
Copy Markdown
Contributor

Curious why lastChild vs firstChild (given it's closer to the marker)

@etrepum

etrepum commented Jan 8, 2025

Copy link
Copy Markdown
Collaborator

In Google Docs it like the marker color is based on what the text color was when the marker was created, but you can change it if you apply a color to its entire contents at once (e.g. changing the left half from black to blue, then changing the right half to blue, will not change the marker to blue, but selecting all of the text and changing it to blue at once will). This would mean storing the color on the list item itself and updating it accordingly, rather than just doing DOM stuff.

@ivailop7

ivailop7 commented Jan 8, 2025

Copy link
Copy Markdown
Collaborator Author

Curious why lastChild vs firstChild (given it's closer to the marker)

I agree it should be the first node, other editors opted for last one, thus I went with this. Probably because of their design. Will polish this sometime over the weekend.

@ivailop7

Copy link
Copy Markdown
Collaborator Author

@etrepum @fantactuka updated the implementaion and the after video in the description. Behaviour is better, because it fixed also matching the marker size to the font-size. That said, still unsure if this is still the most efficient way. Because I have to listen to TextNode changes for the font color/font size. I'm unsure if the cost added on every TextNode 'update' is worth adding this weight. The CSS var approach didn't work, because after updating the variable, it would change the color of all bullets to the latest variable color value, and if having to override the variable value per list item in style is essentially the same overhead.

@etrepum etrepum left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This seems mostly reasonable but maybe it should be exposed as a separate register function so that people can choose to use this behavior or not? They might have different ideas for how this should work and it'll be very tricky to untangle it unless it's a separate call.

Comment thread packages/lexical-list/src/index.ts Resolved
Comment thread packages/lexical-list/src/index.ts Outdated
Comment on lines +113 to +115
if ($isRangeSelection(selection)) {
listItemElement.setAttribute('style', selection.style || '');
}

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This should probably check to see if it's the selection that created the ListItemNode, it could be from paste or whatever. I would probably just have the created and updated work the same way, with this selection case to cover scenarios where there is no text element child

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This should probably check to see if it's the selection that created the ListItemNode, it could be from paste or whatever. I would probably just have the created and updated work the same way, with this selection case to cover scenarios where there is no text element child

Implement the other things, but I'm not sure I'm getting the "if it's the selection that created the ListItemNode" part. Are you referring to handling PASTE and/or SELECTION_CHANGE commands or something else?

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

A created mutation event happens the first time a node is rendered to DOM for whatever reason, so the style of the editor's current selection might not really have anything to do with that newly rendered ListItemNode. It is mostly only relevant to when you press return or some other UI action to create a new ListItemNode. It's reasonable to assume that case should pick up the style. In that case the selection will almost certainly be collapsed inside the ListItemNode.

It's hard to imagine what all of the other scenarios that would render a new ListItemNode could be, paste is certainly one of them. Selection change probably isn't.

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If you were to always copy the style of the selection to an empty ListItemNode so long as the selection is collapsed inside of it then I think it would also work well, it doesn't need to only be applied on create (haven't looked at the code again but that could possibly simplify some other stuff?)

@etrepum

etrepum commented Jan 16, 2025

Copy link
Copy Markdown
Collaborator

Unit tests are failing, looks like they are not expecting style="" to be on these nodes. Might be worth some special casing to avoid putting style="" in the DOM

@ivailop7 ivailop7 left a comment

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is some really good refactoring! Thank you for taking the baton on this one. I can't approve the PR since I'm the creator, but I guess you can self-merge the moment you decide 🙂

@etrepum

etrepum commented Feb 23, 2025

Copy link
Copy Markdown
Collaborator

I think there are still some collab edge cases to smooth down but this route seems to be a pretty good foundation for this feature. Basically we already typically store the intended text format & style (textFormat, textStyle) on elements to work around the fact that we don't allow for empty text nodes… so if we "just" render that as the style on ListItemNode's DOM (when present) then it tends to work as expected. Usefully, ElementNode has a separate style property which we can allow to take precedence in case a user has a need to override what's going on here.

@ivailop7

Copy link
Copy Markdown
Collaborator Author

I think there are still some collab edge cases to smooth down but this route seems to be a pretty good foundation for this feature. Basically we already typically store the intended text format & style (textFormat, textStyle) on elements to work around the fact that we don't allow for empty text nodes… so if we "just" render that as the style on ListItemNode's DOM (when present) then it tends to work as expected. Usefully, ElementNode has a separate style property which we can allow to take precedence in case a user has a need to override what's going on here.

This turned out more involved than I anticipated. Annoyingly it reminds me that we have the same problem for tables, where style doesn't get persisted for empty cells, only formatting does. Anyhow, looking forward to this PR making it to main. Great work again!

@etrepum

etrepum commented Feb 23, 2025

Copy link
Copy Markdown
Collaborator

I suspect that tables would be pretty straightforward to fix in the same way, if this doesn't already fix it as a side-effect

Comment on lines +515 to +519
// TODO this is a workaround for Firefox so that the
// selection picks up the text format
if (browserName === 'firefox') {
await page.keyboard.press('ArrowLeft', {delay: 50});
}

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This requires further investigation but I don't think it should block this feature

This branch was successfully deployed

2 active deployments
Preview – lexical — b5613d94 Deployed Feb 24, 2025 by vercel[bot]
Preview – lexical-playground — b5613d94 Deployed Feb 24, 2025 by vercel[bot]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

CLA Signed This label is managed by the Facebook bot. Authors need to sign the CLA before a PR can be reviewed. extended-tests Run extended e2e tests on a PR

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants