svelte icon indicating copy to clipboard operation
svelte copied to clipboard

Define static properties/methods against a component

Open SystemParadox opened this issue 3 years ago • 2 comments

Describe the problem

Using context='module' we can declare module-level properties for a component.

However, you then have to assign new variables to address the exported properties. If you import multiple components using a similar pattern then you also have to rename them like this:

import FooModal, { events as fooEvents } from './FooModal';
import BarModal, { events as barEvents } from './BarModal';
// fooEvents.on(...)
// barEvents.on(...)
// <FooModal />
// <BarModal />

Describe the proposed solution

It would be much nicer if we could declare static properties/methods against a component so that we can use them like this:

import FooModal from './FooModal';
import BarModal from './BarModal';
// FooModal.on(...)
// BarModal.on(...)
// <FooModal />
// <BarModal />

Alternatives considered

  • Currently using import FooModal, { events as fooEvents }, but it's clumsy and verbose.
  • Could set the static properties in an import file outside the component but this is very hacky and even more clumsy.
  • Not aware of any other way to set static properties on a component.

Importance

would make my life easier

SystemParadox avatar Jun 15 '22 11:06 SystemParadox

did you try:

<script>
  import * as FooModal from "./FooModal.svelte"
  FooModal.someStaticMethod()
</script>

<FooModal.default  {...props} />

ramiroaisen avatar Jul 09 '22 04:07 ramiroaisen

That's an interesting idea but these are generic components that are used frequently, I shouldn't have to pollute everything with .default all over the place.

SystemParadox avatar Jul 11 '22 17:07 SystemParadox