svelte icon indicating copy to clipboard operation
svelte copied to clipboard

Svelte 5: this type of directive is not valid on components

Open orefalo opened this issue 1 year ago • 3 comments
trafficstars

Describe the problem

the analysis around the current props limitation on component has the following shortcomings:

  • it is not intuitive, why can I do <div class:rtl={isRTL}/> and not <MyComp class:rtl={isRTL}/>.
  • It forces the css classes as global, which break proper encapsulation
  • It makes the alternative code seem unpolished and difficult to read.

consider the code below:

<MyComp
 class={`tree${rtl ? ' tree--rtl rtl' : ''}${dragOvering ? 'tree--drag-overing drag-overing' : ''}`} />

Describe the proposed solution

allow myprop:somevalue={condition} manipulations on all elements, HTMLElement or Components.

The above code could be rewritten as:

<MyComp
  class="tree"
  class:tree--rtl={rtl}
  class:rtl={rtl}
  class:tree--drag-overing={dragOvering}
  class:drag-overing={dragOvering} />

for the following benefits:

  • it's intuitive
  • it enforces proper css encapsulation
  • clarity: it's easy to read
  • requires no learning curve, it's been used on HTMLElement

thinking beyond class:, the same construct could be used for any prop (value:me={user==="olivier") or any other myprop:somevalue={condition}). Similar constructs are possible with other frameworks.

In summary, the proposal is more Svelte.

Importance

would make my life easier

orefalo avatar Jun 30 '24 17:06 orefalo

This time around it's a Feature request

orefalo avatar Jun 30 '24 17:06 orefalo

Note that this would not work for all attributes on HTMLElements: in particular, it would clash with the style directive (style:css-attribute={value})

MotionlessTrain avatar Jun 30 '24 17:06 MotionlessTrain

Duplicate of #12229 - issue type (bug report/feature request/etc.) doesn't matter - it was closed because there already is an issue (no matter whether opened or closed) for this topic.

7nik avatar Jun 30 '24 18:06 7nik

closing, still I find the current limitation non intuitive

orefalo avatar Jul 01 '24 03:07 orefalo