svelte icon indicating copy to clipboard operation
svelte copied to clipboard

Feature Request: allow exporting rune from component as dynamic getters

Open mizulu opened this issue 10 months ago • 5 comments

Describe the problem

currently

export function showModal(){}

compiles to

return $.pop({ showModal});

and exporting a rune or a variable is not allowed

export let someVar = 5
export let isOpen = $state()

Describe the proposed solution

This feature suggests exporting a binding to variables / runes using dynamic accessors

export let isOpen = $state();  // allow exporting runes 
export let someVar = 5;         // allow exporting variable binding 
export function showModal();

can be compiled to:

return $.pop({ showModal, get isOpen(){
  return $.get(isOpen)   // use the reactive accessor 
}, 
get someVar(){
  return someVar;  // return local variable
}});

Importance

would make my life easier

mizulu avatar May 31 '25 21:05 mizulu

Is there a reason props can't be used here?

Ocean-OS avatar May 31 '25 21:05 Ocean-OS

It works:

let name = $state('world');

export { name };

7nik avatar May 31 '25 22:05 7nik

as noted above, it is possible to get a getter using the default export ( it actually also exposes a setter )

It will still be DX improvement, to allow export directly in the variable declaration

https://svelte.dev/playground/8b1f89c7bffa4510987266cb7bc7a774?version=5.33.11#H4sIAAAAAAAACn2SQW-cMBCF_4o1ygEkBHcWiKq0t0Y99Fj34MXDxqp3jPDsJhXyf69sk91mm1QIYY1nnp-_xwqkjggtaKOsOwh8md3Cgp1YUGnhZiThWTFCBZOx6KH9sQL_nuNMLED1qvBpnmt_Rpt698rje_XRESOxhxY6Py5m5kGSZHNMx37OJqbFHYWEunlwx9kREm8CEmKzRRab3V7cJXdFuZMkScTtO5wmHLkoyn5YY0Hy6Mg7i7V1h0LC9zghodpE6njJMjaGUlLXXG2lp9tM7Q3plp-M79c8F0QzSOq0OQ-Sttp9EhP3EuJXQithtM6jlhAkR_XcDhUwvjC0vJwwVB8Qvb3-W67v7P6f7uro0Z2IwyvgK1QhIlS0N0AT6nSjS31S1mO5S6Tju2n-TRttrbT-ckbir8YzEi6FBHaHg03YC7z2St7ksWa1HJATwLwXLqHko7Z_c82Owu4mrE7_ExTakAB0-0U0Q_Kc08zFE7Mj4Wi0ZvzVr0Up-kGgrVNkRRmGh7jomtw45PjieD7vw3n_5J4fnVY2anybkS4Sb3P_WQErY58NaWgT2PAHWnYdtZADAAA=

mizulu avatar May 31 '25 22:05 mizulu

For now, it will be too confusing with legacy/svelte4 prop declaration. Exporting function is allowed because its semantics didn't change, but for exported variables it would mean that it defines component prop in legacy mode and component accessor in rune mode, which is confusing.

7nik avatar May 31 '25 22:05 7nik

It will probably be allowed once legacy mode cease to exist https://github.com/sveltejs/svelte/pull/10523

Thiagolino8 avatar May 31 '25 23:05 Thiagolino8