export type Ref = T | RefCallback; export type RefCallback = (value: T) => void; /** * Assigns the provided value to a SolidJS ref * * @example * ```tsx * import type { Component, ParentComponent } from "solid-js"; * import { type Ref, assignRef } from "@material-solid/utils/refs" * * type LoggerElement = { * info: (message: string) => void; * warn: (message: string) => void; * } * type LoggerProps = { * ref: Ref; * } * * const Logger: ParentComponent = (props) => { * assignRef(props.ref, { * info: (message) => console.info(message), * warn: (message) => console.warn(message), * }); * return props.children; * } * * const App: Component = () => { * let logger!: LoggerElement; * * onMount(() => logger.warn("Hello world!")); * * return ( *
* *
* ); * } * ``` */ export const assignRef = ( ref: Ref | undefined, value: T, ) => { (ref as RefCallback | undefined)?.(value); }