fix: filter out empty children

This commit is contained in:
Christel Westerberg
2024-06-18 15:15:15 +02:00
parent ceee9fd154
commit 928b2af698
3 changed files with 17 additions and 5 deletions

View File

@@ -1,6 +1,6 @@
import { Slot } from "@radix-ui/react-slot" import { Slot } from "@radix-ui/react-slot"
import { Children } from "react"
import { checkForEmptyChildren } from "../../utils/checkForEmptyChildren"
import { subtitleVariants } from "./variants" import { subtitleVariants } from "./variants"
import type { SubtitleProps } from "./subtitle" import type { SubtitleProps } from "./subtitle"
@@ -13,7 +13,7 @@ export default function Subtitle({
textTransform, textTransform,
...props ...props
}: SubtitleProps) { }: SubtitleProps) {
if (Children.toArray(props.children).length === 0) { if (checkForEmptyChildren(props.children) === 0) {
return null return null
} }
const Comp = asChild ? Slot : "p" const Comp = asChild ? Slot : "p"

View File

@@ -1,5 +1,4 @@
import { Children } from "react" import { checkForEmptyChildren } from "../../utils/checkForEmptyChildren"
import { headingVariants } from "./variants" import { headingVariants } from "./variants"
import type { HeadingProps } from "./title" import type { HeadingProps } from "./title"
@@ -13,7 +12,7 @@ export default function Title({
textAlign, textAlign,
textTransform, textTransform,
}: HeadingProps) { }: HeadingProps) {
if (Children.toArray(children).length === 0) { if (checkForEmptyChildren(children) === 0) {
return null return null
} }
const Hx = level const Hx = level

View File

@@ -0,0 +1,13 @@
import { Children, type ReactNode } from "react"
export function checkForEmptyChildren(children: ReactNode) {
return Children.toArray(children).filter((child) => {
if (child === "") {
return false
} else if (child == null) {
return false
} else {
return true
}
}).length
}