/* IMPORT */ import isObservable from '~/methods/is_observable'; import useGuarded from '~/hooks/use_guarded'; import useUntracked from '~/hooks/use_untracked'; import {ternary} from '~/oby'; import {isComponent, isFunction, isTruthy} from '~/utils/lang'; import type {Child, FunctionMaybe, ObservableReadonly, Truthy} from '~/types'; /* MAIN */ //TODO: Support an is/guard prop, maybe const If = ({ when, fallback, children }: { when: FunctionMaybe, fallback?: Child, children: Child | (( value: (() => Truthy) ) => Child) }): ObservableReadonly => { if ( isFunction ( children ) && !isObservable ( children ) && !isComponent ( children ) ) { // Calling the children function with an (() => Truthy) const truthy = useGuarded ( when, isTruthy ); return ternary ( when, useUntracked ( () => children ( truthy ) ), fallback ); } else { // Just passing the children along return ternary ( when, children as Child, fallback ); //TSC } }; /* EXPORT */ export default If;