You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
We need to specify what properties are forwarded to <Base>
Another downside, is that if we want to "expose" these props via ex. setContext, or add them to a class that manages some logic we'd have to provide a new object;
We're simply extending the BaseProps, and we include $binding()'s just like that. We can then use a $Props static function to separate the BaseProps from everything else.
<scriptlang='ts'>
const props:DerivedProps&HTMLDivElementProps=$props(DerivedProps)const baseProps =BaseProps.derived(props)/* equivalent to @example const baseProps = { get value() { return props.value }, set value(v) { props.value = v }, get required() { return props.required } }*/basePropsinstanceofBaseProps// true
</script>
<Base {...baseProps}>
...
By using additional utility functions target on which element @attach goes on (via .$attachments), and potentially keys not specified by the child classes of $Props.
<scriptlang='ts'>
const props:DerivedProps&HTMLInputElementProps=$props(DerivedProps)const baseProps =BaseProps.derived(props)const inputProps =DerivedProps.rest(props) // extracts keys not defined within the classes
</script>
<div {...props.$attachments}>
<Base {...baseProps}>
<inputbind:value={props.value} {...inputProps} />
</Base>
</div>
We now have an impactful, systematically structured, reactive system relating to the props, that we can pass on to various logical classes.
It's easy to read, and in my experience levels up the props-game.
Further
Can we go further — should be go further?
As an additional sparkling idea, we could via the $Props handle the incoming and outgoing dataflow in the same mannerism, as bind:value={() => getter, (v) => setter}.
<scriptlang='ts'module>
classPropsextends$Props {// We can a `private` or `protected` property, that acts just like any other Component prop (as discussed so far) #creationDate:Date// We can then define custom getters and setters/** Equivalent to @example$derived(this.#creationDate == undefined || this.#creationDate instanceof Date ? this.#creationDate : new Date(this.#creationDate.toString()))*/get creationDate() {returnthis.#creationDate==undefined||this.#creationDateinstanceofDate?this.#creationDate:newDate(this.#creationDate.toString()) }/** Equivalent to @examplebind:value={...,(value) => value == undefined || value instanceof Date ? value : new Date(value.toString())}*/set creationDate(value) {this.#creationDate=value==undefined||valueinstanceofDate?value:newDate(value.toString()) } }
</script>
This is last part for custom getters and setters is not core of this proposal. It's an additional way this methodology could benefit the developer with more control.
reacted with thumbs up emoji reacted with thumbs down emoji reacted with laugh emoji reacted with hooray emoji reacted with confused emoji reacted with heart emoji reacted with rocket emoji reacted with eyes emoji
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
-
Class props
The ability to re-use, type and extend component props — all in one go.
Introduction
Imagine you have a
Base.svelte
componentAnd we make use of that for a bunch of other components such as
Derived.svelte
Two things are happening here:
bind:value
<Base>
Another downside, is that if we want to "expose" these props via ex.
setContext
, or add them to a class that manages some logic we'd have to provide a new object;And so we repeat ourselves A LOT.
Proposal
I propose that we establish a
$Props
/SvelteProps
class that can be extended to define a$Prop
-interface.Tip
Alternative syntax
We'd use this as normal, but having props already be defined as readonly or $bindable.
The idea, however. Is that
$props(...)
now returns an instance ofBaseProps
, which we can reference. This is where the power comes in:A MAJOR shortcut!
That's not all. Let's extend this badboy
We're simply extending the BaseProps, and we include
$binding()
's just like that. We can then use a$Props
static function to separate theBaseProps
from everything else.By using additional utility functions target on which element
@attach
goes on (via.$attachments
), and potentially keys not specified by the child classes of$Props
.We now have an impactful, systematically structured, reactive system relating to the props, that we can pass on to various logical classes.
It's easy to read, and in my experience levels up the props-game.
Further
Can we go further — should be go further?
As an additional sparkling idea, we could via the
$Props
handle the incoming and outgoing dataflow in the same mannerism, asbind:value={() => getter, (v) => setter}
.This is last part for custom getters and setters is not core of this proposal. It's an additional way this methodology could benefit the developer with more control.
Beta Was this translation helpful? Give feedback.
All reactions