Replacing placeholders/arguments with reactive/virtual elements aka. partial replacing #764
seahindeniz
started this conversation in
Ideas
Replies: 0 comments
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
-
Hello! 👋🏻
I just came across this library and looks very good!
Firstly, I would like to ask if there's a function or utility or anything that gives the ability to change a part of a text with a real DOM element? I'm not talking about replacing string with a markup, an organic replacement.
Problem
The need is to replace translation with organic elements by referencing them using keys (
toc
/pp
) and using the value inside the element by passing it to the rendering scope to have the following outputTranslation
Solution
By using RegExp, split and mapping, utility can return an array of
string
orElement | JSX.Element
which we can use it on the element.I haven't tried this solution on any other framework or vanilla way, but I'm pretty sure that it'll work for all.
Utility
Usage (SolidJS)
Output
Additionally
As you can see from the
children: Record<string, (content: string) => JSX.Element>
parameter, the type of key isstring
. I'm not sure of this, but maybe it can be type-safe, considering that keyed arguments are type safe.Overall, I'm also not sure if this approach is best or suitable for this library or may create performance issue. Although, according to SolidJS, all seems good since the rendering part happen under the core reactivity scope
Beta Was this translation helpful? Give feedback.
All reactions