import { ReactNode, Fragment } from 'react'; import { useBreakpoints } from './useBreakpoints'; import { BreakpointKey } from './breakpoints'; /** * Children and conditions * * Needs provider for `BreakpointsProps` */ export interface MatchBreakpointProps { /** * Children are rendered if the conditions are met */ children: ReactNode; /** * Current breakpoint >= min * @example "desktop" */ min?: K; /** * Current Breakpoint <= max * @example "mobile" */ max?: K; /** * Current breakpoint is not * @example "desktop" * @example ["desktop", "tablet"] */ not?: K | K[]; /** * Current breakpoint is * @example "desktop" * @example ["desktop", "tablet"] */ is?: K | K[]; } /** * Renders children if conditions are met */ export function MatchBreakpoint( props: MatchBreakpointProps, ): JSX.Element { const { currentBreakpoint, breakpoints } = useBreakpoints(); if (props.is != null) { if (Array.isArray(props.is)) { if (props.is.indexOf(currentBreakpoint) < 0) { return ; } } else { if (currentBreakpoint !== props.is) { return ; } } } if (props.not != null) { if (Array.isArray(props.not)) { if (props.not.indexOf(currentBreakpoint) >= 0) { return ; } } else { if (currentBreakpoint === props.not) { return ; } } } if (props.min != null) { if (breakpoints[currentBreakpoint] < breakpoints[props.min]) { return ; } } if (props.max != null) { if (breakpoints[currentBreakpoint] > breakpoints[props.max]) { return ; } } return {props.children}; }