2020-11-26 16:45:25 +01:00
|
|
|
// -----------------------------------------------------------------------------
|
|
|
|
|
// useRef
|
|
|
|
|
// -----------------------------------------------------------------------------
|
|
|
|
|
|
|
|
|
|
import type { Component } from "./component/component";
|
|
|
|
|
import { getCurrent } from "./component/component_node";
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* The purpose of this hook is to allow components to get a reference to a sub
|
|
|
|
|
* html node or component.
|
|
|
|
|
*/
|
|
|
|
|
interface Ref<C extends Component = Component> {
|
|
|
|
|
el: HTMLElement | null;
|
|
|
|
|
comp: C | null;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export function useRef<C extends Component = Component>(name: string): Ref<C> {
|
|
|
|
|
const node = getCurrent()!;
|
|
|
|
|
return {
|
|
|
|
|
get el(): HTMLElement | null {
|
|
|
|
|
const val = node.refs[name];
|
2021-10-21 10:13:08 +02:00
|
|
|
return val || null;
|
2020-11-26 16:45:25 +01:00
|
|
|
// if (val instanceof HTMLElement) {
|
|
|
|
|
// return val;
|
|
|
|
|
// } else if (val instanceof Component) {
|
|
|
|
|
// return val.el;
|
|
|
|
|
// }
|
|
|
|
|
// return null;
|
|
|
|
|
},
|
|
|
|
|
get comp(): C | null {
|
|
|
|
|
return null;
|
|
|
|
|
// const val = node.refs && node.refs[name];
|
|
|
|
|
// return val instanceof Component ? (val as C) : null;
|
|
|
|
|
},
|
|
|
|
|
};
|
|
|
|
|
}
|