Comments (4)
@thezealousfool maybe you'd be interested in knowing I've finally made it possible to pass arbitrary tag names at runtime:
https://github.com/WebReflection/static-params#readme
your example would work via (CodePen)
import {render, html} from 'uhtml';
import {asStatic, asTag} from 'static-params';
const shtml = asTag(html);
const elements = [
{ tag: asStatic("div"), content: "Hello" },
{ tag: asStatic("h1"), content: "World" }
];
render(
this.element,
html`${elements.map((elem) => shtml`<${elem.tag}>${elem.content}</${elem.tag}>`)}`
);
from uhtml.
@Dan-Do the whole poin of projects like uhtml (or lit) is that you never want to manually interfere with the rendered content, but most importantly, Never use innerHTML.
Accordingly, this is probably what you want and, if you need to update that render, you need to put that render inside a callback you can call any time some state changes.
import {render, html} from '//unpkg.com/uhtml?module';
import {asTag} from '//unpkg.com/static-params?module';
const shtml = asTag(html);
const buttonState = {disabled: false, text: 'Click Me'};
const {disabled, text} = buttonState;
const {log} = console;
const views = new Map();
let activeRoute = "/";
views.set('/', `<section route="/" class=${activeRoute == "/" ? "" : "is-hidden"}>Hello! + ${activeRoute == "/" ? "" : "is-hidden"}</section>`);
const loadedViews = ["/"];
render(document.body, html`
<button class="clickable"
onclick=${() => log('clicked')}
?disabled=${!disabled}
>
${text}
</button>
${loadedViews.includes("/") ? html([views.get("/")]) : html``}
`);
from uhtml.
This is a recurrent question I've had for hyperHTML and lighterhtml too, but the short answer is that it's not possible with non vDOM libraries, simply because it's not possible to update/change a tag name in the wild. The logic behind these libraries is always the same: what operation that hole should result? ... and such operation should be supported by the platform.
You would need a reference per each node that uses such technique too, 'cause you must be able to swap on the fly possible different nodes.
The logic here is very different from SSR template engines, you're not working with strings, you are working with real HTML/SVG declarations, and a node that can be a div
and suddenly a p
makes no sense, as you can't change tagName
on the fly, that's not how the DOM works. You can change attributes and content, never the name/kind of the node, you gotta replace it, and it's complicated (needs references and RAM to store somehow these references, etc).
There are ways to work-around this scenario, but arguably the scenario is not needed in the first place, when you have a properly structured layout.
Using eval
is also an option, but one I don't like.
As summary: in 3 years I've never needed this use case and I won't overly complicate the parsing and the runtime cost/performance to support such use case.
If this is vital for your application, I am afraid I suggest you look for something else not standards based.
Thanks for your understanding.
from uhtml.
Hi @WebReflection
Could you help me look at this insert dynamic element using insertAdjacentHTML
?
https://codepen.io/ali33yukisakura/pen/PoOPqve?editors=0011
The attribute "class" of this <section route="/" class=${activeRoute == "/" ? "" : "is-hidden"}>Hello!</section>
is escaped.
from uhtml.
Related Issues (20)
- Error with Parcel HOT 3
- Recursive fragment w/ lost lastChild during target update HOT 16
- This sequence of renders crashes in both V3 and V4 HOT 15
- Custom Element setters no longer called when uhtml renders them with a direct attribute HOT 9
- Bug? Rendering as dom element broken for 4.4.1, not 4.3.5 HOT 14
- FYI: xeito framework HOT 3
- Question about non-changing interpolated content HOT 7
- Working with inheritance HOT 5
- Renderable type doesn't include valueOf? HOT 5
- Incompatible with TypeScript when moduleResolution === nodenext HOT 6
- v4 error: Cannot set property className of #<SVGElement> which has only a getter HOT 6
- Bug: texContent -> textContent HOT 4
- Small issue with v4 HOT 9
- Cannot render empty array HOT 2
- Clarify CJS usage in v4 / CJS report with v4 HOT 1
- Typescript issues HOT 13
- Consider bringing global `uhtml` export back HOT 1
- Cannot set property list of #<HTMLInputElement> which has only a getter HOT 10
- Functions no longer allowed as Holes? HOT 3
- V4 crashes on empty array HOT 10
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from uhtml.