-
Notifications
You must be signed in to change notification settings - Fork 41
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
feature: preserve html inside target elements #28
Merged
Conversation
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
previously, all HTML inside the target element was stripped away when the text was split. With this feature, nested HTML tags within the target elements will be preserved.
Fixed: nested elements not working with position: absolute. Fixed: unexpected white space around nested elements when split lines is enabled. It will now maintain the original spacing even when nested elements do not align with word boundaries. Examples: `<p id="target">Fo<em>o</em> Bar</p>` `<p id="target"><em>Foo</em>, bar</p>` Fixed: nested elements causing unexpected line breaks when split lines is enabled. This was due to the way we detect line breaks. If the `offsetTop` of a nested element was even slightly different than the previous word, it would trigger a line break. This often resulted in unwanted line breaks, especially when using nested elements such as `<button>`. Now, we use a threshold (20% of font size) to determine if a word/element is the start of a new line. In the future, we can make that configurable via options. Removed several unnecessary utility functions
Use Svelte as the framework for storybook examples instead of vanilla HTML.
lukePeavey
added a commit
that referenced
this pull request
Jun 5, 2022
Fixes two issues related to absolute position: 1. Text disappears when text is only split into lines, and the target element does not contain any nested elements. This was a bug introduced by #28. 2. Splitting text with absolute position slightly changes the visual appearance of the text. This was due to the fact that we used `offsetTop` and `offsetLeft`, which do not have sub-pixel precision. Now, we use `getBoundingClientRect` to determine the position of split text nodes. This has a slight negative impact on performance (when absolute position is enabled), but it prevents the text from from visibly shifting around when it is split/un-split.
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Summary
previously, all HTML inside the target element was stripped away when the text was split.
With this feature, HTML elements inside the target element(s) will be preserved when splitting text. This will make it possible to:
Example
Result
Limitations
1. Not fully compatible with splitting text into lines
If the content of a nested element spans multiple lines, it's not possible wrap each line and preserve the nested element. GSAP's plugin has the same limitation. There might be a way to solve this in the future. But for now, this feature will have the caveat that splitting lines is not fully supported.
However, nested elements can be used when splitting lines, as long as there are no line breaks in the nested elements.
TODO
Right now, when split lines is enabled, nested elements are always treated as a word boundary. This results in unwanted white space when nested elements do not align with a word boundary.
For example:
<p id="target"><em>foo</em>, barr</p>
. In this case, there should be no space after theem
; it should be immediately followed by a","
. Another example is nested elements that wrap part of a word:<p id="target"><em>Some</em>thing</p>
.We use the
offsetTop
of each word to detect natural line breaks in the text. If theoffsetTop
of a nested element is slightly different than the previous word, it will trigger a line break. This often results in unwanted line breaks, especially with nested elements such as<button>
. It should be possible to include nested elements that have padding/height/borders etc.For #3