Skip to content

Instantly share code, notes, and snippets.

@developit
Created February 1, 2023 11:45
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save developit/7c927f19b221776eb089815d54d6cf54 to your computer and use it in GitHub Desktop.
Save developit/7c927f19b221776eb089815d54d6cf54 to your computer and use it in GitHub Desktop.
/**
* useSignal, but works as a ref on DOM elements.
* @template T
* @param {T} value
*/
export function useSignalRef(value) {
const ref = /** @type {Signal<T> & { current: T }} */ (useSignal(value));
if (!('current' in ref)) Object.defineProperty(ref, 'current', refSignalProto);
return ref;
}
const refSignalProto = {
configurable: true,
get() {
return this.value;
},
set(v) {
this.value = v;
},
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment