-
-
Notifications
You must be signed in to change notification settings - Fork 118
feat: add jsxBind
function to @preact/signals
#724
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
04a49ab
eab99b1
c949987
27706a1
66d676a
d58a8aa
4d14f3d
a4cb42a
4604dd9
00ad633
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
--- | ||
"@preact/signals": minor | ||
--- | ||
|
||
Introduce the `jsxBind` function for inlined `computed` declarations as JSX attributes or JSX children. |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -84,7 +84,10 @@ function createUpdater(update: () => void) { | |
* A wrapper component that renders a Signal directly as a Text node. | ||
* @todo: in Preact 11, just decorate Signal with `type:null` | ||
*/ | ||
function SignalValue(this: AugmentedComponent, { data }: { data: Signal }) { | ||
function SignalValue( | ||
this: AugmentedComponent, | ||
{ data }: { data: ReadonlySignal } | ||
) { | ||
// hasComputeds.add(this); | ||
|
||
// Store the props.data signal in another signal so that | ||
|
@@ -105,6 +108,10 @@ function SignalValue(this: AugmentedComponent, { data }: { data: Signal }) { | |
|
||
const wrappedSignal = computed(() => { | ||
let s = currentSignal.value.value; | ||
// This is possibly an inline computed from jsxBind | ||
if (typeof s === "function") { | ||
s = s(); | ||
} | ||
return s === 0 ? 0 : s === true ? "" : s || ""; | ||
}); | ||
|
||
|
@@ -172,13 +179,21 @@ Object.defineProperties(Signal.prototype, { | |
/** Inject low-level property/attribute bindings for Signals into Preact's diff */ | ||
hook(OptionsTypes.DIFF, (old, vnode) => { | ||
if (typeof vnode.type === "string") { | ||
const oldSignalProps = vnode.__np; | ||
let signalProps: Record<string, any> | undefined; | ||
|
||
let props = vnode.props; | ||
for (let i in props) { | ||
if (i === "children") continue; | ||
|
||
let value = props[i]; | ||
if ( | ||
value && | ||
typeof value === "object" && | ||
Object.getPrototypeOf(value) === jsxBind | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Just using .prototype will be lower bytes and more performant I think, that being said not too fuzzed can look at this later. Will do one final pass over the weekend and get this merged There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. that wouldn't be equivalent. it's either getPrototypeOf or There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Using constructor would be cheaper and smaller here. if (typeof value === 'object' && value.constructor === jsxBind) {
// ...
}
// ...
export function jsxBind<T>(cb: () => T): T {
return { value: cb, constructor: jsxBind } as any;
} There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @developit The prototype needs to inherit from personally, I don't see an issue with using |
||
) { | ||
value = oldSignalProps?.[i] || computed(value.value); | ||
} | ||
if (value instanceof Signal) { | ||
if (!signalProps) vnode.__np = signalProps = {}; | ||
signalProps[i] = value; | ||
|
@@ -465,6 +480,17 @@ export function useSignalEffect( | |
}, []); | ||
} | ||
|
||
/** | ||
* Bind the given callback to a JSX attribute or JSX child. This allows for "inline computed" | ||
* signals that derive their value from other signals. Like with `useComputed`, any non-signal | ||
* values used in the callback are captured at the time of binding and won't change after that. | ||
*/ | ||
export function jsxBind<T>(cb: () => T): T { | ||
return { value: cb, __proto__: jsxBind } as any; | ||
} | ||
|
||
Object.setPrototypeOf(jsxBind, Signal.prototype); | ||
|
||
/** | ||
* @todo Determine which Reactive implementation we'll be using. | ||
* @internal | ||
|
Uh oh!
There was an error while loading. Please reload this page.