import * as React from 'react' ; export interface WhenProps { [ key ] : boolean | React . ReactNode ; children : React . ReactNode ; } export const When = function When ( props ) { const keys = Object . keys ( props ) . filter ( key => typeof props [ key ] === 'boolean' ) ; const truthy = keys . every ( key => props [ key ] === true ) ; if ( ! truthy ) { return null ; } const children = React . Children . toArray ( props . children ) . filter ( ( c ) : c is React . ReactElement < unknown > => typeof c !== 'string' ) ; const hasSlot = children . some ( child => [ WhenTruthy , WhenFalsy ] . includes ( child . type as any ) ) ; return < > { children . filter ( getFilter ( { truthy , hasSlot } ) ) } < />; } ; export const WhenTruthy = function WhenTruthy ( props ) { return < > { props . children } < />; } ; export const WhenFalsy = function WhenTruthy ( props ) { return < > { props . children } < />; } ; function getFilter ( context ) { if ( ! context . hasSlot ) { return ( ) => context . truthy ; } const Component = context . truthy ? WhenTruthy : WhenFalsy ; return node => typeof node . type !== 'string' && node . type === Component ; }